Skip to content

Commit

Permalink
set up explanation comments for tests
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <ondrej.dubaj@dynatrace.com>
  • Loading branch information
odubajDT committed Mar 24, 2023
1 parent 2ea30e6 commit 155c5bb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
6 changes: 6 additions & 0 deletions controllers/core/featureflagconfiguration/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func TestFlagSourceConfigurationReconciler_Reconcile(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// set up k8s fake client
fakeClient := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(tt.ffConfig, tt.cm).Build()

r := &FeatureFlagConfigurationReconciler{
Expand All @@ -111,6 +112,7 @@ func TestFlagSourceConfigurationReconciler_Reconcile(t *testing.T) {
}

if tt.cmDeleted {
// if configMap should be deleted, we need to set ffConfig as the only OwnerRef before executing the Reconcile function
ffConfig := &v1alpha1.FeatureFlagConfiguration{}
err = fakeClient.Get(ctx, types.NamespacedName{Name: ffConfigName, Namespace: testNamespace}, ffConfig)
require.Nil(t, err)
Expand All @@ -124,26 +126,30 @@ func TestFlagSourceConfigurationReconciler_Reconcile(t *testing.T) {
require.Nil(t, err)
}

// reconcile
_, err = r.Reconcile(ctx, req)
require.Nil(t, err)

ffConfig := &v1alpha1.FeatureFlagConfiguration{}
err = fakeClient.Get(ctx, types.NamespacedName{Name: ffConfigName, Namespace: testNamespace}, ffConfig)
require.Nil(t, err)

// check that the provider name is set correctly
require.Equal(t, tt.wantProvider, ffConfig.Spec.ServiceProvider.Name)

cm := &corev1.ConfigMap{}
err = fakeClient.Get(ctx, types.NamespacedName{Name: cmName, Namespace: testNamespace}, cm)

if !tt.cmDeleted {
// if configMap should not be deleted, check the correct values
require.Nil(t, err)
require.Equal(t, tt.wantCM.Data, cm.Data)
require.Len(t, cm.OwnerReferences, len(tt.wantCM.OwnerReferences))
require.Equal(t, tt.wantCM.OwnerReferences[0].APIVersion, cm.OwnerReferences[0].APIVersion)
require.Equal(t, tt.wantCM.OwnerReferences[0].Name, cm.OwnerReferences[0].Name)
require.Equal(t, tt.wantCM.OwnerReferences[0].Kind, cm.OwnerReferences[0].Kind)
} else {
// if configMap should be deleted, we expect error
require.NotNil(t, err)
}
})
Expand Down
48 changes: 26 additions & 22 deletions controllers/core/flagsourceconfiguration/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,32 @@ func TestFlagSourceConfigurationReconciler_Reconcile(t *testing.T) {
)

tests := []struct {
name string
fsConfig *v1alpha1.FlagSourceConfiguration
deployment *appsv1.Deployment
restarted1 string
restarted2 string
name string
fsConfig *v1alpha1.FlagSourceConfiguration
deployment *appsv1.Deployment
restartedAtValueBeforeReconcile string
restartedAtValueAfterReconcile string
}{
{
name: "deployment gets restarted with rollout",
fsConfig: createTestFSConfig(fsConfigName, testNamespace, deploymentName, true),
deployment: createTestDeployment(fsConfigName, testNamespace, deploymentName),
restarted1: "",
restarted2: time.Now().Format(time.RFC3339),
name: "deployment gets restarted with rollout",
fsConfig: createTestFSConfig(fsConfigName, testNamespace, deploymentName, true),
deployment: createTestDeployment(fsConfigName, testNamespace, deploymentName),
restartedAtValueBeforeReconcile: "",
restartedAtValueAfterReconcile: time.Now().Format(time.RFC3339),
},
{
name: "deployment without rollout",
fsConfig: createTestFSConfig(fsConfigName, testNamespace, deploymentName, false),
deployment: createTestDeployment(fsConfigName, testNamespace, deploymentName),
restarted1: "",
restarted2: "",
name: "deployment without rollout",
fsConfig: createTestFSConfig(fsConfigName, testNamespace, deploymentName, false),
deployment: createTestDeployment(fsConfigName, testNamespace, deploymentName),
restartedAtValueBeforeReconcile: "",
restartedAtValueAfterReconcile: "",
},
{
name: "no deployment",
fsConfig: createTestFSConfig(fsConfigName, testNamespace, deploymentName, true),
deployment: nil,
restarted1: "",
restarted2: "",
name: "no deployment",
fsConfig: createTestFSConfig(fsConfigName, testNamespace, deploymentName, true),
deployment: nil,
restartedAtValueBeforeReconcile: "",
restartedAtValueAfterReconcile: "",
},
}

Expand All @@ -70,6 +70,7 @@ func TestFlagSourceConfigurationReconciler_Reconcile(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// setting up fake k8s client
var fakeClient client.Client
if tt.deployment != nil {
fakeClient = fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(tt.fsConfig, tt.deployment).WithIndex(&appsv1.Deployment{}, fmt.Sprintf("%s/%s", common.OpenFeatureAnnotationPath, common.FlagSourceConfigurationAnnotation), common.FlagSourceConfigurationIndex).Build()
Expand All @@ -84,22 +85,25 @@ func TestFlagSourceConfigurationReconciler_Reconcile(t *testing.T) {
}

if tt.deployment != nil {
// checking that the deployment does have 'restartedAt' set to the expected value before reconciliation
deployment := &appsv1.Deployment{}
err = fakeClient.Get(ctx, types.NamespacedName{Name: deploymentName, Namespace: testNamespace}, deployment)
require.Nil(t, err)
restartAt := deployment.Spec.Template.ObjectMeta.Annotations["kubectl.kubernetes.io/restartedAt"]
require.Equal(t, tt.restarted1, restartAt)
require.Equal(t, tt.restartedAtValueBeforeReconcile, restartAt)
}

// running reconcile function
_, err = r.Reconcile(ctx, req)
require.Nil(t, err)

if tt.deployment != nil {
// checking that the deployment does have 'restartedAt' set to the expected value after reconciliation
deployment := &appsv1.Deployment{}
err = fakeClient.Get(ctx, types.NamespacedName{Name: deploymentName, Namespace: testNamespace}, deployment)
require.Nil(t, err)

require.Equal(t, tt.restarted2, deployment.Spec.Template.ObjectMeta.Annotations["kubectl.kubernetes.io/restartedAt"])
require.Equal(t, tt.restartedAtValueAfterReconcile, deployment.Spec.Template.ObjectMeta.Annotations["kubectl.kubernetes.io/restartedAt"])
}
})

Expand Down

0 comments on commit 155c5bb

Please sign in to comment.