| ... | ... |
@@ -62,7 +62,7 @@ func NewBuildCommand(dockerCli *command.DockerCli) *cobra.Command {
|
| 62 | 62 |
ulimits := make(map[string]*units.Ulimit) |
| 63 | 63 |
options := buildOptions{
|
| 64 | 64 |
tags: opts.NewListOpts(validateTag), |
| 65 |
- buildArgs: opts.NewListOpts(runconfigopts.ValidateEnv), |
|
| 65 |
+ buildArgs: opts.NewListOpts(runconfigopts.ValidateArg), |
|
| 66 | 66 |
ulimits: runconfigopts.NewUlimitOpt(&ulimits), |
| 67 | 67 |
} |
| 68 | 68 |
|
| ... | ... |
@@ -46,6 +46,21 @@ func doesEnvExist(name string) bool {
|
| 46 | 46 |
return false |
| 47 | 47 |
} |
| 48 | 48 |
|
| 49 |
+// ValidateArg validates a build-arg variable and returns it. |
|
| 50 |
+// Build-arg is in the form of <varname>=<value> where <varname> is required. |
|
| 51 |
+func ValidateArg(val string) (string, error) {
|
|
| 52 |
+ arr := strings.Split(val, "=") |
|
| 53 |
+ if len(arr) > 1 && isNotEmpty(arr[0]) {
|
|
| 54 |
+ return val, nil |
|
| 55 |
+ } |
|
| 56 |
+ |
|
| 57 |
+ return "", fmt.Errorf("bad format for build-arg: %s", val)
|
|
| 58 |
+} |
|
| 59 |
+ |
|
| 60 |
+func isNotEmpty(val string) bool {
|
|
| 61 |
+ return len(val) > 0 |
|
| 62 |
+} |
|
| 63 |
+ |
|
| 49 | 64 |
// ValidateExtraHost validates that the specified string is a valid extrahost and returns it. |
| 50 | 65 |
// ExtraHost is in the form of name:ip where the ip has to be a valid ip (IPv4 or IPv6). |
| 51 | 66 |
func ValidateExtraHost(val string) (string, error) {
|
| ... | ... |
@@ -61,6 +61,42 @@ func TestValidateEnv(t *testing.T) {
|
| 61 | 61 |
} |
| 62 | 62 |
} |
| 63 | 63 |
|
| 64 |
+func TestValidateArg(t *testing.T) {
|
|
| 65 |
+ valids := map[string]string{
|
|
| 66 |
+ "_=a": "_=a", |
|
| 67 |
+ "var1=value1": "var1=value1", |
|
| 68 |
+ "_var1=value1": "_var1=value1", |
|
| 69 |
+ "var2=value2=value3": "var2=value2=value3", |
|
| 70 |
+ "var3=abc!qwe": "var3=abc!qwe", |
|
| 71 |
+ "var_4=value 4": "var_4=value 4", |
|
| 72 |
+ "var_5=": "var_5=", |
|
| 73 |
+ } |
|
| 74 |
+ for value, expected := range valids {
|
|
| 75 |
+ actual, err := ValidateArg(value) |
|
| 76 |
+ if err != nil {
|
|
| 77 |
+ t.Fatal(err) |
|
| 78 |
+ } |
|
| 79 |
+ if actual != expected {
|
|
| 80 |
+ t.Fatalf("Expected [%v], got [%v]", expected, actual)
|
|
| 81 |
+ } |
|
| 82 |
+ } |
|
| 83 |
+ |
|
| 84 |
+ invalid := map[string]string{
|
|
| 85 |
+ "foo": "bad format", |
|
| 86 |
+ "=foo": "bad format", |
|
| 87 |
+ "cc c": "bad format", |
|
| 88 |
+ } |
|
| 89 |
+ for value, expectedError := range invalid {
|
|
| 90 |
+ if _, err := ValidateArg(value); err == nil {
|
|
| 91 |
+ t.Fatalf("ValidateArg(`%s`) should have failed validation", value)
|
|
| 92 |
+ } else {
|
|
| 93 |
+ if !strings.Contains(err.Error(), expectedError) {
|
|
| 94 |
+ t.Fatalf("ValidateArg(`%s`) error should contain %q", value, expectedError)
|
|
| 95 |
+ } |
|
| 96 |
+ } |
|
| 97 |
+ } |
|
| 98 |
+} |
|
| 99 |
+ |
|
| 64 | 100 |
func TestValidateExtraHosts(t *testing.T) {
|
| 65 | 101 |
valid := []string{
|
| 66 | 102 |
`myhost:192.168.0.1`, |