Skip to content

Commit

Permalink
Add tests for machine names
Browse files Browse the repository at this point in the history
  • Loading branch information
jsturtevant committed Dec 19, 2020
1 parent 145c137 commit 787f1fe
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 1 deletion.
121 changes: 121 additions & 0 deletions cloud/scope/machine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package scope

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"

infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
azure "sigs.k8s.io/cluster-api-provider-azure/cloud"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
)

func TestMachineScope_Name(t *testing.T) {
type fields struct {
ClusterScoper azure.ClusterScoper
AzureMachine *infrav1.AzureMachine
}
tests := []struct {
name string
machineScope MachineScope
want string
testLength bool
}{
{
name: "linux can be any length",
machineScope: MachineScope{
AzureMachine: &infrav1.AzureMachine{
ObjectMeta: metav1.ObjectMeta{
Name: "machine-with-really-really-long-name",
},
Spec: infrav1.AzureMachineSpec{
OSDisk: infrav1.OSDisk{
OSType: "Linux",
},
},
},
},
want: "machine-with-really-really-long-name",
},
{
name: "Windows name with long MachineName and short cluster name",
machineScope: MachineScope{
ClusterScoper: &ClusterScope{
Cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster",
},
},
},
AzureMachine: &infrav1.AzureMachine{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: "machine-90123456",
},
Spec: infrav1.AzureMachineSpec{
OSDisk: infrav1.OSDisk{
OSType: "Windows",
},
},
Status: infrav1.AzureMachineStatus{},
},
},
want: "cluster-23456",
testLength: true,
},
{
name: "Windows name with long MachineName and long cluster name",
machineScope: MachineScope{
ClusterScoper: &ClusterScope{
Cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster8901234",
},
},
},
AzureMachine: &infrav1.AzureMachine{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: "machine-90123456",
},
Spec: infrav1.AzureMachineSpec{
OSDisk: infrav1.OSDisk{
OSType: "Windows",
},
},
Status: infrav1.AzureMachineStatus{},
},
},
want: "cluster89-23456",
testLength: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

got := tt.machineScope.Name()
if got != tt.want {
t.Errorf("MachineScope.Name() = %v, want %v", got, tt.want)
}

if tt.testLength && len(got) > 15 {
t.Errorf("Length of MachineScope.Name() = %v, want less than %v", len(got), 15)
}
})
}
}
2 changes: 1 addition & 1 deletion cloud/scope/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (m *MachinePoolScope) ScaleSetSpec() azure.ScaleSetSpec {
func (m *MachinePoolScope) Name() string {
// Windows Machine pools names cannot be longer than 9 chars
if m.AzureMachinePool.Spec.Template.OSDisk.OSType == azure.WindowsOS && len(m.AzureMachinePool.Name) > 9 {
return "win" + m.AzureMachinePool.Name[len(m.AzureMachinePool.Name)-5:]
return "win-" + m.AzureMachinePool.Name[len(m.AzureMachinePool.Name)-5:]
}
return m.AzureMachinePool.Name
}
Expand Down
80 changes: 80 additions & 0 deletions cloud/scope/machinepool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package scope

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
"testing"

infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1alpha3"
)

func TestMachinePoolScope_Name(t *testing.T) {
tests := []struct {
name string
machinePoolScope MachinePoolScope
want string
testLength bool
}{
{
name: "linux can be any length",
machinePoolScope: MachinePoolScope{
MachinePool: nil,
AzureMachinePool: &infrav1exp.AzureMachinePool{
ObjectMeta: metav1.ObjectMeta{
Name: "some-really-really-long-name",
},
},
ClusterScoper: nil,
},
want: "some-really-really-long-name",
},
{
name: "windows longer than 9 should be shortened",
machinePoolScope: MachinePoolScope{
MachinePool: nil,
AzureMachinePool: &infrav1exp.AzureMachinePool{
ObjectMeta: metav1.ObjectMeta{
Name: "machine-90123456",
},
Spec: infrav1exp.AzureMachinePoolSpec{
Template: infrav1exp.AzureMachineTemplate{
OSDisk: infrav1.OSDisk{
OSType: "Windows",
},
},
},
},
ClusterScoper: nil,
},
want: "win-23456",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.machinePoolScope.Name()
if got != tt.want {
t.Errorf("MachinePoolScope.Name() = %v, want %v", got, tt.want)
}

if tt.testLength && len(got) > 9 {
t.Errorf("Length of MachinePoolScope.Name() = %v, want less than %v", len(got), 9)
}
})
}
}

0 comments on commit 787f1fe

Please sign in to comment.