Skip to content

Commit

Permalink
Merge pull request #264 from zzxwill/ut
Browse files Browse the repository at this point in the history
Add Unit testcases
  • Loading branch information
zzxwill committed Mar 7, 2022
2 parents 97b3f99 + 0abf57b commit 7760da7
Show file tree
Hide file tree
Showing 2 changed files with 520 additions and 8 deletions.
286 changes: 283 additions & 3 deletions controllers/configuration_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ func TestPreCheck(t *testing.T) {
s := runtime.NewScheme()
v1beta1.AddToScheme(s)
corev1.AddToScheme(s)
corev1.AddToScheme(s)
provider := &v1beta1.Provider{
ObjectMeta: v1.ObjectMeta{
Name: "default",
Expand Down Expand Up @@ -438,6 +439,122 @@ func TestPreCheck(t *testing.T) {
},
want: want{},
},
{
name: "could not find provider",
args: args{
r: r,
configuration: &v1beta1.Configuration{
ObjectMeta: v1.ObjectMeta{
Name: "abc",
},
Spec: v1beta1.ConfigurationSpec{
HCL: "bbb",
},
},
meta: &TFConfigurationMeta{
ConfigurationCMName: "abc",
ProviderReference: &crossplane.Reference{
Namespace: "d",
Name: "default",
},
},
},
want: want{
errMsg: "provider not found",
},
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
if err := tc.args.r.preCheck(ctx, tc.args.configuration, tc.args.meta); (tc.want.errMsg != "") &&
!strings.Contains(err.Error(), tc.want.errMsg) {
t.Errorf("preCheck() error = %v, wantErr %v", err, tc.want.errMsg)
}
})
}
}

func TestPreCheckWhenConfigurationIsChanged(t *testing.T) {
r := &ConfigurationReconciler{}
ctx := context.Background()
s := runtime.NewScheme()
v1beta1.AddToScheme(s)
corev1.AddToScheme(s)
corev1.AddToScheme(s)
provider := &v1beta1.Provider{
ObjectMeta: v1.ObjectMeta{
Name: "default",
Namespace: "default",
},
Status: v1beta1.ProviderStatus{
State: types.ProviderIsNotReady,
},
}
r.Client = fake.NewClientBuilder().WithScheme(s).WithObjects(provider).Build()

provider3 := &v1beta1.Provider{
ObjectMeta: v1.ObjectMeta{
Name: "default",
Namespace: "default",
},
Status: v1beta1.ProviderStatus{
State: types.ProviderIsNotReady,
},
}
configurationCM3 := &corev1.ConfigMap{
ObjectMeta: v1.ObjectMeta{
Name: "abc",
Namespace: "default",
},
}
r.Client = fake.NewClientBuilder().WithScheme(s).WithObjects(provider3, configurationCM3).Build()
meta3 := &TFConfigurationMeta{
ConfigurationCMName: "abc",
ProviderReference: &crossplane.Reference{
Namespace: "default",
Name: "default",
},
CompleteConfiguration: "d",
Namespace: "default",
}

patches := gomonkey.ApplyFunc(reflect.DeepEqual, func(x, y interface{}) bool {
return true
})
defer patches.Reset()

type args struct {
r *ConfigurationReconciler
configuration *v1beta1.Configuration
meta *TFConfigurationMeta
}

type want struct {
errMsg string
}

testcases := []struct {
name string
args args
want want
}{
{
name: "configuration is changed",
args: args{
r: r,
configuration: &v1beta1.Configuration{
ObjectMeta: v1.ObjectMeta{
Name: "abc",
},
Spec: v1beta1.ConfigurationSpec{
HCL: "bbb",
},
},
meta: meta3,
},
want: want{},
},
}

for _, tc := range testcases {
Expand Down Expand Up @@ -481,11 +598,94 @@ func TestTerraformDestroy(t *testing.T) {
k8sClient2 := fake.NewClientBuilder().WithScheme(s).WithObjects(provider1, configuration).Build()
r2.Client = k8sClient2

//r3 := &ConfigurationReconciler{}
//provider1.Status.State = types.ProviderIsReady
//job3 := &batchv1.Job{
// ObjectMeta: metav1.ObjectMeta{
// Name: "a",
// Namespace: "default",
// },
// Status: batchv1.JobStatus{
// Succeeded: int32(1),
// },
//}
//configuration3 := &v1beta1.Configuration{
// ObjectMeta: metav1.ObjectMeta{
// Namespace: "default",
// Name: "b",
// },
//}
//configuration3.Spec.WriteConnectionSecretToReference = &crossplane.SecretReference{
// Name: "b",
// Namespace: "default",
//}
//k8sClient3 := fake.NewClientBuilder().WithScheme(s).WithObjects(provider1, job3, configuration3).Build()
//r3.Client = k8sClient3
//meta3 := &TFConfigurationMeta{
// DestroyJobName: "a",
// Namespace: "b",
// DeleteResource: true,
//}

r4 := &ConfigurationReconciler{}
provider1.Status.State = types.ProviderIsReady
job4 := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "a",
Namespace: "default",
},
Status: batchv1.JobStatus{
Succeeded: int32(1),
},
}
data, _ := json.Marshal(map[string]interface{}{
"name": "abc",
})
variables := &runtime.RawExtension{Raw: data}
configuration4 := &v1beta1.Configuration{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "b",
},
Spec: v1beta1.ConfigurationSpec{
Variable: variables,
},
}
configuration4.Spec.WriteConnectionSecretToReference = &crossplane.SecretReference{
Name: "b",
Namespace: "default",
}
secret4 := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "b",
Namespace: "default",
},
Type: corev1.SecretTypeOpaque,
}
variableSecret4 := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "c",
Namespace: "default",
},
Type: corev1.SecretTypeOpaque,
}
k8sClient4 := fake.NewClientBuilder().WithScheme(s).WithObjects(provider1, job4, secret4, variableSecret4, configuration4).Build()
r4.Client = k8sClient4
meta4 := &TFConfigurationMeta{
DestroyJobName: "a",
Namespace: "default",
DeleteResource: true,
ProviderReference: &crossplane.Reference{
Name: "b",
Namespace: "default",
},
VariableSecretName: "c",
}

type args struct {
r *ConfigurationReconciler
namespace string
configuration *v1beta1.Configuration
k8sClient client.Client
meta *TFConfigurationMeta
}
type want struct {
Expand All @@ -500,7 +700,6 @@ func TestTerraformDestroy(t *testing.T) {
name: "provider is not ready",
args: args{
r: r1,
k8sClient: k8sClient1,
configuration: &v1beta1.Configuration{},
meta: &TFConfigurationMeta{
ConfigurationCMName: "tf-abc",
Expand All @@ -515,7 +714,6 @@ func TestTerraformDestroy(t *testing.T) {
name: "provider is ready",
args: args{
r: r2,
k8sClient: k8sClient2,
configuration: configuration,
meta: &TFConfigurationMeta{
ConfigurationCMName: "tf-abc",
Expand All @@ -527,6 +725,15 @@ func TestTerraformDestroy(t *testing.T) {
errMsg: "The referenced provider could not be retrieved",
},
},
{
name: "could not directly remove resources, and destroy job completes",
args: args{
r: r4,
configuration: configuration4,
meta: meta4,
},
want: want{},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
Expand Down Expand Up @@ -556,3 +763,76 @@ func TestAssembleTerraformJob(t *testing.T) {
assert.Equal(t, containers[0].Image, "c")
assert.Equal(t, containers[1].Image, "d")
}

func TestGetTFOutputs(t *testing.T) {
type args struct {
ctx context.Context
k8sClient client.Client
configuration v1beta1.Configuration
meta *TFConfigurationMeta
}
type want struct {
property map[string]v1beta1.Property
errMsg string
}

ctx := context.Background()
k8sClient1 := fake.NewClientBuilder().Build()
meta1 := &TFConfigurationMeta{}

//scheme := runtime.NewScheme()
//v1beta1.AddToScheme(scheme)
secret2 := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "a",
Namespace: "default",
},
Type: corev1.SecretTypeOpaque,
}
k8sClient2 := fake.NewClientBuilder().WithObjects(secret2).Build()
meta2 := &TFConfigurationMeta{
BackendSecretName: "a",
TerraformBackendNamespace: "default",
}

testcases := map[string]struct {
args args
want want
}{
"could not find backend secret": {
args: args{
ctx: ctx,
k8sClient: k8sClient1,
meta: meta1,
},
want: want{
property: nil,
errMsg: "terraform state file backend secret is not generated",
},
},
"no data in a backend secret": {
args: args{
ctx: ctx,
k8sClient: k8sClient2,
meta: meta2,
},
want: want{
property: nil,
errMsg: "failed to get tfstate from Terraform State secret",
},
},
}

for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
property, err := tc.args.meta.getTFOutputs(tc.args.ctx, tc.args.k8sClient, tc.args.configuration)
if tc.want.errMsg != "" {
if !strings.Contains(err.Error(), tc.want.errMsg) {
t.Errorf("getTFOutputs() error = %v, wantErr %v", err, tc.want.errMsg)
}
}
assert.Equal(t, tc.want.property, property)
})
}

}
Loading

0 comments on commit 7760da7

Please sign in to comment.