|
| 1 | +package supportbundle |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + types "github.com/replicatedhq/troubleshoot/pkg/supportbundle/types" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | +) |
| 13 | + |
| 14 | +func Test_getPodDetails(t *testing.T) { |
| 15 | + podNamespace := "default" |
| 16 | + podName := "hello-27436131-pxhk9" |
| 17 | + |
| 18 | + podsFileContentOldFormat := `[ |
| 19 | + { |
| 20 | + "kind": "Pod", |
| 21 | + "apiVersion": "v1", |
| 22 | + "metadata": { |
| 23 | + "name": "hello-27436131-pxhk9", |
| 24 | + "namespace": "default" |
| 25 | + }, |
| 26 | + "spec": { |
| 27 | + "initContainers": [ |
| 28 | + { |
| 29 | + "name": "remove-lost-found" |
| 30 | + } |
| 31 | + ], |
| 32 | + "containers": [ |
| 33 | + { |
| 34 | + "name": "hello" |
| 35 | + } |
| 36 | + ] |
| 37 | + } |
| 38 | + } |
| 39 | + ]` |
| 40 | + |
| 41 | + podsFileContentNewFormat := fmt.Sprintf(`{ |
| 42 | + "kind": "PodList", |
| 43 | + "apiVersion": "v1", |
| 44 | + "metadata": { |
| 45 | + "resourceVersion": "5389414" |
| 46 | + }, |
| 47 | + "items": %s |
| 48 | + }`, podsFileContentOldFormat) |
| 49 | + |
| 50 | + eventsFileContentOldFormat := `[ |
| 51 | + { |
| 52 | + "kind": "Event", |
| 53 | + "apiVersion": "v1", |
| 54 | + "metadata": { |
| 55 | + "name": "example-nginx.16d85cebe302a9b1", |
| 56 | + "namespace": "default" |
| 57 | + }, |
| 58 | + "involvedObject": { |
| 59 | + "kind": "Deployment", |
| 60 | + "namespace": "default", |
| 61 | + "name": "example-nginx" |
| 62 | + } |
| 63 | + }, |
| 64 | + { |
| 65 | + "kind": "Event", |
| 66 | + "apiVersion": "v1", |
| 67 | + "metadata": { |
| 68 | + "name": "hello-27436131-pxhk9.16d85cf27380b4fa", |
| 69 | + "namespace": "default" |
| 70 | + }, |
| 71 | + "involvedObject": { |
| 72 | + "kind": "Pod", |
| 73 | + "namespace": "default", |
| 74 | + "name": "hello-27436131-pxhk9" |
| 75 | + } |
| 76 | + } |
| 77 | + ]` |
| 78 | + |
| 79 | + eventsFileContentNewFormat := fmt.Sprintf(`{ |
| 80 | + "kind": "EventList", |
| 81 | + "apiVersion": "v1", |
| 82 | + "metadata": { |
| 83 | + "resourceVersion": "5389515" |
| 84 | + }, |
| 85 | + "items": %s |
| 86 | + }`, eventsFileContentOldFormat) |
| 87 | + |
| 88 | + removeLostAndFoundInitContainerLogs := `some logs here` |
| 89 | + helloContainerLogs := `Tue Mar 1 20:53:00 UTC 2022 Hello` |
| 90 | + |
| 91 | + tests := []struct { |
| 92 | + name string |
| 93 | + files map[string][]byte |
| 94 | + expect *types.PodDetails |
| 95 | + }{ |
| 96 | + { |
| 97 | + name: "old support bundle format", |
| 98 | + files: map[string][]byte{ |
| 99 | + getPodsFilePath(podNamespace): []byte(podsFileContentOldFormat), |
| 100 | + getEventsFilePath(podNamespace): []byte(eventsFileContentOldFormat), |
| 101 | + getContainerLogsFilePath(podNamespace, podName, "remove-lost-found"): []byte(removeLostAndFoundInitContainerLogs), |
| 102 | + getContainerLogsFilePath(podNamespace, podName, "hello"): []byte(helloContainerLogs), |
| 103 | + }, |
| 104 | + expect: &types.PodDetails{ |
| 105 | + PodDefinition: corev1.Pod{ |
| 106 | + TypeMeta: metav1.TypeMeta{ |
| 107 | + APIVersion: "v1", |
| 108 | + Kind: "Pod", |
| 109 | + }, |
| 110 | + ObjectMeta: metav1.ObjectMeta{ |
| 111 | + Name: podName, |
| 112 | + Namespace: podNamespace, |
| 113 | + }, |
| 114 | + Spec: corev1.PodSpec{ |
| 115 | + InitContainers: []corev1.Container{ |
| 116 | + { |
| 117 | + Name: "remove-lost-found", |
| 118 | + }, |
| 119 | + }, |
| 120 | + Containers: []corev1.Container{ |
| 121 | + { |
| 122 | + Name: "hello", |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + PodEvents: []corev1.Event{ |
| 128 | + { |
| 129 | + TypeMeta: metav1.TypeMeta{ |
| 130 | + APIVersion: "v1", |
| 131 | + Kind: "Event", |
| 132 | + }, |
| 133 | + ObjectMeta: metav1.ObjectMeta{ |
| 134 | + Name: "hello-27436131-pxhk9.16d85cf27380b4fa", |
| 135 | + Namespace: podNamespace, |
| 136 | + }, |
| 137 | + InvolvedObject: corev1.ObjectReference{ |
| 138 | + Kind: "Pod", |
| 139 | + Namespace: podNamespace, |
| 140 | + Name: podName, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + PodContainers: []types.PodContainer{ |
| 145 | + { |
| 146 | + Name: "remove-lost-found", |
| 147 | + LogsFilePath: getContainerLogsFilePath(podNamespace, podName, "remove-lost-found"), |
| 148 | + IsInitContainer: true, |
| 149 | + }, |
| 150 | + { |
| 151 | + Name: "hello", |
| 152 | + LogsFilePath: getContainerLogsFilePath(podNamespace, podName, "hello"), |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + { |
| 158 | + name: "new support bundle format", |
| 159 | + files: map[string][]byte{ |
| 160 | + getPodsFilePath(podNamespace): []byte(podsFileContentNewFormat), |
| 161 | + getEventsFilePath(podNamespace): []byte(eventsFileContentNewFormat), |
| 162 | + getContainerLogsFilePath(podNamespace, podName, "remove-lost-found"): []byte(removeLostAndFoundInitContainerLogs), |
| 163 | + getContainerLogsFilePath(podNamespace, podName, "hello"): []byte(helloContainerLogs), |
| 164 | + }, |
| 165 | + expect: &types.PodDetails{ |
| 166 | + PodDefinition: corev1.Pod{ |
| 167 | + TypeMeta: metav1.TypeMeta{ |
| 168 | + APIVersion: "v1", |
| 169 | + Kind: "Pod", |
| 170 | + }, |
| 171 | + ObjectMeta: metav1.ObjectMeta{ |
| 172 | + Name: podName, |
| 173 | + Namespace: podNamespace, |
| 174 | + }, |
| 175 | + Spec: corev1.PodSpec{ |
| 176 | + InitContainers: []corev1.Container{ |
| 177 | + { |
| 178 | + Name: "remove-lost-found", |
| 179 | + }, |
| 180 | + }, |
| 181 | + Containers: []corev1.Container{ |
| 182 | + { |
| 183 | + Name: "hello", |
| 184 | + }, |
| 185 | + }, |
| 186 | + }, |
| 187 | + }, |
| 188 | + PodEvents: []corev1.Event{ |
| 189 | + { |
| 190 | + TypeMeta: metav1.TypeMeta{ |
| 191 | + APIVersion: "v1", |
| 192 | + Kind: "Event", |
| 193 | + }, |
| 194 | + ObjectMeta: metav1.ObjectMeta{ |
| 195 | + Name: "hello-27436131-pxhk9.16d85cf27380b4fa", |
| 196 | + Namespace: podNamespace, |
| 197 | + }, |
| 198 | + InvolvedObject: corev1.ObjectReference{ |
| 199 | + Kind: "Pod", |
| 200 | + Namespace: podNamespace, |
| 201 | + Name: podName, |
| 202 | + }, |
| 203 | + }, |
| 204 | + }, |
| 205 | + PodContainers: []types.PodContainer{ |
| 206 | + { |
| 207 | + Name: "remove-lost-found", |
| 208 | + LogsFilePath: getContainerLogsFilePath(podNamespace, podName, "remove-lost-found"), |
| 209 | + IsInitContainer: true, |
| 210 | + }, |
| 211 | + { |
| 212 | + Name: "hello", |
| 213 | + LogsFilePath: getContainerLogsFilePath(podNamespace, podName, "hello"), |
| 214 | + }, |
| 215 | + }, |
| 216 | + }, |
| 217 | + }, |
| 218 | + } |
| 219 | + |
| 220 | + for _, test := range tests { |
| 221 | + t.Run(test.name, func(t *testing.T) { |
| 222 | + req := require.New(t) |
| 223 | + |
| 224 | + actual, err := getPodDetailsFromFiles(test.files, podNamespace, podName) |
| 225 | + req.NoError(err) |
| 226 | + |
| 227 | + assert.Equal(t, test.expect, actual) |
| 228 | + }) |
| 229 | + } |
| 230 | +} |
0 commit comments