Skip to content

Commit

Permalink
another test case
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
  • Loading branch information
bacherfl committed Feb 5, 2024
1 parent a0e740d commit d4d14d9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func (r *KeptnWorkloadReconciler) Reconcile(ctx context.Context, req ctrl.Reques
// Try to find the workload instance
err = r.Get(ctx, types.NamespacedName{Namespace: workload.Namespace, Name: workloadVersionName}, workloadVersion)
if client.IgnoreNotFound(err) != nil {
r.Log.Error(err, "could not get WorkloadVersion")
r.Log.Error(err, "could not get WorkloadVersion", "requestInfo", requestInfo)
return ctrl.Result{RequeueAfter: 10 * time.Second}, err
} else if errors.IsNotFound(err) {
// If the workload instance does not exist, create it
workloadVersion, err := r.createWorkloadVersion(ctx, workload)
Expand All @@ -103,7 +104,7 @@ func (r *KeptnWorkloadReconciler) Reconcile(ctx context.Context, req ctrl.Reques

err = r.Client.Create(ctx, workloadVersion)
if err != nil {
r.Log.Error(err, "could not create WorkloadVersion")
r.Log.Error(err, "could not create WorkloadVersion", "requestInfo", requestInfo)
r.EventSender.Emit(common.PhaseCreateWorkloadVersion, "Warning", workloadVersion, common.PhaseStateFailed, "could not create KeptnWorkloadVersion ", workloadVersion.Spec.Version)
return ctrl.Result{RequeueAfter: 10 * time.Second}, err
}
Expand All @@ -116,7 +117,7 @@ func (r *KeptnWorkloadReconciler) Reconcile(ctx context.Context, req ctrl.Reques
r.Log.Info("updating spec of KeptnWorkloadVersion", "requestInfo", requestInfo, "workloadVersion", workloadVersion.Name)
workloadVersion.Spec.KeptnWorkloadSpec = workload.Spec
if err := r.Client.Update(ctx, workloadVersion); err != nil {
r.Log.Error(err, "could not update spec of Workload")
r.Log.Error(err, "could not update spec of Workload", "requestInfo", requestInfo)
// requeue to try again in case of an unexpected error
return ctrl.Result{RequeueAfter: 10 * time.Second}, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,47 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

func TestKeptnWorkloadReconciler_CannotLookupWorkloadVersion(t *testing.T) {

workload := &klcv1beta1.KeptnWorkload{
ObjectMeta: metav1.ObjectMeta{
Name: "my-workload",
Namespace: "my-namespace",
},
Spec: klcv1beta1.KeptnWorkloadSpec{
AppName: "my-app",
Version: "v1",
ResourceReference: klcv1beta1.ResourceReference{
UID: "id1",
Kind: "ReplicaSet",
Name: "my-replica-set",
},
},
}

r, _ := setupReconciler(workload)

fakeClient := k8sfake.NewClientBuilder().WithScheme(scheme.Scheme).WithInterceptorFuncs(interceptor.Funcs{
Get: func(ctx context.Context, client client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
if key.Name == "my-workload-v1" {
return errors.New("unexpected error")
}
return nil
},
}).WithObjects(workload).Build()

r.Client = fakeClient

res, err := r.Reconcile(context.TODO(), ctrl.Request{
NamespacedName: types.NamespacedName{
Namespace: workload.Namespace,
Name: workload.Name,
},
})
require.NotNil(t, err)
require.Equal(t, 10*time.Second, res.RequeueAfter)
}

func TestKeptnWorkloadReconciler_CreateWorkloadVersion(t *testing.T) {

workload := &klcv1beta1.KeptnWorkload{
Expand Down

0 comments on commit d4d14d9

Please sign in to comment.