2019-09-25 08:35:33 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Ceph-CSI Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
2023-06-02 09:49:22 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2019-09-25 08:35:33 +00:00
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2020-06-17 18:34:12 +00:00
|
|
|
"strings"
|
2019-09-25 08:35:33 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRoundOffBytes(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2019-09-25 08:35:33 +00:00
|
|
|
type args struct {
|
|
|
|
bytes int64
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"1MiB conversions",
|
|
|
|
args{
|
|
|
|
bytes: 1048576,
|
|
|
|
},
|
|
|
|
1048576,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1000kiB conversion",
|
|
|
|
args{
|
|
|
|
bytes: 1000,
|
|
|
|
},
|
|
|
|
1048576, // equal to 1MiB
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1.5Mib conversion",
|
|
|
|
args{
|
|
|
|
bytes: 1572864,
|
|
|
|
},
|
|
|
|
2097152, // equal to 2MiB
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1.1MiB conversion",
|
|
|
|
args{
|
|
|
|
bytes: 1153434,
|
|
|
|
},
|
|
|
|
2097152, // equal to 2MiB
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1.5GiB conversion",
|
|
|
|
args{
|
|
|
|
bytes: 1610612736,
|
|
|
|
},
|
|
|
|
2147483648, // equal to 2GiB
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1.1GiB conversion",
|
|
|
|
args{
|
|
|
|
bytes: 1181116007,
|
|
|
|
},
|
|
|
|
2147483648, // equal to 2GiB
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2024-04-26 08:35:01 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2024-04-26 08:35:01 +00:00
|
|
|
if got := RoundOffBytes(tt.args.bytes); got != tt.want {
|
|
|
|
t.Errorf("RoundOffBytes() = %v, want %v", got, tt.want)
|
2019-09-25 08:35:33 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoundOffVolSize(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2019-09-25 08:35:33 +00:00
|
|
|
type args struct {
|
|
|
|
size int64
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"1MiB conversions",
|
|
|
|
args{
|
|
|
|
size: 1048576,
|
|
|
|
},
|
|
|
|
1, // MiB
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1000kiB conversion",
|
|
|
|
args{
|
|
|
|
size: 1000,
|
|
|
|
},
|
|
|
|
1, // MiB
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1.5Mib conversion",
|
|
|
|
args{
|
|
|
|
size: 1572864,
|
|
|
|
},
|
|
|
|
2, // MiB
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1.1MiB conversion",
|
|
|
|
args{
|
|
|
|
size: 1153434,
|
|
|
|
},
|
|
|
|
2, // MiB
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1.5GiB conversion",
|
|
|
|
args{
|
|
|
|
size: 1610612736,
|
|
|
|
},
|
|
|
|
2048, // MiB
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1.1GiB conversion",
|
|
|
|
args{
|
|
|
|
size: 1181116007,
|
|
|
|
},
|
|
|
|
2048, // MiB
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2024-04-26 08:35:01 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2024-04-26 08:35:01 +00:00
|
|
|
if got := RoundOffVolSize(tt.args.size); got != tt.want {
|
|
|
|
t.Errorf("RoundOffVolSize() = %v, want %v", got, tt.want)
|
2019-09-25 08:35:33 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-06-17 18:30:09 +00:00
|
|
|
|
2020-06-22 06:58:47 +00:00
|
|
|
func TestGetKernelVersion(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2020-06-22 06:58:47 +00:00
|
|
|
version, err := GetKernelVersion()
|
2020-06-17 18:30:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get kernel version: %s", err)
|
|
|
|
}
|
|
|
|
if version == "" {
|
|
|
|
t.Error("version is empty, this is unexpected?!")
|
|
|
|
}
|
2020-06-17 18:34:12 +00:00
|
|
|
if strings.HasSuffix(version, "\x00") {
|
|
|
|
t.Error("version ends with \\x00 byte(s)")
|
|
|
|
}
|
2020-06-17 18:30:09 +00:00
|
|
|
}
|
2020-06-17 09:01:13 +00:00
|
|
|
|
|
|
|
func TestMountOptionsAdd(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2020-06-17 09:01:13 +00:00
|
|
|
moaTests := []struct {
|
|
|
|
name string
|
|
|
|
mountOptions string
|
|
|
|
option []string
|
|
|
|
result string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"add option to empty string",
|
|
|
|
"",
|
|
|
|
[]string{"new_option"},
|
|
|
|
"new_option",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"add empty option to string",
|
|
|
|
"orig_option",
|
|
|
|
[]string{""},
|
|
|
|
"orig_option",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"add empty option to empty string",
|
|
|
|
"",
|
|
|
|
[]string{""},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"add option to single option string",
|
|
|
|
"orig_option",
|
|
|
|
[]string{"new_option"},
|
|
|
|
"orig_option,new_option",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"add option to multi option string",
|
|
|
|
"orig_option,2nd_option",
|
|
|
|
[]string{"new_option"},
|
|
|
|
"orig_option,2nd_option,new_option",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"add redundant option to multi option string",
|
|
|
|
"orig_option,2nd_option",
|
|
|
|
[]string{"2nd_option"},
|
|
|
|
"orig_option,2nd_option",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"add option to multi option string starting with ,",
|
|
|
|
",orig_option,2nd_option",
|
|
|
|
[]string{"new_option"},
|
|
|
|
"orig_option,2nd_option,new_option",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"add option to multi option string with trailing ,",
|
|
|
|
"orig_option,2nd_option,",
|
|
|
|
[]string{"new_option"},
|
|
|
|
"orig_option,2nd_option,new_option",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"add options to multi option string",
|
|
|
|
"orig_option,2nd_option,",
|
|
|
|
[]string{"new_option", "another_option"},
|
|
|
|
"orig_option,2nd_option,new_option,another_option",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"add options (one redundant) to multi option string",
|
|
|
|
"orig_option,2nd_option,",
|
|
|
|
[]string{"new_option", "2nd_option", "another_option"},
|
|
|
|
"orig_option,2nd_option,new_option,another_option",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, moaTest := range moaTests {
|
|
|
|
t.Run(moaTest.name, func(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2024-04-26 08:35:01 +00:00
|
|
|
result := MountOptionsAdd(moaTest.mountOptions, moaTest.option...)
|
|
|
|
if result != moaTest.result {
|
|
|
|
t.Errorf("MountOptionsAdd(): %v, want %v", result, moaTest.result)
|
2020-06-17 09:01:13 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-07-06 20:43:04 +00:00
|
|
|
|
|
|
|
func TestParseKernelRelease(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
badReleases := []string{"x", "5", "5.", "5.4.", "5.x-2-oops", "4.1.x-7-oh", "5.12.x"}
|
|
|
|
for _, release := range badReleases {
|
|
|
|
_, _, _, _, err := parseKernelRelease(release)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("release %q must not be parsed successfully", release)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 12:21:05 +00:00
|
|
|
goodReleases := []string{
|
|
|
|
"5.12", "5.12xlinux", "5.1-2-yam", "3.1-5-x", "5.12.14", "5.12.14xlinux",
|
|
|
|
"5.12.14-xlinux", "5.12.14-99-x", "3.3x-3",
|
|
|
|
}
|
|
|
|
goodVersions := [][]int{
|
|
|
|
{5, 12, 0, 0},
|
|
|
|
{5, 12, 0, 0},
|
|
|
|
{5, 1, 0, 2},
|
|
|
|
{3, 1, 0, 5},
|
|
|
|
{5, 12, 14, 0},
|
|
|
|
{5, 12, 14, 0},
|
|
|
|
{5, 12, 14, 0},
|
|
|
|
{5, 12, 14, 99},
|
|
|
|
{3, 3, 0, 0},
|
|
|
|
}
|
2021-07-06 20:43:04 +00:00
|
|
|
for i, release := range goodReleases {
|
|
|
|
version, patchlevel, sublevel, extraversion, err := parseKernelRelease(release)
|
|
|
|
if err != nil {
|
2022-01-21 09:36:06 +00:00
|
|
|
t.Errorf("parsing error for release %q: %s", release, err)
|
2021-07-06 20:43:04 +00:00
|
|
|
}
|
|
|
|
good := goodVersions[i]
|
|
|
|
if version != good[0] || patchlevel != good[1] || sublevel != good[2] || extraversion != good[3] {
|
|
|
|
t.Errorf("release %q parsed incorrectly: expected (%d.%d.%d-%d), actual (%d.%d.%d-%d)",
|
|
|
|
release, good[0], good[1], good[2], good[3],
|
|
|
|
version, patchlevel, sublevel, extraversion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 07:09:45 +00:00
|
|
|
func TestCheckKernelSupport(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2020-06-22 07:09:45 +00:00
|
|
|
supportsQuota := []string{
|
|
|
|
"4.17.0",
|
|
|
|
"5.0.0",
|
|
|
|
"4.17.0-rc1",
|
|
|
|
"4.18.0-80.el8",
|
|
|
|
"3.10.0-1062.el7.x86_64", // 1st backport
|
|
|
|
"3.10.0-1062.4.1.el7.x86_64", // updated backport
|
|
|
|
}
|
|
|
|
|
|
|
|
noQuota := []string{
|
|
|
|
"2.6.32-754.15.3.el6.x86_64", // too old
|
|
|
|
"3.10.0-123.el7.x86_64", // too old for backport
|
|
|
|
"3.10.0-1062.4.1.el8.x86_64", // nonexisting RHEL-8 kernel
|
|
|
|
"3.11.0-123.el7.x86_64", // nonexisting RHEL-7 kernel
|
|
|
|
}
|
|
|
|
|
|
|
|
quotaSupport := []KernelVersion{
|
|
|
|
{4, 17, 0, 0, "", false}, // standard 4.17+ versions
|
|
|
|
{3, 10, 0, 1062, ".el7", true}, // RHEL-7.7
|
|
|
|
}
|
|
|
|
for _, kernel := range supportsQuota {
|
|
|
|
ok := CheckKernelSupport(kernel, quotaSupport)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("support expected for %s", kernel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kernel := range noQuota {
|
|
|
|
ok := CheckKernelSupport(kernel, quotaSupport)
|
|
|
|
if ok {
|
|
|
|
t.Errorf("no support expected for %s", kernel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
supportsDeepFlatten := []string{
|
2020-07-06 11:26:51 +00:00
|
|
|
"5.1.0", // 5.1+ supports deep-flatten
|
2020-06-22 07:09:45 +00:00
|
|
|
"5.3.0",
|
2020-07-06 11:26:51 +00:00
|
|
|
"4.18.0-193.9.1.el8_2.x86_64", // RHEL 8.2 kernel
|
2020-06-22 07:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
noDeepFlatten := []string{
|
|
|
|
"4.18.0", // too old
|
|
|
|
"3.10.0-123.el7.x86_64", // too old for backport
|
|
|
|
"3.10.0-1062.4.1.el8.x86_64", // nonexisting RHEL-8 kernel
|
|
|
|
"3.11.0-123.el7.x86_64", // nonexisting RHEL-7 kernel
|
|
|
|
}
|
|
|
|
|
|
|
|
deepFlattenSupport := []KernelVersion{
|
2020-07-06 11:26:51 +00:00
|
|
|
{5, 1, 0, 0, "", false}, // standard 5.1+ versions
|
|
|
|
{4, 18, 0, 193, ".el8", true}, // RHEL 8.2 backport
|
2020-06-22 07:09:45 +00:00
|
|
|
}
|
|
|
|
for _, kernel := range supportsDeepFlatten {
|
|
|
|
ok := CheckKernelSupport(kernel, deepFlattenSupport)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("support expected for %s", kernel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kernel := range noDeepFlatten {
|
|
|
|
ok := CheckKernelSupport(kernel, deepFlattenSupport)
|
|
|
|
if ok {
|
|
|
|
t.Errorf("no support expected for %s", kernel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-12 08:49:25 +00:00
|
|
|
|
|
|
|
func TestRoundOffCephFSVolSize(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
size int64
|
|
|
|
want int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"1000kiB conversion",
|
|
|
|
1000,
|
|
|
|
4194304, // 4 MiB
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1MiB conversions",
|
|
|
|
1048576,
|
|
|
|
4194304, // 4 MiB
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1.5Mib conversion",
|
|
|
|
1677722,
|
|
|
|
4194304, // 4 MiB
|
|
|
|
},
|
2023-10-10 11:09:37 +00:00
|
|
|
{
|
|
|
|
"101MB conversion",
|
|
|
|
101000000,
|
|
|
|
104857600, // 100MiB
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"500MB conversion",
|
|
|
|
500000000,
|
|
|
|
503316480, // 480MiB
|
|
|
|
},
|
2022-07-12 08:49:25 +00:00
|
|
|
{
|
|
|
|
"1023MiB conversion",
|
|
|
|
1072693248,
|
|
|
|
1073741824, // 1024 MiB
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1.5GiB conversion",
|
|
|
|
1585446912,
|
|
|
|
2147483648, // 2 GiB
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1555MiB conversion",
|
|
|
|
1630535680,
|
|
|
|
2147483648, // 2 GiB
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2024-04-26 08:35:01 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-07-12 08:49:25 +00:00
|
|
|
t.Parallel()
|
2024-04-26 08:35:01 +00:00
|
|
|
if got := RoundOffCephFSVolSize(tt.size); got != tt.want {
|
|
|
|
t.Errorf("RoundOffCephFSVolSize() = %v, want %v", got, tt.want)
|
2022-07-12 08:49:25 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|