|
1 | 1 | package azure |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
4 | 6 | "testing" |
5 | 7 |
|
| 8 | + . "github.com/onsi/gomega" |
6 | 9 | configv1 "github.com/openshift/api/config/v1" |
7 | 10 | "github.com/stretchr/testify/assert" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + azure "sigs.k8s.io/cloud-provider-azure/pkg/provider" |
| 13 | + |
| 14 | + ratelimitconfig "sigs.k8s.io/cloud-provider-azure/pkg/provider/config" |
8 | 15 |
|
9 | 16 | "github.com/openshift/cluster-cloud-controller-manager-operator/pkg/config" |
10 | 17 | ) |
11 | 18 |
|
| 19 | +const ( |
| 20 | + infraCloudConfName = "test-config" |
| 21 | + infraCloudConfKey = "foo" |
| 22 | +) |
| 23 | + |
12 | 24 | func TestResourcesRenderingSmoke(t *testing.T) { |
13 | 25 |
|
14 | 26 | tc := []struct { |
@@ -79,3 +91,155 @@ func TestResourcesRenderingSmoke(t *testing.T) { |
79 | 91 | }) |
80 | 92 | } |
81 | 93 | } |
| 94 | + |
| 95 | +func makeInfrastructureResource(platform configv1.PlatformType, cloudName configv1.AzureCloudEnvironment) *configv1.Infrastructure { |
| 96 | + cfg := configv1.Infrastructure{ |
| 97 | + ObjectMeta: metav1.ObjectMeta{ |
| 98 | + Name: "cluster", |
| 99 | + }, |
| 100 | + Status: configv1.InfrastructureStatus{ |
| 101 | + PlatformStatus: &configv1.PlatformStatus{ |
| 102 | + Type: platform, |
| 103 | + }, |
| 104 | + }, |
| 105 | + Spec: configv1.InfrastructureSpec{ |
| 106 | + CloudConfig: configv1.ConfigMapFileReference{ |
| 107 | + Name: infraCloudConfName, |
| 108 | + Key: infraCloudConfKey, |
| 109 | + }, |
| 110 | + PlatformSpec: configv1.PlatformSpec{ |
| 111 | + Type: platform, |
| 112 | + }, |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + if platform == configv1.AzurePlatformType { |
| 117 | + cfg.Status.PlatformStatus.Azure = &configv1.AzurePlatformStatus{ |
| 118 | + CloudName: cloudName, |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return &cfg |
| 123 | +} |
| 124 | + |
| 125 | +// This test is a little complicated with all the JSON marshalling and |
| 126 | +// unmarshalling, but it is necessary due to the nature of how this data |
| 127 | +// is stored in Kuberenetes. The ConfigMaps containing the cloud config |
| 128 | +// will have string encoded JSON objects in them, due to the non-deterministic |
| 129 | +// natue of map object in Go we will need to examine the data instead of |
| 130 | +// comparing strings. |
| 131 | +func TestCloudConfigTransformer(t *testing.T) { |
| 132 | + tc := []struct { |
| 133 | + name string |
| 134 | + source azure.Config |
| 135 | + expected azure.Config |
| 136 | + infra *configv1.Infrastructure |
| 137 | + errMsg string |
| 138 | + }{ |
| 139 | + { |
| 140 | + name: "Non Azure returns an error", |
| 141 | + source: azure.Config{}, |
| 142 | + infra: makeInfrastructureResource(configv1.AzurePlatformType, configv1.AzureStackCloud), |
| 143 | + errMsg: fmt.Sprintf("invalid platform, expected CloudName to be %s", configv1.AzurePublicCloud), |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "Azure sets the vmType to standard and cloud to AzurePublicCloud when neither is set", |
| 147 | + source: azure.Config{}, |
| 148 | + expected: azure.Config{VMType: "standard", AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzurePublicCloud)}}, |
| 149 | + infra: makeInfrastructureResource(configv1.AzurePlatformType, configv1.AzurePublicCloud), |
| 150 | + }, |
| 151 | + { |
| 152 | + name: "Azure doesn't modify vmType if user set", |
| 153 | + source: azure.Config{VMType: "vmss"}, |
| 154 | + expected: azure.Config{VMType: "vmss", AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzurePublicCloud)}}, |
| 155 | + infra: makeInfrastructureResource(configv1.AzurePlatformType, configv1.AzurePublicCloud), |
| 156 | + }, |
| 157 | + { |
| 158 | + name: "Azure sets the cloud to AzurePublicCloud and keeps existing fields", |
| 159 | + source: azure.Config{ |
| 160 | + ResourceGroup: "test-rg", |
| 161 | + }, |
| 162 | + expected: azure.Config{VMType: "standard", ResourceGroup: "test-rg", AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzurePublicCloud)}}, |
| 163 | + infra: makeInfrastructureResource(configv1.AzurePlatformType, configv1.AzurePublicCloud), |
| 164 | + }, |
| 165 | + { |
| 166 | + name: "Azure keeps the cloud set to AzurePublicCloud", |
| 167 | + source: azure.Config{AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzurePublicCloud)}}, |
| 168 | + expected: azure.Config{VMType: "standard", AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzurePublicCloud)}}, |
| 169 | + infra: makeInfrastructureResource(configv1.AzurePlatformType, configv1.AzurePublicCloud), |
| 170 | + }, |
| 171 | + { |
| 172 | + name: "Azure keeps the cloud set to US Gov cloud", |
| 173 | + source: azure.Config{AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzureUSGovernmentCloud)}}, |
| 174 | + expected: azure.Config{VMType: "standard", AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzureUSGovernmentCloud)}}, |
| 175 | + infra: makeInfrastructureResource(configv1.AzurePlatformType, configv1.AzureUSGovernmentCloud), |
| 176 | + }, |
| 177 | + { |
| 178 | + name: "Azure keeps the cloud set to China cloud", |
| 179 | + source: azure.Config{AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzureChinaCloud)}}, |
| 180 | + expected: azure.Config{VMType: "standard", AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzureChinaCloud)}}, |
| 181 | + infra: makeInfrastructureResource(configv1.AzurePlatformType, configv1.AzureChinaCloud), |
| 182 | + }, |
| 183 | + { |
| 184 | + name: "Azure keeps the cloud set to German cloud", |
| 185 | + source: azure.Config{AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzureGermanCloud)}}, |
| 186 | + expected: azure.Config{VMType: "standard", AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzureGermanCloud)}}, |
| 187 | + infra: makeInfrastructureResource(configv1.AzurePlatformType, configv1.AzureGermanCloud), |
| 188 | + }, |
| 189 | + { |
| 190 | + name: "Azure throws an error if the infra has an invalid cloud", |
| 191 | + source: azure.Config{}, |
| 192 | + infra: makeInfrastructureResource(configv1.AzurePlatformType, "AzureAnotherCloud"), |
| 193 | + errMsg: "status.platformStatus.azure.cloudName: Unsupported value: \"AzureAnotherCloud\": supported values: \"AzureChinaCloud\", \"AzureGermanCloud\", \"AzurePublicCloud\", \"AzureStackCloud\", \"AzureUSGovernmentCloud\"", |
| 194 | + }, |
| 195 | + { |
| 196 | + name: "Azure keeps the cloud set in the source when there is not one set in infrastructure", |
| 197 | + source: azure.Config{AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzurePublicCloud)}}, |
| 198 | + expected: azure.Config{VMType: "standard", AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzurePublicCloud)}}, |
| 199 | + infra: makeInfrastructureResource(configv1.AzurePlatformType, ""), |
| 200 | + }, |
| 201 | + { |
| 202 | + name: "Azure sets the cloud to match the infrastructure if an empty string is provided in source", |
| 203 | + source: azure.Config{AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: ""}}, |
| 204 | + expected: azure.Config{VMType: "standard", AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzurePublicCloud)}}, |
| 205 | + infra: makeInfrastructureResource(configv1.AzurePlatformType, configv1.AzurePublicCloud), |
| 206 | + }, |
| 207 | + { |
| 208 | + name: "Azure sets the cloud to match the infrastructure if an empty string is provided in source and the infrastructure is non standard", |
| 209 | + source: azure.Config{AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: ""}}, |
| 210 | + expected: azure.Config{VMType: "standard", AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzureUSGovernmentCloud)}}, |
| 211 | + infra: makeInfrastructureResource(configv1.AzurePlatformType, configv1.AzureUSGovernmentCloud), |
| 212 | + }, |
| 213 | + { |
| 214 | + name: "Azure returns an error if the source config conflicts with the infrastructure", |
| 215 | + source: azure.Config{AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzurePublicCloud)}}, |
| 216 | + infra: makeInfrastructureResource(configv1.AzurePlatformType, configv1.AzureUSGovernmentCloud), |
| 217 | + errMsg: "invalid user-provided cloud.conf: \\\"cloud\\\" field in user-provided\n\t\t\t\tcloud.conf conflicts with infrastructure object", |
| 218 | + }, |
| 219 | + { |
| 220 | + name: "Azure keeps the cloud set to AzurePublicCloud if the source is upper case", |
| 221 | + source: azure.Config{AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: "AZUREPUBLICCLOUD"}}, |
| 222 | + expected: azure.Config{VMType: "standard", AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzurePublicCloud)}}, |
| 223 | + infra: makeInfrastructureResource(configv1.AzurePlatformType, configv1.AzurePublicCloud), |
| 224 | + }, |
| 225 | + } |
| 226 | + |
| 227 | + for _, tc := range tc { |
| 228 | + t.Run(tc.name, func(t *testing.T) { |
| 229 | + g := NewWithT(t) |
| 230 | + |
| 231 | + src, err := json.Marshal(tc.source) |
| 232 | + g.Expect(err).NotTo(HaveOccurred(), "Marshal of source data should succeed") |
| 233 | + |
| 234 | + actual, err := CloudConfigTransformer(string(src), tc.infra, nil) |
| 235 | + if tc.errMsg != "" { |
| 236 | + g.Expect(err).Should(MatchError(tc.errMsg)) |
| 237 | + g.Expect(actual).Should(Equal("")) |
| 238 | + } else { |
| 239 | + var observed azure.Config |
| 240 | + g.Expect(json.Unmarshal([]byte(actual), &observed)).To(Succeed(), "Unmarshal of observed data should succeed") |
| 241 | + g.Expect(observed).Should(Equal(tc.expected)) |
| 242 | + } |
| 243 | + }) |
| 244 | + } |
| 245 | +} |
0 commit comments