Merge pull request #8960 from q384566678/add-ut

add some unit tests
This commit is contained in:
Kubernetes Prow Robot 2020-05-16 15:37:36 -07:00 committed by GitHub
commit 66b5322037
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 239 additions and 2 deletions

View File

@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
@ -7,3 +7,10 @@ go_library(
visibility = ["//visibility:public"],
deps = ["//pkg/apis/kops:go_default_library"],
)
go_test(
name = "go_default_test",
srcs = ["volumes_test.go"],
embed = [":go_default_library"],
deps = ["//pkg/apis/kops:go_default_library"],
)

View File

@ -0,0 +1,41 @@
/*
Copyright 2020 The Kubernetes 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
http://www.apache.org/licenses/LICENSE-2.0
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 defaults
import (
"testing"
"k8s.io/kops/pkg/apis/kops"
)
func TestDefaultInstanceGroupVolumeSize(t *testing.T) {
tests := []struct {
role kops.InstanceGroupRole
expected int32
}{
{
role: "Node2",
expected: -1,
},
}
for _, test := range tests {
result, _ := DefaultInstanceGroupVolumeSize(test.role)
if test.expected != result {
t.Errorf("Expected %d, got %d", test.expected, result)
}
}
}

View File

@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
@ -16,3 +16,13 @@ go_library(
"//vendor/k8s.io/client-go/util/homedir:go_default_library",
],
)
go_test(
name = "go_default_test",
srcs = [
"equals_test.go",
"hash_test.go",
"sanitize_test.go",
],
embed = [":go_default_library"],
)

View File

@ -0,0 +1,83 @@
/*
Copyright 2020 The Kubernetes 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
http://www.apache.org/licenses/LICENSE-2.0
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 utils
import (
"testing"
)
func TestStringSlicesEqual(t *testing.T) {
tests := []struct {
l []string
r []string
expected bool
}{
{
l: []string{"a", "b"},
r: []string{"a"},
expected: false,
},
{
l: []string{"a", "b"},
r: []string{"a", "c"},
expected: false,
},
{
l: []string{"a", "b"},
r: []string{"a", "b"},
expected: true,
},
}
for _, test := range tests {
result := StringSlicesEqual(test.l, test.r)
if test.expected != result {
t.Errorf("Expected %v, got %v", test.expected, result)
}
}
}
func TestStringSlicesEqualIgnoreOrder(t *testing.T) {
tests := []struct {
l []string
r []string
expected bool
}{
{
l: []string{"a", "b"},
r: []string{"a"},
expected: false,
},
{
l: []string{"a", "b"},
r: []string{"a", "c"},
expected: false,
},
{
l: []string{"a", "b"},
r: []string{"b", "a"},
expected: true,
},
}
for _, test := range tests {
result := StringSlicesEqualIgnoreOrder(test.l, test.r)
if test.expected != result {
t.Errorf("Expected %v, got %v", test.expected, result)
}
}
}

View File

@ -0,0 +1,52 @@
/*
Copyright 2020 The Kubernetes 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
http://www.apache.org/licenses/LICENSE-2.0
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 utils
import (
"testing"
)
func TestHashString(t *testing.T) {
tests := []struct {
s string
expectedStr string
}{
{
s: "test",
expectedStr: "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3",
},
{
s: "~!*/?",
expectedStr: "783c2ea26549ce90df7cd90a27bf9094c226c406",
},
{
s: "测试1",
expectedStr: "6d972f7f1450aba1f7496f685c3c656c4fca9624",
},
{
s: "-897668",
expectedStr: "c8facb588b36948d5da0e3a7e16977a701331b0a",
},
}
for _, test := range tests {
result, _ := HashString(test.s)
if test.expectedStr != result {
t.Errorf("Expected %s, got %s", test.expectedStr, result)
}
}
}

View File

@ -0,0 +1,44 @@
/*
Copyright 2020 The Kubernetes 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
http://www.apache.org/licenses/LICENSE-2.0
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 utils
import (
"testing"
)
func TestSanitizeString(t *testing.T) {
tests := []struct {
s string
expected string
}{
{
s: "te_ST-01",
expected: "te_ST-01",
},
{
s: "(/%test!&*)~!@#$%^&<>?/+",
expected: "___test_________________",
},
}
for _, test := range tests {
result := SanitizeString(test.s)
if test.expected != result {
t.Errorf("Expected %s, got %s", test.expected, result)
}
}
}