@@ -6,7 +6,6 @@ package kubernetes
66import (
77 "context"
88 "encoding/json"
9- "fmt"
109 "strings"
1110 "testing"
1211 "time"
@@ -17,10 +16,7 @@ import (
1716 corev1 "k8s.io/api/core/v1"
1817 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1918 k8stypes "k8s.io/apimachinery/pkg/types"
20- "k8s.io/apimachinery/pkg/util/intstr"
21- appsv1apply "k8s.io/client-go/applyconfigurations/apps/v1"
2219 corev1apply "k8s.io/client-go/applyconfigurations/core/v1"
23- metav1apply "k8s.io/client-go/applyconfigurations/meta/v1"
2420 "k8s.io/client-go/kubernetes"
2521 "k8s.io/client-go/kubernetes/fake"
2622 "k8s.io/client-go/rest"
@@ -44,107 +40,6 @@ func (m *mockPlatformDetector) DetectPlatform(_ *rest.Config) (Platform, error)
4440 return m .platform , m .err
4541}
4642
47- func TestStatefulSetApplyAssociativeListReorderingDoesNotChangeGeneration (t * testing.T ) {
48- t .Parallel ()
49-
50- ctx := t .Context ()
51- clientset := fake .NewClientset ()
52-
53- buildApply := func (envOrder []string , portOrder []int32 ) * appsv1apply.StatefulSetApplyConfiguration {
54- envValues := map [string ]string {
55- "ALPHA" : "1" ,
56- "BETA" : "2" ,
57- }
58-
59- envVars := make ([]* corev1apply.EnvVarApplyConfiguration , 0 , len (envOrder ))
60- for _ , name := range envOrder {
61- envVars = append (envVars , corev1apply .EnvVar ().WithName (name ).WithValue (envValues [name ]))
62- }
63-
64- ports := make ([]* corev1apply.ContainerPortApplyConfiguration , 0 , len (portOrder ))
65- for _ , port := range portOrder {
66- ports = append (ports , corev1apply .ContainerPort ().
67- WithContainerPort (port ).
68- WithProtocol (corev1 .ProtocolTCP ))
69- }
70-
71- return appsv1apply .StatefulSet ("reordered" , defaultNamespace ).
72- WithSpec (appsv1apply .StatefulSetSpec ().
73- WithServiceName ("reordered" ).
74- WithReplicas (1 ).
75- WithSelector (metav1apply .LabelSelector ().
76- WithMatchLabels (map [string ]string {"app" : "reordered" })).
77- WithTemplate (corev1apply .PodTemplateSpec ().
78- WithLabels (map [string ]string {"app" : "reordered" }).
79- WithSpec (corev1apply .PodSpec ().
80- WithContainers (corev1apply .Container ().
81- WithName ("mcp" ).
82- WithImage ("test-image" ).
83- WithEnv (envVars ... ).
84- WithPorts (ports ... )))))
85- }
86-
87- first , err := clientset .AppsV1 ().StatefulSets (defaultNamespace ).Apply (
88- ctx ,
89- buildApply ([]string {"ALPHA" , "BETA" }, []int32 {8080 , 9090 }),
90- metav1.ApplyOptions {FieldManager : "test-manager" , Force : true },
91- )
92- require .NoError (t , err )
93-
94- second , err := clientset .AppsV1 ().StatefulSets (defaultNamespace ).Apply (
95- ctx ,
96- buildApply ([]string {"BETA" , "ALPHA" }, []int32 {9090 , 8080 }),
97- metav1.ApplyOptions {FieldManager : "test-manager" , Force : true },
98- )
99- require .NoError (t , err )
100-
101- assert .Equal (t , first .Generation , second .Generation )
102- assert .Len (t , second .Spec .Template .Spec .Containers , 1 )
103- assert .Len (t , second .Spec .Template .Spec .Containers [0 ].Env , 2 )
104- assert .Len (t , second .Spec .Template .Spec .Containers [0 ].Ports , 2 )
105- }
106-
107- func TestServiceApplyAssociativeListReorderingDoesNotChangeGeneration (t * testing.T ) {
108- t .Parallel ()
109-
110- ctx := t .Context ()
111- clientset := fake .NewClientset ()
112-
113- buildApply := func (portOrder []int32 ) * corev1apply.ServiceApplyConfiguration {
114- ports := make ([]* corev1apply.ServicePortApplyConfiguration , 0 , len (portOrder ))
115- for _ , port := range portOrder {
116- ports = append (ports , corev1apply .ServicePort ().
117- WithName (fmt .Sprintf ("port-%d" , port )).
118- WithPort (port ).
119- WithTargetPort (intstr .FromInt32 (port )).
120- WithProtocol (corev1 .ProtocolTCP ))
121- }
122-
123- return corev1apply .Service ("reordered-svc" , defaultNamespace ).
124- WithSpec (corev1apply .ServiceSpec ().
125- WithSelector (map [string ]string {"app" : "reordered" }).
126- WithPorts (ports ... ).
127- WithType (corev1 .ServiceTypeClusterIP ))
128- }
129-
130- first , err := clientset .CoreV1 ().Services (defaultNamespace ).Apply (
131- ctx ,
132- buildApply ([]int32 {8080 , 9090 }),
133- metav1.ApplyOptions {FieldManager : "test-manager" , Force : true },
134- )
135- require .NoError (t , err )
136-
137- second , err := clientset .CoreV1 ().Services (defaultNamespace ).Apply (
138- ctx ,
139- buildApply ([]int32 {9090 , 8080 }),
140- metav1.ApplyOptions {FieldManager : "test-manager" , Force : true },
141- )
142- require .NoError (t , err )
143-
144- assert .Equal (t , first .Generation , second .Generation )
145- assert .Len (t , second .Spec .Ports , 2 )
146- }
147-
14843// TestCreateContainerWithPodTemplatePatch tests that the pod template patch is correctly applied
14944func TestCreateContainerWithPodTemplatePatch (t * testing.T ) {
15045 t .Parallel ()
0 commit comments