-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve error messaging when mandatory flag is missing in file c…
…onvert (#1429) (#1487) * fix: improve error messaging when mandatory flag is missing in file convert (#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. * add a reusable validation function for strings * assert converted file contents and address review feedback * limit file convert tests to yaml format for now
- Loading branch information
1 parent
2c9a599
commit 6472d17
Showing
6 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package integration | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
func Test_FileConvert(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
convertCmdSourceFormat string | ||
convertCmdDestinationFormat string | ||
errorExpected bool | ||
errorString string | ||
expectedOutputFile string | ||
}{ | ||
{ | ||
name: "Valid source and destination format", | ||
convertCmdSourceFormat: "kong-gateway-2.x", | ||
convertCmdDestinationFormat: "kong-gateway-3.x", | ||
errorExpected: false, | ||
expectedOutputFile: "testdata/file-convert/001-kong-gateway-config/kong-gateway-3-x.yaml", | ||
}, | ||
{ | ||
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) { | ||
output, 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) | ||
|
||
return | ||
} | ||
|
||
assert.NoError(t, err) | ||
|
||
var expectedOutput interface{} | ||
var currentOutput interface{} | ||
content, err := os.ReadFile(tc.expectedOutputFile) | ||
assert.NoError(t, err) | ||
|
||
err = yaml.Unmarshal(content, &expectedOutput) | ||
assert.NoError(t, err) | ||
err = yaml.Unmarshal([]byte(output), ¤tOutput) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedOutput, currentOutput) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
tests/integration/testdata/file-convert/001-kong-gateway-config/kong-gateway-2-x.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
10 changes: 10 additions & 0 deletions
10
tests/integration/testdata/file-convert/001-kong-gateway-config/kong-gateway-3-x.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
_format_version: "3.0" | ||
services: | ||
- connect_timeout: 60000 | ||
id: 58076db2-28b6-423b-ba39-a797193017f7 | ||
host: mockbin.org | ||
name: svc1 | ||
port: 80 | ||
protocol: http | ||
read_timeout: 60000 | ||
retries: 5 |