-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for namespace and deployment controllers
Signed-off-by: Ruben Vargas <[email protected]>
- Loading branch information
1 parent
9966d94
commit 924a284
Showing
2 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package namespace | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1" | ||
"github.com/jaegertracing/jaeger-operator/pkg/inject" | ||
) | ||
|
||
func TestReconcilieDeployment(t *testing.T) { | ||
depNamespacedName := types.NamespacedName{ | ||
Name: "jaeger-query", | ||
Namespace: "my-ns", | ||
} | ||
|
||
jaeger := v1.NewJaeger(types.NamespacedName{ | ||
Namespace: "observability", | ||
Name: "my-instance", | ||
}) | ||
|
||
s := scheme.Scheme | ||
s.AddKnownTypes(v1.SchemeGroupVersion, jaeger) | ||
s.AddKnownTypes(v1.SchemeGroupVersion, &v1.JaegerList{}) | ||
|
||
testCases := []struct { | ||
desc string | ||
dep *appsv1.Deployment | ||
expectedContiners int | ||
}{ | ||
{ | ||
desc: "Should not remove the instance from a jaeger component", | ||
dep: inject.Sidecar(jaeger, &appsv1.Deployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: depNamespacedName.Name, | ||
Namespace: depNamespacedName.Namespace, | ||
Annotations: map[string]string{}, | ||
Labels: map[string]string{ | ||
"app": "jaeger", | ||
}, | ||
}, | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{{ | ||
Name: "only_container", | ||
}}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
expectedContiners: 2, | ||
}, | ||
{ | ||
desc: "Should remove the instance", | ||
dep: inject.Sidecar(jaeger, &appsv1.Deployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: depNamespacedName.Name, | ||
Namespace: depNamespacedName.Namespace, | ||
Annotations: map[string]string{}, | ||
}, | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{{ | ||
Name: "only_container", | ||
}}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
expectedContiners: 1, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
|
||
assert.Equal(t, 2, len(tc.dep.Spec.Template.Spec.Containers)) | ||
ns := &corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: depNamespacedName.Namespace, | ||
}, | ||
} | ||
|
||
cl := fake.NewFakeClient(tc.dep, ns) | ||
r := &ReconcileNamespace{ | ||
client: cl, | ||
rClient: cl, | ||
scheme: s, | ||
} | ||
req := reconcile.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: depNamespacedName.Namespace, | ||
}, | ||
} | ||
|
||
_, err := r.Reconcile(req) | ||
persisted := &appsv1.Deployment{} | ||
cl.Get(context.Background(), depNamespacedName, persisted) | ||
|
||
assert.Equal(t, tc.expectedContiners, len(persisted.Spec.Template.Spec.Containers)) | ||
|
||
require.NoError(t, err) | ||
}) | ||
} | ||
} |