Skip to content

Commit 14b488c

Browse files
authored
Fix for https://relationalai.atlassian.net/browse/RAI-9265 - Failed to create user as it already exists (#59)
* Fix for RAI-9265 * Added the sdk prefix with the email * Make the test to fail if the teardown fails.
1 parent 4d5f75c commit 14b488c

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.18
44

55
require (
66
github.com/apache/arrow/go/v7 v7.0.0
7+
github.com/google/uuid v1.1.2
78
github.com/jessevdk/go-flags v1.5.0
89
github.com/pkg/errors v0.9.1
910
github.com/shopspring/decimal v1.3.1

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
136136
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
137137
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
138138
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
139+
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
139140
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
140141
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
141142
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=

rai/main_test.go

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os"
1414
"testing"
1515

16+
"github.com/google/uuid"
1617
"github.com/pkg/errors"
1718
)
1819

@@ -152,21 +153,37 @@ func newTestClient() (*Client, error) {
152153
return testClient, nil
153154
}
154155

155-
func tearDown(client *Client) {
156-
err := client.DeleteDatabase(test.databaseName)
157-
if err != nil {
158-
fmt.Println(errors.Wrapf(err, "error deleting database: %s", test.databaseName))
156+
func tearDown(client *Client) int {
157+
exitCode := 0
158+
159+
// Find and delete the database
160+
// Finding is required because test could have already deleted the database.
161+
db, _ := client.GetDatabase(test.databaseName)
162+
if db != nil {
163+
err := client.DeleteDatabase(test.databaseName)
164+
if err != nil {
165+
fmt.Println(errors.Wrapf(err, "error deleting database: %s", test.databaseName))
166+
exitCode = 1
167+
}
159168
}
160-
err = client.DeleteEngine(test.engineName)
161-
if err != nil {
162-
fmt.Println(errors.Wrapf(err, "error deleting engine: %s", test.engineName))
169+
170+
// Find and delete the engine
171+
// Finding is required because test could have already deleted the engine.
172+
engine, _ := client.GetEngine(test.engineName)
173+
if engine != nil {
174+
err := client.DeleteEngine(test.engineName)
175+
if err != nil {
176+
fmt.Println(errors.Wrapf(err, "error deleting engine: %s", test.engineName))
177+
exitCode = 1
178+
}
163179
}
164180

165181
user, _ := client.FindUser(test.userEmail)
166182
if user != nil {
167183
_, err := client.DeleteUser(user.ID)
168184
if err != nil {
169185
fmt.Println(errors.Wrapf(err, "error deleting user: %s", test.userEmail))
186+
exitCode = 1
170187
}
171188
}
172189

@@ -175,19 +192,29 @@ func tearDown(client *Client) {
175192
_, err := client.DeleteOAuthClient(c.ID)
176193
if err != nil {
177194
fmt.Println(errors.Wrapf(err, "error deleting oauth client: %s", test.oauthClient))
195+
exitCode = 1
178196
}
179197
}
198+
199+
return exitCode
180200
}
181201

182202
// Global setup & teardown for golang SDK tests.
183203
func TestMain(m *testing.M) {
184204
var err error
185205

206+
// Generating a random email address.
207+
// Using a common email address can create user creation or deletion issues in edge cases -
208+
// when tests run in parallel on multiple machines, for example, the CI/CD workflows.
209+
// Context: https://relationalai.atlassian.net/browse/RAI-9265
210+
userEmail := fmt.Sprintf("rai-sdk-go-%s@relational.ai", uuid.New().String())
211+
// Same is true for oAuthClient client name.
212+
oAuthClient := fmt.Sprintf("rai-sdk-go-%s", uuid.New().String())
186213
flag.StringVar(&test.databaseName, "d", "rai-sdk-go", "test database name")
187214
flag.StringVar(&test.engineName, "e", "rai-sdk-go", "test engine name")
188215
flag.StringVar(&test.engineSize, "s", "S", "test engine size")
189-
flag.StringVar(&test.oauthClient, "c", "rai-sdk-go", "test OAuth client name")
190-
flag.StringVar(&test.userEmail, "u", "rai-sdk-go@relational.ai", "test user name")
216+
flag.StringVar(&test.oauthClient, "c", oAuthClient, "test OAuth client name")
217+
flag.StringVar(&test.userEmail, "u", userEmail, "test user name")
191218
flag.BoolVar(&test.noTeardown, "no-teardown", false, "don't teardown test resources")
192219
flag.BoolVar(&test.showQuery, "show-query", false, "display query string")
193220
flag.Parse()
@@ -207,7 +234,9 @@ func TestMain(m *testing.M) {
207234
code := m.Run()
208235
if !test.noTeardown {
209236
fmt.Println("Tearing down resources ....")
210-
tearDown(test.client)
237+
if exitCode := tearDown(test.client); exitCode > 0 {
238+
os.Exit(exitCode)
239+
}
211240
}
212241
os.Exit(code)
213242
}

0 commit comments

Comments
 (0)