Skip to content

Commit

Permalink
added unit tests, adapted component tests
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 Nov 11, 2022
1 parent 09e9a6f commit ca92b79
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 17 deletions.
26 changes: 17 additions & 9 deletions operator/controllers/keptnworkloadinstance/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,24 @@ func (r *KeptnWorkloadInstanceReconciler) getAppVersionForWorkloadInstance(ctx c
if err := r.Client.List(ctx, apps, client.InNamespace(wli.Namespace)); err != nil {
return false, klcv1alpha1.KeptnAppVersion{}, err
}
latestVersion := klcv1alpha1.KeptnAppVersion{}
oldVersion, err := version.NewVersion("0.0.0")

workloadFound, latestVersion, err := getLatestAppVersion(apps, wli)
if err != nil {
r.Log.Error(err, "could not look up KeptnAppVersion for WorkloadInstance")
return false, latestVersion, err
}

if latestVersion.Spec.Version == "" || !workloadFound {
return false, klcv1alpha1.KeptnAppVersion{}, nil
}
return true, latestVersion, nil
}

func getLatestAppVersion(apps *klcv1alpha1.KeptnAppVersionList, wli *klcv1alpha1.KeptnWorkloadInstance) (bool, klcv1alpha1.KeptnAppVersion, error) {
latestVersion := klcv1alpha1.KeptnAppVersion{}
// ignore the potential error since this can not return an error with 0.0.0
oldVersion, _ := version.NewVersion("0.0.0")

workloadFound := false
for _, app := range apps.Items {
if app.Spec.AppName == wli.Spec.AppName {
Expand All @@ -291,8 +304,7 @@ func (r *KeptnWorkloadInstanceReconciler) getAppVersionForWorkloadInstance(ctx c
workloadFound = true
newVersion, err := version.NewVersion(app.Spec.Version)
if err != nil {
r.Log.Error(err, "could not parse version")
return false, latestVersion, err
return false, klcv1alpha1.KeptnAppVersion{}, err
}
if newVersion.GreaterThan(oldVersion) {
latestVersion = app
Expand All @@ -302,9 +314,5 @@ func (r *KeptnWorkloadInstanceReconciler) getAppVersionForWorkloadInstance(ctx c
}
}
}

if latestVersion.Spec.Version == "" || !workloadFound {
return false, klcv1alpha1.KeptnAppVersion{}, nil
}
return true, latestVersion, nil
return workloadFound, latestVersion, nil
}
221 changes: 216 additions & 5 deletions operator/controllers/keptnworkloadinstance/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package keptnworkloadinstance

import (
"context"
"testing"

"github.com/keptn/lifecycle-toolkit/operator/api/v1alpha1"
klcv1alpha1 "github.com/keptn/lifecycle-toolkit/operator/api/v1alpha1"
"github.com/stretchr/testify/require"
testrequire "github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"testing"
)

func TestKeptnWorkloadInstanceReconciler_IsPodRunning(t *testing.T) {
Expand All @@ -20,7 +20,7 @@ func TestKeptnWorkloadInstanceReconciler_IsPodRunning(t *testing.T) {
r := &KeptnWorkloadInstanceReconciler{
Client: fake.NewClientBuilder().WithLists(podList).Build(),
}
isPodRunning, err := r.isPodRunning(context.TODO(), v1alpha1.ResourceReference{UID: types.UID("pod1")}, "node1")
isPodRunning, err := r.isPodRunning(context.TODO(), klcv1alpha1.ResourceReference{UID: types.UID("pod1")}, "node1")
testrequire.Nil(t, err)
if !isPodRunning {
t.Errorf("Wrong!")
Expand All @@ -29,7 +29,7 @@ func TestKeptnWorkloadInstanceReconciler_IsPodRunning(t *testing.T) {
r2 := &KeptnWorkloadInstanceReconciler{
Client: fake.NewClientBuilder().WithLists(podList2).Build(),
}
isPodRunning, err = r2.isPodRunning(context.TODO(), v1alpha1.ResourceReference{UID: types.UID("pod1")}, "node1")
isPodRunning, err = r2.isPodRunning(context.TODO(), klcv1alpha1.ResourceReference{UID: types.UID("pod1")}, "node1")
testrequire.Nil(t, err)
if isPodRunning {
t.Errorf("Wrong!")
Expand All @@ -50,3 +50,214 @@ func makeNominatedPod(podName string, nodeName string, phase v1.PodPhase) v1.Pod
},
}
}

func Test_getLatestAppVersion(t *testing.T) {
type args struct {
apps *klcv1alpha1.KeptnAppVersionList
wli *klcv1alpha1.KeptnWorkloadInstance
}
tests := []struct {
name string
args args
wantFound bool
wantAppVersion klcv1alpha1.KeptnAppVersion
wantErr bool
}{
{
name: "app version found",
args: args{
apps: &klcv1alpha1.KeptnAppVersionList{
Items: []klcv1alpha1.KeptnAppVersion{
{
ObjectMeta: metav1.ObjectMeta{
Name: "my-app",
Namespace: "default",
},
Spec: klcv1alpha1.KeptnAppVersionSpec{
KeptnAppSpec: klcv1alpha1.KeptnAppSpec{
Version: "1.0",
Workloads: []klcv1alpha1.KeptnWorkloadRef{
{
Name: "my-workload",
Version: "1.0",
},
},
},
AppName: "my-app",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "my-app",
Namespace: "default",
},
Spec: klcv1alpha1.KeptnAppVersionSpec{
KeptnAppSpec: klcv1alpha1.KeptnAppSpec{
Version: "2.0",
Workloads: []klcv1alpha1.KeptnWorkloadRef{
{
Name: "my-workload",
Version: "1.0",
},
},
},
AppName: "my-app",
},
},
},
},
wli: &klcv1alpha1.KeptnWorkloadInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "my-workloadinstance",
Namespace: "default",
},
Spec: klcv1alpha1.KeptnWorkloadInstanceSpec{
KeptnWorkloadSpec: klcv1alpha1.KeptnWorkloadSpec{
AppName: "my-app",
Version: "1.0",
},
WorkloadName: "my-app-my-workload",
},
},
},
wantFound: true,
wantAppVersion: klcv1alpha1.KeptnAppVersion{
ObjectMeta: metav1.ObjectMeta{
Name: "my-app",
Namespace: "default",
},
Spec: klcv1alpha1.KeptnAppVersionSpec{
KeptnAppSpec: klcv1alpha1.KeptnAppSpec{
Version: "2.0",
Workloads: []klcv1alpha1.KeptnWorkloadRef{
{
Name: "my-workload",
Version: "1.0",
},
},
},
AppName: "my-app",
},
},
wantErr: false,
},
{
name: "app version not found",
args: args{
apps: &klcv1alpha1.KeptnAppVersionList{
Items: []klcv1alpha1.KeptnAppVersion{
{
ObjectMeta: metav1.ObjectMeta{
Name: "my-app",
Namespace: "default",
},
Spec: klcv1alpha1.KeptnAppVersionSpec{
KeptnAppSpec: klcv1alpha1.KeptnAppSpec{
Version: "1.0",
Workloads: []klcv1alpha1.KeptnWorkloadRef{
{
Name: "my-other-workload",
Version: "1.0",
},
},
},
AppName: "my-app",
},
},
},
},
wli: &klcv1alpha1.KeptnWorkloadInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "my-workloadinstance",
Namespace: "default",
},
Spec: klcv1alpha1.KeptnWorkloadInstanceSpec{
KeptnWorkloadSpec: klcv1alpha1.KeptnWorkloadSpec{
AppName: "my-app",
Version: "1.0",
},
WorkloadName: "my-app-my-workload",
},
},
},
wantFound: false,
wantAppVersion: klcv1alpha1.KeptnAppVersion{},
wantErr: false,
},
{
name: "app version with invalid version",
args: args{
apps: &klcv1alpha1.KeptnAppVersionList{
Items: []klcv1alpha1.KeptnAppVersion{
{
ObjectMeta: metav1.ObjectMeta{
Name: "my-app",
Namespace: "default",
},
Spec: klcv1alpha1.KeptnAppVersionSpec{
KeptnAppSpec: klcv1alpha1.KeptnAppSpec{
Version: "",
Workloads: []klcv1alpha1.KeptnWorkloadRef{
{
Name: "my-workload",
Version: "1.0",
},
},
},
AppName: "my-app",
},
},
},
},
wli: &klcv1alpha1.KeptnWorkloadInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "my-workloadinstance",
Namespace: "default",
},
Spec: klcv1alpha1.KeptnWorkloadInstanceSpec{
KeptnWorkloadSpec: klcv1alpha1.KeptnWorkloadSpec{
AppName: "my-app",
Version: "1.0",
},
WorkloadName: "my-app-my-workload",
},
},
},
wantFound: false,
wantAppVersion: klcv1alpha1.KeptnAppVersion{},
wantErr: true,
},
{
name: "app version list empty",
args: args{
apps: &klcv1alpha1.KeptnAppVersionList{
Items: []klcv1alpha1.KeptnAppVersion{},
},
wli: &klcv1alpha1.KeptnWorkloadInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "my-workloadinstance",
Namespace: "default",
},
Spec: klcv1alpha1.KeptnWorkloadInstanceSpec{
KeptnWorkloadSpec: klcv1alpha1.KeptnWorkloadSpec{
AppName: "my-app",
Version: "1.0",
},
WorkloadName: "my-app-my-workload",
},
},
},
wantFound: false,
wantAppVersion: klcv1alpha1.KeptnAppVersion{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
found, gotAppVersion, err := getLatestAppVersion(tt.args.apps, tt.args.wli)
require.Equal(t, tt.wantErr, err != nil)
require.Equal(t, tt.wantFound, found)
require.Equal(t, tt.wantAppVersion, gotAppVersion)
})
}
}
6 changes: 3 additions & 3 deletions operator/test/component/workloadinstancecontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ var _ = Describe("KeptnWorkloadInstanceController", Ordered, func() {
},
Spec: klcv1alpha1.KeptnWorkloadInstanceSpec{
KeptnWorkloadSpec: klcv1alpha1.KeptnWorkloadSpec{},
WorkloadName: "app-wname-" + wiName,
WorkloadName: "wi-test-app-wname-" + wiName,
TraceId: map[string]string{"traceparent": "00-0f89f15e562489e2e171eca1cf9ba958-d2fa6dbbcbf7e29a-01"},
},
}
By("Creating WorkloadInstance")
err := k8sClient.Create(context.TODO(), wi)
Expect(err).To(BeNil())

By("Ensuring WorkloadInstance ends up in a failed state")
By("Ensuring WorkloadInstance does not progress to next phase")
wiNameObj := types.NamespacedName{
Namespace: wi.Namespace,
Name: wi.Name,
Expand Down Expand Up @@ -134,7 +134,7 @@ func createAppVersionInCluster(name string, namespace string, version string) *k
Version: version,
Workloads: []klcv1alpha1.KeptnWorkloadRef{
{
Name: "app-wname",
Name: "wi-test-app-wname",
Version: "2.0",
},
},
Expand Down

0 comments on commit ca92b79

Please sign in to comment.