Skip to content

Commit

Permalink
fix: improve error messaging when mandatory flag is missing in file c…
Browse files Browse the repository at this point in the history
…onvert (#1429)

When mandatory flags for file convert are missing, the error message is not helpful. In this commit, we have added additional validation to communicate more details in the message.
  • Loading branch information
harshadixit12 committed Jan 10, 2025
1 parent 2c9a599 commit 8a293d9
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cmd/file_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ into another compatible format. For example, a configuration for 'kong-gateway-2
can be converted into a 'kong-gateway-3.x' configuration file.`,
Args: validateNoArgs,
RunE: execute,
PreRunE: func(_ *cobra.Command, _ []string) error {
if convertCmdSourceFormat != string(convert.FormatKongGateway) &&
convertCmdSourceFormat != string(convert.FormatKongGateway2x) {
return fmt.Errorf("invalid value '%s' found for the 'from' flag. Allowed values: [%s, %s]",
convertCmdSourceFormat, string(convert.FormatKongGateway), string(convert.FormatKongGateway2x))
}
if convertCmdDestinationFormat != string(convert.FormatKonnect) &&
convertCmdDestinationFormat != string(convert.FormatKongGateway3x) {
return fmt.Errorf("invalid value '%s' found for the 'to' flag. Allowed values: [%s, %s]",
convertCmdDestinationFormat, string(convert.FormatKonnect), string(convert.FormatKongGateway3x))
}
return nil
},
}

sourceFormats := []convert.Format{convert.FormatKongGateway, convert.FormatKongGateway2x}
Expand Down
56 changes: 56 additions & 0 deletions tests/integration/file_convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package integration

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_FileConvert(t *testing.T) {
tests := []struct {
name string
convertCmdSourceFormat string
convertCmdDestinationFormat string
errorExpected bool
errorString string
}{
{
name: "Valid source and destination format",
convertCmdSourceFormat: "kong-gateway-2.x",
convertCmdDestinationFormat: "kong-gateway-3.x",
errorExpected: false,
},
{
name: "Invalid source format",
convertCmdSourceFormat: "random-value",
convertCmdDestinationFormat: "kong-gateway-3.x",
errorExpected: true,
errorString: "invalid value 'random-value' found for the 'from' flag." +
" Allowed values: [kong-gateway, kong-gateway-2.x]",
},
{
name: "Invalid destination format",
convertCmdSourceFormat: "kong-gateway-2.x",
convertCmdDestinationFormat: "random-value",
errorExpected: true,
errorString: "invalid value 'random-value' found for the 'to' flag." +
" Allowed values: [konnect, kong-gateway-3.x]",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
_, err := fileConvert(
"--from", tc.convertCmdSourceFormat,
"--to", tc.convertCmdDestinationFormat,
"--input-file", "testdata/file-convert/001-kong-gateway-config/kong-gateway-2-x.yaml",
)

if tc.errorExpected {
assert.Error(t, err)
assert.Contains(t, err.Error(), tc.errorString)
} else {
assert.NoError(t, err)
}
})
}
}
22 changes: 22 additions & 0 deletions tests/integration/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,28 @@ func lint(opts ...string) (string, error) {
return stripansi.Strip(string(out)), cmdErr
}

func fileConvert(opts ...string) (string, error) {
deckCmd := cmd.NewRootCmd()
args := []string{"file", "convert"}
if len(opts) > 0 {
args = append(args, opts...)
}
deckCmd.SetArgs(args)

// capture command output to be used during tests
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

cmdErr := deckCmd.ExecuteContext(context.Background())

w.Close()
out, _ := io.ReadAll(r)
os.Stdout = rescueStdout

return stripansi.Strip(string(out)), cmdErr
}

func ping(opts ...string) error {
deckCmd := cmd.NewRootCmd()
args := []string{"gateway", "ping"}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
_format_version: "1.1"
services:
- connect_timeout: 60000
id: 58076db2-28b6-423b-ba39-a797193017f7
host: mockbin.org
name: svc1
port: 80
protocol: http
read_timeout: 60000
retries: 5

0 comments on commit 8a293d9

Please sign in to comment.