|
| 1 | +package host |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/jumpstarter-dev/jumpstarter-lab-config/api/v1alpha1" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | +) |
| 10 | + |
| 11 | +func TestDeadAnnotationFiltering(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + exporterInstances []*v1alpha1.ExporterInstance |
| 15 | + expectedAliveInstances int |
| 16 | + expectedDeadInstances int |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "no dead instances", |
| 20 | + exporterInstances: []*v1alpha1.ExporterInstance{ |
| 21 | + { |
| 22 | + ObjectMeta: metav1.ObjectMeta{ |
| 23 | + Name: "instance1", |
| 24 | + }, |
| 25 | + }, |
| 26 | + { |
| 27 | + ObjectMeta: metav1.ObjectMeta{ |
| 28 | + Name: "instance2", |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + expectedAliveInstances: 2, |
| 33 | + expectedDeadInstances: 0, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "one dead instance", |
| 37 | + exporterInstances: []*v1alpha1.ExporterInstance{ |
| 38 | + { |
| 39 | + ObjectMeta: metav1.ObjectMeta{ |
| 40 | + Name: "instance1", |
| 41 | + }, |
| 42 | + }, |
| 43 | + { |
| 44 | + ObjectMeta: metav1.ObjectMeta{ |
| 45 | + Name: "instance2", |
| 46 | + Annotations: map[string]string{ |
| 47 | + "dead": "true", |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + expectedAliveInstances: 1, |
| 53 | + expectedDeadInstances: 1, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "all dead instances", |
| 57 | + exporterInstances: []*v1alpha1.ExporterInstance{ |
| 58 | + { |
| 59 | + ObjectMeta: metav1.ObjectMeta{ |
| 60 | + Name: "instance1", |
| 61 | + Annotations: map[string]string{ |
| 62 | + "dead": "true", |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + ObjectMeta: metav1.ObjectMeta{ |
| 68 | + Name: "instance2", |
| 69 | + Annotations: map[string]string{ |
| 70 | + "dead": "true", |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + expectedAliveInstances: 0, |
| 76 | + expectedDeadInstances: 2, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "dead annotation with false value", |
| 80 | + exporterInstances: []*v1alpha1.ExporterInstance{ |
| 81 | + { |
| 82 | + ObjectMeta: metav1.ObjectMeta{ |
| 83 | + Name: "instance1", |
| 84 | + Annotations: map[string]string{ |
| 85 | + "dead": "false", |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + expectedAliveInstances: 0, |
| 91 | + expectedDeadInstances: 1, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "dead annotation with other value", |
| 95 | + exporterInstances: []*v1alpha1.ExporterInstance{ |
| 96 | + { |
| 97 | + ObjectMeta: metav1.ObjectMeta{ |
| 98 | + Name: "instance1", |
| 99 | + Annotations: map[string]string{ |
| 100 | + "dead": "maybe", |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + expectedAliveInstances: 0, |
| 106 | + expectedDeadInstances: 1, |
| 107 | + }, |
| 108 | + } |
| 109 | + |
| 110 | + for _, tt := range tests { |
| 111 | + t.Run(tt.name, func(t *testing.T) { |
| 112 | + // Simulate the filtering logic from SyncExporterHosts |
| 113 | + aliveInstances := []*v1alpha1.ExporterInstance{} |
| 114 | + deadCount := 0 |
| 115 | + |
| 116 | + for _, exporterInstance := range tt.exporterInstances { |
| 117 | + if _, exists := exporterInstance.Annotations["dead"]; exists { |
| 118 | + deadCount++ |
| 119 | + } else { |
| 120 | + aliveInstances = append(aliveInstances, exporterInstance) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + assert.Equal(t, tt.expectedAliveInstances, len(aliveInstances), "unexpected number of alive instances") |
| 125 | + assert.Equal(t, tt.expectedDeadInstances, deadCount, "unexpected number of dead instances") |
| 126 | + }) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func TestHostSkippingBehavior(t *testing.T) { |
| 131 | + tests := []struct { |
| 132 | + name string |
| 133 | + exporterInstances []*v1alpha1.ExporterInstance |
| 134 | + shouldSkipHost bool |
| 135 | + expectedAlivCount int |
| 136 | + description string |
| 137 | + }{ |
| 138 | + { |
| 139 | + name: "mixed dead and alive instances - should NOT skip host", |
| 140 | + exporterInstances: []*v1alpha1.ExporterInstance{ |
| 141 | + { |
| 142 | + ObjectMeta: metav1.ObjectMeta{ |
| 143 | + Name: "alive-instance", |
| 144 | + }, |
| 145 | + }, |
| 146 | + { |
| 147 | + ObjectMeta: metav1.ObjectMeta{ |
| 148 | + Name: "dead-instance", |
| 149 | + Annotations: map[string]string{ |
| 150 | + "dead": "true", |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + shouldSkipHost: false, // Host should be processed because there's an alive instance |
| 156 | + expectedAlivCount: 1, |
| 157 | + description: "When there are both dead and alive instances, host should be processed", |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "all instances dead - should SKIP host", |
| 161 | + exporterInstances: []*v1alpha1.ExporterInstance{ |
| 162 | + { |
| 163 | + ObjectMeta: metav1.ObjectMeta{ |
| 164 | + Name: "dead-instance-1", |
| 165 | + Annotations: map[string]string{ |
| 166 | + "dead": "true", |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + { |
| 171 | + ObjectMeta: metav1.ObjectMeta{ |
| 172 | + Name: "dead-instance-2", |
| 173 | + Annotations: map[string]string{ |
| 174 | + "dead": "true", |
| 175 | + }, |
| 176 | + }, |
| 177 | + }, |
| 178 | + }, |
| 179 | + shouldSkipHost: true, // Host should be skipped because all instances are dead |
| 180 | + expectedAlivCount: 0, |
| 181 | + description: "When all instances are dead, host should be skipped entirely", |
| 182 | + }, |
| 183 | + { |
| 184 | + name: "all instances alive - should NOT skip host", |
| 185 | + exporterInstances: []*v1alpha1.ExporterInstance{ |
| 186 | + { |
| 187 | + ObjectMeta: metav1.ObjectMeta{ |
| 188 | + Name: "alive-instance-1", |
| 189 | + }, |
| 190 | + }, |
| 191 | + { |
| 192 | + ObjectMeta: metav1.ObjectMeta{ |
| 193 | + Name: "alive-instance-2", |
| 194 | + }, |
| 195 | + }, |
| 196 | + }, |
| 197 | + shouldSkipHost: false, // Host should be processed because all instances are alive |
| 198 | + expectedAlivCount: 2, |
| 199 | + description: "When all instances are alive, host should be processed normally", |
| 200 | + }, |
| 201 | + } |
| 202 | + |
| 203 | + for _, tt := range tests { |
| 204 | + t.Run(tt.name, func(t *testing.T) { |
| 205 | + // Simulate the new logic from SyncExporterHosts |
| 206 | + // Check if all instances are dead |
| 207 | + allDead := true |
| 208 | + aliveCount := 0 |
| 209 | + |
| 210 | + for _, exporterInstance := range tt.exporterInstances { |
| 211 | + if _, exists := exporterInstance.Annotations["dead"]; !exists { |
| 212 | + allDead = false |
| 213 | + aliveCount++ |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + // Host is skipped only if all instances are dead (and there are instances) |
| 218 | + hostWouldBeSkipped := len(tt.exporterInstances) > 0 && allDead |
| 219 | + |
| 220 | + assert.Equal(t, tt.shouldSkipHost, hostWouldBeSkipped, tt.description) |
| 221 | + assert.Equal(t, tt.expectedAlivCount, aliveCount, "unexpected number of alive instances") |
| 222 | + }) |
| 223 | + } |
| 224 | +} |
0 commit comments