Skip to content

Commit a24b52c

Browse files
authored
acc: Add IsServicePrincipal option (#3315)
## Why In some unit tests (e.g. run_as) we set current user to service principal and that's important setting for those tests. This allows converting those tests to acceptance tests.
1 parent 6dfbfd6 commit a24b52c

File tree

10 files changed

+63
-20
lines changed

10 files changed

+63
-20
lines changed

acceptance/internal/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ type TestConfig struct {
5252
// If true and Cloud=true, run this test only if a default warehouse is available in the cloud environment
5353
RequiresWarehouse *bool
5454

55+
// If set, current user will be set to a service principal-like UUID instead of email (default is false)
56+
IsServicePrincipal *bool
57+
5558
// List of additional replacements to apply on this test.
5659
// Old is a regexp, New is a replacement expression.
5760
Repls []testdiff.Replacement

acceptance/internal/handlers.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strconv"
99

1010
"github.com/databricks/databricks-sdk-go/service/catalog"
11-
"github.com/databricks/databricks-sdk-go/service/iam"
1211

1312
"github.com/databricks/databricks-sdk-go/service/compute"
1413
"github.com/databricks/databricks-sdk-go/service/jobs"
@@ -18,11 +17,6 @@ import (
1817
"github.com/databricks/databricks-sdk-go/service/workspace"
1918
)
2019

21-
var TestUser = iam.User{
22-
Id: "1000012345",
23-
UserName: "[email protected]",
24-
}
25-
2620
var TestMetastore = catalog.MetastoreAssignment{
2721
DefaultCatalogName: "hive_metastore",
2822
MetastoreId: "120efa64-9b68-46ba-be38-f319458430d2",
@@ -74,7 +68,7 @@ func addDefaultHandlers(server *testserver.Server) {
7468
server.Handle("GET", "/api/2.0/preview/scim/v2/Me", func(req testserver.Request) any {
7569
return testserver.Response{
7670
Headers: map[string][]string{"X-Databricks-Org-Id": {"900800700600"}},
77-
Body: TestUser,
71+
Body: req.Workspace.CurrentUser(),
7872
}
7973
})
8074

acceptance/internal/prepare_server.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,16 @@ func PrepareServerAndClient(t *testing.T, config TestConfig, logRequests bool, o
4747
// Use a unique token for each test. This allows us to maintain
4848
// separate state for each test in fake workspaces.
4949
tokenSuffix := strings.ReplaceAll(uuid.NewString(), "-", "")
50-
token := "dbapi" + tokenSuffix
50+
51+
var token string
52+
var testUser iam.User
53+
if isTruePtr(config.IsServicePrincipal) {
54+
token = testserver.ServicePrincipalTokenPrefix + tokenSuffix
55+
testUser = testserver.TestUserSP
56+
} else {
57+
token = testserver.UserNameTokenPrefix + tokenSuffix
58+
testUser = testserver.TestUser
59+
}
5160

5261
if cloudEnv != "" {
5362
w, err := databricks.NewWorkspaceClient()
@@ -79,7 +88,7 @@ func PrepareServerAndClient(t *testing.T, config TestConfig, logRequests bool, o
7988
Token: token,
8089
}
8190

82-
return cfg, TestUser
91+
return cfg, testUser
8392
}
8493

8594
// Default case. Start a dedicated local server for the test with the server stubs configured
@@ -92,7 +101,7 @@ func PrepareServerAndClient(t *testing.T, config TestConfig, logRequests bool, o
92101

93102
// For the purposes of replacements, use testUser for local runs.
94103
// Note, users might have overriden /api/2.0/preview/scim/v2/Me but that should not affect the replacement:
95-
return cfg, TestUser
104+
return cfg, testUser
96105
}
97106

98107
func recordRequestsCallback(t *testing.T, includeHeaders []string, outputDir string) func(request *testserver.Request) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Local = true
2+
Cloud = false
3+
4+
[EnvMatrix]
5+
DATABRICKS_CLI_DEPLOYMENT = ["terraform", "direct-exp"]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
>>> [CLI] current-user me
3+
{
4+
"id":"[USERID]",
5+
"userName":"X649126d-dc78-4e80-9c8d-86e6b3d2ffb3"
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
trace $CLI current-user me | sed 's/e649126d/X649126d/'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
IsServicePrincipal = true

libs/testserver/fake_workspace.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/databricks/databricks-sdk-go/service/apps"
1515
"github.com/databricks/databricks-sdk-go/service/catalog"
1616
"github.com/databricks/databricks-sdk-go/service/dashboards"
17+
"github.com/databricks/databricks-sdk-go/service/iam"
1718
"github.com/databricks/databricks-sdk-go/service/jobs"
1819
"github.com/databricks/databricks-sdk-go/service/pipelines"
1920
"github.com/databricks/databricks-sdk-go/service/sql"
@@ -26,19 +27,33 @@ import (
2627
// (encoding/json without options parses numbers into float64)
2728
// These are also easier to spot / replace in test output compared to numbers with one or few digits.
2829
const (
29-
TestJobID = 4611686018427387911
30-
TestRunID = 2305843009213693969
30+
TestJobID = 4611686018427387911
31+
TestRunID = 2305843009213693969
32+
UserNameTokenPrefix = "dbapi0"
33+
ServicePrincipalTokenPrefix = "dbapi1"
34+
UserID = "1000012345"
3135
)
3236

37+
var TestUser = iam.User{
38+
Id: UserID,
39+
UserName: "[email protected]",
40+
}
41+
42+
var TestUserSP = iam.User{
43+
Id: UserID,
44+
UserName: "e649126d-dc78-4e80-9c8d-86e6b3d2ffb3",
45+
}
46+
3347
type FileEntry struct {
3448
Info workspace.ObjectInfo
3549
Data []byte
3650
}
3751

3852
// FakeWorkspace holds a state of a workspace for acceptance tests.
3953
type FakeWorkspace struct {
40-
mu sync.Mutex
41-
url string
54+
mu sync.Mutex
55+
url string
56+
isServicePrincipal bool
4257

4358
directories map[string]bool
4459
files map[string]FileEntry
@@ -119,9 +134,10 @@ func MapDelete[K comparable, V any](w *FakeWorkspace, collection map[K]V, key K)
119134
return Response{}
120135
}
121136

122-
func NewFakeWorkspace(url string) *FakeWorkspace {
137+
func NewFakeWorkspace(url, token string) *FakeWorkspace {
123138
return &FakeWorkspace{
124-
url: url,
139+
url: url,
140+
isServicePrincipal: strings.HasPrefix(token, ServicePrincipalTokenPrefix),
125141
directories: map[string]bool{
126142
"/Workspace": true,
127143
},
@@ -144,6 +160,14 @@ func NewFakeWorkspace(url string) *FakeWorkspace {
144160
}
145161
}
146162

163+
func (s *FakeWorkspace) CurrentUser() iam.User {
164+
if s != nil && s.isServicePrincipal {
165+
return TestUserSP
166+
} else {
167+
return TestUser
168+
}
169+
}
170+
147171
func (s *FakeWorkspace) WorkspaceGetStatus(path string) Response {
148172
defer s.LockUnlock()()
149173

libs/testserver/pipelines_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ func createTestPipeline(t *testing.T, workspace *FakeWorkspace) string {
2626
}
2727

2828
func TestPipelineStartUpdate_HandlesNonExistentPipeline(t *testing.T) {
29-
workspace := NewFakeWorkspace("http://test")
29+
workspace := NewFakeWorkspace("http://test", "dbapi123")
3030

3131
response := workspace.PipelineStartUpdate("non-existent-pipeline")
3232
assert.Equal(t, 404, response.StatusCode)
3333
assert.Contains(t, response.Body.(map[string]string)["message"], "The specified pipeline non-existent-pipeline was not found")
3434
}
3535

3636
func TestPipelineGetUpdate_HandlesNonExistent(t *testing.T) {
37-
workspace := NewFakeWorkspace("http://test")
37+
workspace := NewFakeWorkspace("http://test", "dbapi123")
3838

3939
response := workspace.PipelineGetUpdate("non-existent-pipeline", "some-update-id")
4040
assert.Equal(t, 404, response.StatusCode)
@@ -47,7 +47,7 @@ func TestPipelineGetUpdate_HandlesNonExistent(t *testing.T) {
4747
}
4848

4949
func TestPipelineStop_AfterUpdate(t *testing.T) {
50-
workspace := NewFakeWorkspace("http://test")
50+
workspace := NewFakeWorkspace("http://test", "dbapi123")
5151

5252
pipelineId := createTestPipeline(t, workspace)
5353

libs/testserver/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func (s *Server) getWorkspaceForToken(token string) *FakeWorkspace {
242242
defer s.mu.Unlock()
243243

244244
if _, ok := s.fakeWorkspaces[token]; !ok {
245-
s.fakeWorkspaces[token] = NewFakeWorkspace(s.Server.URL)
245+
s.fakeWorkspaces[token] = NewFakeWorkspace(s.Server.URL, token)
246246
}
247247

248248
return s.fakeWorkspaces[token]

0 commit comments

Comments
 (0)