-
Notifications
You must be signed in to change notification settings - Fork 950
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
test: add parseSysctl and parseNetwork unit test #697
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -329,3 +329,97 @@ func Test_parseDeviceMappings(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func Test_parseSysctls(t *testing.T) { | ||
type result struct { | ||
sysctls map[string]string | ||
err error | ||
} | ||
type TestCases struct { | ||
input []string | ||
expect result | ||
} | ||
|
||
testCases := []TestCases{ | ||
{ | ||
input: []string{"a=b"}, | ||
expect: result{ | ||
sysctls: map[string]string{"a": "b"}, | ||
err: nil, | ||
}, | ||
}, | ||
{ | ||
input: []string{"ab"}, | ||
expect: result{ | ||
sysctls: nil, | ||
err: fmt.Errorf("invalid sysctl: %s: sysctl must be in format of key=value", "ab"), | ||
}, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
sysctl, err := parseSysctls(testCase.input) | ||
assert.Equal(t, testCase.expect.sysctls, sysctl) | ||
assert.Equal(t, testCase.expect.err, err) | ||
} | ||
} | ||
|
||
func Test_parseNetwork(t *testing.T) { | ||
type net struct { | ||
name string | ||
ip string | ||
} | ||
type result struct { | ||
network net | ||
err error | ||
} | ||
type TestCases struct { | ||
input string | ||
expect result | ||
} | ||
|
||
testCases := []TestCases{ | ||
{ | ||
input: "", | ||
expect: result{ | ||
err: fmt.Errorf("invalid network: cannot be empty"), | ||
network: net{name: "", ip: ""}, | ||
}, | ||
}, | ||
{ | ||
input: "121.0.0.1", | ||
expect: result{ | ||
err: nil, | ||
network: net{name: "", ip: "121.0.0.1"}, | ||
}, | ||
}, | ||
{ | ||
input: "myHost", | ||
expect: result{ | ||
err: nil, | ||
network: net{name: "myHost", ip: ""}, | ||
}, | ||
}, | ||
{ | ||
input: "myHost:121.0.0.1", | ||
expect: result{ | ||
err: nil, | ||
network: net{name: "myHost", ip: "121.0.0.1"}, | ||
}, | ||
}, | ||
{ | ||
input: "myHost:myHost", | ||
expect: result{ | ||
err: fmt.Errorf("invalid network ip: %s", "myHost"), | ||
network: net{name: "", ip: ""}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
name, ip, error := parseNetwork(testCase.input) | ||
assert.Equal(t, testCase.expect.err, error) | ||
assert.Equal(t, testCase.expect.network.name, name) | ||
assert.Equal(t, testCase.expect.network.ip, ip) | ||
} | ||
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. @allencloud However, we need to know whether or not the type of the error is the exact one we are expecting for. In this case, I don't think the unit test framework generated by vscode is good enough. |
||
} |
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.
@allencloud As you can see, in the unit test case generated by vscode, we can only detect the existence of an error, true or false.
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.
yeah, I think it has some drawbacks.