|
| 1 | +package collect |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | +) |
| 10 | + |
| 11 | +func TestCreatePodStruct(t *testing.T) { |
| 12 | + runPodCollector := &troubleshootv1beta2.RunPod{ |
| 13 | + Namespace: "test-namespace", |
| 14 | + Name: "test-pod", |
| 15 | + Annotations: map[string]string{ |
| 16 | + "annotation1": "value1", |
| 17 | + "annotation2": "value2", |
| 18 | + }, |
| 19 | + PodSpec: corev1.PodSpec{ |
| 20 | + Containers: []corev1.Container{ |
| 21 | + { |
| 22 | + Name: "test-container", |
| 23 | + Image: "test-image", |
| 24 | + }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + } |
| 28 | + |
| 29 | + expectedPod := corev1.Pod{ |
| 30 | + ObjectMeta: metav1.ObjectMeta{ |
| 31 | + Name: "test-pod", |
| 32 | + Namespace: "test-namespace", |
| 33 | + Labels: map[string]string{"troubleshoot-role": "run-collector"}, |
| 34 | + Annotations: map[string]string{"annotation1": "value1", "annotation2": "value2"}, |
| 35 | + }, |
| 36 | + TypeMeta: metav1.TypeMeta{ |
| 37 | + APIVersion: "v1", |
| 38 | + Kind: "Pod", |
| 39 | + }, |
| 40 | + Spec: corev1.PodSpec{ |
| 41 | + Containers: []corev1.Container{ |
| 42 | + { |
| 43 | + Name: "test-container", |
| 44 | + Image: "test-image", |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + pod := createPodStruct(runPodCollector) |
| 51 | + |
| 52 | + if pod.Name != expectedPod.Name { |
| 53 | + t.Errorf("Expected pod name %s, but got %s", expectedPod.Name, pod.Name) |
| 54 | + } |
| 55 | + |
| 56 | + if pod.Namespace != expectedPod.Namespace { |
| 57 | + t.Errorf("Expected pod namespace %s, but got %s", expectedPod.Namespace, pod.Namespace) |
| 58 | + } |
| 59 | + |
| 60 | + if len(pod.Labels) != len(expectedPod.Labels) { |
| 61 | + t.Errorf("Expected %d labels, but got %d", len(expectedPod.Labels), len(pod.Labels)) |
| 62 | + } |
| 63 | + |
| 64 | + for key, value := range expectedPod.Labels { |
| 65 | + if pod.Labels[key] != value { |
| 66 | + t.Errorf("Expected label %s=%s, but got %s=%s", key, value, key, pod.Labels[key]) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if len(pod.Annotations) != len(expectedPod.Annotations) { |
| 71 | + t.Errorf("Expected %d annotations, but got %d", len(expectedPod.Annotations), len(pod.Annotations)) |
| 72 | + } |
| 73 | + |
| 74 | + for key, value := range expectedPod.Annotations { |
| 75 | + if pod.Annotations[key] != value { |
| 76 | + t.Errorf("Expected annotation %s=%s, but got %s=%s", key, value, key, pod.Annotations[key]) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if len(pod.Spec.Containers) != len(expectedPod.Spec.Containers) { |
| 81 | + t.Errorf("Expected %d containers, but got %d", len(expectedPod.Spec.Containers), len(pod.Spec.Containers)) |
| 82 | + } |
| 83 | + |
| 84 | + for i, container := range expectedPod.Spec.Containers { |
| 85 | + if pod.Spec.Containers[i].Name != container.Name { |
| 86 | + t.Errorf("Expected container name %s, but got %s", container.Name, pod.Spec.Containers[i].Name) |
| 87 | + } |
| 88 | + |
| 89 | + if pod.Spec.Containers[i].Image != container.Image { |
| 90 | + t.Errorf("Expected container image %s, but got %s", container.Image, pod.Spec.Containers[i].Image) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments