|
| 1 | +package servicemirror |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "log" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/linkerd/linkerd2/controller/gen/apis/link/v1alpha2" |
| 11 | + l5dcrdinformer "github.com/linkerd/linkerd2/controller/gen/client/informers/externalversions" |
| 12 | + "github.com/linkerd/linkerd2/controller/k8s" |
| 13 | + corev1 "k8s.io/api/core/v1" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/apimachinery/pkg/types" |
| 16 | +) |
| 17 | + |
| 18 | +const nsName = "ns1" |
| 19 | +const linkName = "linkName" |
| 20 | + |
| 21 | +func TestLinkHandlers(t *testing.T) { |
| 22 | + k8sAPI, l5dAPI, err := k8s.NewFakeAPIWithL5dClient() |
| 23 | + if err != nil { |
| 24 | + t.Fatal(err) |
| 25 | + } |
| 26 | + k8sAPI.Sync(nil) |
| 27 | + |
| 28 | + informerFactory := l5dcrdinformer.NewSharedInformerFactoryWithOptions( |
| 29 | + l5dAPI, |
| 30 | + k8s.ResyncTime, |
| 31 | + l5dcrdinformer.WithNamespace(nsName), |
| 32 | + ) |
| 33 | + informer := informerFactory.Link().V1alpha2().Links().Informer() |
| 34 | + informerFactory.Start(context.Background().Done()) |
| 35 | + |
| 36 | + results := make(chan *v1alpha2.Link, 100) |
| 37 | + _, err = informer.AddEventHandler(GetLinkHandlers(results, linkName)) |
| 38 | + if err != nil { |
| 39 | + t.Fatal(err) |
| 40 | + } |
| 41 | + |
| 42 | + // test that a message is received when a link is created |
| 43 | + _, err = k8sAPI.Client.CoreV1().Namespaces().Create(context.Background(), &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: nsName}}, metav1.CreateOptions{}) |
| 44 | + if err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + |
| 48 | + link := &v1alpha2.Link{ |
| 49 | + ObjectMeta: metav1.ObjectMeta{ |
| 50 | + Name: linkName, |
| 51 | + Namespace: nsName, |
| 52 | + }, |
| 53 | + Spec: v1alpha2.LinkSpec{ProbeSpec: v1alpha2.ProbeSpec{Timeout: "30s"}}, |
| 54 | + } |
| 55 | + _, err = l5dAPI.LinkV1alpha2().Links(nsName).Create(context.Background(), link, metav1.CreateOptions{}) |
| 56 | + if err != nil { |
| 57 | + t.Fatal(err) |
| 58 | + } |
| 59 | + |
| 60 | + select { |
| 61 | + case link := <-results: |
| 62 | + if link.GetName() != linkName { |
| 63 | + t.Fatalf("Expected LinkName, got %s", link.GetName()) |
| 64 | + } |
| 65 | + case <-time.After(time.Second): |
| 66 | + t.Fatal("Timed out waiting for message") |
| 67 | + } |
| 68 | + |
| 69 | + // test that a message is received when a link spec is updated |
| 70 | + patch := map[string]any{ |
| 71 | + "spec": map[string]any{ |
| 72 | + "probeSpec": map[string]any{ |
| 73 | + "timeout": "60s", |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | + patchBytes, err := json.Marshal(patch) |
| 78 | + if err != nil { |
| 79 | + log.Fatalf("Failed to marshal patch: %v", err) |
| 80 | + } |
| 81 | + _, err = l5dAPI.LinkV1alpha2().Links(nsName).Patch( |
| 82 | + context.Background(), |
| 83 | + linkName, |
| 84 | + types.MergePatchType, |
| 85 | + patchBytes, |
| 86 | + metav1.PatchOptions{}, |
| 87 | + ) |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("Failed to patch link: %s", err) |
| 90 | + } |
| 91 | + |
| 92 | + select { |
| 93 | + case link := <-results: |
| 94 | + if link.GetName() != linkName { |
| 95 | + t.Fatalf("Expected LinkName, got %s", link.GetName()) |
| 96 | + } |
| 97 | + case <-time.After(time.Second): |
| 98 | + t.Fatal("Timed out waiting for message") |
| 99 | + } |
| 100 | + |
| 101 | + // test that a message is _not_ received when a link status is updated |
| 102 | + patch = map[string]any{ |
| 103 | + "status": map[string]any{ |
| 104 | + "foo": "bar", |
| 105 | + }, |
| 106 | + } |
| 107 | + patchBytes, err = json.Marshal(patch) |
| 108 | + if err != nil { |
| 109 | + log.Fatalf("Failed to marshal patch: %v", err) |
| 110 | + } |
| 111 | + _, err = l5dAPI.LinkV1alpha2().Links(nsName).Patch( |
| 112 | + context.Background(), |
| 113 | + linkName, |
| 114 | + types.MergePatchType, |
| 115 | + patchBytes, |
| 116 | + metav1.PatchOptions{}, |
| 117 | + "status", |
| 118 | + ) |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("Failed to patch link: %s", err) |
| 121 | + } |
| 122 | + |
| 123 | + select { |
| 124 | + case link := <-results: |
| 125 | + t.Fatalf("Received unexpected message: %v", link) |
| 126 | + case <-time.After(time.Second): |
| 127 | + } |
| 128 | + |
| 129 | + // test that a nil message is received when a link is deleted |
| 130 | + if err := l5dAPI.LinkV1alpha2().Links(nsName).Delete(context.Background(), linkName, metav1.DeleteOptions{}); err != nil { |
| 131 | + t.Fatalf("Failed to delete link: %s", err) |
| 132 | + } |
| 133 | + select { |
| 134 | + case link := <-results: |
| 135 | + if link != nil { |
| 136 | + t.Fatalf("Expected nil, got %v", link) |
| 137 | + } |
| 138 | + case <-time.After(time.Second): |
| 139 | + t.Fatal("Timed out waiting for message") |
| 140 | + } |
| 141 | +} |
0 commit comments