Skip to content

Commit

Permalink
Merge pull request #49 from xataio/integ-test-alignment
Browse files Browse the repository at this point in the history
Improvements to integration tests setup
  • Loading branch information
philkra authored Dec 22, 2023
2 parents 6600529 + daacd07 commit fce93e3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 72 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ on: [push]

jobs:
build:
runs-on: [self-hosted, default]
#runs-on: [self-hosted, default]
runs-on: default
steps:
- uses: actions/checkout@v3
- name: Set up Go
Expand Down
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ test: ## run unit tests

integration-test: ## run integration tests
TEST_DIRECTORY=./internal/integration-tests go run gotest.tools/gotestsum@latest --format testname
$(MAKE) clean-workspaces

download-openapi-specs: ## download openapi specs
@echo "Downloading openapi specs"
Expand Down
69 changes: 10 additions & 59 deletions internal/integration-tests/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ package integrationtests
import (
"context"
"fmt"
"math/rand"
"net/http"
"os"
"strings"
"time"

//"strings"

"github.com/hashicorp/go-retryablehttp"
"github.com/xataio/xata-go/xata"
Expand Down Expand Up @@ -68,12 +69,11 @@ func setupDatabase() (*config, error) {
)

db, err := databaseCli.Create(ctx, xata.CreateDatabaseRequest{
DatabaseName: "db" + cfg.testID,
DatabaseName: "sdk-integration-test-go-" + cfg.testID,
WorkspaceID: xata.String(cfg.wsID),
Region: &cfg.region,
UI: &xata.UI{Color: xata.String("RED")},
BranchMetaData: &xata.BranchMetadata{
Repository: xata.String("github.com/my/repository"),
Repository: xata.String("github.com/xataio/xata-go"),
Branch: xata.String("feature-branch"),
Stage: xata.String("testing"),
Labels: &[]string{"development"},
Expand Down Expand Up @@ -354,64 +354,15 @@ func cleanup(cfg *config) error {
return err
}
}
if cfg.wsID != "" {
workspaceCli, err := xata.NewWorkspacesClient(
xata.WithAPIKey(cfg.apiKey),
xata.WithHTTPClient(cfg.httpCli),
)
if err != nil {
return err
}

err = workspaceCli.Delete(ctx, cfg.wsID)
if err != nil {
return err
}
}

return nil
}

func testIdentifier() string {
currentTime := time.Now()

// Print the time
return fmt.Sprintf(
"integration-test_%d-%d-%d_%d_%d_%d",
currentTime.Year(),
currentTime.Month(),
currentTime.Day(),
currentTime.Hour(),
currentTime.Minute(),
currentTime.Second(),
)
}

func cleanAllWorkspaces() error {
ctx := context.Background()
apiKey, found := os.LookupEnv("XATA_API_KEY")
if !found {
return fmt.Errorf("%s not found in env vars", "XATA_API_KEY")
}

workspaceCli, err := xata.NewWorkspacesClient(xata.WithAPIKey(apiKey), xata.WithHTTPClient(retryablehttp.NewClient().StandardClient()))
if err != nil {
return err
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234566789"
b := make([]byte, 8)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}

listResponse, err := workspaceCli.List(ctx)
if err != nil {
return err
}

for _, ws := range listResponse.Workspaces {
if strings.Contains(ws.Name, "integration-test") {
err = workspaceCli.Delete(ctx, ws.Id)
if err != nil {
return err
}
}
}

return nil
return string(b)
}
11 changes: 0 additions & 11 deletions internal/integration-tests/setup_test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package integrationtests

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -31,13 +30,3 @@ func Test_setup_cleanup(t *testing.T) {
}
})
}

func Test_cleanupIntegrationWorkspaces(t *testing.T) {
if _, found := os.LookupEnv("CLEAN_UP_INTEGRATION_WORKSPACES"); !found {
t.Skip("skipping integration workspaces cleanup")
}
err := cleanAllWorkspaces()
if err != nil {
t.Fatal(err)
}
}

0 comments on commit fce93e3

Please sign in to comment.