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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
10 changes: 10 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export DBT_CLOUD_ACCOUNT_ID=1234
export DBT_CLOUD_HOST_URL=https://<api_host>/api
export DBT_CLOUD_TOKEN=<api_token>
export DBT_CLOUD_PERSONAL_ACCESS_TOKEN=<api_token>
export ACC_TEST_DBT_CLOUD_USER_ID=4321
export [email protected]
export ACC_TEST_DBT_CLOUD_GROUP_IDS=1,2,3
export ACC_TEST_AZURE_DEVOPS_PROJECT_NAME=test-project
export [email protected]:<github-org>/<dbt-project>.git
export ACC_TEST_GITHUB_APP_INSTALLATION_ID=5678
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ terraform-provider*
autogen
TODO.md
NOTES.md
.DS_Store
.DS_Store
.env
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,36 @@ The CLI [dbtcloud-terraforming](https://github.com/dbt-labs/dbtcloud-terraformin

## Running Acceptance Tests

Currently, acceptance tests, run via `make test-acceptance` must be done on your
own account
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. 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_HOST_URL=https://<host>/api
DBT_CLOUD_TOKEN=<api_token>
DBT_CLOUD_PERSONAL_ACCESS_TOKEN=<api_token>
ACC_TEST_DBT_CLOUD_USER_EMAIL=<email_address>
ACC_TEST_DBT_CLOUD_USER_ID=4321
ACC_TEST_DBT_CLOUD_GROUP_IDS=1,2,3
ACC_TEST_AZURE_DEVOPS_PROJECT_NAME=test-project
[email protected]:<github-org>/<dbt-project>.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

Expand Down
105 changes: 105 additions & 0 deletions pkg/framework/acctest_config/acctest_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package acctest_config

import (
"log"
"os"
"strconv"
)

var AcceptanceTestConfig = buildAcctestConfig()

func buildAcctestConfig() AcctestConfig {
return AcctestConfig{
DbtCloudAccountId: determineIntValue("DBT_CLOUD_ACCOUNT_ID", 1, 1),
DbtCloudServiceToken: os.Getenv("DBT_CLOUD_TOKEN"),
DbtCloudPersonalAccessToken: os.Getenv("DBT_CLOUD_PERSONAL_ACCESS_TOKEN"),
DbtCloudHostUrl: determineStringValue("DBT_CLOUD_HOST_URL", "", ""),
DbtCloudVersion: "latest",

DbtCloudUserId: determineIntValue(
"ACC_TEST_DBT_CLOUD_USER_ID",
1,
54461,
),
DbtCloudUserEmail: determineStringValue(
"ACC_TEST_DBT_CLOUD_USER_EMAIL",
"d"+"ev@"+"db"+"tla"+"bs.c"+"om",
"beno"+"it"+".per"+"igaud"+"@"+"fisht"+"ownanalytics"+"."+"com",
),
DbtCloudGroupIds: determineStringValue(
"ACC_TEST_DBT_CLOUD_GROUP_IDS",
"1,2,3",
"531585,531584,531583",
),
AzureDevOpsProjectName: determineStringValue(
"ACC_TEST_AZURE_DEVOPS_PROJECT_NAME",
"dbt-cloud-ado-project",
"dbt-cloud-ado-project",
),
GitHubRepoUrl: determineStringValue(
"ACC_TEST_GITHUB_REPO_URL",
"git://github.com/dbt-labs/jaffle_shop.git",
"git://github.com/dbt-labs/jaffle_shop.git",
),
GitHubAppInstallationId: determineIntValue(
"ACC_TEST_GITHUB_APP_INSTALLATION_ID",
28374841,
28374841,
),
}
}

type AcctestConfig struct {
DbtCloudAccountId int
DbtCloudServiceToken string
DbtCloudPersonalAccessToken string
DbtCloudHostUrl string
DbtCloudVersion string

DbtCloudUserId int
DbtCloudUserEmail string
DbtCloudGroupIds string
AzureDevOpsProjectName string
GitHubRepoUrl string
GitHubAppInstallationId int
}

func IsDbtCloudPR() bool {
return os.Getenv("DBT_CLOUD_ACCOUNT_ID") == "1"
}

func IsCI() bool {
return os.Getenv("CI") != ""
}

func determineStringValue(envVarKey string, dbtCloudPRValue string, ciValue string) string {
val := os.Getenv(envVarKey)
if val != "" {
return val
} else if IsDbtCloudPR() {
return dbtCloudPRValue
} else if IsCI() {
return ciValue
} else {
log.Printf("Unable to determine %s value, tests may fail", envVarKey)
return ""
}
}

func determineIntValue(envVarKey string, dbtCloudPRValue int, ciValue int) int {
val := os.Getenv(envVarKey)
if val != "" {
intVal, err := strconv.Atoi(val)
if err != nil {
log.Fatalf("Unable to determine %s value for test: %v", envVarKey, err)
}
return intVal
} else if IsDbtCloudPR() {
return dbtCloudPRValue
} else if IsCI() {
return ciValue
} else {
log.Printf("Unable to determine %s value, tests may fail", envVarKey)
return -1
}
}
8 changes: 0 additions & 8 deletions pkg/framework/acctest_helper/acctest_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ func SharedClient() (*dbt_cloud.Client, error) {
return &client, nil
}

const (
DBT_CLOUD_VERSION = "latest"
)

var TestAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"dbtcloud": func() (tfprotov6.ProviderServer, error) {
upgradedSdkProvider, err := tf5to6server.UpgradeServer(
Expand Down Expand Up @@ -73,10 +69,6 @@ func TestAccPreCheck(t *testing.T) {
}
}

func IsDbtCloudPR() bool {
return os.Getenv("DBT_CLOUD_ACCOUNT_ID") == "1"
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package azure_dev_ops_project_test

import (
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config"
"os"
"testing"

Expand All @@ -10,19 +11,18 @@ import (
)

func TestAccDbtCloudAzureDevOpsProject(t *testing.T) {
//TODO: Remove both env var checks when this gets configurted in CI and the variables are parameterized
//TODO: Remove both env var checks when this gets configured in CI and the variables are parameterized
if os.Getenv("CI") != "" {
t.Skip("Skipping Azure DevOps Project datasource test in CI " +
"until Azure integration and a personal access token are available")
}

if os.Getenv("DBT_CLOUD_PERSONAL_ACCESS_TOKEN") == "" {
personalAccessToken := acctest_config.AcceptanceTestConfig.DbtCloudPersonalAccessToken
if personalAccessToken == "" {
t.Skip("Skipping Azure DevOps Project datasource because no personal access token is available")
}

//TODO: Parameterize these values when a standard method is available for parameterization
adoProjectName := "dbt-cloud-ado-project"
personalAccessToken := os.Getenv("DBT_CLOUD_PERSONAL_ACCESS_TOKEN")
adoProjectName := acctest_config.AcceptanceTestConfig.AzureDevOpsProjectName

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest_helper.TestAccPreCheck(t) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package environment_test

import (
"fmt"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config"
"testing"

"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper"
Expand Down Expand Up @@ -70,5 +71,5 @@ func environment(projectName, environmentName string) string {
project_id = dbtcloud_project.test_project.id
environment_id = dbtcloud_environment.test_environment.environment_id
}
`, projectName, environmentName, acctest_helper.DBT_CLOUD_VERSION)
`, projectName, environmentName, acctest_config.AcceptanceTestConfig.DbtCloudVersion)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package environment_test

import (
"fmt"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config"
"testing"

"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper"
Expand Down Expand Up @@ -95,5 +96,12 @@ func environments(
project_id = dbtcloud_project.test_project2.id
depends_on = [dbtcloud_environment.test_environment1, dbtcloud_environment.test_environment2]
}
`, randomProjectName1, randomProjectName2, randomEnvironmentName1, acctest_helper.DBT_CLOUD_VERSION, randomEnvironmentName2, acctest_helper.DBT_CLOUD_VERSION)
`,
randomProjectName1,
randomProjectName2,
randomEnvironmentName1,
acctest_config.AcceptanceTestConfig.DbtCloudVersion,
randomEnvironmentName2,
acctest_config.AcceptanceTestConfig.DbtCloudVersion,
)
}
3 changes: 2 additions & 1 deletion pkg/framework/objects/job/data_source_all_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package job_test

import (
"fmt"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config"
"testing"

"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper"
Expand Down Expand Up @@ -137,5 +138,5 @@ func jobs(jobName string, jobName2 string) string {
dbtcloud_job.test_job2,
]
}
`, acctest_helper.DBT_CLOUD_VERSION, acctest_helper.DBT_CLOUD_VERSION, jobName, jobName2)
`, acctest_config.AcceptanceTestConfig.DbtCloudVersion, acctest_config.AcceptanceTestConfig.DbtCloudVersion, jobName, jobName2)
}
16 changes: 6 additions & 10 deletions pkg/framework/objects/notification/data_source_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package notification_test

import (
"fmt"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config"
"testing"
"time"

Expand All @@ -12,16 +13,11 @@ import (

func TestAccDbtCloudNotificationDataSource(t *testing.T) {

if acctest_helper.IsDbtCloudPR() {
if acctest_config.IsDbtCloudPR() {
t.Skip("Skipping notifications in dbt Cloud CI for now")
}

var userID string
if acctest_helper.IsDbtCloudPR() {
userID = "1"
} else {
userID = "100"
}
userID := acctest_config.AcceptanceTestConfig.DbtCloudUserId

currentTime := time.Now().Unix()
notificationEmail := fmt.Sprintf("%[email protected]", currentTime)
Expand Down Expand Up @@ -58,7 +54,7 @@ func TestAccDbtCloudNotificationDataSource(t *testing.T) {
})
}

func notification(projectName, userID, notificationEmail string) string {
func notification(projectName string, userID int, notificationEmail string) string {
return fmt.Sprintf(`
resource "dbtcloud_project" "test_notification_project" {
name = "%s"
Expand Down Expand Up @@ -86,7 +82,7 @@ func notification(projectName, userID, notificationEmail string) string {
}

resource "dbtcloud_notification" "test_notification_external" {
user_id = %s
user_id = %d
on_failure = [dbtcloud_job.test_notification_job_1.id]
notification_type = 4
external_email = "%s"
Expand All @@ -95,5 +91,5 @@ func notification(projectName, userID, notificationEmail string) string {
data "dbtcloud_notification" "test_notification_external" {
notification_id = dbtcloud_notification.test_notification_external.id
}
`, projectName, acctest_helper.DBT_CLOUD_VERSION, userID, notificationEmail)
`, projectName, acctest_config.AcceptanceTestConfig.DbtCloudVersion, userID, notificationEmail)
}
Loading
Loading