@@ -76,7 +76,7 @@ func TestCreateEC2NodeClassFromKopsLaunchTemplateInfo(t *testing.T) {
7676 ec2NodeClassString , err := CreateEC2NodeClassFromKopsLaunchTemplateInfo (kopsCluster , kmp , kmp .Name , terraformOutputDir )
7777 if tc .expectedError != nil {
7878 g .Expect (err ).To (HaveOccurred ())
79- g .Expect (err ) .To (Equal (tc .expectedError ))
79+ g .Expect (err . Error ()) .To (Equal (tc .expectedError . Error () ))
8080 } else {
8181 g .Expect (err ).ToNot (HaveOccurred ())
8282 expectedOutput , err := os .ReadFile (tc .expectedOutputFile )
@@ -232,135 +232,7 @@ func TestCreateEC2NodeClassV1FromKopsLaunchTemplateInfo(t *testing.T) {
232232 ec2NodeClass , err := CreateEC2NodeClassV1FromKopsLaunchTemplateInfo (kopsCluster , kmp , kmp .Name , terraformOutputDir )
233233 if tc .expectedError != nil {
234234 g .Expect (err ).To (HaveOccurred ())
235- g .Expect (err ).To (Equal (tc .expectedError ))
236- } else {
237- g .Expect (err ).ToNot (HaveOccurred ())
238- g .Expect (err ).NotTo (HaveOccurred ())
239- g .Expect (ec2NodeClass ).To (Equal (tc .expectedEC2NodeClass ))
240-
241- }
242- })
243- }
244-
245- }
246-
247- func TestCreateEC2NodeClassV1FromKopsLaunchTemplateInfo (t * testing.T ) {
248- testCases := []struct {
249- description string
250- kopsMachinePoolFunction func (kopsMachinePool * infrastructurev1alpha1.KopsMachinePool ) * infrastructurev1alpha1.KopsMachinePool
251- userData string
252- expectedError error
253- expectedEC2NodeClass * karpenterv1.EC2NodeClass
254- }{
255- {
256- description : "should return the populated ec2 node class v1" ,
257- expectedEC2NodeClass : & karpenterv1.EC2NodeClass {
258- TypeMeta : metav1.TypeMeta {
259- APIVersion : "karpenter.k8s.aws/v1" ,
260- Kind : "EC2NodeClass" ,
261- },
262- ObjectMeta : metav1.ObjectMeta {
263- Name : "test-machine-pool" ,
264- Labels : map [string ]string {
265- "kops.k8s.io/managed-by" : "kops-controller" ,
266- },
267- },
268- Spec : karpenterv1.EC2NodeClassSpec {
269- AMIFamily : & karpenterv1 .AMIFamilyCustom ,
270- AMISelectorTerms : []karpenterv1.AMISelectorTerm {
271- {
272- Name : "ubuntu-v1" ,
273- },
274- },
275- MetadataOptions : & karpenterv1.MetadataOptions {
276- HTTPEndpoint : helpers .StringPtr ("enabled" ),
277- HTTPProtocolIPv6 : helpers .StringPtr ("disabled" ),
278- HTTPPutResponseHopLimit : helpers .Int64Ptr (3 ),
279- HTTPTokens : helpers .StringPtr ("required" ),
280- },
281- AssociatePublicIPAddress : helpers .BoolPtr (false ),
282- BlockDeviceMappings : []* karpenterv1.BlockDeviceMapping {
283- {
284- DeviceName : helpers .StringPtr ("/dev/sda1" ),
285- EBS : defaultBlockDeviceV1 ,
286- RootVolume : true ,
287- },
288- },
289- Role : "nodes.test-cluster.test.k8s.cluster" ,
290- SecurityGroupSelectorTerms : []karpenterv1.SecurityGroupSelectorTerm {
291- {
292- Name : "nodes.test-cluster.test.k8s.cluster" ,
293- },
294- {
295- Tags : map [string ]string {
296- "karpenter/test-cluster.test.k8s.cluster/test-machine-pool" : "true" ,
297- },
298- },
299- },
300- SubnetSelectorTerms : []karpenterv1.SubnetSelectorTerm {
301- {
302- Tags : map [string ]string {
303- "kops.k8s.io/instance-group/test-machine-pool" : "*" ,
304- "kubernetes.io/cluster/test-cluster.test.k8s.cluster" : "*" ,
305- },
306- },
307- },
308- Tags : map [string ]string {
309- "k8s.io/cluster-autoscaler/node-template/label/node-role.kubernetes.io/node" : "" ,
310- "Name" : "test-cluster.test.k8s.cluster/test-machine-pool" ,
311- "kops.k8s.io/instancegroup" : "test-machine-pool" ,
312- "KubernetesCluster" : "test-cluster.test.k8s.cluster" ,
313- },
314- Kubelet : & karpenterv1.KubeletConfiguration {},
315- UserData : helpers .StringPtr ("dummy content" ),
316- },
317- },
318- userData : "dummy content" ,
319- },
320- {
321- description : "should fail with a invalid image" ,
322- kopsMachinePoolFunction : func (kopsMachinePool * infrastructurev1alpha1.KopsMachinePool ) * infrastructurev1alpha1.KopsMachinePool {
323- kopsMachinePool .Spec .KopsInstanceGroupSpec .Image = "invalid-image"
324- return kopsMachinePool
325- },
326- expectedError : fmt .Errorf ("invalid image format, should receive image source" ),
327- userData : "dummy content" ,
328- },
329- {
330- description : "should fail with empty user data" ,
331- expectedError : fmt .Errorf ("user data file is empty" ),
332- },
333- }
334- RegisterFailHandler (Fail )
335- g := NewWithT (t )
336-
337- for _ , tc := range testCases {
338-
339- t .Run (tc .description , func (t * testing.T ) {
340-
341- kopsCluster := helpers .NewKopsCluster ("test-cluster" )
342-
343- kmp := helpers .NewKopsMachinePool ("test-machine-pool" , "default" , "test-cluster" )
344-
345- terraformOutputDir := filepath .Join (os .TempDir (), kopsCluster .Name )
346-
347- err := os .RemoveAll (terraformOutputDir )
348- g .Expect (err ).NotTo (HaveOccurred ())
349-
350- err = os .MkdirAll (filepath .Join (terraformOutputDir , "data" ), os .ModePerm )
351- g .Expect (err ).NotTo (HaveOccurred ())
352-
353- err = os .WriteFile (terraformOutputDir + "/data/aws_launch_template_" + kmp .Name + "." + kopsCluster .Name + "_user_data" , []byte (tc .userData ), 0644 )
354- g .Expect (err ).NotTo (HaveOccurred ())
355-
356- if tc .kopsMachinePoolFunction != nil {
357- kmp = tc .kopsMachinePoolFunction (kmp )
358- }
359-
360- ec2NodeClass , err := CreateEC2NodeClassV1FromKopsLaunchTemplateInfo (kopsCluster , kmp , kmp .Name , terraformOutputDir )
361- if tc .expectedError != nil {
362- g .Expect (err ).To (HaveOccurred ())
363- g .Expect (err ).To (Equal (tc .expectedError ))
235+ g .Expect (err .Error ()).To (Equal (tc .expectedError .Error ()))
364236 } else {
365237 g .Expect (err ).ToNot (HaveOccurred ())
366238 g .Expect (err ).NotTo (HaveOccurred ())
0 commit comments