mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 07:27:21 +00:00
Using assertions
Using assertions for unit tests: 1. cmd/kube-controller-manager/app/controller_manager_test.go 2. pkg/controller/bootstrap/jws_test.go 3. pkg/controller/cloud/node_controller_test.go 4. pkg/controller/controller_utils_test.go
This commit is contained in:
@@ -18,12 +18,9 @@ package cloud
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/client-go/kubernetes/fake"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
@@ -39,6 +36,9 @@ import (
|
||||
"k8s.io/kubernetes/pkg/controller/testutil"
|
||||
kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis"
|
||||
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestEnsureNodeExistsByProviderIDOrNodeName(t *testing.T) {
|
||||
@@ -129,26 +129,20 @@ func TestEnsureNodeExistsByProviderIDOrNodeName(t *testing.T) {
|
||||
|
||||
instances, _ := fc.Instances()
|
||||
exists, err := ensureNodeExistsByProviderIDOrExternalID(instances, tc.node)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, tc.expectedCalls, fc.Calls,
|
||||
"expected cloud provider methods `%v` to be called but `%v` was called ",
|
||||
tc.expectedCalls, fc.Calls)
|
||||
|
||||
if !reflect.DeepEqual(fc.Calls, tc.expectedCalls) {
|
||||
t.Errorf("expected cloud provider methods `%v` to be called but `%v` was called ", tc.expectedCalls, fc.Calls)
|
||||
}
|
||||
assert.False(t, tc.existsByProviderID && tc.existsByProviderID != exists,
|
||||
"expected exist by provider id to be `%t` but got `%t`",
|
||||
tc.existsByProviderID, exists)
|
||||
|
||||
if tc.existsByProviderID && tc.existsByProviderID != exists {
|
||||
t.Errorf("expected exist by provider id to be `%t` but got `%t`", tc.existsByProviderID, exists)
|
||||
}
|
||||
|
||||
if tc.existsByNodeName && tc.existsByNodeName != exists {
|
||||
t.Errorf("expected exist by node name to be `%t` but got `%t`", tc.existsByNodeName, exists)
|
||||
}
|
||||
|
||||
if !tc.existsByNodeName && !tc.existsByProviderID && exists {
|
||||
t.Error("node is not supposed to exist")
|
||||
}
|
||||
assert.False(t, tc.existsByNodeName && tc.existsByNodeName != exists,
|
||||
"expected exist by node name to be `%t` but got `%t`", tc.existsByNodeName, exists)
|
||||
|
||||
assert.False(t, !tc.existsByNodeName && !tc.existsByProviderID && exists,
|
||||
"node is not supposed to exist")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -239,9 +233,9 @@ func TestNodeDeleted(t *testing.T) {
|
||||
case <-time.After(wait.ForeverTestTimeout):
|
||||
t.Errorf("Timed out waiting %v for node to be deleted", wait.ForeverTestTimeout)
|
||||
}
|
||||
if len(fnh.DeletedNodes) != 1 || fnh.DeletedNodes[0].Name != "node0" {
|
||||
t.Errorf("Node was not deleted")
|
||||
}
|
||||
|
||||
assert.Equal(t, 1, len(fnh.DeletedNodes), "Node was not deleted")
|
||||
assert.Equal(t, "node0", fnh.DeletedNodes[0].Name, "Node was not deleted")
|
||||
}
|
||||
|
||||
// This test checks that a node with the external cloud provider taint is cloudprovider initialized
|
||||
@@ -314,14 +308,9 @@ func TestNodeInitialized(t *testing.T) {
|
||||
|
||||
cloudNodeController.AddCloudNode(fnh.Existing[0])
|
||||
|
||||
if len(fnh.UpdatedNodes) != 1 || fnh.UpdatedNodes[0].Name != "node0" {
|
||||
t.Errorf("Node was not updated")
|
||||
}
|
||||
|
||||
if len(fnh.UpdatedNodes[0].Spec.Taints) != 0 {
|
||||
t.Errorf("Node Taint was not removed after cloud init")
|
||||
}
|
||||
|
||||
assert.Equal(t, 1, len(fnh.UpdatedNodes), "Node was not updated")
|
||||
assert.Equal(t, "node0", fnh.UpdatedNodes[0].Name, "Node was not updated")
|
||||
assert.Equal(t, 0, len(fnh.UpdatedNodes[0].Spec.Taints), "Node Taint was not removed after cloud init")
|
||||
}
|
||||
|
||||
// This test checks that a node without the external cloud provider taint are NOT cloudprovider initialized
|
||||
@@ -383,10 +372,7 @@ func TestNodeIgnored(t *testing.T) {
|
||||
eventBroadcaster.StartLogging(glog.Infof)
|
||||
|
||||
cloudNodeController.AddCloudNode(fnh.Existing[0])
|
||||
|
||||
if len(fnh.UpdatedNodes) != 0 {
|
||||
t.Errorf("Node was wrongly updated")
|
||||
}
|
||||
assert.Equal(t, 0, len(fnh.UpdatedNodes), "Node was wrongly updated")
|
||||
|
||||
}
|
||||
|
||||
@@ -461,13 +447,9 @@ func TestGCECondition(t *testing.T) {
|
||||
|
||||
cloudNodeController.AddCloudNode(fnh.Existing[0])
|
||||
|
||||
if len(fnh.UpdatedNodes) != 1 && fnh.UpdatedNodes[0].Name != "node0" {
|
||||
t.Errorf("Node was not updated")
|
||||
}
|
||||
|
||||
if len(fnh.UpdatedNodes[0].Status.Conditions) != 2 {
|
||||
t.Errorf("No new conditions were added for GCE")
|
||||
}
|
||||
assert.Equal(t, 1, len(fnh.UpdatedNodes), "Node was not updated")
|
||||
assert.Equal(t, "node0", fnh.UpdatedNodes[0].Name, "Node was not updated")
|
||||
assert.Equal(t, 2, len(fnh.UpdatedNodes[0].Status.Conditions), "No new conditions were added for GCE")
|
||||
|
||||
conditionAdded := false
|
||||
for _, cond := range fnh.UpdatedNodes[0].Status.Conditions {
|
||||
@@ -476,9 +458,7 @@ func TestGCECondition(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
if !conditionAdded {
|
||||
t.Errorf("Network Route Condition for GCE not added by external cloud intializer")
|
||||
}
|
||||
assert.True(t, conditionAdded, "Network Route Condition for GCE not added by external cloud intializer")
|
||||
}
|
||||
|
||||
// This test checks that a node with the external cloud provider taint is cloudprovider initialized and
|
||||
@@ -557,21 +537,14 @@ func TestZoneInitialized(t *testing.T) {
|
||||
|
||||
cloudNodeController.AddCloudNode(fnh.Existing[0])
|
||||
|
||||
if len(fnh.UpdatedNodes) != 1 && fnh.UpdatedNodes[0].Name != "node0" {
|
||||
t.Errorf("Node was not updated")
|
||||
}
|
||||
|
||||
if len(fnh.UpdatedNodes[0].ObjectMeta.Labels) != 2 {
|
||||
t.Errorf("Node label for Region and Zone were not set")
|
||||
}
|
||||
|
||||
if fnh.UpdatedNodes[0].ObjectMeta.Labels[kubeletapis.LabelZoneRegion] != "us-west" {
|
||||
t.Errorf("Node Region not correctly updated")
|
||||
}
|
||||
|
||||
if fnh.UpdatedNodes[0].ObjectMeta.Labels[kubeletapis.LabelZoneFailureDomain] != "us-west-1a" {
|
||||
t.Errorf("Node FailureDomain not correctly updated")
|
||||
}
|
||||
assert.Equal(t, 1, len(fnh.UpdatedNodes), "Node was not updated")
|
||||
assert.Equal(t, "node0", fnh.UpdatedNodes[0].Name, "Node was not updated")
|
||||
assert.Equal(t, 2, len(fnh.UpdatedNodes[0].ObjectMeta.Labels),
|
||||
"Node label for Region and Zone were not set")
|
||||
assert.Equal(t, "us-west", fnh.UpdatedNodes[0].ObjectMeta.Labels[kubeletapis.LabelZoneRegion],
|
||||
"Node Region not correctly updated")
|
||||
assert.Equal(t, "us-west-1a", fnh.UpdatedNodes[0].ObjectMeta.Labels[kubeletapis.LabelZoneFailureDomain],
|
||||
"Node FailureDomain not correctly updated")
|
||||
}
|
||||
|
||||
// This test checks that a node with the external cloud provider taint is cloudprovider initialized and
|
||||
@@ -655,13 +628,9 @@ func TestNodeAddresses(t *testing.T) {
|
||||
|
||||
cloudNodeController.AddCloudNode(fnh.Existing[0])
|
||||
|
||||
if len(fnh.UpdatedNodes) != 1 && fnh.UpdatedNodes[0].Name != "node0" {
|
||||
t.Errorf("Node was not updated")
|
||||
}
|
||||
|
||||
if len(fnh.UpdatedNodes[0].Status.Addresses) != 3 {
|
||||
t.Errorf("Node status not updated")
|
||||
}
|
||||
assert.Equal(t, 1, len(fnh.UpdatedNodes), "Node was not updated")
|
||||
assert.Equal(t, "node0", fnh.UpdatedNodes[0].Name, "Node was not updated")
|
||||
assert.Equal(t, 3, len(fnh.UpdatedNodes[0].Status.Addresses), "Node status not updated")
|
||||
|
||||
fakeCloud.Addresses = []v1.NodeAddress{
|
||||
{
|
||||
@@ -680,9 +649,8 @@ func TestNodeAddresses(t *testing.T) {
|
||||
|
||||
updatedNodes := fnh.GetUpdatedNodesCopy()
|
||||
|
||||
if len(updatedNodes[0].Status.Addresses) != 2 {
|
||||
t.Errorf("Node Addresses not correctly updated")
|
||||
}
|
||||
assert.Equal(t, 2, len(updatedNodes[0].Status.Addresses), "Node Addresses not correctly updated")
|
||||
|
||||
}
|
||||
|
||||
// This test checks that a node with the external cloud provider taint is cloudprovider initialized and
|
||||
@@ -775,13 +743,9 @@ func TestNodeProvidedIPAddresses(t *testing.T) {
|
||||
|
||||
cloudNodeController.AddCloudNode(fnh.Existing[0])
|
||||
|
||||
if len(fnh.UpdatedNodes) != 1 && fnh.UpdatedNodes[0].Name != "node0" {
|
||||
t.Errorf("Node was not updated")
|
||||
}
|
||||
|
||||
if len(fnh.UpdatedNodes[0].Status.Addresses) != 1 {
|
||||
t.Errorf("Node status unexpectedly updated")
|
||||
}
|
||||
assert.Equal(t, 1, len(fnh.UpdatedNodes), "Node was not updated")
|
||||
assert.Equal(t, "node0", fnh.UpdatedNodes[0].Name, "Node was not updated")
|
||||
assert.Equal(t, 1, len(fnh.UpdatedNodes[0].Status.Addresses), "Node status unexpectedly updated")
|
||||
|
||||
cloudNodeController.Run()
|
||||
|
||||
@@ -789,9 +753,8 @@ func TestNodeProvidedIPAddresses(t *testing.T) {
|
||||
|
||||
updatedNodes := fnh.GetUpdatedNodesCopy()
|
||||
|
||||
if len(updatedNodes[0].Status.Addresses) != 1 || updatedNodes[0].Status.Addresses[0].Address != "10.0.0.1" {
|
||||
t.Errorf("Node Addresses not correctly updated")
|
||||
}
|
||||
assert.Equal(t, 1, len(updatedNodes[0].Status.Addresses), 1, "Node Addresses not correctly updated")
|
||||
assert.Equal(t, "10.0.0.1", updatedNodes[0].Status.Addresses[0].Address, "Node Addresses not correctly updated")
|
||||
}
|
||||
|
||||
// Tests that node address changes are detected correctly
|
||||
@@ -816,9 +779,9 @@ func TestNodeAddressesChangeDetected(t *testing.T) {
|
||||
Address: "132.143.154.163",
|
||||
},
|
||||
}
|
||||
if nodeAddressesChangeDetected(addressSet1, addressSet2) {
|
||||
t.Errorf("Node address changes are not detected correctly")
|
||||
}
|
||||
|
||||
assert.False(t, nodeAddressesChangeDetected(addressSet1, addressSet2),
|
||||
"Node address changes are not detected correctly")
|
||||
|
||||
addressSet1 = []v1.NodeAddress{
|
||||
{
|
||||
@@ -840,9 +803,9 @@ func TestNodeAddressesChangeDetected(t *testing.T) {
|
||||
Address: "132.143.154.163",
|
||||
},
|
||||
}
|
||||
if !nodeAddressesChangeDetected(addressSet1, addressSet2) {
|
||||
t.Errorf("Node address changes are not detected correctly")
|
||||
}
|
||||
|
||||
assert.True(t, nodeAddressesChangeDetected(addressSet1, addressSet2),
|
||||
"Node address changes are not detected correctly")
|
||||
|
||||
addressSet1 = []v1.NodeAddress{
|
||||
{
|
||||
@@ -868,9 +831,9 @@ func TestNodeAddressesChangeDetected(t *testing.T) {
|
||||
Address: "132.143.154.164",
|
||||
},
|
||||
}
|
||||
if !nodeAddressesChangeDetected(addressSet1, addressSet2) {
|
||||
t.Errorf("Node address changes are not detected correctly")
|
||||
}
|
||||
|
||||
assert.True(t, nodeAddressesChangeDetected(addressSet1, addressSet2),
|
||||
"Node address changes are not detected correctly")
|
||||
|
||||
addressSet1 = []v1.NodeAddress{
|
||||
{
|
||||
@@ -896,9 +859,9 @@ func TestNodeAddressesChangeDetected(t *testing.T) {
|
||||
Address: "hostname.zone.region.aws.test",
|
||||
},
|
||||
}
|
||||
if !nodeAddressesChangeDetected(addressSet1, addressSet2) {
|
||||
t.Errorf("Node address changes are not detected correctly")
|
||||
}
|
||||
|
||||
assert.True(t, nodeAddressesChangeDetected(addressSet1, addressSet2),
|
||||
"Node address changes are not detected correctly")
|
||||
|
||||
addressSet1 = []v1.NodeAddress{
|
||||
{
|
||||
@@ -920,9 +883,9 @@ func TestNodeAddressesChangeDetected(t *testing.T) {
|
||||
Address: "132.143.154.163",
|
||||
},
|
||||
}
|
||||
if !nodeAddressesChangeDetected(addressSet1, addressSet2) {
|
||||
t.Errorf("Node address changes are not detected correctly")
|
||||
}
|
||||
|
||||
assert.True(t, nodeAddressesChangeDetected(addressSet1, addressSet2),
|
||||
"Node address changes are not detected correctly")
|
||||
}
|
||||
|
||||
// This test checks that a node is set with the correct providerID
|
||||
@@ -1003,13 +966,9 @@ func TestNodeProviderID(t *testing.T) {
|
||||
|
||||
cloudNodeController.AddCloudNode(fnh.Existing[0])
|
||||
|
||||
if len(fnh.UpdatedNodes) != 1 || fnh.UpdatedNodes[0].Name != "node0" {
|
||||
t.Errorf("Node was not updated")
|
||||
}
|
||||
|
||||
if fnh.UpdatedNodes[0].Spec.ProviderID != "test://12345" {
|
||||
t.Errorf("Node ProviderID not set correctly")
|
||||
}
|
||||
assert.Equal(t, 1, len(fnh.UpdatedNodes), "Node was not updated")
|
||||
assert.Equal(t, "node0", fnh.UpdatedNodes[0].Name, "Node was not updated")
|
||||
assert.Equal(t, "test://12345", fnh.UpdatedNodes[0].Spec.ProviderID, "Node ProviderID not set correctly")
|
||||
}
|
||||
|
||||
// This test checks that a node's provider ID will not be overwritten
|
||||
@@ -1091,12 +1050,8 @@ func TestNodeProviderIDAlreadySet(t *testing.T) {
|
||||
|
||||
cloudNodeController.AddCloudNode(fnh.Existing[0])
|
||||
|
||||
if len(fnh.UpdatedNodes) != 1 || fnh.UpdatedNodes[0].Name != "node0" {
|
||||
t.Errorf("Node was not updated")
|
||||
}
|
||||
|
||||
assert.Equal(t, 1, len(fnh.UpdatedNodes), "Node was not updated")
|
||||
assert.Equal(t, "node0", fnh.UpdatedNodes[0].Name, "Node was not updated")
|
||||
// CCM node controller should not overwrite provider if it's already set
|
||||
if fnh.UpdatedNodes[0].Spec.ProviderID != "test-provider-id" {
|
||||
t.Errorf("Node ProviderID not set correctly")
|
||||
}
|
||||
assert.Equal(t, "test-provider-id", fnh.UpdatedNodes[0].Spec.ProviderID, "Node ProviderID not set correctly")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user