Skip to content

Commit

Permalink
Merge pull request #222 from laurafitzgerald/update-update
Browse files Browse the repository at this point in the history
update client postgres reconcile function
  • Loading branch information
openshift-merge-robot authored Jun 18, 2021
2 parents fb5f153 + c501c61 commit e5e2916
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 9 deletions.
7 changes: 4 additions & 3 deletions pkg/client/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ func ReconcilePostgres(ctx context.Context, client client.Client, productName, d
},
},
}
if applyImmediately {
pg.Spec.ApplyImmediately = applyImmediately
}

// execute logic to modify the resource before creation
// e.g. add owner refs
Expand All @@ -85,6 +82,10 @@ func ReconcilePostgres(ctx context.Context, client client.Client, productName, d
Name: secretName,
Namespace: secretNs,
}

if applyImmediately {
pg.Spec.ApplyImmediately = applyImmediately
}
return nil
})
if err != nil {
Expand Down
124 changes: 118 additions & 6 deletions pkg/client/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/integr8ly/cloud-resource-operator/apis/integreatly/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -166,6 +167,13 @@ func TestReconcilePostgres(t *testing.T) {
t.Fatal("failed to build scheme", err)
}

upgradePostgres := &v1alpha1.Postgres{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
}

type args struct {
ctx context.Context
client client.Client
Expand All @@ -186,7 +194,7 @@ func TestReconcilePostgres(t *testing.T) {
wantErr bool
}{
{
name: "test successful creation",
name: "test successful creation on create",
args: args{
ctx: context.TODO(),
client: fake.NewFakeClientWithScheme(scheme),
Expand Down Expand Up @@ -221,7 +229,7 @@ func TestReconcilePostgres(t *testing.T) {
wantErr: false,
},
{
name: "test modification function",
name: "test modification function on create",
args: args{
ctx: context.TODO(),
client: fake.NewFakeClientWithScheme(scheme),
Expand All @@ -235,7 +243,7 @@ func TestReconcilePostgres(t *testing.T) {
applyImmediately: true,
modifyFunc: func(cr v1.Object) error {
cr.SetLabels(map[string]string{
"cro": "test",
"productName": "test",
})
return nil
},
Expand All @@ -246,7 +254,7 @@ func TestReconcilePostgres(t *testing.T) {
Namespace: "test",
ResourceVersion: "1",
Labels: map[string]string{
"cro": "test",
"productName": "test",
},
},
Spec: croType.ResourceTypeSpec{
Expand All @@ -262,7 +270,7 @@ func TestReconcilePostgres(t *testing.T) {
wantErr: false,
},
{
name: "test modification function error",
name: "test modification function error on create",
args: args{
ctx: context.TODO(),
client: fake.NewFakeClientWithScheme(scheme),
Expand All @@ -281,6 +289,110 @@ func TestReconcilePostgres(t *testing.T) {
want: nil,
wantErr: true,
},
{
name: "test successful creation on upgrade",
args: args{
ctx: context.TODO(),
client: fake.NewFakeClientWithScheme(scheme, upgradePostgres),
deploymentType: "managed",
tier: "production",
productName: "test",
name: "test",
ns: "test",
secretName: "test",
secretNs: "test",
applyImmediately: false,
modifyFunc: nil,
},
want: &v1alpha1.Postgres{
TypeMeta: v1.TypeMeta{
Kind: "Postgres",
APIVersion: "integreatly.org/v1alpha1",
},
ObjectMeta: v1.ObjectMeta{
Name: "test",
Namespace: "test",
ResourceVersion: "1",
Labels: map[string]string{
"productName": "test",
},
},
Spec: croType.ResourceTypeSpec{
Type: "managed",
Tier: "production",
SecretRef: &croType.SecretRef{
Name: "test",
Namespace: "test",
},
},
},
wantErr: false,
},
{
name: "test modification function on upgrade",
args: args{
ctx: context.TODO(),
client: fake.NewFakeClientWithScheme(scheme, upgradePostgres),
deploymentType: "managed",
productName: "test",
tier: "production",
name: "test",
ns: "test",
secretName: "test",
secretNs: "test",
applyImmediately: true,
modifyFunc: func(cr v1.Object) error {
cr.SetLabels(map[string]string{
"productName": "test",
})
return nil
},
},
want: &v1alpha1.Postgres{
TypeMeta: v1.TypeMeta{
Kind: "Postgres",
APIVersion: "integreatly.org/v1alpha1",
},
ObjectMeta: v1.ObjectMeta{
Name: "test",
Namespace: "test",
ResourceVersion: "1",
Labels: map[string]string{
"productName": "test",
},
},
Spec: croType.ResourceTypeSpec{
Type: "managed",
Tier: "production",
SecretRef: &croType.SecretRef{
Name: "test",
Namespace: "test",
},
ApplyImmediately: true,
},
},
wantErr: false,
},
{
name: "test modification function error on upgrade",
args: args{
ctx: context.TODO(),
client: fake.NewFakeClientWithScheme(scheme, upgradePostgres),
deploymentType: "workshop",
tier: "development",
productName: "test",
name: "test",
ns: "test",
secretName: "test",
secretNs: "test",
applyImmediately: false,
modifyFunc: func(cr v1.Object) error {
return errors.New("error executing function")
},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -290,7 +402,7 @@ func TestReconcilePostgres(t *testing.T) {
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ReconcilePostgres() got = %v, want %v", got, tt.want)
t.Errorf("ReconcilePostgres() \n got = %v, \n want= %v", got, tt.want)
}
})
}
Expand Down

0 comments on commit e5e2916

Please sign in to comment.