Skip to content

Commit 26f2fbb

Browse files
authored
Fix Azure CLI tests on Go 1.19 (#1538)
In Go 1.19 the Command and LookPath no longer allow results from a PATH search to be found relative to the current directory. https://go.dev/doc/go1.19#os-exec-path
1 parent 016df8f commit 26f2fbb

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed

common/azure_auth_test.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"net/http/httptest"
88
"os"
9+
"path/filepath"
910
"strings"
1011
"sync"
1112
"testing"
@@ -29,7 +30,8 @@ func TestAddSpManagementTokenVisitor(t *testing.T) {
2930

3031
func TestAddSpManagementTokenVisitor_Refreshed(t *testing.T) {
3132
defer CleanupEnvironment()()
32-
os.Setenv("PATH", "testdata:/bin")
33+
p, _ := filepath.Abs("./testdata")
34+
os.Setenv("PATH", p+":/bin")
3335

3436
aa := DatabricksClient{}
3537
r := httptest.NewRequest("GET", "/a/b/c", http.NoBody)
@@ -45,7 +47,8 @@ func TestAddSpManagementTokenVisitor_Refreshed(t *testing.T) {
4547

4648
func TestAddSpManagementTokenVisitor_RefreshedError(t *testing.T) {
4749
defer CleanupEnvironment()()
48-
os.Setenv("PATH", "testdata:/bin")
50+
p, _ := filepath.Abs("./testdata")
51+
os.Setenv("PATH", p+":/bin")
4952
os.Setenv("FAIL", "yes")
5053

5154
aa := DatabricksClient{}
@@ -274,7 +277,8 @@ func TestMaybeExtendError(t *testing.T) {
274277

275278
func TestGetJWTProperty_AzureCLI_SP(t *testing.T) {
276279
defer CleanupEnvironment()()
277-
os.Setenv("PATH", "testdata:/bin")
280+
p, _ := filepath.Abs("./testdata")
281+
os.Setenv("PATH", p+":/bin")
278282

279283
aa := DatabricksClient{
280284
AzureClientID: "a",
@@ -289,7 +293,8 @@ func TestGetJWTProperty_AzureCLI_SP(t *testing.T) {
289293

290294
func TestGetJWTProperty_NonAzure(t *testing.T) {
291295
defer CleanupEnvironment()()
292-
os.Setenv("PATH", "testdata:/bin")
296+
p, _ := filepath.Abs("./testdata")
297+
os.Setenv("PATH", p+":/bin")
293298

294299
aa := DatabricksClient{
295300
Host: "https://abc.cloud.databricks.com",
@@ -301,7 +306,8 @@ func TestGetJWTProperty_NonAzure(t *testing.T) {
301306

302307
func TestGetJWTProperty_AzureCli_Error(t *testing.T) {
303308
defer CleanupEnvironment()()
304-
os.Setenv("PATH", "testdata:/bin")
309+
p, _ := filepath.Abs("./testdata")
310+
os.Setenv("PATH", p+":/bin")
305311

306312
// token without expiry in this case
307313
client, server := singleRequestServer(t, "POST", "/api/2.0/token/create", `{
@@ -350,7 +356,8 @@ func newTestJwt(t *testing.T, claims jwt.MapClaims) string {
350356

351357
func TestGetJWTProperty_AzureCli(t *testing.T) {
352358
defer CleanupEnvironment()()
353-
os.Setenv("PATH", "testdata:/bin")
359+
p, _ := filepath.Abs("./testdata")
360+
os.Setenv("PATH", p+":/bin")
354361
os.Setenv("TF_AAD_TOKEN", newTestJwt(t, jwt.MapClaims{
355362
"tid": "some-tenant",
356363
}))
@@ -367,7 +374,8 @@ func TestGetJWTProperty_AzureCli(t *testing.T) {
367374

368375
func TestGetJWTProperty_Authenticate_Fail(t *testing.T) {
369376
defer CleanupEnvironment()()
370-
os.Setenv("PATH", "testdata:/bin")
377+
p, _ := filepath.Abs("./testdata")
378+
os.Setenv("PATH", p+":/bin")
371379
os.Setenv("FAIL", "yes")
372380

373381
client := &DatabricksClient{
@@ -383,7 +391,8 @@ func TestGetJWTProperty_Authenticate_Fail(t *testing.T) {
383391

384392
func TestGetJWTProperty_makeGetRequest_Fail(t *testing.T) {
385393
defer CleanupEnvironment()()
386-
os.Setenv("PATH", "testdata:/bin")
394+
p, _ := filepath.Abs("./testdata")
395+
os.Setenv("PATH", p+":/bin")
387396
os.Setenv("TF_AAD_TOKEN", newTestJwt(t, jwt.MapClaims{
388397
"tid": "some-tenant",
389398
}))
@@ -400,7 +409,8 @@ func TestGetJWTProperty_makeGetRequest_Fail(t *testing.T) {
400409

401410
func TestGetJWTProperty_authVisitor_Fail(t *testing.T) {
402411
defer CleanupEnvironment()()
403-
os.Setenv("PATH", "testdata:/bin")
412+
p, _ := filepath.Abs("./testdata")
413+
os.Setenv("PATH", p+":/bin")
404414

405415
client := &DatabricksClient{
406416
Host: "https://adb-1232.azuredatabricks.net",
@@ -414,7 +424,8 @@ func TestGetJWTProperty_authVisitor_Fail(t *testing.T) {
414424

415425
func TestGetJWTProperty_AzureCli_Error_DB_PAT(t *testing.T) {
416426
defer CleanupEnvironment()()
417-
os.Setenv("PATH", "testdata:/bin")
427+
p, _ := filepath.Abs("./testdata")
428+
os.Setenv("PATH", p+":/bin")
418429
os.Setenv("TF_AAD_TOKEN", "dapi123")
419430

420431
srv, client := setupJwtTestClient()
@@ -428,7 +439,8 @@ func TestGetJWTProperty_AzureCli_Error_DB_PAT(t *testing.T) {
428439

429440
func TestGetJWTProperty_AzureCli_Error_No_TenantID(t *testing.T) {
430441
defer CleanupEnvironment()()
431-
os.Setenv("PATH", "testdata:/bin")
442+
p, _ := filepath.Abs("./testdata")
443+
os.Setenv("PATH", p+":/bin")
432444
os.Setenv("TF_AAD_TOKEN", newTestJwt(t, jwt.MapClaims{
433445
"something": "else",
434446
}))
@@ -444,7 +456,8 @@ func TestGetJWTProperty_AzureCli_Error_No_TenantID(t *testing.T) {
444456

445457
func TestGetJWTProperty_AzureCli_Error_EmptyToken(t *testing.T) {
446458
defer CleanupEnvironment()()
447-
os.Setenv("PATH", "testdata:/bin")
459+
p, _ := filepath.Abs("./testdata")
460+
os.Setenv("PATH", p+":/bin")
448461
os.Setenv("TF_AAD_TOKEN", " ")
449462

450463
srv, client := setupJwtTestClient()

common/azure_cli_auth_test.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"net/http/httptest"
88
"os"
9+
"path/filepath"
910
"strings"
1011
"sync"
1112
"testing"
@@ -17,7 +18,8 @@ import (
1718

1819
func TestAzureCliAuth(t *testing.T) {
1920
defer CleanupEnvironment()()
20-
os.Setenv("PATH", "testdata:/bin")
21+
p, _ := filepath.Abs("./testdata")
22+
os.Setenv("PATH", p+":/bin")
2123
// fake expiration date for az mock cli
2224
os.Setenv("EXPIRE", "15M")
2325

@@ -67,7 +69,8 @@ func TestOAuthToken_CornerCases(t *testing.T) {
6769

6870
func TestEnsureFreshWithContext(t *testing.T) {
6971
defer CleanupEnvironment()()
70-
os.Setenv("PATH", "testdata:/bin")
72+
p, _ := filepath.Abs("./testdata")
73+
os.Setenv("PATH", p+":/bin")
7174

7275
rct := refreshableCliToken{
7376
token: &adal.Token{
@@ -81,7 +84,8 @@ func TestEnsureFreshWithContext(t *testing.T) {
8184

8285
func TestRefreshWithContext(t *testing.T) {
8386
defer CleanupEnvironment()()
84-
os.Setenv("PATH", "testdata:/bin")
87+
p, _ := filepath.Abs("./testdata")
88+
os.Setenv("PATH", p+":/bin")
8589

8690
rct := refreshableCliToken{
8791
token: &adal.Token{
@@ -95,7 +99,8 @@ func TestRefreshWithContext(t *testing.T) {
9599

96100
func TestRefreshExchangeWithContext(t *testing.T) {
97101
defer CleanupEnvironment()()
98-
os.Setenv("PATH", "testdata:/bin")
102+
p, _ := filepath.Abs("./testdata")
103+
os.Setenv("PATH", p+":/bin")
99104

100105
rct := refreshableCliToken{
101106
token: &adal.Token{
@@ -109,7 +114,8 @@ func TestRefreshExchangeWithContext(t *testing.T) {
109114

110115
func TestInternalRefresh_ExitError(t *testing.T) {
111116
defer CleanupEnvironment()()
112-
os.Setenv("PATH", "testdata:/bin")
117+
p, _ := filepath.Abs("./testdata")
118+
os.Setenv("PATH", p+":/bin")
113119
os.Setenv("FAIL", "yes")
114120

115121
rct := refreshableCliToken{
@@ -138,7 +144,8 @@ func TestInternalRefresh_OtherError(t *testing.T) {
138144

139145
func TestInternalRefresh_Corrupt(t *testing.T) {
140146
defer CleanupEnvironment()()
141-
os.Setenv("PATH", "testdata:/bin")
147+
p, _ := filepath.Abs("./testdata")
148+
os.Setenv("PATH", p+":/bin")
142149
os.Setenv("FAIL", "corrupt")
143150

144151
rct := refreshableCliToken{
@@ -153,7 +160,8 @@ func TestInternalRefresh_Corrupt(t *testing.T) {
153160

154161
func TestInternalRefresh_CorruptExpire(t *testing.T) {
155162
defer CleanupEnvironment()()
156-
os.Setenv("PATH", "testdata:/bin")
163+
p, _ := filepath.Abs("./testdata")
164+
os.Setenv("PATH", p+":/bin")
157165
os.Setenv("EXPIRE", "corrupt")
158166

159167
rct := refreshableCliToken{

provider/provider_test.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"path/filepath"
78
"strings"
89
"testing"
910

@@ -286,14 +287,15 @@ func TestConfig_ConfigProfileAndPassword(t *testing.T) {
286287
var azResourceID = "/subscriptions/a/resourceGroups/b/providers/Microsoft.Databricks/workspaces/c"
287288

288289
func TestConfig_AzureCliHost(t *testing.T) {
290+
p, _ := filepath.Abs("../common/testdata")
289291
providerFixture{
290292
// this test will skip ensureWorkspaceUrl
291293
host: "x",
292294
azureResourceID: azResourceID,
293295
env: map[string]string{
294296
// // these may fail on windows. use docker container for testing.
295-
"PATH": "../common/testdata",
296-
"HOME": "../common/testdata",
297+
"PATH": p,
298+
"HOME": p,
297299
},
298300
assertAzure: true,
299301
assertHost: "https://x",
@@ -302,12 +304,13 @@ func TestConfig_AzureCliHost(t *testing.T) {
302304
}
303305

304306
func TestConfig_AzureCliHost_Fail(t *testing.T) {
307+
p, _ := filepath.Abs("../common/testdata")
305308
providerFixture{
306309
azureResourceID: azResourceID,
307310
env: map[string]string{
308311
// these may fail on windows. use docker container for testing.
309-
"PATH": "../common/testdata",
310-
"HOME": "../common/testdata",
312+
"PATH": p,
313+
"HOME": p,
311314
"FAIL": "yes",
312315
},
313316
assertError: "cannot configure azure-cli auth: Invoking Azure CLI " +
@@ -328,27 +331,29 @@ func TestConfig_AzureCliHost_AzNotInstalled(t *testing.T) {
328331
}
329332

330333
func TestConfig_AzureCliHost_PatConflict(t *testing.T) {
334+
p, _ := filepath.Abs("../common/testdata")
331335
providerFixture{
332336
azureResourceID: azResourceID,
333337
token: "x",
334338
env: map[string]string{
335339
// these may fail on windows. use docker container for testing.
336-
"PATH": "../common/testdata",
337-
"HOME": "../common/testdata",
340+
"PATH": p,
341+
"HOME": p,
338342
},
339343
assertError: "More than one authorization method configured: azure and token",
340344
}.apply(t)
341345
}
342346

343347
func TestConfig_AzureCliHostAndResourceID(t *testing.T) {
348+
p, _ := filepath.Abs("../common/testdata")
344349
providerFixture{
345350
// omit request to management endpoint to get workspace properties
346351
azureResourceID: azResourceID,
347352
host: "x",
348353
env: map[string]string{
349354
// these may fail on windows. use docker container for testing.
350-
"PATH": "../common/testdata",
351-
"HOME": "../common/testdata",
355+
"PATH": p,
356+
"HOME": p,
352357
},
353358
assertAzure: true,
354359
assertHost: "https://x",
@@ -357,13 +362,14 @@ func TestConfig_AzureCliHostAndResourceID(t *testing.T) {
357362
}
358363

359364
func TestConfig_AzureAndPasswordConflict(t *testing.T) {
365+
p, _ := filepath.Abs("../common/testdata")
360366
providerFixture{
361367
host: "x",
362368
azureResourceID: azResourceID,
363369
env: map[string]string{
364370
// these may fail on windows. use docker container for testing.
365-
"PATH": "../common/testdata",
366-
"HOME": "../common/testdata",
371+
"PATH": p,
372+
"HOME": p,
367373
"DATABRICKS_USERNAME": "x",
368374
},
369375
assertError: "More than one authorization method configured: azure and password",

0 commit comments

Comments
 (0)