|
| 1 | +package store |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | +) |
| 10 | + |
| 11 | +// TestMapDomainSecrets_ReturnsPointers verifies that MapDomainSecrets returns pointers |
| 12 | +func TestMapDomainSecrets_ReturnsPointers(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + store Store |
| 16 | + }{ |
| 17 | + {"OptimizedStore", NewOptimizedStore()}, |
| 18 | + {"LegacyStore", &StoreAdapter{LegacyStore: New()}}, |
| 19 | + } |
| 20 | + |
| 21 | + for _, tt := range tests { |
| 22 | + t.Run(tt.name, func(t *testing.T) { |
| 23 | + // Create test secrets |
| 24 | + secret1 := &corev1.Secret{ |
| 25 | + ObjectMeta: metav1.ObjectMeta{ |
| 26 | + Name: "secret1", |
| 27 | + Namespace: "default", |
| 28 | + Annotations: map[string]string{ |
| 29 | + "envoy.kaasops.io/domains": "domain1.com", |
| 30 | + }, |
| 31 | + }, |
| 32 | + Data: map[string][]byte{ |
| 33 | + "tls.crt": []byte("cert1"), |
| 34 | + "tls.key": []byte("key1"), |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + secret2 := &corev1.Secret{ |
| 39 | + ObjectMeta: metav1.ObjectMeta{ |
| 40 | + Name: "secret2", |
| 41 | + Namespace: "default", |
| 42 | + Annotations: map[string]string{ |
| 43 | + "envoy.kaasops.io/domains": "domain2.com", |
| 44 | + }, |
| 45 | + }, |
| 46 | + Data: map[string][]byte{ |
| 47 | + "tls.crt": []byte("cert2"), |
| 48 | + "tls.key": []byte("key2"), |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + tt.store.SetSecret(secret1) |
| 53 | + tt.store.SetSecret(secret2) |
| 54 | + |
| 55 | + // Get domain secrets map |
| 56 | + domainSecrets := tt.store.MapDomainSecrets() |
| 57 | + |
| 58 | + // Verify we got pointers |
| 59 | + assert.NotNil(t, domainSecrets) |
| 60 | + assert.Len(t, domainSecrets, 2) |
| 61 | + |
| 62 | + // Verify correct data |
| 63 | + s1, ok := domainSecrets["domain1.com"] |
| 64 | + assert.True(t, ok, "domain1.com should exist") |
| 65 | + assert.NotNil(t, s1, "domain1.com secret should not be nil") |
| 66 | + assert.Equal(t, "secret1", s1.Name) |
| 67 | + assert.Equal(t, []byte("cert1"), s1.Data["tls.crt"]) |
| 68 | + |
| 69 | + s2, ok := domainSecrets["domain2.com"] |
| 70 | + assert.True(t, ok, "domain2.com should exist") |
| 71 | + assert.NotNil(t, s2, "domain2.com secret should not be nil") |
| 72 | + assert.Equal(t, "secret2", s2.Name) |
| 73 | + assert.Equal(t, []byte("cert2"), s2.Data["tls.crt"]) |
| 74 | + |
| 75 | + // Verify that each domain has a unique pointer |
| 76 | + assert.NotSame(t, s1, s2, "pointers should be different") |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// TestMapDomainSecrets_ImmutableCopy verifies that returned map is a copy |
| 82 | +func TestMapDomainSecrets_ImmutableCopy(t *testing.T) { |
| 83 | + tests := []struct { |
| 84 | + name string |
| 85 | + store Store |
| 86 | + }{ |
| 87 | + {"OptimizedStore", NewOptimizedStore()}, |
| 88 | + {"LegacyStore", &StoreAdapter{LegacyStore: New()}}, |
| 89 | + } |
| 90 | + |
| 91 | + for _, tt := range tests { |
| 92 | + t.Run(tt.name, func(t *testing.T) { |
| 93 | + secret := &corev1.Secret{ |
| 94 | + ObjectMeta: metav1.ObjectMeta{ |
| 95 | + Name: "secret", |
| 96 | + Namespace: "default", |
| 97 | + Annotations: map[string]string{ |
| 98 | + "envoy.kaasops.io/domains": "test.com", |
| 99 | + }, |
| 100 | + }, |
| 101 | + Data: map[string][]byte{"key": []byte("value")}, |
| 102 | + } |
| 103 | + |
| 104 | + tt.store.SetSecret(secret) |
| 105 | + |
| 106 | + // Get first copy |
| 107 | + map1 := tt.store.MapDomainSecrets() |
| 108 | + |
| 109 | + // Get second copy |
| 110 | + map2 := tt.store.MapDomainSecrets() |
| 111 | + |
| 112 | + // Verify both have the data |
| 113 | + assert.Len(t, map1, 1) |
| 114 | + assert.Len(t, map2, 1) |
| 115 | + |
| 116 | + // Pointers should be different (new allocation each time) |
| 117 | + assert.NotSame(t, map1["test.com"], map2["test.com"], |
| 118 | + "each call should return new pointers to prevent accidental mutation") |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
0 commit comments