|
| 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 | +} |
0 commit comments