Skip to content

Commit 3bc99f6

Browse files
authored
Fix TestUcAccJobRunAsMutations (#3474)
* Fix TestUcAccJobRunAsMutations * fixed
1 parent cac7606 commit 3bc99f6

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

internal/acceptance/init_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,19 +354,29 @@ func isInDebug() bool {
354354
return strings.HasPrefix(path.Base(ex), "__debug_bin")
355355
}
356356

357+
func setDebugLogger() {
358+
logger.DefaultLogger = &logger.SimpleLogger{
359+
Level: logger.LevelDebug,
360+
}
361+
}
362+
357363
func loadWorkspaceEnv(t *testing.T) {
364+
setDebugLogger()
358365
loadDebugEnvIfRunsFromIDE(t, "workspace")
359366
}
360367

361368
func loadAccountEnv(t *testing.T) {
369+
setDebugLogger()
362370
loadDebugEnvIfRunsFromIDE(t, "account")
363371
}
364372

365373
func loadUcwsEnv(t *testing.T) {
374+
setDebugLogger()
366375
loadDebugEnvIfRunsFromIDE(t, "ucws")
367376
}
368377

369378
func loadUcacctEnv(t *testing.T) {
379+
setDebugLogger()
370380
loadDebugEnvIfRunsFromIDE(t, "ucacct")
371381
}
372382

@@ -393,6 +403,20 @@ func isCloudEnvInList(t *testing.T, cloudEnvs []string) bool {
393403
return slices.Contains(cloudEnvs, cloudEnv)
394404
}
395405

406+
func isAuthedAsWorkspaceServicePrincipal(ctx context.Context) (bool, error) {
407+
w := databricks.Must(databricks.NewWorkspaceClient())
408+
user, err := w.CurrentUser.Me(ctx)
409+
if err != nil {
410+
return false, err
411+
}
412+
for _, emailValue := range user.Emails {
413+
if emailValue.Primary && strings.Contains(emailValue.Value, "@") {
414+
return false, nil
415+
}
416+
}
417+
return true, nil
418+
}
419+
396420
// loads debug environment from ~/.databricks/debug-env.json
397421
func loadDebugEnvIfRunsFromIDE(t *testing.T, key string) {
398422
if !isInDebug() {

internal/acceptance/job_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/databricks/terraform-provider-databricks/common"
1313
"github.com/databricks/terraform-provider-databricks/qa"
1414
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/require"
1516
)
1617

1718
func TestAccJobTasks(t *testing.T) {
@@ -355,9 +356,21 @@ func TestUcAccJobRunAsServicePrincipal(t *testing.T) {
355356
})
356357
}
357358

359+
func getRunAsAttribute(t *testing.T, ctx context.Context) string {
360+
isSp, err := isAuthedAsWorkspaceServicePrincipal(ctx)
361+
require.NoError(t, err)
362+
if isSp {
363+
return "service_principal_name"
364+
}
365+
return "user_name"
366+
}
367+
358368
func TestUcAccJobRunAsMutations(t *testing.T) {
359369
loadUcwsEnv(t)
360370
spId := GetEnvOrSkipTest(t, "ACCOUNT_LEVEL_SERVICE_PRINCIPAL_ID")
371+
// Note: the attribute must match the type of principal that the test is run as.
372+
ctx := context.Background()
373+
attribute := getRunAsAttribute(t, ctx)
361374
unityWorkspaceLevel(
362375
t,
363376
// Provision job with service principal `run_as`
@@ -366,7 +379,7 @@ func TestUcAccJobRunAsMutations(t *testing.T) {
366379
},
367380
// Update job to a user `run_as`
368381
step{
369-
Template: runAsTemplate(`user_name = data.databricks_current_user.me.user_name`),
382+
Template: runAsTemplate(attribute + ` = data.databricks_current_user.me.user_name`),
370383
},
371384
// Update job back to a service principal `run_as`
372385
step{

0 commit comments

Comments
 (0)