Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Parameterize acceptance tests for local runs #322

Merged
merged 17 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Parameterize acceptance tests for local runs
  • Loading branch information
csquire committed Mar 12, 2025
commit bbd82aeda2afda823a5e6e6a8aeb191980fe0a8e
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export DBT_CLOUD_ACCOUNT_ID=1234
export DBT_CLOUD_USER_ID=4321
export [email protected]
export DBT_CLOUD_TOKEN=<api_token>
export DBT_CLOUD_HOST_URL=https://<api_host>/api
export DBT_CLOUD_GROUP_IDS=1,2,3
export [email protected]:my-org/dbt-fundamentals.git
export ACC_TEST_GITHUB_APP_INSTALLATION_ID=5678
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,32 @@ The CLI [dbtcloud-terraforming](https://github.com/dbt-labs/dbtcloud-terraformin
Acceptance tests are executed by running the `make test-acceptance` command.

For the acceptance tests to work locally, the following environment variables must be set to appropriate values
for a dbt Cloud account the tests can interact with:
for a dbt Cloud account the tests can interact with. All dbt Cloud resources referenced by the environment variables
(e.g. user id, email address, and group ids) must exist in the dbt Cloud account.
```
DBT_CLOUD_ACCOUNT_ID=1234
DBT_CLOUD_USER_ID=4321
DBT_CLOUD_USER_EMAIL=<email_adress>
DBT_CLOUD_USER_EMAIL=<email_address>
DBT_CLOUD_TOKEN=<api_token>
DBT_CLOUD_HOST_URL=https://<host>/api
DBT_CLOUD_GROUP_IDS=1,2,3
[email protected]:my-org/dbt-fundamentals.git
ACC_TEST_GITHUB_APP_INSTALLATION_ID=1234
```

To assist with setting the environment variables, the `.env.example` file can be copied to `.env` and the values updated.
The variables can then be loaded into the environment by running `source .env` on Mac or Linux.

**A note on the Repository Acceptance Tests**
The Repository Acceptance Tests require a GitHub repository to be set up and the dbt Cloud GitHub App installed.

`ACC_TEST_GITHUB_REPO_URL` must be set to the SSH URL of a repository

`ACC_TEST_GITHUB_APP_INSTALLATION_ID` must be set to the installation ID of the GitHub App.
The installation ID can be found by navigating to `Settings` -> `Applications`,
and clicking `Configure` on the dbt Cloud GitHub App. The installation ID can be found in the url, for example,
`https://github.com/settings/installations/<installation_id>`

## Acknowledgement

Thanks to Gary James [[GtheSheep](https://github.com/GtheSheep)], for all the effort put in creating this provider originally
Expand Down
38 changes: 35 additions & 3 deletions pkg/framework/acctest_helper/acctest_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func IsDbtCloudPR() bool {
// GetDbtCloudUserId returns the user ID to use for acceptance tests.
// Currently, this utilizes some legacy logic to determine the user ID.
// If the DBT_CLOUD_USER_ID environment variable is fully adopted, this
// function could be simplified.
// function can be simplified.
func GetDbtCloudUserId() int {
if IsDbtCloudPR() {
return 1
Expand All @@ -99,7 +99,7 @@ func GetDbtCloudUserId() int {
// GetDbtCloudUserEmail returns the user email to use for acceptance tests.
// Currently, this utilizes some legacy logic to determine the user email.
// If the DBT_CLOUD_USER_EMAIL environment variable is fully adopted, this
// function could be simplified.
// function can be simplified.
func GetDbtCloudUserEmail() string {
if IsDbtCloudPR() {
return "d" + "ev@" + "db" + "tla" + "bs.c" + "om"
Expand All @@ -117,7 +117,7 @@ func GetDbtCloudUserEmail() string {
// GetDbtCloudGroupIds returns the group IDs to use for acceptance tests.
// Currently, this utilizes some legacy logic to determine the group IDs.
// If the DBT_CLOUD_GROUP_IDS environment variable is fully adopted, this
// function could be simplified.
// function can be simplified.
func GetDbtCloudGroupIds() string {
var groupIds string
if IsDbtCloudPR() {
Expand All @@ -133,6 +133,38 @@ func GetDbtCloudGroupIds() string {
return fmt.Sprintf("[%s]", groupIds)
}

// GetGitHubRepoUrl returns the GitHub repository URL to use for acceptance tests.
// Currently, this utilizes some legacy logic to determine the GitHub repository URL.
// If the ACC_TEST_GITHUB_REPO_URL environment variable is fully adopted, this
// function can be simplified.
func GetGitHubRepoUrl() string {
if IsDbtCloudPR() || os.Getenv("CI") != "" {
return "git://github.com/dbt-labs/jaffle_shop.git"
} else {
url := os.Getenv("ACC_TEST_GITHUB_REPO_URL")
if url == "" {
log.Fatalf("Unable to determine GitHub repository url for test")
}
return url
}
}

// GetGitHubAppInstallationId returns the GitHub app installation ID to use for acceptance tests.
// Currently, this utilizes some legacy logic to determine the GitHub app installation ID.
// If the ACC_TEST_GITHUB_APP_INSTALLATION_ID environment variable is fully adopted, this
// function can be simplified.
func GetGitHubAppInstallationId() int {
if IsDbtCloudPR() || os.Getenv("CI") != "" {
return 28374841
} else {
id, err := strconv.Atoi(os.Getenv("ACC_TEST_GITHUB_APP_INSTALLATION_ID"))
if err != nil {
log.Fatalf("Unable to determine GitHub app installation id for test: %v", err)
}
return id
}
}

func HelperTestResourceSchema[R resource.Resource](t *testing.T, r R) {
ctx := context.Background()

Expand Down
14 changes: 10 additions & 4 deletions pkg/sdkv2/resources/repository_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func TestAccDbtCloudRepositoryResource(t *testing.T) {
},
})

repoUrlGithubApplication := "git://github.com/dbt-labs/jaffle_shop.git"
repoUrlGithubApplication := acctest_helper.GetGitHubRepoUrl()
githubAppInstallationId := acctest_helper.GetGitHubAppInstallationId()
projectNameGithubApplication := strings.ToUpper(
acctest.RandStringFromCharSet(10, acctest.CharSetAlpha),
)
Expand All @@ -71,6 +72,7 @@ func TestAccDbtCloudRepositoryResource(t *testing.T) {
Config: testAccDbtCloudRepositoryResourceGithubApplicationConfig(
repoUrlGithubApplication,
projectNameGithubApplication,
githubAppInstallationId,
),
Check: resource.ComposeTestCheckFunc(
testAccCheckDbtCloudRepositoryExists(
Expand Down Expand Up @@ -115,7 +117,11 @@ resource "dbtcloud_repository" "test_repository_github" {
`, projectName, repoUrl)
}

func testAccDbtCloudRepositoryResourceGithubApplicationConfig(repoUrl, projectName string) string {
func testAccDbtCloudRepositoryResourceGithubApplicationConfig(
repoUrl string,
projectName string,
githubAppInstallationId int,
) string {
return fmt.Sprintf(`
resource "dbtcloud_project" "test_project" {
name = "%s"
Expand All @@ -124,11 +130,11 @@ resource "dbtcloud_project" "test_project" {
resource "dbtcloud_repository" "test_repository_github_application" {
remote_url = "%s"
project_id = dbtcloud_project.test_project.id
github_installation_id = 28374841
github_installation_id = %d
git_clone_strategy = "github_app"
pull_request_url_template = "https://github.com/my-org/my-repo/compare/qa...{{source}}"
}
`, projectName, repoUrl)
`, projectName, repoUrl, githubAppInstallationId)
}

//
Expand Down