Skip to content

Commit 7e71782

Browse files
committed
cli/command/context: fix error-handling of skip-tls-verify
Before 2b9a4d5, this function would use "errors.Wrap" which returns nil if the original error was nil. fmt.Errorf does not do this, so without a nil check, it would unconditionally return an error; docker context create arm64 --docker host=ssh://172.17.101.26,skip-tls-verify=False unable to create docker endpoint config: name: %!w(<nil>) Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent f9ced58 commit 7e71782

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

cli/command/context/create_test.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,42 +59,87 @@ func TestCreate(t *testing.T) {
5959
cli := makeFakeCli(t)
6060
assert.NilError(t, cli.ContextStore().CreateOrUpdate(store.Metadata{Name: "existing-context"}))
6161
tests := []struct {
62+
doc string
6263
options CreateOptions
6364
expecterErr string
6465
}{
6566
{
67+
doc: "empty name",
6668
expecterErr: `context name cannot be empty`,
6769
},
6870
{
71+
doc: "reserved name",
6972
options: CreateOptions{
7073
Name: "default",
7174
},
7275
expecterErr: `"default" is a reserved context name`,
7376
},
7477
{
78+
doc: "whitespace-only name",
7579
options: CreateOptions{
7680
Name: " ",
7781
},
7882
expecterErr: `context name " " is invalid`,
7983
},
8084
{
85+
doc: "existing context",
8186
options: CreateOptions{
8287
Name: "existing-context",
8388
},
8489
expecterErr: `context "existing-context" already exists`,
8590
},
8691
{
92+
doc: "invalid docker host",
8793
options: CreateOptions{
8894
Name: "invalid-docker-host",
8995
Docker: map[string]string{
90-
keyHost: "some///invalid/host",
96+
"host": "some///invalid/host",
9197
},
9298
},
9399
expecterErr: `unable to parse docker host`,
94100
},
101+
{
102+
doc: "ssh host with skip-tls-verify=false",
103+
options: CreateOptions{
104+
Name: "skip-tls-verify-false",
105+
Docker: map[string]string{
106+
"host": "ssh://example.com,skip-tls-verify=false",
107+
},
108+
},
109+
},
110+
{
111+
doc: "ssh host with skip-tls-verify=true",
112+
options: CreateOptions{
113+
Name: "skip-tls-verify-true",
114+
Docker: map[string]string{
115+
"host": "ssh://example.com,skip-tls-verify=true",
116+
},
117+
},
118+
},
119+
{
120+
doc: "ssh host with skip-tls-verify=INVALID",
121+
options: CreateOptions{
122+
Name: "skip-tls-verify-invalid",
123+
Docker: map[string]string{
124+
"host": "ssh://example.com",
125+
"skip-tls-verify": "INVALID",
126+
},
127+
},
128+
expecterErr: `unable to create docker endpoint config: skip-tls-verify: parsing "INVALID": invalid syntax`,
129+
},
130+
{
131+
doc: "unknown option",
132+
options: CreateOptions{
133+
Name: "unknown-option",
134+
Docker: map[string]string{
135+
"UNKNOWN": "value",
136+
},
137+
},
138+
expecterErr: `unable to create docker endpoint config: unrecognized config key: UNKNOWN`,
139+
},
95140
}
96141
for _, tc := range tests {
97-
t.Run(tc.options.Name, func(t *testing.T) {
142+
t.Run(tc.doc, func(t *testing.T) {
98143
err := RunCreate(cli, &tc.options)
99144
if tc.expecterErr == "" {
100145
assert.NilError(t, err)

cli/command/context/options.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,14 @@ func parseBool(config map[string]string, name string) (bool, error) {
6868
return false, nil
6969
}
7070
res, err := strconv.ParseBool(strVal)
71-
return res, fmt.Errorf("name: %w", err)
71+
if err != nil {
72+
var nErr *strconv.NumError
73+
if errors.As(err, &nErr) {
74+
return res, fmt.Errorf("%s: parsing %q: %w", name, nErr.Num, nErr.Err)
75+
}
76+
return res, fmt.Errorf("%s: %w", name, err)
77+
}
78+
return res, nil
7279
}
7380

7481
func validateConfig(config map[string]string, allowedKeys map[string]struct{}) error {

0 commit comments

Comments
 (0)