|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package resolver |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | +) |
| 13 | + |
| 14 | +// TestAttachNamespace function |
| 15 | +func TestAttachNamespace(t *testing.T) { |
| 16 | + result := attachNamespace("testResource", "testNamespace") |
| 17 | + if result != "testResource@testNamespace" { |
| 18 | + t.Errorf("attachNamespace was incorrect, got: %s, want: %s.", result, "testResource@testNamespace") |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +// TestGetServiceAndNamespace function |
| 23 | +func TestGetServiceAndNamespace(t *testing.T) { |
| 24 | + service := &corev1.Service{ |
| 25 | + ObjectMeta: metav1.ObjectMeta{ |
| 26 | + Name: "testService", |
| 27 | + Namespace: "testNamespace", |
| 28 | + }, |
| 29 | + } |
| 30 | + result := getServiceAndNamespace(service) |
| 31 | + if result != "testService@testNamespace" { |
| 32 | + t.Errorf("getServiceAndNamespace was incorrect, got: %s, want: %s.", result, "testService@testNamespace") |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// TestExtractResourceAndNamespace function |
| 37 | +func TestExtractResourceAndNamespace(t *testing.T) { |
| 38 | + // Test normal case |
| 39 | + name, namespace := extractResourceAndNamespace("testService@testNamespace") |
| 40 | + if name != "testService" || namespace != "testNamespace" { |
| 41 | + t.Errorf("extractResourceAndNamespace was incorrect, got: %s and %s, want: %s and %s.", name, namespace, "testService", "testNamespace") |
| 42 | + } |
| 43 | + |
| 44 | + // Test invalid case |
| 45 | + name, namespace = extractResourceAndNamespace("invalid") |
| 46 | + if name != "" || namespace != "" { |
| 47 | + t.Errorf("extractResourceAndNamespace was incorrect, got: %s and %s, want: %s and %s.", name, namespace, "", "") |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestExtractWorkloadNameFromRS(t *testing.T) { |
| 52 | + testCases := []struct { |
| 53 | + name string |
| 54 | + replicaSetName string |
| 55 | + want string |
| 56 | + shouldErr bool |
| 57 | + }{ |
| 58 | + { |
| 59 | + name: "Valid ReplicaSet Name", |
| 60 | + replicaSetName: "my-deployment-5859ffc7ff", |
| 61 | + want: "my-deployment", |
| 62 | + shouldErr: false, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "Invalid ReplicaSet Name - No Hyphen", |
| 66 | + replicaSetName: "mydeployment5859ffc7ff", |
| 67 | + want: "", |
| 68 | + shouldErr: true, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "Invalid ReplicaSet Name - Less Than 10 Suffix Characters", |
| 72 | + replicaSetName: "my-deployment-bc2", |
| 73 | + want: "", |
| 74 | + shouldErr: true, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "Invalid ReplicaSet Name - More Than 10 Suffix Characters", |
| 78 | + replicaSetName: "my-deployment-5859ffc7ffx", |
| 79 | + want: "", |
| 80 | + shouldErr: true, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "Invalid ReplicaSet Name - Invalid Characters in Suffix", |
| 84 | + replicaSetName: "my-deployment-aeiou12345", |
| 85 | + want: "", |
| 86 | + shouldErr: true, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "Invalid ReplicaSet Name - Empty String", |
| 90 | + replicaSetName: "", |
| 91 | + want: "", |
| 92 | + shouldErr: true, |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + for _, tc := range testCases { |
| 97 | + t.Run(tc.name, func(t *testing.T) { |
| 98 | + got, err := extractWorkloadNameFromRS(tc.replicaSetName) |
| 99 | + |
| 100 | + if (err != nil) != tc.shouldErr { |
| 101 | + t.Errorf("extractWorkloadNameFromRS() error = %v, wantErr %v", err, tc.shouldErr) |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + if got != tc.want { |
| 106 | + t.Errorf("extractWorkloadNameFromRS() = %v, want %v", got, tc.want) |
| 107 | + } |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestExtractWorkloadNameFromPodName(t *testing.T) { |
| 113 | + testCases := []struct { |
| 114 | + name string |
| 115 | + podName string |
| 116 | + want string |
| 117 | + shouldErr bool |
| 118 | + }{ |
| 119 | + { |
| 120 | + name: "Valid Pod Name", |
| 121 | + podName: "my-replicaset-bc24f", |
| 122 | + want: "my-replicaset", |
| 123 | + shouldErr: false, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "Invalid Pod Name - No Hyphen", |
| 127 | + podName: "myreplicasetbc24f", |
| 128 | + want: "", |
| 129 | + shouldErr: true, |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "Invalid Pod Name - Less Than 5 Suffix Characters", |
| 133 | + podName: "my-replicaset-bc2", |
| 134 | + want: "", |
| 135 | + shouldErr: true, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "Invalid Pod Name - More Than 5 Suffix Characters", |
| 139 | + podName: "my-replicaset-bc24f5", |
| 140 | + want: "", |
| 141 | + shouldErr: true, |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "Invalid Pod Name - Empty String", |
| 145 | + podName: "", |
| 146 | + want: "", |
| 147 | + shouldErr: true, |
| 148 | + }, |
| 149 | + } |
| 150 | + |
| 151 | + for _, tc := range testCases { |
| 152 | + t.Run(tc.name, func(t *testing.T) { |
| 153 | + got, err := extractWorkloadNameFromPodName(tc.podName) |
| 154 | + |
| 155 | + if (err != nil) != tc.shouldErr { |
| 156 | + t.Errorf("extractWorkloadNameFromPodName() error = %v, wantErr %v", err, tc.shouldErr) |
| 157 | + return |
| 158 | + } |
| 159 | + |
| 160 | + if got != tc.want { |
| 161 | + t.Errorf("extractWorkloadNameFromPodName() = %v, want %v", got, tc.want) |
| 162 | + } |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// TestGetWorkloadAndNamespace function |
| 168 | +func TestGetWorkloadAndNamespace(t *testing.T) { |
| 169 | + // Test ReplicaSet case |
| 170 | + pod := &corev1.Pod{ |
| 171 | + ObjectMeta: metav1.ObjectMeta{ |
| 172 | + Name: "testPod", |
| 173 | + Namespace: "testNamespace", |
| 174 | + OwnerReferences: []metav1.OwnerReference{ |
| 175 | + { |
| 176 | + Kind: "ReplicaSet", |
| 177 | + Name: "testDeployment-5d68bc5f49", |
| 178 | + }, |
| 179 | + }, |
| 180 | + }, |
| 181 | + } |
| 182 | + result := getWorkloadAndNamespace(pod) |
| 183 | + if result != "testDeployment@testNamespace" { |
| 184 | + t.Errorf("getDeploymentAndNamespace was incorrect, got: %s, want: %s.", result, "testDeployment@testNamespace") |
| 185 | + } |
| 186 | + |
| 187 | + // Test StatefulSet case |
| 188 | + pod.ObjectMeta.OwnerReferences[0].Kind = "StatefulSet" |
| 189 | + pod.ObjectMeta.OwnerReferences[0].Name = "testStatefulSet" |
| 190 | + result = getWorkloadAndNamespace(pod) |
| 191 | + if result != "testStatefulSet@testNamespace" { |
| 192 | + t.Errorf("getWorkloadAndNamespace was incorrect, got: %s, want: %s.", result, "testStatefulSet@testNamespace") |
| 193 | + } |
| 194 | + |
| 195 | + // Test Other case |
| 196 | + pod.ObjectMeta.OwnerReferences[0].Kind = "Other" |
| 197 | + pod.ObjectMeta.OwnerReferences[0].Name = "testOther" |
| 198 | + result = getWorkloadAndNamespace(pod) |
| 199 | + if result != "" { |
| 200 | + t.Errorf("getWorkloadAndNamespace was incorrect, got: %s, want: %s.", result, "") |
| 201 | + } |
| 202 | + |
| 203 | + // Test no OwnerReferences case |
| 204 | + pod.ObjectMeta.OwnerReferences = nil |
| 205 | + result = getWorkloadAndNamespace(pod) |
| 206 | + if result != "" { |
| 207 | + t.Errorf("getWorkloadAndNamespace was incorrect, got: %s, want: %s.", result, "") |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +func TestExtractIPPort(t *testing.T) { |
| 212 | + // Test valid IP:Port |
| 213 | + ip, port, ok := extractIPPort("192.0.2.0:8080") |
| 214 | + assert.Equal(t, "192.0.2.0", ip) |
| 215 | + assert.Equal(t, "8080", port) |
| 216 | + assert.True(t, ok) |
| 217 | + |
| 218 | + // Test invalid IP:Port |
| 219 | + ip, port, ok = extractIPPort("192.0.2:8080") |
| 220 | + assert.Equal(t, "", ip) |
| 221 | + assert.Equal(t, "", port) |
| 222 | + assert.False(t, ok) |
| 223 | + |
| 224 | + // Test IP only |
| 225 | + ip, port, ok = extractIPPort("192.0.2.0") |
| 226 | + assert.Equal(t, "", ip) |
| 227 | + assert.Equal(t, "", port) |
| 228 | + assert.False(t, ok) |
| 229 | +} |
0 commit comments