Skip to content
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 1 commit into from
Feb 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions cli/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,97 @@ func Test_parseDeviceMappings(t *testing.T) {
})
Copy link
Contributor Author

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.

Copy link
Collaborator

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.

}
}

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)
}
Copy link
Contributor Author

@ZouRui89 ZouRui89 Feb 5, 2018

Choose a reason for hiding this comment

The 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.

}