-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
add some unit tests #8960
add some unit tests #8960
Changes from all commits
2d6e814
e608e4d
0b79c41
599ed6d
1a1be5f
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 |
---|---|---|
@@ -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, | ||
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. @geojaz this particular one could perhaps have some value. I suppose we want to preserve 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. If we were to add another role, it would at least be nice to see this test failing. 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. Great point +1 @olemarkus |
||
}, | ||
} | ||
for _, test := range tests { | ||
result, _ := DefaultInstanceGroupVolumeSize(test.role) | ||
if test.expected != result { | ||
t.Errorf("Expected %d, got %d", test.expected, result) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
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. FYI: I think this is a much more useful unit test than the volumes_test. |
||
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) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
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. I think this is also a fairly useful test... could you add some more spicy or exciting edge cases? For these relatively simple functions, I would prefer to exercise the hard cases and assume the libraries generally do what they're supposed to. |
||
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) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} |
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.
This test feels like it creates maintenance work rather than unit testing "kops logic". That is to say,
DefaultInstanceGroupVolumeSize
basically just returns different constants with a switch.IMO, changes that would break this test are likely to be intentional changes, so my vote would be to remove
volumes_test.go
.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.
Fine.