diff --git a/cmd/file_convert.go b/cmd/file_convert.go index 37debd53e..415bec59c 100644 --- a/cmd/file_convert.go +++ b/cmd/file_convert.go @@ -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} diff --git a/tests/integration/file_convert_test.go b/tests/integration/file_convert_test.go new file mode 100644 index 000000000..c3ac15c2a --- /dev/null +++ b/tests/integration/file_convert_test.go @@ -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) + } + }) + } +} diff --git a/tests/integration/test_utils.go b/tests/integration/test_utils.go index 41a23b40e..41718b7bb 100644 --- a/tests/integration/test_utils.go +++ b/tests/integration/test_utils.go @@ -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"} diff --git a/tests/integration/testdata/file-convert/001-kong-gateway-config/kong-gateway-2-x.yaml b/tests/integration/testdata/file-convert/001-kong-gateway-config/kong-gateway-2-x.yaml new file mode 100644 index 000000000..398199a29 --- /dev/null +++ b/tests/integration/testdata/file-convert/001-kong-gateway-config/kong-gateway-2-x.yaml @@ -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 \ No newline at end of file