Skip to content

Tests, refactor and coverage on package opts#13694

Merged
calavera merged 1 commit intomoby:masterfrom
vdemeester:opts-test-coverage
Jul 14, 2015
Merged

Tests, refactor and coverage on package opts#13694
calavera merged 1 commit intomoby:masterfrom
vdemeester:opts-test-coverage

Conversation

@vdemeester
Copy link
Copy Markdown
Member

This is the time for opts to get covered 🐸 and a little bit refactored.

ValidateEnv and ParseEnvFile uses a regexp to valide that it is a correct environment variable that is passed. The definition used :

Variables set by the user must have a name consisting solely of alphabetics, numerics, and underscores - the first of which must not be numeric.

ValidateLabel is made consistent with the LABEL in Dockerfile as it allows multiple = like LABEL label=value1=value2.

# before
+ go test -test.timeout=30m github.com/docker/docker/opts
PASS
coverage: 33.8% of statements
#after
+ go test -test.timeout=30m github.com/docker/docker/opts
PASS
coverage: 92.8% of statements

There is a few FIXME in this one :

  • ValidatePath(":/test") is valid, although it will fail after I think
  • func ValidateEnv(val string) (string, error) always returns nil for error. I guess it's for coherence of returns with the other validate method, so I think I can remove my FIXME comment.
  • There is FIXMEs for ListOpts.GetAll() and ListOpts.GetMap asking if we can remove it. They are used in runconfig/parse.go ; GetMap could/should probably have another name but..

Signed-off-by: Vincent Demeester vincent@sbr.pm

@vdemeester vdemeester force-pushed the opts-test-coverage branch 3 times, most recently from e2990f3 to 1e2ba46 Compare June 3, 2015 08:13
opts/opts.go Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't you fix it rather than leaving a comment? 😉

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, and it shouldn't :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I was asking if this was ok before doing it 😸. Now that I know it is I will fix it 😉.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok that's kinda what I guessed, it has to be there because it is used as a type ValidatorFctType func(val string) (string, error). I think I should just remove the comment for now 😝.

Environment(s) and thus Label(s) (that are validated the same way) are always valid, just rewritten a bit sometimes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha :)

@vdemeester vdemeester force-pushed the opts-test-coverage branch from 1e2ba46 to 3ee81b8 Compare June 5, 2015 06:42
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we're inconsistent. If you added a "=" after each of those you'd get an error like:

docker: poorly formatted environment: variable 'a b c' has white spaces.

Yet w/o the "=" it passes it thru to the container. We should either allow both or error both.
I think we should detect the spaces and error in both cases. Want to fix that as part of this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@duglin I sure do 😉

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a certain consistency in the fact that we are able to run docker run -e "something with space" busybox /bin/sh ; although the environment variables is not set in the container. I should probably validate this too (ValidateEnv).

@vdemeester vdemeester force-pushed the opts-test-coverage branch 2 times, most recently from a64cde1 to 7d2c339 Compare June 5, 2015 22:03
@vdemeester
Copy link
Copy Markdown
Member Author

Ok, so I've updated the envfile end env validation.

  • For envfile, it will fail if there is spaces even without =, a file like this would fail :

    some spaces
  • Almost the same for env (and now there is a case it returns an error 😝 ), it's no more possible to do something like : docker run -e "some spaces" ….

For the sake of it, I wasn't sure if it was possible to define a environment variable with spaces, so I tried (with bash and zsh) :

$ export SOME\ SPACES=value
bash: export: `SOME SPACES=value': not a valid identifier
$ export "SOME SPACES"=value
bash: export: `SOME SPACES=value': not a valid identifier

☺️

@duglin
Copy link
Copy Markdown
Contributor

duglin commented Jun 5, 2015

yep - that's what I got in my testing as well. thanks!!

@vdemeester vdemeester force-pushed the opts-test-coverage branch from 7d2c339 to dcc377d Compare June 7, 2015 19:26
@rhatdan
Copy link
Copy Markdown
Contributor

rhatdan commented Jun 8, 2015

LABEL call just stole code from ENV. It should follow the same standard of whatever ENV allows, I believe.

@vdemeester
Copy link
Copy Markdown
Member Author

Rebased to master and move a test from #13879 in ip_test.go.

opts/envfile.go Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor thing, and up to you, but I think if len(data) > 1 would be better just because it'll be faster than looping over the string looking for =

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds about right, will do :)

@vdemeester vdemeester force-pushed the opts-test-coverage branch from b3c0d6c to bfca3fe Compare July 9, 2015 12:20
opts/opts.go Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we may need to externalize and then use validMountMode() from daemon/volumes.go since the list of modes is more than just rw and ro. What do you think?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right 😸. I'll look at that this evening.

@duglin
Copy link
Copy Markdown
Contributor

duglin commented Jul 9, 2015

Just one question/comment! Looking good!

@vdemeester vdemeester force-pushed the opts-test-coverage branch from bfca3fe to cf9a020 Compare July 9, 2015 16:45
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one will not fail anymore, so removing it :).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meaning, now it is supposed to work with this syntax and not fail like on this test 😉.

@vdemeester
Copy link
Copy Markdown
Member Author

@duglin I think it's done.. I'm not found of the move I made of rwMode and roMode. WDYT ? 😅

@duglin
Copy link
Copy Markdown
Contributor

duglin commented Jul 9, 2015

@vdemeester yea, lets keep that code into volumes.go and just externalize validateMountMode() instead of moving it all into opt.go. To me, volumes (sort of) owns all that logic and the opts stuff is more like a user of it.

@vdemeester
Copy link
Copy Markdown
Member Author

@duglin The problem is there seems to be a cycle loop of import (if I import daemon in the package opts) . I'm gonna move it into github.com/docker/docker/volume.

@vdemeester vdemeester force-pushed the opts-test-coverage branch 3 times, most recently from 91fb81f to 37e4bef Compare July 10, 2015 15:00
@vdemeester
Copy link
Copy Markdown
Member Author

@duglin quick update :

  1. Moved ValidateMountMode to github.com/docker/docker/volume.
  2. Fixed few tests (runconfig, etc..), e.g. that would failed because they were considered rwz that would be later rejected as not supported.
  3. Added a bunch of godoc 😉.

- Refactor opts.ValidatePath and add an opts.ValidateDevice
  ValidePath will now accept : containerPath:mode, hostPath:containerPath:mode
  and hostPath:containerPath.
  ValidateDevice will have the same behavior as current.

- Refactor opts.ValidateEnv, opts.ParseEnvFile
  Environment variables will now be validated with the following
  definition :
  > Environment variables set by the user must have a name consisting
  > solely of alphabetics, numerics, and underscores - the first of
  > which must not be numeric.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
@vdemeester vdemeester force-pushed the opts-test-coverage branch from 37e4bef to dfc6c04 Compare July 12, 2015 08:34
@vdemeester
Copy link
Copy Markdown
Member Author

@duglin Another update 😅.
I've added a ValidateDevice function that will do the same as ValidatePath but without validating the mount mode (https://github.com/docker/docker/pull/13694/files#diff-def63b7f78492fa1f0dc4e219a650df4R229). It should fix the DockerSuite.TestDevicePermissions integration test.

@duglin
Copy link
Copy Markdown
Contributor

duglin commented Jul 14, 2015

@vdemeester thanks for the hard work!
LGTM

ping @LK4D4 you still ok with this?

@calavera
Copy link
Copy Markdown
Contributor

LGTM

calavera added a commit that referenced this pull request Jul 14, 2015
Tests, refactor and coverage on package opts
@calavera calavera merged commit ecdbf86 into moby:master Jul 14, 2015
@thaJeztah
Copy link
Copy Markdown
Member

🎉 thanks!!

@vdemeester vdemeester deleted the opts-test-coverage branch July 15, 2015 05:01
@vdemeester
Copy link
Copy Markdown
Member Author

🎉 😊
@duglin thanks for all the reviews !

@thaJeztah
Copy link
Copy Markdown
Member

Added impact labels, because the validation-rules for environment variables changed in this PR (see #14727)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants