Merge pull request #5850 from thaJeztah/fix_context_err

cli/command/context: fix error-handling of skip-tls-verify
This commit is contained in:
Sebastiaan van Stijn 2025-02-20 18:37:04 +01:00 committed by GitHub
commit 2493a96027
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 55 additions and 3 deletions

View File

@ -59,42 +59,87 @@ func TestCreate(t *testing.T) {
cli := makeFakeCli(t)
assert.NilError(t, cli.ContextStore().CreateOrUpdate(store.Metadata{Name: "existing-context"}))
tests := []struct {
doc string
options CreateOptions
expecterErr string
}{
{
doc: "empty name",
expecterErr: `context name cannot be empty`,
},
{
doc: "reserved name",
options: CreateOptions{
Name: "default",
},
expecterErr: `"default" is a reserved context name`,
},
{
doc: "whitespace-only name",
options: CreateOptions{
Name: " ",
},
expecterErr: `context name " " is invalid`,
},
{
doc: "existing context",
options: CreateOptions{
Name: "existing-context",
},
expecterErr: `context "existing-context" already exists`,
},
{
doc: "invalid docker host",
options: CreateOptions{
Name: "invalid-docker-host",
Docker: map[string]string{
keyHost: "some///invalid/host",
"host": "some///invalid/host",
},
},
expecterErr: `unable to parse docker host`,
},
{
doc: "ssh host with skip-tls-verify=false",
options: CreateOptions{
Name: "skip-tls-verify-false",
Docker: map[string]string{
"host": "ssh://example.com,skip-tls-verify=false",
},
},
},
{
doc: "ssh host with skip-tls-verify=true",
options: CreateOptions{
Name: "skip-tls-verify-true",
Docker: map[string]string{
"host": "ssh://example.com,skip-tls-verify=true",
},
},
},
{
doc: "ssh host with skip-tls-verify=INVALID",
options: CreateOptions{
Name: "skip-tls-verify-invalid",
Docker: map[string]string{
"host": "ssh://example.com",
"skip-tls-verify": "INVALID",
},
},
expecterErr: `unable to create docker endpoint config: skip-tls-verify: parsing "INVALID": invalid syntax`,
},
{
doc: "unknown option",
options: CreateOptions{
Name: "unknown-option",
Docker: map[string]string{
"UNKNOWN": "value",
},
},
expecterErr: `unable to create docker endpoint config: unrecognized config key: UNKNOWN`,
},
}
for _, tc := range tests {
t.Run(tc.options.Name, func(t *testing.T) {
t.Run(tc.doc, func(t *testing.T) {
err := RunCreate(cli, &tc.options)
if tc.expecterErr == "" {
assert.NilError(t, err)

View File

@ -68,7 +68,14 @@ func parseBool(config map[string]string, name string) (bool, error) {
return false, nil
}
res, err := strconv.ParseBool(strVal)
return res, fmt.Errorf("name: %w", err)
if err != nil {
var nErr *strconv.NumError
if errors.As(err, &nErr) {
return res, fmt.Errorf("%s: parsing %q: %w", name, nErr.Num, nErr.Err)
}
return res, fmt.Errorf("%s: %w", name, err)
}
return res, nil
}
func validateConfig(config map[string]string, allowedKeys map[string]struct{}) error {