Skip to content

Commit

Permalink
fixed an issue when searching for default privileges
Browse files Browse the repository at this point in the history
Signed-off-by: Joaquín Fernández Campo <[email protected]>
  • Loading branch information
xocasdashdash committed Oct 27, 2024
1 parent 65128d0 commit b26300f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 19 deletions.
43 changes: 28 additions & 15 deletions pkg/controller/postgresql/default_privileges/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,39 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
var query xsql.Query
selectDefaultPrivilegesQuery(gp, &query)

var grants []string
err := c.db.Scan(ctx, query, &grants)
if err != nil && !xsql.IsNoRows(err) {
var defaultPrivileges []string

rows, err := c.db.Query(ctx, query)
if xsql.IsNoRows(err) {
return managed.ExternalObservation{ResourceExists: false}, nil
}

if err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, errSelectDefaultPrivileges)
}
defer rows.Close()
for rows.Next() {
var privilege string
if err := rows.Scan(&privilege); err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, errSelectDefaultPrivileges)
}
defaultPrivileges = append(defaultPrivileges, privilege)
}

// Check for any errors encountered during iteration
if err := rows.Err(); err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, errSelectDefaultPrivileges)
}
if len(grants) == 0 {

// If no default privileges are found, the resource does not exist.
// Maybe this is covered by the xsql.IsNoRows(err) check above?
if len(defaultPrivileges) == 0 {
return managed.ExternalObservation{ResourceExists: false}, nil
}

// Grants have no way of being 'not up to date' - if they exist, they are up to date
cr.SetConditions(xpv1.Available())

resourceMatches := matchingGrants(grants, gp.Privileges.ToStringSlice())
resourceMatches := matchingGrants(defaultPrivileges, gp.Privileges.ToStringSlice())
return managed.ExternalObservation{
ResourceLateInitialized: false,
// check that the list of grants matches the expected grants
Expand Down Expand Up @@ -286,15 +306,8 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
err := c.db.ExecTx(ctx, []xsql.Query{
deleteQuery, createQuery,
})
errString := errCreateDefaultPrivileges
if err != nil {
errString = fmt.Sprintf(`
%s
delete: |%s|
create: |%s|
`, errString, deleteQuery.String, createQuery.String)
}
return managed.ExternalCreation{}, errors.Wrap(err, errString)

return managed.ExternalCreation{}, errors.Wrap(err, errCreateDefaultPrivileges)
}

func (c *external) Update(
Expand Down
40 changes: 36 additions & 4 deletions pkg/controller/postgresql/default_privileges/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"database/sql"
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/crossplane-contrib/provider-sql/apis/postgresql/v1alpha1"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand Down Expand Up @@ -221,6 +222,9 @@ func TestObserve(t *testing.T) {
reason: "We should return ResourceExists: false when no default grant is found",
fields: fields{
db: mockDB{
MockQuery: func(ctx context.Context, q xsql.Query) (*sql.Rows, error) {
return mockRowsToSQLRows(sqlmock.NewRows([]string{})), nil
},
MockScan: func(ctx context.Context, q xsql.Query, dest ...interface{}) error {
// Default value is empty, so we don't need to do anything here
return nil
Expand Down Expand Up @@ -248,6 +252,12 @@ func TestObserve(t *testing.T) {
reason: "We should return any errors encountered while trying to show the default grant",
fields: fields{
db: mockDB{
MockQuery: func(ctx context.Context, q xsql.Query) (*sql.Rows, error) {
r := sqlmock.NewRows([]string{"PRIVILEGE"}).
AddRow("UPDATE").
AddRow("SELECT")
return mockRowsToSQLRows(r), errBoom
},
MockScan: func(ctx context.Context, q xsql.Query, dest ...interface{}) error {
return errBoom
},
Expand Down Expand Up @@ -275,11 +285,22 @@ func TestObserve(t *testing.T) {
reason: "We should return no error if we can find the right permissions in the default grant",
fields: fields{
db: mockDB{
MockScan: func(ctx context.Context, q xsql.Query, dest ...interface{}) error {
bv := dest[0].(*[]string)
*bv = []string{"SELECT", "UPDATE"}
return nil
MockQuery: func(ctx context.Context, q xsql.Query) (*sql.Rows, error) {
r := sqlmock.NewRows([]string{"PRIVILEGE"}).
AddRow("UPDATE").
AddRow("SELECT")
return mockRowsToSQLRows(r), nil
},
// MockScan: func(ctx context.Context, q xsql.Query, dest ...interface{}) error {
// if len(dest) == 0 {
// runtime.Breakpoint()
// return nil
// }
// // populate the dest slice with the expected values
// // so we can compare them in the test
// *dest[0].(*string) = "SELECT"
// return nil
// },
},
},
args: args{
Expand Down Expand Up @@ -320,6 +341,17 @@ func TestObserve(t *testing.T) {
}
}

func mockRowsToSQLRows(mockRows *sqlmock.Rows) *sql.Rows {
db, mock, _ := sqlmock.New()
mock.ExpectQuery("select").WillReturnRows(mockRows)
rows, err := db.Query("select")
if err != nil {
println("%v", err)
return nil
}
return rows
}

func TestCreate(t *testing.T) {
errBoom := errors.New("boom")

Expand Down

0 comments on commit b26300f

Please sign in to comment.