github.com/openshift/installer@v1.4.17/pkg/asset/manifests/vsphere/cloudproviderconfig_test.go (about) 1 package vsphere 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 9 vsphere "github.com/openshift/installer/pkg/types/vsphere" 10 ) 11 12 var ( 13 expectedIniConfig = `[Global] 14 secret-name = "vsphere-creds" 15 secret-namespace = "kube-system" 16 insecure-flag = "1" 17 18 [VirtualCenter "test-vcenter"] 19 port = "443" 20 21 datacenters = "test-datacenter,test-datacenter2" 22 23 [Workspace] 24 server = "test-vcenter" 25 datacenter = "test-datacenter" 26 default-datastore = "test-datastore" 27 folder = "/test-datacenter/vm/test-folder" 28 resourcepool-path = "/test-datacenter/host/cluster/Resources/test-resourcepool" 29 30 ` 31 32 expectIniLabelsSection = `[Labels] 33 region = "openshift-region" 34 zone = "openshift-zone" 35 ` 36 37 expectedYamlConfig = `global: 38 user: "" 39 password: "" 40 server: "" 41 port: 0 42 insecureFlag: true 43 datacenters: [] 44 soapRoundtripCount: 0 45 caFile: "" 46 thumbprint: "" 47 secretName: vsphere-creds 48 secretNamespace: kube-system 49 secretsDirectory: "" 50 apiDisable: false 51 apiBinding: "" 52 ipFamily: [] 53 vcenter: 54 test-vcenter: 55 user: "" 56 password: "" 57 tenantref: "" 58 server: test-vcenter 59 port: 443 60 insecureFlag: true 61 datacenters: 62 - test-datacenter 63 - test-datacenter2 64 soapRoundtripCount: 0 65 caFile: "" 66 thumbprint: "" 67 secretref: "" 68 secretName: "" 69 secretNamespace: "" 70 ipFamily: [] 71 labels: 72 zone: openshift-zone 73 region: openshift-region 74 ` 75 ) 76 77 func validPlatform() *vsphere.Platform { 78 return &vsphere.Platform{ 79 VCenters: []vsphere.VCenter{ 80 { 81 Server: "test-vcenter", 82 Port: 443, 83 Username: "test-username", 84 Password: "test-password", 85 Datacenters: []string{ 86 "test-datacenter", 87 "test-datacenter2", 88 }, 89 }, 90 }, 91 FailureDomains: []vsphere.FailureDomain{ 92 { 93 Name: "test-dz-east-1a", 94 Server: "test-vcenter", 95 Topology: vsphere.Topology{ 96 Datacenter: "test-datacenter", 97 ComputeCluster: "/test-datacenter/host/cluster", 98 Networks: []string{ 99 "test-network-1", 100 }, 101 Datastore: "test-datastore", 102 ResourcePool: "/test-datacenter/host/cluster/Resources/test-resourcepool", 103 Folder: "/test-datacenter/vm/test-folder", 104 }, 105 }, 106 { 107 Name: "test-dz-east-2a", 108 Server: "test-vcenter", 109 Topology: vsphere.Topology{ 110 Datacenter: "test-datacenter2", 111 ComputeCluster: "/test-datacenter2/host/cluster", 112 Networks: []string{ 113 "test-network-1", 114 }, 115 Datastore: "test-datastore2", 116 ResourcePool: "/test-datacenter2/host/cluster/Resources/test-resourcepool", 117 Folder: "/test-datacenter2/vm/test-folder", 118 }, 119 }, 120 { 121 Name: "test-dz-east-3a", 122 Server: "test-vcenter", 123 Topology: vsphere.Topology{ 124 Datacenter: "test-datacenter2", 125 ComputeCluster: "/test-datacenter2/host/cluster", 126 Networks: []string{ 127 "test-network-1", 128 }, 129 Datastore: "test-datastore2", 130 ResourcePool: "/test-datacenter2/host/cluster/Resources/test-resourcepool", 131 Folder: "/test-datacenter2/vm/test-folder", 132 }, 133 }, 134 }, 135 } 136 } 137 138 func TestCloudProviderConfig(t *testing.T) { 139 cases := []struct { 140 name string 141 platform *vsphere.Platform 142 cloudProviderFunc func(string, *vsphere.Platform) (string, error) 143 expectedCloudConfig string 144 }{ 145 { 146 name: "valid intree cloud provider config", 147 platform: validPlatform(), 148 cloudProviderFunc: CloudProviderConfigIni, 149 expectedCloudConfig: expectedIniConfig + expectIniLabelsSection, 150 }, 151 { 152 name: "valid single failure domain intree cloud provider config", 153 platform: func() *vsphere.Platform { 154 p := validPlatform() 155 156 p.FailureDomains = p.FailureDomains[0:1] 157 p.VCenters[0].Datacenters = p.VCenters[0].Datacenters[0:1] 158 159 return p 160 }(), 161 cloudProviderFunc: CloudProviderConfigIni, 162 expectedCloudConfig: func() string { 163 // only a single datacenter would be provided to the datacenters 164 ini := strings.ReplaceAll(expectedIniConfig, ",test-datacenter2", "") 165 return ini 166 }(), 167 }, 168 { 169 name: "valid out of tree yaml cloud provider config", 170 platform: validPlatform(), 171 cloudProviderFunc: CloudProviderConfigYaml, 172 expectedCloudConfig: expectedYamlConfig, 173 }, 174 } 175 176 for _, tc := range cases { 177 t.Run(tc.name, func(t *testing.T) { 178 var cloudConfig string 179 var err error 180 cloudConfig, err = tc.cloudProviderFunc("infraID", tc.platform) 181 assert.NoError(t, err, "failed to create cloud provider config") 182 assert.Equal(t, tc.expectedCloudConfig, cloudConfig, "unexpected cloud provider config") 183 }) 184 } 185 }