Skip to content

Commit 316612a

Browse files
committed
Improve testing of NewDriver function
1 parent a71697f commit 316612a

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

pkg/azurelustre/azurelustre.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const (
5353
volumeIDTemplate = "%s#%s#%s#%s#%s#%s"
5454
subnetTemplate = "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s/subnets/%s"
5555

56+
DefaultAzureConfigFileEnv = "AZURE_CONFIG_FILE"
57+
DefaultConfigFilePathLinux = "/etc/kubernetes/azure.json"
58+
5659
amlFilesystemNameMaxLength = 80
5760

5861
AgentNotReadyNodeTaintKeySuffix = "/agent-not-ready"
@@ -177,15 +180,21 @@ func NewDriver(options *DriverOptions) *Driver {
177180

178181
ctx := context.Background()
179182

180-
// Will need to change if we ever support non-AKS clusters
181-
AKSConfigFile := "/etc/kubernetes/azure.json"
182-
183183
az := &azure.Cloud{}
184+
185+
credFile, ok := os.LookupEnv(DefaultAzureConfigFileEnv)
186+
if ok && strings.TrimSpace(credFile) != "" {
187+
klog.V(2).Infof("%s env var set as %v", DefaultAzureConfigFileEnv, credFile)
188+
} else {
189+
credFile = DefaultConfigFilePathLinux
190+
klog.V(2).Infof("use default %s env var: %v", DefaultAzureConfigFileEnv, credFile)
191+
}
192+
184193
config, err := configloader.Load[azure.Config](ctx, nil, &configloader.FileLoaderConfig{
185-
FilePath: AKSConfigFile,
194+
FilePath: credFile,
186195
})
187196
if err != nil {
188-
klog.V(2).Infof("failed to get cloud config from file %s: %v", AKSConfigFile, err)
197+
klog.V(2).Infof("failed to get cloud config from file %s: %v", credFile, err)
189198
}
190199

191200
if config == nil {
@@ -374,6 +383,7 @@ type JSONPatch struct {
374383
}
375384

376385
// removeTaintInBackground removes the taint from the node in a goroutine with retry logic
386+
// TODO: We could test this properly with synctest when we move to go 1.25
377387
func removeTaintInBackground(k8sClient kubernetes.Interface, nodeName, driverName string, backoff wait.Backoff, removalFunc func(kubernetes.Interface, string, string) error) {
378388
klog.V(2).Infof("starting background node taint removal for node %s", nodeName)
379389
go func() {

pkg/azurelustre/azurelustre_test.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,54 @@ func (f *FakeDynamicProvisioner) GetSkuValuesForLocation(_ context.Context, loca
131131
}
132132

133133
func TestNewDriver(t *testing.T) {
134+
fakeConfigFile := "fake-cred-file.json"
135+
fakeConfigContent := `{
136+
"tenantId": "fake-tenant-id",
137+
"subscriptionId": "fake-subscription-id",
138+
"aadClientId": "fake-client-id",
139+
"aadClientSecret": "fake-client-secret",
140+
"resourceGroup": "fake-resource-group",
141+
"location": "fake-location",
142+
}`
143+
144+
if err := os.WriteFile(fakeConfigFile, []byte(fakeConfigContent), 0o600); err != nil {
145+
t.Error(err)
146+
}
147+
148+
defer func() {
149+
if err := os.Remove(fakeConfigFile); err != nil {
150+
t.Error(err)
151+
}
152+
}()
153+
154+
t.Setenv(DefaultAzureConfigFileEnv, fakeConfigFile)
155+
134156
driverOptions := DriverOptions{
135157
NodeID: fakeNodeID,
136-
DriverName: DefaultDriverName,
158+
DriverName: fakeDriverName,
137159
EnableAzureLustreMockMount: false,
138160
EnableAzureLustreMockDynProv: true,
161+
WorkingMountDir: "/tmp",
162+
RemoveNotReadyTaint: true,
139163
}
140164
d := NewDriver(&driverOptions)
141165
assert.NotNil(t, d)
166+
assert.NotNil(t, d.cloud)
167+
assert.NotNil(t, d.dynamicProvisioner)
168+
assert.Equal(t, "fake-resource-group", d.resourceGroup)
169+
assert.Equal(t, "fake-location", d.location)
170+
assert.Equal(t, fakeNodeID, d.NodeID)
171+
assert.Equal(t, fakeDriverName, d.Name)
172+
assert.Equal(t, "fake-subscription-id", d.cloud.SubscriptionID)
173+
assert.Equal(t, "fake-tenant-id", d.cloud.TenantID)
174+
assert.Equal(t, "fake-client-id", d.cloud.AADClientID)
175+
assert.Equal(t, "fake-client-secret", d.cloud.AADClientSecret)
176+
assert.Equal(t, "fake-location", d.cloud.Location)
177+
assert.Equal(t, "fake-resource-group", d.cloud.ResourceGroup)
178+
assert.Equal(t, "/tmp", d.workingMountDir)
179+
assert.True(t, d.enableAzureLustreMockDynProv, "enableAzureLustreMockDynProv should be true")
180+
assert.False(t, d.enableAzureLustreMockMount, "enableAzureLustreMockMount should be false")
181+
assert.True(t, d.removeNotReadyTaint, "removeNotReadyTaint should be true")
142182
}
143183

144184
func TestIsCorruptedDir(t *testing.T) {

0 commit comments

Comments
 (0)