...

Source file src/k8s.io/kubernetes/pkg/controller/replicaset/replica_set_utils_test.go

Documentation: k8s.io/kubernetes/pkg/controller/replicaset

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // If you make changes to this file, you should also make the corresponding change in ReplicationController.
    18  
    19  package replicaset
    20  
    21  import (
    22  	"fmt"
    23  	"reflect"
    24  	"testing"
    25  
    26  	apps "k8s.io/api/apps/v1"
    27  	"k8s.io/api/core/v1"
    28  )
    29  
    30  func TestCalculateStatus(t *testing.T) {
    31  	labelMap := map[string]string{"name": "foo"}
    32  	fullLabelMap := map[string]string{"name": "foo", "type": "production"}
    33  	notFullyLabelledRS := newReplicaSet(1, labelMap)
    34  	// Set replica num to 2 for status condition testing (diff < 0, diff > 0)
    35  	fullyLabelledRS := newReplicaSet(2, fullLabelMap)
    36  	longMinReadySecondsRS := newReplicaSet(1, fullLabelMap)
    37  	longMinReadySecondsRS.Spec.MinReadySeconds = 3600
    38  
    39  	rsStatusTests := []struct {
    40  		name                     string
    41  		replicaset               *apps.ReplicaSet
    42  		filteredPods             []*v1.Pod
    43  		expectedReplicaSetStatus apps.ReplicaSetStatus
    44  	}{
    45  		{
    46  			"1 fully labelled pod",
    47  			fullyLabelledRS,
    48  			[]*v1.Pod{
    49  				newPod("pod1", fullyLabelledRS, v1.PodRunning, nil, true),
    50  			},
    51  			apps.ReplicaSetStatus{
    52  				Replicas:             1,
    53  				FullyLabeledReplicas: 1,
    54  				ReadyReplicas:        1,
    55  				AvailableReplicas:    1,
    56  			},
    57  		},
    58  		{
    59  			"1 not fully labelled pod",
    60  			notFullyLabelledRS,
    61  			[]*v1.Pod{
    62  				newPod("pod1", notFullyLabelledRS, v1.PodRunning, nil, true),
    63  			},
    64  			apps.ReplicaSetStatus{
    65  				Replicas:             1,
    66  				FullyLabeledReplicas: 0,
    67  				ReadyReplicas:        1,
    68  				AvailableReplicas:    1,
    69  			},
    70  		},
    71  		{
    72  			"2 fully labelled pods",
    73  			fullyLabelledRS,
    74  			[]*v1.Pod{
    75  				newPod("pod1", fullyLabelledRS, v1.PodRunning, nil, true),
    76  				newPod("pod2", fullyLabelledRS, v1.PodRunning, nil, true),
    77  			},
    78  			apps.ReplicaSetStatus{
    79  				Replicas:             2,
    80  				FullyLabeledReplicas: 2,
    81  				ReadyReplicas:        2,
    82  				AvailableReplicas:    2,
    83  			},
    84  		},
    85  		{
    86  			"2 not fully labelled pods",
    87  			notFullyLabelledRS,
    88  			[]*v1.Pod{
    89  				newPod("pod1", notFullyLabelledRS, v1.PodRunning, nil, true),
    90  				newPod("pod2", notFullyLabelledRS, v1.PodRunning, nil, true),
    91  			},
    92  			apps.ReplicaSetStatus{
    93  				Replicas:             2,
    94  				FullyLabeledReplicas: 0,
    95  				ReadyReplicas:        2,
    96  				AvailableReplicas:    2,
    97  			},
    98  		},
    99  		{
   100  			"1 fully labelled pod, 1 not fully labelled pod",
   101  			notFullyLabelledRS,
   102  			[]*v1.Pod{
   103  				newPod("pod1", notFullyLabelledRS, v1.PodRunning, nil, true),
   104  				newPod("pod2", fullyLabelledRS, v1.PodRunning, nil, true),
   105  			},
   106  			apps.ReplicaSetStatus{
   107  				Replicas:             2,
   108  				FullyLabeledReplicas: 1,
   109  				ReadyReplicas:        2,
   110  				AvailableReplicas:    2,
   111  			},
   112  		},
   113  		{
   114  			"1 non-ready pod",
   115  			fullyLabelledRS,
   116  			[]*v1.Pod{
   117  				newPod("pod1", fullyLabelledRS, v1.PodPending, nil, true),
   118  			},
   119  			apps.ReplicaSetStatus{
   120  				Replicas:             1,
   121  				FullyLabeledReplicas: 1,
   122  				ReadyReplicas:        0,
   123  				AvailableReplicas:    0,
   124  			},
   125  		},
   126  		{
   127  			"1 ready but non-available pod",
   128  			longMinReadySecondsRS,
   129  			[]*v1.Pod{
   130  				newPod("pod1", longMinReadySecondsRS, v1.PodRunning, nil, true),
   131  			},
   132  			apps.ReplicaSetStatus{
   133  				Replicas:             1,
   134  				FullyLabeledReplicas: 1,
   135  				ReadyReplicas:        1,
   136  				AvailableReplicas:    0,
   137  			},
   138  		},
   139  	}
   140  
   141  	for _, test := range rsStatusTests {
   142  		replicaSetStatus := calculateStatus(test.replicaset, test.filteredPods, nil)
   143  		if !reflect.DeepEqual(replicaSetStatus, test.expectedReplicaSetStatus) {
   144  			t.Errorf("%s: unexpected replicaset status: expected %v, got %v", test.name, test.expectedReplicaSetStatus, replicaSetStatus)
   145  		}
   146  	}
   147  }
   148  
   149  func TestCalculateStatusConditions(t *testing.T) {
   150  	labelMap := map[string]string{"name": "foo"}
   151  	rs := newReplicaSet(2, labelMap)
   152  	replicaFailureRS := newReplicaSet(10, labelMap)
   153  	replicaFailureRS.Status.Conditions = []apps.ReplicaSetCondition{
   154  		{
   155  			Type:   apps.ReplicaSetReplicaFailure,
   156  			Status: v1.ConditionTrue,
   157  		},
   158  	}
   159  
   160  	rsStatusConditionTests := []struct {
   161  		name                         string
   162  		replicaset                   *apps.ReplicaSet
   163  		filteredPods                 []*v1.Pod
   164  		manageReplicasErr            error
   165  		expectedReplicaSetConditions []apps.ReplicaSetCondition
   166  	}{
   167  
   168  		{
   169  			"manageReplicasErr != nil && failureCond == nil, diff < 0",
   170  			rs,
   171  			[]*v1.Pod{
   172  				newPod("pod1", rs, v1.PodRunning, nil, true),
   173  			},
   174  			fmt.Errorf("fake manageReplicasErr"),
   175  			[]apps.ReplicaSetCondition{
   176  				{
   177  					Type:    apps.ReplicaSetReplicaFailure,
   178  					Status:  v1.ConditionTrue,
   179  					Reason:  "FailedCreate",
   180  					Message: "fake manageReplicasErr",
   181  				},
   182  			},
   183  		},
   184  		{
   185  			"manageReplicasErr != nil && failureCond == nil, diff > 0",
   186  			rs,
   187  			[]*v1.Pod{
   188  				newPod("pod1", rs, v1.PodRunning, nil, true),
   189  				newPod("pod2", rs, v1.PodRunning, nil, true),
   190  				newPod("pod3", rs, v1.PodRunning, nil, true),
   191  			},
   192  			fmt.Errorf("fake manageReplicasErr"),
   193  			[]apps.ReplicaSetCondition{
   194  				{
   195  					Type:    apps.ReplicaSetReplicaFailure,
   196  					Status:  v1.ConditionTrue,
   197  					Reason:  "FailedDelete",
   198  					Message: "fake manageReplicasErr",
   199  				},
   200  			},
   201  		},
   202  		{
   203  			"manageReplicasErr == nil && failureCond != nil",
   204  			replicaFailureRS,
   205  			[]*v1.Pod{
   206  				newPod("pod1", replicaFailureRS, v1.PodRunning, nil, true),
   207  			},
   208  			nil,
   209  			nil,
   210  		},
   211  		{
   212  			"manageReplicasErr != nil && failureCond != nil",
   213  			replicaFailureRS,
   214  			[]*v1.Pod{
   215  				newPod("pod1", replicaFailureRS, v1.PodRunning, nil, true),
   216  			},
   217  			fmt.Errorf("fake manageReplicasErr"),
   218  			[]apps.ReplicaSetCondition{
   219  				{
   220  					Type:   apps.ReplicaSetReplicaFailure,
   221  					Status: v1.ConditionTrue,
   222  				},
   223  			},
   224  		},
   225  		{
   226  			"manageReplicasErr == nil && failureCond == nil",
   227  			rs,
   228  			[]*v1.Pod{
   229  				newPod("pod1", rs, v1.PodRunning, nil, true),
   230  			},
   231  			nil,
   232  			nil,
   233  		},
   234  	}
   235  
   236  	for _, test := range rsStatusConditionTests {
   237  		replicaSetStatus := calculateStatus(test.replicaset, test.filteredPods, test.manageReplicasErr)
   238  		// all test cases have at most 1 status condition
   239  		if len(replicaSetStatus.Conditions) > 0 {
   240  			test.expectedReplicaSetConditions[0].LastTransitionTime = replicaSetStatus.Conditions[0].LastTransitionTime
   241  		}
   242  		if !reflect.DeepEqual(replicaSetStatus.Conditions, test.expectedReplicaSetConditions) {
   243  			t.Errorf("%s: unexpected replicaset status: expected %v, got %v", test.name, test.expectedReplicaSetConditions, replicaSetStatus.Conditions)
   244  		}
   245  	}
   246  }
   247  

View as plain text