-
Notifications
You must be signed in to change notification settings - Fork 24
Add ConvertCPUSharesToCPUWeight #17
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -534,16 +534,27 @@ func TestGetHugePageSizeImpl(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestConvertCPUSharesToCgroupV2Value(t *testing.T) { | ||
| cases := map[uint64]uint64{ | ||
| 0: 0, | ||
| 2: 1, | ||
| 262144: 10000, | ||
| func TestConvertCPUSharesToCPUWeight(t *testing.T) { | ||
| cases := []struct { | ||
| in, out uint64 | ||
| isErr bool | ||
| }{ | ||
| {in: 0, out: 0}, | ||
| {in: 2, out: 1}, | ||
| {in: 262144, out: 10000}, | ||
| {in: 1, isErr: true}, | ||
| {in: 262145, isErr: true}, | ||
| } | ||
| for i, expected := range cases { | ||
| got := ConvertCPUSharesToCgroupV2Value(i) | ||
| if got != expected { | ||
| t.Errorf("expected ConvertCPUSharesToCgroupV2Value(%d) to be %d, got %d", i, expected, got) | ||
| for _, tc := range cases { | ||
| got, err := ConvertCPUSharesToCPUWeight(tc.in) | ||
|
Comment on lines
+548
to
+549
Member
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 might as well make it a subtests; for the "should have no error" case, we can check both the error and the output. for _, tc := range cases {
t.Run(strconv.FormatUint(tc.in, 10), func(t *testing.T) {
got, err := ConvertCPUSharesToCPUWeight(tc.in)
if tc.isErr {
if err == nil {
t.Error("expected error, got nil")
}
} else {
if err != nil {
t.Errorf("expected no error, got: %v", err)
}
if got != tc.out {
t.Errorf("want %d, got %d", tc.out, got)
}
}
})
}Or, if we want the tests to be more descriptive on intent, and make sure we're matching the right error; func TestConvertCPUSharesToCPUWeight(t *testing.T) {
cases := []struct {
doc string
in, out uint64
expErr string
}{
{doc: "valid zero", in: 0, out: 0},
{doc: "valid min value", in: 2, out: 1},
{doc: "valid max value", in: 262144, out: 10000},
{doc: "out of bound min", in: 1, expErr: "cpu-shares should be between 2 and 262144"},
{doc: "out of bound max", in: 262145, expErr: "cpu-shares should be between 2 and 262144"},
}
for _, tc := range cases {
t.Run(tc.doc, func(t *testing.T) {
got, err := ConvertCPUSharesToCPUWeight(tc.in)
if tc.expErr != "" {
if err == nil || err.Error() != tc.expErr {
t.Errorf("expected error %q, got %v", tc.expErr, err)
}
return
}
if err != nil {
t.Errorf("expected no error, got: %v", err)
}
if got != tc.out {
t.Errorf("want %d, got %d", tc.out, got)
}
})
}
} |
||
| if tc.isErr { | ||
| if err == nil { | ||
| t.Errorf("ConvertCPUSharesToCPUWeight(%d): expected error, got nil", tc.in) | ||
| } | ||
| } else if err != nil { | ||
| t.Errorf("ConvertCPUSharesToCPUWeight(%d): expected error, got nil", tc.in) | ||
|
Member
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. The error message here doesn't match what's expected (we do not expect an error here) |
||
| } else if got != tc.out { | ||
| t.Errorf("ConvertCPUSharesToCPUWeight(%d): want %d, got %d", tc.in, tc.out, got) | ||
| } | ||
| } | ||
| } | ||
|
|
||
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.