Skip to content

Commit d7cc0b9

Browse files
Adds vmType tests for Azure CloudConfigTransformer
Adds tests for the vmType transformations.
1 parent 64e61fe commit d7cc0b9

2 files changed

Lines changed: 99 additions & 2 deletions

File tree

pkg/cloud/azure/azure.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,16 @@ func NewProviderAssets(config config.OperatorConfig) (common.CloudProviderAssets
113113

114114
func IsAzure(infra *configv1.Infrastructure) bool {
115115
if infra.Status.PlatformStatus != nil &&
116-
infra.Status.PlatformStatus.Type == configv1.AzurePlatformType {
116+
infra.Status.PlatformStatus.Type == configv1.AzurePlatformType &&
117+
infra.Status.PlatformStatus.Azure.CloudName != configv1.AzureStackCloud {
117118
return true
118119
}
119120
return false
120121
}
121122

122123
func CloudConfigTransformer(source string, infra *configv1.Infrastructure, network *configv1.Network) (string, error) {
123124
if !IsAzure(infra) {
124-
return "", fmt.Errorf("invalid platform, expected to be Azure")
125+
return "", fmt.Errorf("invalid platform, expected CloudName to be %s", configv1.AzurePublicCloud)
125126
}
126127

127128
var cfg azure.Config

pkg/cloud/azure/azure_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
package azure
22

33
import (
4+
"encoding/json"
5+
"fmt"
46
"testing"
57

8+
. "github.com/onsi/gomega"
69
configv1 "github.com/openshift/api/config/v1"
710
"github.com/stretchr/testify/assert"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
azure "sigs.k8s.io/cloud-provider-azure/pkg/provider"
813

914
"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"
1021
)
1122

1223
func TestResourcesRenderingSmoke(t *testing.T) {
@@ -79,3 +90,88 @@ func TestResourcesRenderingSmoke(t *testing.T) {
7990
})
8091
}
8192
}
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

Comments
 (0)