@@ -22,6 +22,8 @@ import (
2222 . "github.com/onsi/gomega"
2323)
2424
25+ var validSSHPublicKey = generateSSHPublicKey ()
26+
2527func TestAzureMachine_ValidateCreate (t * testing.T ) {
2628 g := NewWithT (t )
2729
@@ -60,6 +62,21 @@ func TestAzureMachine_ValidateCreate(t *testing.T) {
6062 machine : createMachineWithImageByID (t , "" ),
6163 wantErr : true ,
6264 },
65+ {
66+ name : "azuremachine with valid SSHPublicKey" ,
67+ machine : createMachineWithSSHPublicKey (t , validSSHPublicKey ),
68+ wantErr : false ,
69+ },
70+ {
71+ name : "azuremachine without SSHPublicKey" ,
72+ machine : createMachineWithSSHPublicKey (t , "" ),
73+ wantErr : true ,
74+ },
75+ {
76+ name : "azuremachine with invalid SSHPublicKey" ,
77+ machine : createMachineWithSSHPublicKey (t , "invalid ssh key" ),
78+ wantErr : true ,
79+ },
6380 }
6481 for _ , tc := range tests {
6582 t .Run (tc .name , func (t * testing.T ) {
@@ -73,6 +90,64 @@ func TestAzureMachine_ValidateCreate(t *testing.T) {
7390 }
7491}
7592
93+ func TestAzureMachine_ValidateUpdate (t * testing.T ) {
94+ g := NewWithT (t )
95+
96+ tests := []struct {
97+ name string
98+ oldMachine * AzureMachine
99+ machine * AzureMachine
100+ wantErr bool
101+ }{
102+ {
103+ name : "azuremachine with valid SSHPublicKey" ,
104+ oldMachine : createMachineWithSSHPublicKey (t , "" ),
105+ machine : createMachineWithSSHPublicKey (t , validSSHPublicKey ),
106+ wantErr : false ,
107+ },
108+ {
109+ name : "azuremachine without SSHPublicKey" ,
110+ oldMachine : createMachineWithSSHPublicKey (t , "" ),
111+ machine : createMachineWithSSHPublicKey (t , "" ),
112+ wantErr : true ,
113+ },
114+ {
115+ name : "azuremachine with invalid SSHPublicKey" ,
116+ oldMachine : createMachineWithSSHPublicKey (t , "" ),
117+ machine : createMachineWithSSHPublicKey (t , "invalid ssh key" ),
118+ wantErr : true ,
119+ },
120+ }
121+ for _ , tc := range tests {
122+ t .Run (tc .name , func (t * testing.T ) {
123+ err := tc .machine .ValidateUpdate (tc .oldMachine )
124+ if tc .wantErr {
125+ g .Expect (err ).To (HaveOccurred ())
126+ } else {
127+ g .Expect (err ).NotTo (HaveOccurred ())
128+ }
129+ })
130+ }
131+ }
132+
133+ func TestAzureMachine_Default (t * testing.T ) {
134+ g := NewWithT (t )
135+
136+ type test struct {
137+ machine * AzureMachine
138+ }
139+
140+ existingPublicKey := validSSHPublicKey
141+ publicKeyExistTest := test {machine : createMachineWithSSHPublicKey (t , existingPublicKey )}
142+ publicKeyNotExistTest := test {machine : createMachineWithSSHPublicKey (t , "" )}
143+
144+ publicKeyExistTest .machine .Default ()
145+ g .Expect (publicKeyExistTest .machine .Spec .SSHPublicKey ).To (Equal (existingPublicKey ))
146+
147+ publicKeyNotExistTest .machine .Default ()
148+ g .Expect (publicKeyNotExistTest .machine .Spec .SSHPublicKey ).To (Not (BeEmpty ()))
149+ }
150+
76151func createMachineWithSharedImage (t * testing.T , subscriptionID , resourceGroup , name , gallery , version string ) * AzureMachine {
77152 image := & Image {
78153 SharedGallery : & AzureSharedGalleryImage {
@@ -86,7 +161,8 @@ func createMachineWithSharedImage(t *testing.T, subscriptionID, resourceGroup, n
86161
87162 return & AzureMachine {
88163 Spec : AzureMachineSpec {
89- Image : image ,
164+ Image : image ,
165+ SSHPublicKey : validSSHPublicKey ,
90166 },
91167 }
92168
@@ -104,7 +180,8 @@ func createMachineWithtMarketPlaceImage(t *testing.T, publisher, offer, sku, ver
104180
105181 return & AzureMachine {
106182 Spec : AzureMachineSpec {
107- Image : image ,
183+ Image : image ,
184+ SSHPublicKey : validSSHPublicKey ,
108185 },
109186 }
110187}
@@ -116,7 +193,8 @@ func createMachineWithImageByID(t *testing.T, imageID string) *AzureMachine {
116193
117194 return & AzureMachine {
118195 Spec : AzureMachineSpec {
119- Image : image ,
196+ Image : image ,
197+ SSHPublicKey : validSSHPublicKey ,
120198 },
121199 }
122200}
0 commit comments