|
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" |
8 | 13 |
|
9 | 14 | "github.com/openshift/cluster-cloud-controller-manager-operator/pkg/config" |
| 15 | + ratelimitconfig "sigs.k8s.io/cloud-provider-azure/pkg/provider/config" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + infraCloudConfName = "test-config" |
| 20 | + infraCloudConfKey = "foo" |
10 | 21 | ) |
11 | 22 |
|
12 | 23 | func TestResourcesRenderingSmoke(t *testing.T) { |
@@ -79,3 +90,88 @@ func TestResourcesRenderingSmoke(t *testing.T) { |
79 | 90 | }) |
80 | 91 | } |
81 | 92 | } |
| 93 | + |
| 94 | +func makeInfrastructureResource(platform configv1.PlatformType, cloudName configv1.AzureCloudEnvironment) *configv1.Infrastructure { |
| 95 | + cfg := configv1.Infrastructure{ |
| 96 | + ObjectMeta: metav1.ObjectMeta{ |
| 97 | + Name: "cluster", |
| 98 | + }, |
| 99 | + Status: configv1.InfrastructureStatus{ |
| 100 | + PlatformStatus: &configv1.PlatformStatus{ |
| 101 | + Type: platform, |
| 102 | + }, |
| 103 | + }, |
| 104 | + Spec: configv1.InfrastructureSpec{ |
| 105 | + CloudConfig: configv1.ConfigMapFileReference{ |
| 106 | + Name: infraCloudConfName, |
| 107 | + Key: infraCloudConfKey, |
| 108 | + }, |
| 109 | + PlatformSpec: configv1.PlatformSpec{ |
| 110 | + Type: platform, |
| 111 | + }, |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + if platform == configv1.AzurePlatformType { |
| 116 | + cfg.Status.PlatformStatus.Azure = &configv1.AzurePlatformStatus{ |
| 117 | + CloudName: cloudName, |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return &cfg |
| 122 | +} |
| 123 | + |
| 124 | +// This test is a little complicated with all the JSON marshalling and |
| 125 | +// unmarshalling, but it is necessary due to the nature of how this data |
| 126 | +// is stored in Kuberenetes. The ConfigMaps containing the cloud config |
| 127 | +// will have string encoded JSON objects in them, due to the non-deterministic |
| 128 | +// natue of map object in Go we will need to examine the data instead of |
| 129 | +// comparing strings. |
| 130 | +func TestCloudConfigTransformer(t *testing.T) { |
| 131 | + tc := []struct { |
| 132 | + name string |
| 133 | + source azure.Config |
| 134 | + expected azure.Config |
| 135 | + infra *configv1.Infrastructure |
| 136 | + errMsg string |
| 137 | + }{ |
| 138 | + { |
| 139 | + name: "Azure sets the vmType to standard", |
| 140 | + source: azure.Config{}, |
| 141 | + expected: azure.Config{VMType: "standard", AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzurePublicCloud)}}, |
| 142 | + infra: makeInfrastructureResource(configv1.AzurePlatformType, configv1.AzurePublicCloud), |
| 143 | + }, |
| 144 | + { |
| 145 | + name: "Azure doesn't modify vmType if user set", |
| 146 | + source: azure.Config{VMType: "vmss"}, |
| 147 | + expected: azure.Config{VMType: "vmss", AzureAuthConfig: ratelimitconfig.AzureAuthConfig{Cloud: string(configv1.AzurePublicCloud)}}, |
| 148 | + infra: makeInfrastructureResource(configv1.AzurePlatformType, configv1.AzurePublicCloud), |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "Non Azure returns an error", |
| 152 | + source: azure.Config{}, |
| 153 | + infra: makeInfrastructureResource(configv1.AzurePlatformType, configv1.AzureStackCloud), |
| 154 | + errMsg: fmt.Sprintf("invalid platform, expected CloudName to be %s", configv1.AzurePublicCloud), |
| 155 | + }, |
| 156 | + } |
| 157 | + |
| 158 | + for _, tc := range tc { |
| 159 | + t.Run(tc.name, func(t *testing.T) { |
| 160 | + g := NewWithT(t) |
| 161 | + |
| 162 | + src, err := json.Marshal(tc.source) |
| 163 | + g.Expect(err).NotTo(HaveOccurred(), "Marshal of source data should succeed") |
| 164 | + |
| 165 | + actual, err := CloudConfigTransformer(string(src), tc.infra, nil) |
| 166 | + if tc.errMsg != "" { |
| 167 | + g.Expect(err).Should(MatchError(tc.errMsg)) |
| 168 | + g.Expect(actual).Should(Equal("")) |
| 169 | + } else { |
| 170 | + var observed azure.Config |
| 171 | + g.Expect(json.Unmarshal([]byte(actual), &observed)).To(Succeed(), "Unmarshal of observed data should succeed") |
| 172 | + |
| 173 | + g.Expect(observed).Should(Equal(tc.expected)) |
| 174 | + } |
| 175 | + }) |
| 176 | + } |
| 177 | +} |
0 commit comments