Skip to content

Commit

Permalink
Fix race condition on IMPORTED PRIVILEGES database grant
Browse files Browse the repository at this point in the history
  • Loading branch information
sjauld committed Jul 18, 2019
1 parent 738c616 commit c8f5496
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
6 changes: 6 additions & 0 deletions pkg/resources/database_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ func ReadDatabaseGrant(data *schema.ResourceData, meta interface{}) error {
return err
}

// IMPORTED PRIVILEGES is not a real resource, so we can't actually verify
// that it is still there. Just exit for now
if priv == "IMPORTED PRIVILEGES" {
return nil
}

builder := snowflake.DatabaseGrant(dbName)

return readGenericGrant(data, meta, builder)
Expand Down
19 changes: 19 additions & 0 deletions pkg/resources/database_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ func TestDatabaseGrantCreate(t *testing.T) {
})
}

func TestDatabaseGrantRead(t *testing.T) {
a := assert.New(t)

d := databaseGrant(t, "test-database|||IMPORTED PRIVILIGES", map[string]interface{}{
"database_name": "test-database",
"privilege": "IMPORTED PRIVILIGES",
"roles": []string{"test-role-1", "test-role-2"},
"shares": []string{"test-share-1", "test-share-2"},
})

a.NotNil(d)

WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
expectReadDatabaseGrant(mock)
err := resources.ReadDatabaseGrant(d, db)
a.NoError(err)
})
}

func expectReadDatabaseGrant(mock sqlmock.Sqlmock) {
rows := sqlmock.NewRows([]string{
"created_on", "privilege", "granted_on", "name", "granted_to", "grantee_name", "grant_option", "granted_by",
Expand Down
44 changes: 26 additions & 18 deletions pkg/resources/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ import (
"github.com/stretchr/testify/assert"
)

func warehouse(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData {
a := assert.New(t)
d := schema.TestResourceDataRaw(t, resources.Warehouse().Schema, params)
a.NotNil(d)
d.SetId(id)
return d
}

func database(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData {
a := assert.New(t)
d := schema.TestResourceDataRaw(t, resources.Database().Schema, params)
Expand All @@ -28,14 +20,26 @@ func database(t *testing.T, id string, params map[string]interface{}) *schema.Re
return d
}

func user(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData {
func databaseGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData {
a := assert.New(t)
d := schema.TestResourceDataRaw(t, resources.User().Schema, params)
d := schema.TestResourceDataRaw(t, resources.DatabaseGrant().Schema, params)
a.NotNil(d)
d.SetId(id)
return d
}

func fixture(name string) (string, error) {
b, err := ioutil.ReadFile(filepath.Join("testdata", name))
return string(b), err
}

func providers() map[string]terraform.ResourceProvider {
p := provider.Provider()
return map[string]terraform.ResourceProvider{
"snowflake": p,
}
}

func role(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData {
a := assert.New(t)
d := schema.TestResourceDataRaw(t, resources.Role().Schema, params)
Expand All @@ -52,14 +56,18 @@ func roleGrants(t *testing.T, id string, params map[string]interface{}) *schema.
return d
}

func providers() map[string]terraform.ResourceProvider {
p := provider.Provider()
return map[string]terraform.ResourceProvider{
"snowflake": p,
}
func user(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData {
a := assert.New(t)
d := schema.TestResourceDataRaw(t, resources.User().Schema, params)
a.NotNil(d)
d.SetId(id)
return d
}

func fixture(name string) (string, error) {
b, err := ioutil.ReadFile(filepath.Join("testdata", name))
return string(b), err
func warehouse(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData {
a := assert.New(t)
d := schema.TestResourceDataRaw(t, resources.Warehouse().Schema, params)
a.NotNil(d)
d.SetId(id)
return d
}

0 comments on commit c8f5496

Please sign in to comment.