Skip to content

Commit

Permalink
fix: add sweepers (#1203)
Browse files Browse the repository at this point in the history
* add sweepers

* update
  • Loading branch information
sfc-gh-swinkler authored Sep 8, 2022
1 parent 2d4b618 commit 6c004a3
Show file tree
Hide file tree
Showing 16 changed files with 269 additions and 54 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ jobs:
SNOWFLAKE_ACCOUNT_THIRD: ${{ secrets.SNOWFLAKE_ACCOUNT_THIRD }}
run: make test-acceptance

- name: sweepers cleanup
if: always()
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SNOWFLAKE_USER: ${{ secrets.SNOWFLAKE_USER }}
SNOWFLAKE_PASSWORD: ${{ secrets.SNOWFLAKE_PASSWORD }}
SNOWFLAKE_ACCOUNT: ${{ secrets.SNOWFLAKE_ACCOUNT }}
SNOWFLAKE_ROLE: ${{ secrets.SNOWFLAKE_ROLE }}
SNOWFLAKE_WAREHOUSE: ${{ secrets.SNOWFLAKE_WAREHOUSE }}
run: make sweep

- name: find comment
if: ${{ always() }}
uses: peter-evans/find-comment@v1
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ setup: ## setup development dependencies
curl -sfL https://raw.githubusercontent.com/reviewdog/reviewdog/master/install.sh| sh
.PHONY: setup

sweep:
@echo "WARNING: This will destroy infrastructure. Use only in development accounts."
go test ./pkg/resources -v -timeout 10m -sweep=prod

lint: ## run the fast go linters
./bin/reviewdog -conf .reviewdog.yml -diff "git diff main"
.PHONY: lint
Expand Down
39 changes: 39 additions & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"crypto/rsa"
"database/sql"
"encoding/json"
"encoding/pem"
"fmt"
Expand Down Expand Up @@ -505,3 +506,41 @@ func GetOauthAccessToken(
}
return result.AccessToken, nil
}

func GetDatabaseHandleFromEnv() (db *sql.DB, err error) {
account := os.Getenv("SNOWFLAKE_ACCOUNT")
user := os.Getenv("SNOWFLAKE_USER")
password := os.Getenv("SNOWFLAKE_PASSWORD")
browserAuth := os.Getenv("SNOWFLAKE_BROWSER_AUTH") == "true"
privateKeyPath := os.Getenv("SNOWFLAKE_PRIVATE_KEY_PATH")
privateKey := os.Getenv("SNOWFLAKE_PRIVATE_KEY")
privateKeyPassphrase := os.Getenv("SNOWFLAKE_PRIVATE_KEY_PASSPHRASE")
oauthAccessToken := os.Getenv("SNOWFLAKE_OAUTH_ACCESS_TOKEN")
region := os.Getenv("SNOWFLAKE_REGION")
role := os.Getenv("SNOWFLAKE_ROLE")
host := os.Getenv("SNOWFLAKE_HOST")
warehouse := os.Getenv("SNOWFLAKE_WAREHOUSE")

dsn, err := DSN(
account,
user,
password,
browserAuth,
privateKeyPath,
privateKey,
privateKeyPassphrase,
oauthAccessToken,
region,
role,
host,
warehouse,
)
if err != nil {
return nil, err
}
db, err = sql.Open("snowflake", dsn)
if err != nil {
return nil, err
}
return db, nil
}
6 changes: 1 addition & 5 deletions pkg/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ func TestProvider(t *testing.T) {
r.NoError(err)
}

// func TestConfigureProvider(t *testing.T) {
// // r := require.New(t)
// }

func TestDSN(t *testing.T) {
type args struct {
account,
Expand Down Expand Up @@ -181,7 +177,7 @@ func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}

//NewTestClient returns *http.Client with Transport replaced to avoid making real calls
// NewTestClient returns *http.Client with Transport replaced to avoid making real calls
func NewTestClient(fn RoundTripFunc) *http.Client {
return &http.Client{
Transport: RoundTripFunc(fn),
Expand Down
4 changes: 2 additions & 2 deletions pkg/resources/database_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func TestAcc_Database(t *testing.T) {
t.Skip("Skipping TestAccDatabase")
}

prefix := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
prefix2 := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
prefix := "tst-terraform" + strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
prefix2 := "tst-terraform" + strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))

resource.ParallelTest(t, resource.TestCase{
Providers: providers(),
Expand Down
46 changes: 23 additions & 23 deletions pkg/resources/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func TestDatabaseCreate(t *testing.T) {
r := require.New(t)

in := map[string]interface{}{
"name": "good_name",
"name": "tst-terraform-good_name",
"comment": "great comment",
}
d := schema.TestResourceDataRaw(t, resources.Database().Schema, in)
r.NotNil(d)

WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectExec(`CREATE DATABASE "good_name" COMMENT = 'great comment'`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`CREATE DATABASE "tst-terraform-good_name" COMMENT = 'great comment'`).WillReturnResult(sqlmock.NewResult(1, 1))
expectRead(mock)
err := resources.CreateDatabase(d, db)
r.NoError(err)
Expand All @@ -41,7 +41,7 @@ func TestDatabase_Create_WithValidReplicationConfiguration(t *testing.T) {
r := require.New(t)

in := map[string]interface{}{
"name": "good_name",
"name": "tst-terraform-good_name",
"comment": "great comment",
"replication_configuration": []interface{}{map[string]interface{}{
"accounts": []interface{}{"account1", "account2"},
Expand All @@ -52,8 +52,8 @@ func TestDatabase_Create_WithValidReplicationConfiguration(t *testing.T) {
r.NotNil(d)

WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectExec(`CREATE DATABASE "good_name" COMMENT = 'great comment'`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`ALTER DATABASE "good_name" ENABLE REPLICATION TO ACCOUNTS account1, account2`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`CREATE DATABASE "tst-terraform-good_name" COMMENT = 'great comment'`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`ALTER DATABASE "tst-terraform-good_name" ENABLE REPLICATION TO ACCOUNTS account1, account2`).WillReturnResult(sqlmock.NewResult(1, 1))
expectRead(mock)

err := resources.CreateDatabase(d, db)
Expand All @@ -65,7 +65,7 @@ func TestDatabase_Create_WithReplicationConfig_AndFalseIgnoreEditionCheck(t *tes
r := require.New(t)

in := map[string]interface{}{
"name": "good_name",
"name": "tst-terraform-good_name",
"comment": "great comment",
"replication_configuration": []interface{}{map[string]interface{}{
"accounts": []interface{}{"acc_to_replicate"},
Expand All @@ -82,22 +82,22 @@ func TestDatabase_Create_WithReplicationConfig_AndFalseIgnoreEditionCheck(t *tes
}

func expectRead(mock sqlmock.Sqlmock) {
dbRows := sqlmock.NewRows([]string{"created_on", "name", "is_default", "is_current", "origin", "owner", "comment", "options", "retention_time"}).AddRow("created_on", "good_name", "is_default", "is_current", "origin", "owner", "mock comment", "options", "1")
mock.ExpectQuery("SHOW DATABASES LIKE 'good_name'").WillReturnRows(dbRows)
dbRows := sqlmock.NewRows([]string{"created_on", "name", "is_default", "is_current", "origin", "owner", "comment", "options", "retention_time"}).AddRow("created_on", "tst-terraform-good_name", "is_default", "is_current", "origin", "owner", "mock comment", "options", "1")
mock.ExpectQuery("SHOW DATABASES LIKE 'tst-terraform-good_name'").WillReturnRows(dbRows)
}

func TestDatabaseRead(t *testing.T) {
r := require.New(t)

d := database(t, "good_name", map[string]interface{}{
"name": "good_name",
d := database(t, "tst-terraform-good_name", map[string]interface{}{
"name": "tst-terraform-good_name",
})

WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
expectRead(mock)
err := resources.ReadDatabase(d, db)
r.NoError(err)
r.Equal("good_name", d.Get("name").(string))
r.Equal("tst-terraform-good_name", d.Get("name").(string))
r.Equal("mock comment", d.Get("comment").(string))
r.Equal(1, d.Get("data_retention_time_in_days").(int))
})
Expand All @@ -106,10 +106,10 @@ func TestDatabaseRead(t *testing.T) {
func TestDatabaseDelete(t *testing.T) {
r := require.New(t)

d := database(t, "drop_it", map[string]interface{}{"name": "drop_it"})
d := database(t, "tst-terraform-good_name-drop_it", map[string]interface{}{"name": "tst-terraform-good_name-drop_it"})

WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectExec(`DROP DATABASE "drop_it"`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`DROP DATABASE "tst-terraform-good_name-drop_it"`).WillReturnResult(sqlmock.NewResult(1, 1))
err := resources.DeleteDatabase(d, db)
r.NoError(err)
})
Expand All @@ -119,7 +119,7 @@ func TestDatabaseCreateFromShare(t *testing.T) {
r := require.New(t)

in := map[string]interface{}{
"name": "good_name",
"name": "tst-terraform-good_name",
"from_share": map[string]interface{}{
"provider": "abc123",
"share": "my_share",
Expand All @@ -129,7 +129,7 @@ func TestDatabaseCreateFromShare(t *testing.T) {
r.NotNil(d)

WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectExec(`CREATE DATABASE "good_name" FROM SHARE "abc123"."my_share"`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`CREATE DATABASE "tst-terraform-good_name" FROM SHARE "abc123"."my_share"`).WillReturnResult(sqlmock.NewResult(1, 1))
expectRead(mock)
err := resources.CreateDatabase(d, db)
r.NoError(err)
Expand All @@ -140,14 +140,14 @@ func TestDatabaseCreateFromDatabase(t *testing.T) {
r := require.New(t)

in := map[string]interface{}{
"name": "good_name",
"name": "tst-terraform-good_name",
"from_database": "abc123",
}
d := schema.TestResourceDataRaw(t, resources.Database().Schema, in)
r.NotNil(d)

WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectExec(`CREATE DATABASE "good_name" CLONE "abc123"`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`CREATE DATABASE "tst-terraform-good_name" CLONE "abc123"`).WillReturnResult(sqlmock.NewResult(1, 1))
expectRead(mock)
err := resources.CreateDatabase(d, db)
r.NoError(err)
Expand All @@ -158,14 +158,14 @@ func TestDatabaseCreateFromReplica(t *testing.T) {
r := require.New(t)

in := map[string]interface{}{
"name": "good_name",
"name": "tst-terraform-good_name",
"from_replica": "abc123",
}
d := schema.TestResourceDataRaw(t, resources.Database().Schema, in)
r.NotNil(d)

WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectExec(`CREATE DATABASE "good_name" AS REPLICA OF "abc123"`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`CREATE DATABASE "tst-terraform-good_name" AS REPLICA OF "abc123"`).WillReturnResult(sqlmock.NewResult(1, 1))
expectRead(mock)
err := resources.CreateDatabase(d, db)
r.NoError(err)
Expand All @@ -176,15 +176,15 @@ func TestDatabaseCreateTransient(t *testing.T) {
r := require.New(t)

in := map[string]interface{}{
"name": "good_name",
"name": "tst-terraform-good_name",
"comment": "great comment",
"is_transient": true,
}
d := schema.TestResourceDataRaw(t, resources.Database().Schema, in)
r.NotNil(d)

WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectExec(`CREATE TRANSIENT DATABASE "good_name" COMMENT = 'great comment'`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`CREATE TRANSIENT DATABASE "tst-terraform-good_name" COMMENT = 'great comment'`).WillReturnResult(sqlmock.NewResult(1, 1))
expectRead(mock)
err := resources.CreateDatabase(d, db)
r.NoError(err)
Expand All @@ -195,15 +195,15 @@ func TestDatabaseCreateTransientFromDatabase(t *testing.T) {
r := require.New(t)

in := map[string]interface{}{
"name": "good_name",
"name": "tst-terraform-good_name",
"from_database": "abc123",
"is_transient": true,
}
d := schema.TestResourceDataRaw(t, resources.Database().Schema, in)
r.NotNil(d)

WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectExec(`CREATE TRANSIENT DATABASE "good_name" CLONE "abc123"`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`CREATE TRANSIENT DATABASE "tst-terraform-good_name" CLONE "abc123"`).WillReturnResult(sqlmock.NewResult(1, 1))
expectRead(mock)
err := resources.CreateDatabase(d, db)
r.NoError(err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/resources/role_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
)

func TestAcc_Role(t *testing.T) {
prefix := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
prefix2 := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
prefix := "tst-terraform" + strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
prefix2 := "tst-terraform" + strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))

resource.ParallelTest(t, resource.TestCase{
Providers: providers(),
Expand Down
10 changes: 5 additions & 5 deletions pkg/resources/role_grants_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ func testCheckRolesAndUsers(t *testing.T, path string, roles, users []string) fu
}

func TestAcc_GrantRole(t *testing.T) {
role1 := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
role2 := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
role3 := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
user1 := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
user2 := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
role1 := "tst-terraform" + strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
role2 := "tst-terraform" + strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
role3 := "tst-terraform" + strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
user1 := "tst-terraform" + strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
user2 := "tst-terraform" + strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))

basicChecks := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_role.r", "name", role1),
Expand Down
4 changes: 2 additions & 2 deletions pkg/resources/role_ownership_grant_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

func TestAcc_RoleOwnershipGrant_defaults(t *testing.T) {
onRoleName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
toRoleName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
onRoleName := "tst-terraform" + acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
toRoleName := "tst-terraform" +acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)

resource.ParallelTest(t, resource.TestCase{
Providers: providers(),
Expand Down
Loading

0 comments on commit 6c004a3

Please sign in to comment.