-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: improve error messaging when mandatory flag is missing in file convert (#1429) #1487
Merged
harshadixit12
merged 4 commits into
main
from
fix/mandatory-flag-error-message-for-file-convert
Jan 15, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8a293d9
fix: improve error messaging when mandatory flag is missing in file c…
harshadixit12 9266055
add a reusable validation function for strings
harshadixit12 b71e5de
assert converted file contents and address review feedback
harshadixit12 4fa47bf
limit file convert tests to yaml format for now
harshadixit12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can return from here to avoid the else block. |
||
|
||
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have sourceFormats and destinationFormats defined. We can use direct type-conversion on them.
Could you try this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried, direct type conversion does not seem to be supported for composite types (here array)