| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame^] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Jan Schär | a48bd3c | 2024-07-29 17:22:18 +0200 | [diff] [blame] | 4 | package object |
| 5 | |
| 6 | // Taken and modified from the Kubernetes plugin of CoreDNS, under Apache 2.0. |
| 7 | |
| 8 | import ( |
| 9 | "testing" |
| 10 | |
| 11 | api "k8s.io/api/core/v1" |
| 12 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | "k8s.io/client-go/tools/cache" |
| 14 | ) |
| 15 | |
| 16 | func TestDefaultProcessor(t *testing.T) { |
| 17 | pbuild := DefaultProcessor(ToService) |
| 18 | reh := cache.ResourceEventHandlerFuncs{} |
| 19 | idx := cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc, cache.Indexers{}) |
| 20 | processor := pbuild(idx, reh) |
| 21 | testProcessor(t, processor, idx) |
| 22 | } |
| 23 | |
| 24 | func testProcessor(t *testing.T, processor cache.ProcessFunc, idx cache.Indexer) { |
| 25 | obj := &api.Service{ |
| 26 | ObjectMeta: metav1.ObjectMeta{Name: "service1", Namespace: "test1"}, |
| 27 | Spec: api.ServiceSpec{ |
| 28 | Type: api.ServiceTypeExternalName, |
| 29 | ExternalName: "example.com.", |
| 30 | Ports: []api.ServicePort{{Port: 80}}, |
| 31 | }, |
| 32 | } |
| 33 | obj2 := &api.Service{ |
| 34 | ObjectMeta: metav1.ObjectMeta{Name: "service2", Namespace: "test1"}, |
| 35 | Spec: api.ServiceSpec{ |
| 36 | ClusterIP: "5.6.7.8", |
| 37 | ClusterIPs: []string{"5.6.7.8"}, |
| 38 | Ports: []api.ServicePort{{Port: 80}}, |
| 39 | }, |
| 40 | } |
| 41 | |
| 42 | // Add the objects |
| 43 | err := processor(cache.Deltas{ |
| 44 | {Type: cache.Added, Object: obj.DeepCopy()}, |
| 45 | {Type: cache.Added, Object: obj2.DeepCopy()}, |
| 46 | }, false) |
| 47 | if err != nil { |
| 48 | t.Fatalf("add failed: %v", err) |
| 49 | } |
| 50 | got, exists, err := idx.Get(obj) |
| 51 | if err != nil { |
| 52 | t.Fatalf("get added object failed: %v", err) |
| 53 | } |
| 54 | if !exists { |
| 55 | t.Fatal("added object not found in index") |
| 56 | } |
| 57 | svc, ok := got.(*Service) |
| 58 | if !ok { |
| 59 | t.Fatal("object in index was incorrect type") |
| 60 | } |
| 61 | if svc.ExternalName != obj.Spec.ExternalName { |
| 62 | t.Fatalf("expected '%v', got '%v'", obj.Spec.ExternalName, svc.ExternalName) |
| 63 | } |
| 64 | |
| 65 | // Update an object |
| 66 | obj.Spec.ExternalName = "2.example.com." |
| 67 | err = processor(cache.Deltas{{ |
| 68 | Type: cache.Updated, |
| 69 | Object: obj.DeepCopy(), |
| 70 | }}, false) |
| 71 | if err != nil { |
| 72 | t.Fatalf("update failed: %v", err) |
| 73 | } |
| 74 | got, exists, err = idx.Get(obj) |
| 75 | if err != nil { |
| 76 | t.Fatalf("get updated object failed: %v", err) |
| 77 | } |
| 78 | if !exists { |
| 79 | t.Fatal("updated object not found in index") |
| 80 | } |
| 81 | svc, ok = got.(*Service) |
| 82 | if !ok { |
| 83 | t.Fatal("object in index was incorrect type") |
| 84 | } |
| 85 | if svc.ExternalName != obj.Spec.ExternalName { |
| 86 | t.Fatalf("expected '%v', got '%v'", obj.Spec.ExternalName, svc.ExternalName) |
| 87 | } |
| 88 | |
| 89 | // Delete an object |
| 90 | err = processor(cache.Deltas{{ |
| 91 | Type: cache.Deleted, |
| 92 | Object: obj2.DeepCopy(), |
| 93 | }}, false) |
| 94 | if err != nil { |
| 95 | t.Fatalf("delete test failed: %v", err) |
| 96 | } |
| 97 | _, exists, err = idx.Get(obj2) |
| 98 | if err != nil { |
| 99 | t.Fatalf("get deleted object failed: %v", err) |
| 100 | } |
| 101 | if exists { |
| 102 | t.Fatal("deleted object found in index") |
| 103 | } |
| 104 | |
| 105 | // Delete an object via tombstone |
| 106 | key, _ := cache.MetaNamespaceKeyFunc(obj) |
| 107 | tombstone := cache.DeletedFinalStateUnknown{Key: key, Obj: svc} |
| 108 | err = processor(cache.Deltas{{ |
| 109 | Type: cache.Deleted, |
| 110 | Object: tombstone, |
| 111 | }}, false) |
| 112 | if err != nil { |
| 113 | t.Fatalf("tombstone delete test failed: %v", err) |
| 114 | } |
| 115 | _, exists, err = idx.Get(svc) |
| 116 | if err != nil { |
| 117 | t.Fatalf("get tombstone deleted object failed: %v", err) |
| 118 | } |
| 119 | if exists { |
| 120 | t.Fatal("tombstone deleted object found in index") |
| 121 | } |
| 122 | } |