mirror of
				https://github.com/k3s-io/kubernetes.git
				synced 2025-11-04 07:49:35 +00:00 
			
		
		
		
	The code was already 99% similar between RC and RS. This is a wild idea to try to deduplicate the two controllers in a type-safe manner without adding tons of boilerplate, and without using code generation. They are still separate resources. This is a refactor that isn't intended to change any behavior.
		
			
				
	
	
		
			185 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			185 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
/*
 | 
						|
Copyright 2017 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 replication
 | 
						|
 | 
						|
import (
 | 
						|
	"reflect"
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"k8s.io/api/core/v1"
 | 
						|
)
 | 
						|
 | 
						|
var (
 | 
						|
	imagePullBackOff v1.ReplicationControllerConditionType = "ImagePullBackOff"
 | 
						|
 | 
						|
	condImagePullBackOff = func() v1.ReplicationControllerCondition {
 | 
						|
		return v1.ReplicationControllerCondition{
 | 
						|
			Type:   imagePullBackOff,
 | 
						|
			Status: v1.ConditionTrue,
 | 
						|
			Reason: "NonExistentImage",
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	condReplicaFailure = func() v1.ReplicationControllerCondition {
 | 
						|
		return v1.ReplicationControllerCondition{
 | 
						|
			Type:   v1.ReplicationControllerReplicaFailure,
 | 
						|
			Status: v1.ConditionTrue,
 | 
						|
			Reason: "OtherFailure",
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	condReplicaFailure2 = func() v1.ReplicationControllerCondition {
 | 
						|
		return v1.ReplicationControllerCondition{
 | 
						|
			Type:   v1.ReplicationControllerReplicaFailure,
 | 
						|
			Status: v1.ConditionTrue,
 | 
						|
			Reason: "AnotherFailure",
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	status = func() *v1.ReplicationControllerStatus {
 | 
						|
		return &v1.ReplicationControllerStatus{
 | 
						|
			Conditions: []v1.ReplicationControllerCondition{condReplicaFailure()},
 | 
						|
		}
 | 
						|
	}
 | 
						|
)
 | 
						|
 | 
						|
func TestGetCondition(t *testing.T) {
 | 
						|
	exampleStatus := status()
 | 
						|
 | 
						|
	tests := []struct {
 | 
						|
		name string
 | 
						|
 | 
						|
		status     v1.ReplicationControllerStatus
 | 
						|
		condType   v1.ReplicationControllerConditionType
 | 
						|
		condStatus v1.ConditionStatus
 | 
						|
		condReason string
 | 
						|
 | 
						|
		expected bool
 | 
						|
	}{
 | 
						|
		{
 | 
						|
			name: "condition exists",
 | 
						|
 | 
						|
			status:   *exampleStatus,
 | 
						|
			condType: v1.ReplicationControllerReplicaFailure,
 | 
						|
 | 
						|
			expected: true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name: "condition does not exist",
 | 
						|
 | 
						|
			status:   *exampleStatus,
 | 
						|
			condType: imagePullBackOff,
 | 
						|
 | 
						|
			expected: false,
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	for _, test := range tests {
 | 
						|
		cond := GetCondition(test.status, test.condType)
 | 
						|
		exists := cond != nil
 | 
						|
		if exists != test.expected {
 | 
						|
			t.Errorf("%s: expected condition to exist: %t, got: %t", test.name, test.expected, exists)
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func TestSetCondition(t *testing.T) {
 | 
						|
	tests := []struct {
 | 
						|
		name string
 | 
						|
 | 
						|
		status *v1.ReplicationControllerStatus
 | 
						|
		cond   v1.ReplicationControllerCondition
 | 
						|
 | 
						|
		expectedStatus *v1.ReplicationControllerStatus
 | 
						|
	}{
 | 
						|
		{
 | 
						|
			name: "set for the first time",
 | 
						|
 | 
						|
			status: &v1.ReplicationControllerStatus{},
 | 
						|
			cond:   condReplicaFailure(),
 | 
						|
 | 
						|
			expectedStatus: &v1.ReplicationControllerStatus{Conditions: []v1.ReplicationControllerCondition{condReplicaFailure()}},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name: "simple set",
 | 
						|
 | 
						|
			status: &v1.ReplicationControllerStatus{Conditions: []v1.ReplicationControllerCondition{condImagePullBackOff()}},
 | 
						|
			cond:   condReplicaFailure(),
 | 
						|
 | 
						|
			expectedStatus: &v1.ReplicationControllerStatus{Conditions: []v1.ReplicationControllerCondition{condImagePullBackOff(), condReplicaFailure()}},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name: "overwrite",
 | 
						|
 | 
						|
			status: &v1.ReplicationControllerStatus{Conditions: []v1.ReplicationControllerCondition{condReplicaFailure()}},
 | 
						|
			cond:   condReplicaFailure2(),
 | 
						|
 | 
						|
			expectedStatus: &v1.ReplicationControllerStatus{Conditions: []v1.ReplicationControllerCondition{condReplicaFailure2()}},
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	for _, test := range tests {
 | 
						|
		SetCondition(test.status, test.cond)
 | 
						|
		if !reflect.DeepEqual(test.status, test.expectedStatus) {
 | 
						|
			t.Errorf("%s: expected status: %v, got: %v", test.name, test.expectedStatus, test.status)
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func TestRemoveCondition(t *testing.T) {
 | 
						|
	tests := []struct {
 | 
						|
		name string
 | 
						|
 | 
						|
		status   *v1.ReplicationControllerStatus
 | 
						|
		condType v1.ReplicationControllerConditionType
 | 
						|
 | 
						|
		expectedStatus *v1.ReplicationControllerStatus
 | 
						|
	}{
 | 
						|
		{
 | 
						|
			name: "remove from empty status",
 | 
						|
 | 
						|
			status:   &v1.ReplicationControllerStatus{},
 | 
						|
			condType: v1.ReplicationControllerReplicaFailure,
 | 
						|
 | 
						|
			expectedStatus: &v1.ReplicationControllerStatus{},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name: "simple remove",
 | 
						|
 | 
						|
			status:   &v1.ReplicationControllerStatus{Conditions: []v1.ReplicationControllerCondition{condReplicaFailure()}},
 | 
						|
			condType: v1.ReplicationControllerReplicaFailure,
 | 
						|
 | 
						|
			expectedStatus: &v1.ReplicationControllerStatus{},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name: "doesn't remove anything",
 | 
						|
 | 
						|
			status:   status(),
 | 
						|
			condType: imagePullBackOff,
 | 
						|
 | 
						|
			expectedStatus: status(),
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	for _, test := range tests {
 | 
						|
		RemoveCondition(test.status, test.condType)
 | 
						|
		if !reflect.DeepEqual(test.status, test.expectedStatus) {
 | 
						|
			t.Errorf("%s: expected status: %v, got: %v", test.name, test.expectedStatus, test.status)
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 |