rewrite TestParseRestartPolicy to use sub-tests

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-08-28 11:23:37 +02:00
parent fcb2b7920e
commit 4cce7bb2fc
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 62 additions and 25 deletions

View File

@ -700,34 +700,71 @@ func TestRunFlagsParseShmSize(t *testing.T) {
} }
func TestParseRestartPolicy(t *testing.T) { func TestParseRestartPolicy(t *testing.T) {
invalids := map[string]string{ tests := []struct {
"always:2:3": "invalid restart policy format: maximum retry count must be an integer", input string
"on-failure:invalid": "invalid restart policy format: maximum retry count must be an integer", expected container.RestartPolicy
} expectedErr string
valids := map[string]container.RestartPolicy{ }{
"": {}, {
"always": { input: "",
Name: "always",
MaximumRetryCount: 0,
}, },
"on-failure:1": { {
input: "no",
expected: container.RestartPolicy{
Name: "no",
},
},
{
input: "always",
expected: container.RestartPolicy{
Name: "always",
},
},
{
input: "always:1",
expected: container.RestartPolicy{
Name: "always",
MaximumRetryCount: 1,
},
},
{
input: "always:2:3",
expectedErr: "invalid restart policy format: maximum retry count must be an integer",
},
{
input: "on-failure:1",
expected: container.RestartPolicy{
Name: "on-failure", Name: "on-failure",
MaximumRetryCount: 1, MaximumRetryCount: 1,
}, },
},
{
input: "on-failure:invalid",
expectedErr: "invalid restart policy format: maximum retry count must be an integer",
},
{
input: "unless-stopped",
expected: container.RestartPolicy{
Name: "unless-stopped",
},
},
{
input: "unless-stopped:invalid",
expectedErr: "invalid restart policy format: maximum retry count must be an integer",
},
} }
for restart, expectedError := range invalids { for _, tc := range tests {
if _, _, _, err := parseRun([]string{fmt.Sprintf("--restart=%s", restart), "img", "cmd"}); err == nil || err.Error() != expectedError { tc := tc
t.Fatalf("Expected an error with message '%v' for %v, got %v", expectedError, restart, err) t.Run(tc.input, func(t *testing.T) {
} _, hostConfig, _, err := parseRun([]string{"--restart=" + tc.input, "img", "cmd"})
} if tc.expectedErr != "" {
for restart, expected := range valids { assert.Check(t, is.Nil(hostConfig))
_, hostconfig, _, err := parseRun([]string{fmt.Sprintf("--restart=%v", restart), "img", "cmd"}) assert.Check(t, is.Error(err, tc.expectedErr))
if err != nil { } else {
t.Fatal(err) assert.Check(t, is.DeepEqual(hostConfig.RestartPolicy, tc.expected))
} assert.Check(t, err)
if hostconfig.RestartPolicy != expected {
t.Fatalf("Expected %v, got %v", expected, hostconfig.RestartPolicy)
} }
})
} }
} }