Skip to content

Commit 9f4589d

Browse files
committed
Add tests for machine names
1 parent 736a04e commit 9f4589d

File tree

3 files changed

+204
-1
lines changed

3 files changed

+204
-1
lines changed

cloud/scope/machine_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package scope
18+
19+
import (
20+
"testing"
21+
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
24+
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
25+
azure "sigs.k8s.io/cluster-api-provider-azure/cloud"
26+
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
27+
)
28+
29+
func TestMachineScope_Name(t *testing.T) {
30+
type fields struct {
31+
ClusterScoper azure.ClusterScoper
32+
AzureMachine *infrav1.AzureMachine
33+
}
34+
tests := []struct {
35+
name string
36+
machineScope MachineScope
37+
want string
38+
testLength bool
39+
}{
40+
{
41+
name: "linux can be any length",
42+
machineScope: MachineScope{
43+
AzureMachine: &infrav1.AzureMachine{
44+
ObjectMeta: metav1.ObjectMeta{
45+
Name: "machine-with-really-really-long-name",
46+
},
47+
Spec: infrav1.AzureMachineSpec{
48+
OSDisk: infrav1.OSDisk{
49+
OSType: "Linux",
50+
},
51+
},
52+
},
53+
},
54+
want: "machine-with-really-really-long-name",
55+
},
56+
{
57+
name: "Windows name with long MachineName and short cluster name",
58+
machineScope: MachineScope{
59+
ClusterScoper: &ClusterScope{
60+
Cluster: &clusterv1.Cluster{
61+
ObjectMeta: metav1.ObjectMeta{
62+
Name: "cluster",
63+
},
64+
},
65+
},
66+
AzureMachine: &infrav1.AzureMachine{
67+
TypeMeta: metav1.TypeMeta{},
68+
ObjectMeta: metav1.ObjectMeta{
69+
Name: "machine-90123456",
70+
},
71+
Spec: infrav1.AzureMachineSpec{
72+
OSDisk: infrav1.OSDisk{
73+
OSType: "Windows",
74+
},
75+
},
76+
Status: infrav1.AzureMachineStatus{},
77+
},
78+
},
79+
want: "cluster-23456",
80+
testLength: true,
81+
},
82+
{
83+
name: "Windows name with long MachineName and long cluster name",
84+
machineScope: MachineScope{
85+
ClusterScoper: &ClusterScope{
86+
Cluster: &clusterv1.Cluster{
87+
ObjectMeta: metav1.ObjectMeta{
88+
Name: "cluster8901234",
89+
},
90+
},
91+
},
92+
AzureMachine: &infrav1.AzureMachine{
93+
TypeMeta: metav1.TypeMeta{},
94+
ObjectMeta: metav1.ObjectMeta{
95+
Name: "machine-90123456",
96+
},
97+
Spec: infrav1.AzureMachineSpec{
98+
OSDisk: infrav1.OSDisk{
99+
OSType: "Windows",
100+
},
101+
},
102+
Status: infrav1.AzureMachineStatus{},
103+
},
104+
},
105+
want: "cluster89-23456",
106+
testLength: true,
107+
},
108+
}
109+
for _, tt := range tests {
110+
t.Run(tt.name, func(t *testing.T) {
111+
112+
got := tt.machineScope.Name()
113+
if got != tt.want {
114+
t.Errorf("MachineScope.Name() = %v, want %v", got, tt.want)
115+
}
116+
117+
if tt.testLength && len(got) > 15 {
118+
t.Errorf("Length of MachineScope.Name() = %v, want less than %v", len(got), 15)
119+
}
120+
})
121+
}
122+
}

cloud/scope/machinepool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (m *MachinePoolScope) ScaleSetSpec() azure.ScaleSetSpec {
126126
func (m *MachinePoolScope) Name() string {
127127
// Windows Machine pools names cannot be longer than 9 chars
128128
if m.AzureMachinePool.Spec.Template.OSDisk.OSType == azure.WindowsOS && len(m.AzureMachinePool.Name) > 9 {
129-
return "win" + m.AzureMachinePool.Name[len(m.AzureMachinePool.Name)-5:]
129+
return "win-" + m.AzureMachinePool.Name[len(m.AzureMachinePool.Name)-5:]
130130
}
131131
return m.AzureMachinePool.Name
132132
}

cloud/scope/machinepool_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package scope
18+
19+
import (
20+
"testing"
21+
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
24+
25+
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1alpha3"
26+
)
27+
28+
func TestMachinePoolScope_Name(t *testing.T) {
29+
tests := []struct {
30+
name string
31+
machinePoolScope MachinePoolScope
32+
want string
33+
testLength bool
34+
}{
35+
{
36+
name: "linux can be any length",
37+
machinePoolScope: MachinePoolScope{
38+
MachinePool: nil,
39+
AzureMachinePool: &infrav1exp.AzureMachinePool{
40+
ObjectMeta: metav1.ObjectMeta{
41+
Name: "some-really-really-long-name",
42+
},
43+
},
44+
ClusterScoper: nil,
45+
},
46+
want: "some-really-really-long-name",
47+
},
48+
{
49+
name: "windows longer than 9 should be shortened",
50+
machinePoolScope: MachinePoolScope{
51+
MachinePool: nil,
52+
AzureMachinePool: &infrav1exp.AzureMachinePool{
53+
ObjectMeta: metav1.ObjectMeta{
54+
Name: "machine-90123456",
55+
},
56+
Spec: infrav1exp.AzureMachinePoolSpec{
57+
Template: infrav1exp.AzureMachineTemplate{
58+
OSDisk: infrav1.OSDisk{
59+
OSType: "Windows",
60+
},
61+
},
62+
},
63+
},
64+
ClusterScoper: nil,
65+
},
66+
want: "win-23456",
67+
},
68+
}
69+
for _, tt := range tests {
70+
t.Run(tt.name, func(t *testing.T) {
71+
got := tt.machinePoolScope.Name()
72+
if got != tt.want {
73+
t.Errorf("MachinePoolScope.Name() = %v, want %v", got, tt.want)
74+
}
75+
76+
if tt.testLength && len(got) > 9 {
77+
t.Errorf("Length of MachinePoolScope.Name() = %v, want less than %v", len(got), 9)
78+
}
79+
})
80+
}
81+
}

0 commit comments

Comments
 (0)