Skip to content

Commit

Permalink
merge createPodsWithFilter with createPods
Browse files Browse the repository at this point in the history
  • Loading branch information
saza-ku committed Jan 6, 2025
1 parent bad9470 commit eb4b4a0
Showing 1 changed file with 35 additions and 57 deletions.
92 changes: 35 additions & 57 deletions simulator/resourceapplier/resourceapplier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ func TestResourceApplier_createPods(t *testing.T) {
t.Parallel()

tests := []struct {
name string
podToApply *corev1.Pod
podAfterApply *corev1.Pod
wantErr bool
name string
podToCreate *corev1.Pod
podAfterCreate *corev1.Pod
filter FilteringFunction
filtered bool
wantErr bool
}{
{
name: "create a Pod",
podToApply: &corev1.Pod{
podToCreate: &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
Expand All @@ -48,7 +50,7 @@ func TestResourceApplier_createPods(t *testing.T) {
},
},
},
podAfterApply: &corev1.Pod{
podAfterCreate: &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
Expand All @@ -66,56 +68,10 @@ func TestResourceApplier_createPods(t *testing.T) {
},
},
},
wantErr: false,
filter: nil,
filtered: false,
wantErr: false,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

client, mapper := prepare()
service := New(client, mapper, Options{})

p, err := runtime.DefaultUnstructuredConverter.ToUnstructured(tt.podToApply)
if err != nil {
t.Fatalf("failed to convert pod to unstructured: %v", err)
}
unstructedPod := &unstructured.Unstructured{Object: p}
err = service.Create(context.Background(), unstructedPod)
if (err != nil) != tt.wantErr {
t.Errorf("createPods() error = %v, wantErr %v", err, tt.wantErr)
return
}

got, err := getResource(tt.podToApply.GroupVersionKind(), tt.podToApply.Name, tt.podToApply.Namespace, mapper, client)
if err != nil {
t.Fatalf("failed to get pod when comparing: %v", err)
}
var gotPod corev1.Pod
err = runtime.DefaultUnstructuredConverter.FromUnstructured(got.UnstructuredContent(), &gotPod)
if err != nil {
t.Fatalf("failed to convert got unstructured to pod: %v", err)
}

if diff := cmp.Diff(*tt.podAfterApply, gotPod); diff != "" {
t.Errorf("createPods() mismatch (-want +got):\n%s", diff)
}
})
}
}

func TestResourceApplier_createPodsWithFilter(t *testing.T) {
t.Parallel()

tests := []struct {
name string
podToCreate *corev1.Pod
filter FilteringFunction
filtered bool
wantErr bool
}{
{
name: "create a Pod but it should not be created because of the filter",
podToCreate: &corev1.Pod{
Expand Down Expand Up @@ -168,6 +124,24 @@ func TestResourceApplier_createPodsWithFilter(t *testing.T) {
},
},
},
podAfterCreate: &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
Namespace: "default",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "container-1",
Image: "image-1",
},
},
},
},
filter: func(_ context.Context, resource *unstructured.Unstructured, _ *Clients) (bool, error) {
if resource.GetLabels()["ignore"] == "true" {
return false, nil
Expand Down Expand Up @@ -216,8 +190,8 @@ func TestResourceApplier_createPodsWithFilter(t *testing.T) {
t.Fatalf("failed to convert got unstructured to pod: %v", err)
}

if diff := cmp.Diff(*tt.podToCreate, gotPod); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
if diff := cmp.Diff(*tt.podAfterCreate, gotPod); diff != "" {
t.Errorf("createPods() mismatch (-want +got):\n%s", diff)
}
})
}
Expand Down Expand Up @@ -601,6 +575,10 @@ func getResource(gvk schema.GroupVersionKind, name, namespace string, mapper met
}

func setFilter(gvk schema.GroupVersionKind, filter FilteringFunction, mapper meta.RESTMapper) Options {
if filter == nil {
return Options{}
}

m, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
panic(err)
Expand Down

0 comments on commit eb4b4a0

Please sign in to comment.