Memory values with uppercase or other docker-supported units such as `1G`, `512M` or `1GiB` are now passed to docker unchanged instead of having an extra `m` appended, and the k3s scheduler interprets memory values with the same binary units so a value means the same thing on both schedulers. Invalid `--memory` and `--memory-swap` values are now rejected by `resource:limit` and `resource:reserve`. Closes #9153.
56 lines
2.4 KiB
Go
56 lines
2.4 KiB
Go
package resource
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestFormatResourceValueForDocker(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
key string
|
|
value string
|
|
expected string
|
|
wantErr bool
|
|
}{
|
|
{name: "unitless memory defaults to megabytes", key: "memory", value: "100", expected: "100m"},
|
|
{name: "unitless decimal memory defaults to megabytes", key: "memory", value: "1.5", expected: "1.5m"},
|
|
{name: "lowercase memory unit", key: "memory", value: "512m", expected: "512m"},
|
|
{name: "uppercase megabyte unit", key: "memory", value: "512M", expected: "512M"},
|
|
{name: "uppercase gigabyte unit", key: "memory", value: "1G", expected: "1G"},
|
|
{name: "uppercase kilobyte unit", key: "memory", value: "1024K", expected: "1024K"},
|
|
{name: "terabyte unit", key: "memory", value: "1t", expected: "1t"},
|
|
{name: "two letter unit", key: "memory", value: "512MB", expected: "512MB"},
|
|
{name: "binary unit", key: "memory", value: "1GiB", expected: "1GiB"},
|
|
{name: "byte unit", key: "memory", value: "100b", expected: "100b"},
|
|
{name: "memory rejects negative values", key: "memory", value: "-1", wantErr: true},
|
|
{name: "memory rejects extra arguments", key: "memory", value: "100G --foo", wantErr: true},
|
|
{name: "memory rejects invalid units", key: "memory", value: "1Gm", wantErr: true},
|
|
{name: "unlimited memory-swap", key: "memory-swap", value: "-1", expected: "-1"},
|
|
{name: "unitless memory-swap stays in bytes", key: "memory-swap", value: "512", expected: "512"},
|
|
{name: "uppercase memory-swap unit", key: "memory-swap", value: "2G", expected: "2G"},
|
|
{name: "memory-swap rejects invalid units", key: "memory-swap", value: "1x", wantErr: true},
|
|
{name: "memory-swap rejects other negative values", key: "memory-swap", value: "-2", wantErr: true},
|
|
{name: "other keys are passed through", key: "cpus", value: "1.5", expected: "1.5"},
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
value, err := formatResourceValueForDocker(testCase.key, testCase.value)
|
|
if testCase.wantErr {
|
|
if err == nil {
|
|
t.Fatalf("expected an error for %s=%s, got %s", testCase.key, testCase.value, value)
|
|
}
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
t.Fatalf("unexpected error for %s=%s: %v", testCase.key, testCase.value, err)
|
|
}
|
|
|
|
if value != testCase.expected {
|
|
t.Fatalf("expected %s, got %s", testCase.expected, value)
|
|
}
|
|
})
|
|
}
|
|
}
|