github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/cse_internal_unit_test.go (about) 1 //go:build unit || ALL 2 3 package govcd 4 5 import ( 6 semver "github.com/hashicorp/go-version" 7 "os" 8 "reflect" 9 "strings" 10 "testing" 11 ) 12 13 // Test_cseClusterSettingsInternal_generateCapiYamlAsJsonString tests the generateCapiYamlAsJsonString method with a 14 // cseClusterSettingsInternal receiver. Given some valid or invalid CSE Settings, the tests runs the generateCapiYamlAsJsonString 15 // method and checks that the returned JSON string corresponds to the expected settings that were specified. 16 func Test_cseClusterSettingsInternal_generateCapiYamlAsJsonString(t *testing.T) { 17 cseVersion, err := semver.NewVersion("4.2.1") 18 if err != nil { 19 t.Fatal(err) 20 } 21 capiYaml, err := os.ReadFile("test-resources/capiYaml.yaml") 22 if err != nil { 23 t.Fatalf("could not read YAML test file: %s", err) 24 } 25 baseUnmarshaledYaml, err := unmarshalMultipleYamlDocuments(string(capiYaml)) 26 if err != nil { 27 t.Fatalf("could not unmarshal YAML test file: %s", err) 28 } 29 30 tests := []struct { 31 name string 32 input cseClusterSettingsInternal 33 expectedFunc func() []map[string]interface{} 34 wantErr string 35 }{ 36 { 37 name: "correct YAML without optionals", 38 input: cseClusterSettingsInternal{ 39 CseVersion: *cseVersion, 40 Name: "test1", 41 OrganizationName: "tenant_org", 42 VdcName: "tenant_vdc", 43 NetworkName: "tenant_net_routed", 44 KubernetesTemplateOvaName: "ubuntu-2004-kube-v1.25.7+vmware.2-tkg.1-8a74b9f12e488c54605b3537acb683bc", 45 TkgVersionBundle: tkgVersionBundle{ 46 EtcdVersion: "v3.5.6_vmware.9", 47 CoreDnsVersion: "v1.9.3_vmware.8", 48 TkgVersion: "v2.2.0", 49 TkrVersion: "v1.25.7---vmware.2-tkg.1", 50 KubernetesVersion: "v1.25.7+vmware.2", 51 }, 52 CatalogName: "tkgm_catalog", 53 ControlPlane: cseControlPlaneSettingsInternal{ 54 MachineCount: 1, 55 DiskSizeGi: 20, 56 SizingPolicyName: "TKG small", 57 StorageProfileName: "*", 58 }, 59 WorkerPools: []cseWorkerPoolSettingsInternal{ 60 { 61 Name: "node-pool-1", 62 MachineCount: 1, 63 DiskSizeGi: 20, 64 SizingPolicyName: "TKG small", 65 StorageProfileName: "*", 66 }, 67 }, 68 VcdKeConfig: vcdKeConfig{ 69 MaxUnhealthyNodesPercentage: 100, 70 NodeStartupTimeout: "900", 71 NodeNotReadyTimeout: "300", 72 NodeUnknownTimeout: "200", 73 ContainerRegistryUrl: "projects.registry.vmware.com/tkg", 74 }, 75 Owner: "dummy", 76 ApiToken: "dummy", 77 VcdUrl: "https://www.my-vcd-instance.com", 78 PodCidr: "100.96.0.0/11", 79 ServiceCidr: "100.64.0.0/13", 80 }, 81 expectedFunc: func() []map[string]interface{} { 82 return baseUnmarshaledYaml 83 }, 84 }, 85 { 86 name: "correct YAML without MachineHealthCheck", 87 input: cseClusterSettingsInternal{ 88 CseVersion: *cseVersion, 89 Name: "test1", 90 OrganizationName: "tenant_org", 91 VdcName: "tenant_vdc", 92 NetworkName: "tenant_net_routed", 93 KubernetesTemplateOvaName: "ubuntu-2004-kube-v1.25.7+vmware.2-tkg.1-8a74b9f12e488c54605b3537acb683bc", 94 TkgVersionBundle: tkgVersionBundle{ 95 EtcdVersion: "v3.5.6_vmware.9", 96 CoreDnsVersion: "v1.9.3_vmware.8", 97 TkgVersion: "v2.2.0", 98 TkrVersion: "v1.25.7---vmware.2-tkg.1", 99 KubernetesVersion: "v1.25.7+vmware.2", 100 }, 101 CatalogName: "tkgm_catalog", 102 ControlPlane: cseControlPlaneSettingsInternal{ 103 MachineCount: 1, 104 DiskSizeGi: 20, 105 SizingPolicyName: "TKG small", 106 StorageProfileName: "*", 107 }, 108 WorkerPools: []cseWorkerPoolSettingsInternal{ 109 { 110 Name: "node-pool-1", 111 MachineCount: 1, 112 DiskSizeGi: 20, 113 SizingPolicyName: "TKG small", 114 StorageProfileName: "*", 115 }, 116 }, 117 VcdKeConfig: vcdKeConfig{ 118 ContainerRegistryUrl: "projects.registry.vmware.com/tkg", 119 }, 120 Owner: "dummy", 121 ApiToken: "dummy", 122 VcdUrl: "https://www.my-vcd-instance.com", 123 PodCidr: "100.96.0.0/11", 124 ServiceCidr: "100.64.0.0/13", 125 }, 126 // The expected result is the base YAML without the MachineHealthCheck 127 expectedFunc: func() []map[string]interface{} { 128 var result []map[string]interface{} 129 for _, doc := range baseUnmarshaledYaml { 130 if doc["kind"] == "MachineHealthCheck" { 131 continue // Remove the MachineHealthCheck document from the expected result 132 } 133 result = append(result, doc) 134 } 135 return result 136 }, 137 }, 138 { 139 name: "correct YAML with every possible option", 140 input: cseClusterSettingsInternal{ 141 CseVersion: *cseVersion, 142 Name: "test1", 143 OrganizationName: "tenant_org", 144 VdcName: "tenant_vdc", 145 NetworkName: "tenant_net_routed", 146 KubernetesTemplateOvaName: "ubuntu-2004-kube-v1.25.7+vmware.2-tkg.1-8a74b9f12e488c54605b3537acb683bc", 147 TkgVersionBundle: tkgVersionBundle{ 148 EtcdVersion: "v3.5.6_vmware.9", 149 CoreDnsVersion: "v1.9.3_vmware.8", 150 TkgVersion: "v2.2.0", 151 TkrVersion: "v1.25.7---vmware.2-tkg.1", 152 KubernetesVersion: "v1.25.7+vmware.2", 153 }, 154 CatalogName: "tkgm_catalog", 155 ControlPlane: cseControlPlaneSettingsInternal{ 156 MachineCount: 1, 157 DiskSizeGi: 20, 158 SizingPolicyName: "TKG small", 159 StorageProfileName: "*", 160 Ip: "1.2.3.4", 161 }, 162 WorkerPools: []cseWorkerPoolSettingsInternal{ 163 { 164 Name: "node-pool-1", 165 MachineCount: 1, 166 DiskSizeGi: 20, 167 SizingPolicyName: "TKG small", 168 StorageProfileName: "*", 169 }, 170 }, 171 VcdKeConfig: vcdKeConfig{ 172 MaxUnhealthyNodesPercentage: 100, 173 NodeStartupTimeout: "900", 174 NodeNotReadyTimeout: "300", 175 NodeUnknownTimeout: "200", 176 ContainerRegistryUrl: "projects.registry.vmware.com/tkg", 177 Base64Certificates: []string{ 178 "Zm9vCg==", 179 "Zm9vMgo=", 180 }, 181 }, 182 VirtualIpSubnet: "6.7.8.9/24", 183 Owner: "dummy", 184 ApiToken: "dummy", 185 VcdUrl: "https://www.my-vcd-instance.com", 186 PodCidr: "100.96.0.0/11", 187 ServiceCidr: "100.64.0.0/13", 188 }, 189 // The expected result is the base YAML with the Control Plane extra IPs 190 expectedFunc: func() []map[string]interface{} { 191 var result []map[string]interface{} 192 for _, doc := range baseUnmarshaledYaml { 193 if doc["kind"] == "VCDCluster" { 194 // Add the extra items to the document of the expected result 195 doc["spec"].(map[string]interface{})["controlPlaneEndpoint"] = map[string]interface{}{"host": "1.2.3.4"} 196 doc["spec"].(map[string]interface{})["controlPlaneEndpoint"].(map[string]interface{})["port"] = float64(6443) 197 doc["spec"].(map[string]interface{})["loadBalancerConfigSpec"] = map[string]interface{}{"vipSubnet": "6.7.8.9/24"} 198 } 199 if doc["kind"] == "KubeadmControlPlane" { 200 doc["spec"].(map[string]interface{})["kubeadmConfigSpec"].(map[string]interface{})["files"] = []interface{}{ 201 map[string]interface{}{ 202 "encoding": "base64", 203 "content": "Zm9vCg==", 204 "owner": "root", 205 "permissions": "0644", 206 "path": "/etc/ssl/certs/custom_certificate_0.crt", 207 }, 208 map[string]interface{}{ 209 "encoding": "base64", 210 "content": "Zm9vMgo=", 211 "owner": "root", 212 "permissions": "0644", 213 "path": "/etc/ssl/certs/custom_certificate_1.crt", 214 }, 215 } 216 } 217 if doc["kind"] == "KubeadmConfigTemplate" { 218 doc["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["files"] = []interface{}{ 219 map[string]interface{}{ 220 "encoding": "base64", 221 "content": "Zm9vCg==", 222 "owner": "root", 223 "permissions": "0644", 224 "path": "/etc/ssl/certs/custom_certificate_0.crt", 225 }, 226 map[string]interface{}{ 227 "encoding": "base64", 228 "content": "Zm9vMgo=", 229 "owner": "root", 230 "permissions": "0644", 231 "path": "/etc/ssl/certs/custom_certificate_1.crt", 232 }, 233 } 234 } 235 result = append(result, doc) 236 } 237 return result 238 }, 239 }, 240 { 241 name: "wrong YAML with both Placement and vGPU policies in a Worker Pool", 242 input: cseClusterSettingsInternal{ 243 CseVersion: *cseVersion, 244 WorkerPools: []cseWorkerPoolSettingsInternal{ 245 { 246 Name: "node-pool-1", 247 MachineCount: 1, 248 DiskSizeGi: 20, 249 SizingPolicyName: "TKG small", 250 PlacementPolicyName: "policy", 251 VGpuPolicyName: "policy", 252 StorageProfileName: "*", 253 }, 254 }, 255 }, 256 wantErr: "the Worker Pool 'node-pool-1' should have either a Placement Policy or a vGPU Policy, not both", 257 }, 258 } 259 for _, tt := range tests { 260 t.Run(tt.name, func(t *testing.T) { 261 got, err := tt.input.generateCapiYamlAsJsonString() 262 if err != nil { 263 if err.Error() != tt.wantErr { 264 t.Errorf("generateCapiYamlAsJsonString() error = %v, wantErr %v", err, tt.wantErr) 265 } 266 return 267 } 268 gotUnmarshaled, err := unmarshalMultipleYamlDocuments(strings.NewReplacer("\\n", "\n", "\\\"", "\"").Replace(got)) 269 if err != nil { 270 t.Fatalf("could not unmarshal obtained YAML: %s", err) 271 } 272 273 expected := tt.expectedFunc() 274 if !reflect.DeepEqual(expected, gotUnmarshaled) { 275 t.Errorf("generateCapiYamlAsJsonString() got =\n%#v\nwant =\n%#v\n", gotUnmarshaled, expected) 276 } 277 }) 278 } 279 }