Skip to content

Commit bbfce3d

Browse files
committed
Update unit tests
1 parent ad1b061 commit bbfce3d

File tree

6 files changed

+112
-620
lines changed

6 files changed

+112
-620
lines changed

azure/scope/machine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ func (m *MachineScope) GetVMImage(ctx context.Context) (*infrav1.Image, error) {
725725
}
726726

727727
log.Info("No image specified for machine, using default Linux Image", "machine", m.AzureMachine.GetName())
728-
return svc.GetDefaultUbuntuImage(ctx, m.Location(), ptr.Deref(m.Machine.Spec.Version, ""))
728+
return svc.GetDefaultLinuxImage(ctx, m.Location(), ptr.Deref(m.Machine.Spec.Version, ""))
729729
}
730730

731731
// SetSubnetName defaults the AzureMachine subnet name to the name of one the subnets with the machine role when there is only one of them.

azure/scope/machine_test.go

Lines changed: 8 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,7 @@ func TestMachineScope_GetVMImage(t *testing.T) {
15471547
expectedErr string
15481548
}{
15491549
{
1550-
name: "returns AzureMachine image is found if present in the AzureMachine spec",
1550+
name: "returns AzureMachine image if present in the AzureMachine spec",
15511551
machineScope: MachineScope{
15521552
AzureMachine: &infrav1.AzureMachine{
15531553
ObjectMeta: metav1.ObjectMeta{
@@ -1566,7 +1566,7 @@ func TestMachineScope_GetVMImage(t *testing.T) {
15661566
expectedErr: "",
15671567
},
15681568
{
1569-
name: "if no image is specified and os specified is windows with version below 1.22, returns windows dockershim image",
1569+
name: "if no image is specified and os specified is windows, returns windows containerd image",
15701570
machineScope: MachineScope{
15711571
Machine: &clusterv1.Machine{
15721572
ObjectMeta: metav1.ObjectMeta{
@@ -1589,42 +1589,13 @@ func TestMachineScope_GetVMImage(t *testing.T) {
15891589
ClusterScoper: clusterMock,
15901590
},
15911591
want: func() *infrav1.Image {
1592-
image, _ := svc.GetDefaultWindowsImage(context.TODO(), "", "1.20.1", "dockershim", "")
1592+
image, _ := svc.GetDefaultWindowsImage(context.TODO(), "", "1.20.1", "containerd", "")
15931593
return image
15941594
}(),
15951595
expectedErr: "",
15961596
},
15971597
{
1598-
name: "if no image is specified and os specified is windows with version is 1.22+ with no annotation, returns windows containerd image",
1599-
machineScope: MachineScope{
1600-
Machine: &clusterv1.Machine{
1601-
ObjectMeta: metav1.ObjectMeta{
1602-
Name: "machine-name",
1603-
},
1604-
Spec: clusterv1.MachineSpec{
1605-
Version: ptr.To("1.22.1"),
1606-
},
1607-
},
1608-
AzureMachine: &infrav1.AzureMachine{
1609-
ObjectMeta: metav1.ObjectMeta{
1610-
Name: "machine-name",
1611-
},
1612-
Spec: infrav1.AzureMachineSpec{
1613-
OSDisk: infrav1.OSDisk{
1614-
OSType: azure.WindowsOS,
1615-
},
1616-
},
1617-
},
1618-
ClusterScoper: clusterMock,
1619-
},
1620-
want: func() *infrav1.Image {
1621-
image, _ := svc.GetDefaultWindowsImage(context.TODO(), "", "1.22.1", "containerd", "")
1622-
return image
1623-
}(),
1624-
expectedErr: "",
1625-
},
1626-
{
1627-
name: "if no image is specified and os specified is windows with version is 1.22+ with annotation dockershim, returns windows dockershim image",
1598+
name: "if no image is specified and os specified is windows with annotation dockershim, returns error",
16281599
machineScope: MachineScope{
16291600
Machine: &clusterv1.Machine{
16301601
ObjectMeta: metav1.ObjectMeta{
@@ -1653,71 +1624,10 @@ func TestMachineScope_GetVMImage(t *testing.T) {
16531624
image, _ := svc.GetDefaultWindowsImage(context.TODO(), "", "1.22.1", "dockershim", "")
16541625
return image
16551626
}(),
1656-
expectedErr: "",
1657-
},
1658-
{
1659-
name: "if no image is specified and os specified is windows with version is less and 1.22 with annotation dockershim, returns windows dockershim image",
1660-
machineScope: MachineScope{
1661-
Machine: &clusterv1.Machine{
1662-
ObjectMeta: metav1.ObjectMeta{
1663-
Name: "machine-name",
1664-
},
1665-
Spec: clusterv1.MachineSpec{
1666-
Version: ptr.To("1.21.1"),
1667-
},
1668-
},
1669-
AzureMachine: &infrav1.AzureMachine{
1670-
ObjectMeta: metav1.ObjectMeta{
1671-
Name: "machine-name",
1672-
Annotations: map[string]string{
1673-
"runtime": "dockershim",
1674-
},
1675-
},
1676-
Spec: infrav1.AzureMachineSpec{
1677-
OSDisk: infrav1.OSDisk{
1678-
OSType: azure.WindowsOS,
1679-
},
1680-
},
1681-
},
1682-
ClusterScoper: clusterMock,
1683-
},
1684-
want: func() *infrav1.Image {
1685-
image, _ := svc.GetDefaultWindowsImage(context.TODO(), "", "1.21.1", "dockershim", "")
1686-
return image
1687-
}(),
1688-
expectedErr: "",
1627+
expectedErr: "unsupported runtime dockershim",
16891628
},
16901629
{
1691-
name: "if no image is specified and os specified is windows with version is less and 1.22 with annotation containerd, returns error",
1692-
machineScope: MachineScope{
1693-
Machine: &clusterv1.Machine{
1694-
ObjectMeta: metav1.ObjectMeta{
1695-
Name: "machine-name",
1696-
},
1697-
Spec: clusterv1.MachineSpec{
1698-
Version: ptr.To("1.21.1"),
1699-
},
1700-
},
1701-
AzureMachine: &infrav1.AzureMachine{
1702-
ObjectMeta: metav1.ObjectMeta{
1703-
Name: "machine-name",
1704-
Annotations: map[string]string{
1705-
"runtime": "containerd",
1706-
},
1707-
},
1708-
Spec: infrav1.AzureMachineSpec{
1709-
OSDisk: infrav1.OSDisk{
1710-
OSType: azure.WindowsOS,
1711-
},
1712-
},
1713-
},
1714-
ClusterScoper: clusterMock,
1715-
},
1716-
want: nil,
1717-
expectedErr: "containerd image only supported in 1.22+",
1718-
},
1719-
{
1720-
name: "if no image is specified and os specified is windows with windowsServerVersion annotation set to 2019, retrurns 2019 image",
1630+
name: "if no image is specified and os specified is windows with windowsServerVersion annotation set to 2019, returns error",
17211631
machineScope: MachineScope{
17221632
Machine: &clusterv1.Machine{
17231633
ObjectMeta: metav1.ObjectMeta{
@@ -1746,7 +1656,7 @@ func TestMachineScope_GetVMImage(t *testing.T) {
17461656
image, _ := svc.GetDefaultWindowsImage(context.TODO(), "", "1.23.3", "", "windows-2019")
17471657
return image
17481658
}(),
1749-
expectedErr: "",
1659+
expectedErr: "unsupported osAndVersion windows-2019",
17501660
},
17511661
{
17521662
name: "if no image is specified and os specified is windows with windowsServerVersion annotation set to 2022, retrurns 2022 image",
@@ -1799,7 +1709,7 @@ func TestMachineScope_GetVMImage(t *testing.T) {
17991709
ClusterScoper: clusterMock,
18001710
},
18011711
want: func() *infrav1.Image {
1802-
image, _ := svc.GetDefaultUbuntuImage(context.TODO(), "", "1.20.1")
1712+
image, _ := svc.GetDefaultLinuxImage(context.TODO(), "", "1.20.1")
18031713
return image
18041714
}(),
18051715
expectedErr: "",

azure/scope/machinepool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ func (m *MachinePoolScope) GetVMImage(ctx context.Context) (*infrav1.Image, erro
800800
log.V(4).Info("No image specified for machine, using default Windows Image", "machine", m.MachinePool.GetName(), "runtime", runtime, "windowsServerVersion", windowsServerVersion)
801801
defaultImage, err = svc.GetDefaultWindowsImage(ctx, m.Location(), ptr.Deref(m.MachinePool.Spec.Template.Spec.Version, ""), runtime, windowsServerVersion)
802802
} else {
803-
defaultImage, err = svc.GetDefaultUbuntuImage(ctx, m.Location(), ptr.Deref(m.MachinePool.Spec.Template.Spec.Version, ""))
803+
defaultImage, err = svc.GetDefaultLinuxImage(ctx, m.Location(), ptr.Deref(m.MachinePool.Spec.Template.Spec.Version, ""))
804804
}
805805

806806
if err != nil {

azure/scope/machinepool_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -421,14 +421,10 @@ func TestMachinePoolScope_GetVMImage(t *testing.T) {
421421
Verify: func(g *WithT, amp *infrav1exp.AzureMachinePool, vmImage *infrav1.Image, err error) {
422422
g.Expect(err).NotTo(HaveOccurred())
423423
image := &infrav1.Image{
424-
Marketplace: &infrav1.AzureMarketplaceImage{
425-
ImagePlan: infrav1.ImagePlan{
426-
Publisher: "cncf-upstream",
427-
Offer: "capi",
428-
SKU: "k8s-1dot19dot11-ubuntu-1804",
429-
},
430-
Version: "latest",
431-
ThirdPartyImage: false,
424+
ComputeGallery: &infrav1.AzureComputeGalleryImage{
425+
Gallery: "capzed-489de9a5-a0a0-4e79-a806-ad5479ec43a5",
426+
Name: "capi-ubun2-2404",
427+
Version: "1.19.11",
432428
},
433429
}
434430
g.Expect(vmImage).To(Equal(image))

azure/services/virtualmachineimages/images.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,23 @@ func New(auth azure.Authorizer) (*Service, error) {
4545
}, nil
4646
}
4747

48-
// GetDefaultUbuntuImage returns the default image spec for Ubuntu.
49-
func (s *Service) GetDefaultUbuntuImage(ctx context.Context, _, k8sVersion string) (*infrav1.Image, error) {
50-
_, _, done := tele.StartSpanWithLogger(ctx, "azure.services.virtualmachineimages.GetDefaultUbuntuImage")
48+
// GetDefaultLinuxImage returns the default image spec for Ubuntu.
49+
func (s *Service) GetDefaultLinuxImage(ctx context.Context, _, k8sVersion string) (*infrav1.Image, error) {
50+
_, _, done := tele.StartSpanWithLogger(ctx, "azure.services.virtualmachineimages.GetDefaultLinuxImage")
5151
defer done()
5252

5353
v, err := semver.ParseTolerant(k8sVersion)
5454
if err != nil {
5555
return nil, errors.Wrapf(err, "unable to parse Kubernetes version \"%s\"", k8sVersion)
5656
}
5757

58-
defaultImage := &infrav1.Image{
58+
return &infrav1.Image{
5959
ComputeGallery: &infrav1.AzureComputeGalleryImage{
6060
Gallery: azure.DefaultPublicGalleryName,
6161
Name: azure.DefaultLinuxGalleryImageName,
6262
Version: v.String(),
6363
},
64-
}
65-
66-
return defaultImage, nil
64+
}, nil
6765
}
6866

6967
// GetDefaultWindowsImage returns the default image spec for Windows.
@@ -84,13 +82,11 @@ func (s *Service) GetDefaultWindowsImage(ctx context.Context, _, k8sVersion, run
8482
return nil, errors.Errorf("unsupported osAndVersion %s", osAndVersion)
8583
}
8684

87-
defaultImage := &infrav1.Image{
85+
return &infrav1.Image{
8886
ComputeGallery: &infrav1.AzureComputeGalleryImage{
8987
Gallery: azure.DefaultPublicGalleryName,
9088
Name: azure.DefaultWindowsGalleryImageName,
9189
Version: v.String(),
9290
},
93-
}
94-
95-
return defaultImage, nil
91+
}, nil
9692
}

0 commit comments

Comments
 (0)