1 package cmd
2
3 import (
4 "testing"
5
6 pb "github.com/linkerd/linkerd2-proxy-api/go/destination"
7 "github.com/linkerd/linkerd2/controller/api/util"
8 )
9
10 type endpointsExp struct {
11 options *endpointsOptions
12 authorities []string
13 endpoints []util.AuthorityEndpoints
14 file string
15 }
16
17 func TestEndpoints(t *testing.T) {
18 options := newEndpointsOptions()
19
20 t.Run("Returns endpoints same namespace", func(t *testing.T) {
21 testEndpointsCall(endpointsExp{
22 options: options,
23 authorities: []string{"emoji-svc.emojivoto.svc.cluster.local:8080", "voting-svc.emojivoto.svc.cluster.local:8080"},
24 endpoints: []util.AuthorityEndpoints{
25 {
26 Namespace: "emojivoto",
27 ServiceID: "emoji-svc",
28 Pods: []util.PodDetails{
29 {
30 Name: "emoji-6bf9f47bd5-jjcrl",
31 IP: 16909060,
32 Port: 8080,
33 },
34 },
35 },
36 {
37 Namespace: "emojivoto",
38 ServiceID: "voting-svc",
39 Pods: []util.PodDetails{
40 {
41 Name: "voting-7bf9f47bd5-jjdrl",
42 IP: 84281096,
43 Port: 8080,
44 },
45 },
46 },
47 },
48 file: "endpoints_one_output.golden",
49 }, t)
50 })
51
52 t.Run("Returns endpoints different namespace", func(t *testing.T) {
53 testEndpointsCall(endpointsExp{
54 options: options,
55 authorities: []string{"emoji-svc.emojivoto.svc.cluster.local:8080", "voting-svc.emojivoto2.svc.cluster.local:8080"},
56 endpoints: []util.AuthorityEndpoints{
57 {
58 Namespace: "emojivoto",
59 ServiceID: "emoji-svc",
60 Pods: []util.PodDetails{
61 {
62 Name: "emoji-6bf9f47bd5-jjcrl",
63 IP: 16909060,
64 Port: 8080,
65 },
66 },
67 },
68 {
69 Namespace: "emojivoto2",
70 ServiceID: "voting-svc",
71 Pods: []util.PodDetails{
72 {
73 Name: "voting-7bf9f47bd5-jjdrl",
74 IP: 84281096,
75 Port: 8080,
76 },
77 },
78 },
79 },
80 file: "endpoints_two_outputs.golden",
81 }, t)
82 })
83
84 options.outputFormat = jsonOutput
85 t.Run("Returns endpoints same namespace (json)", func(t *testing.T) {
86 testEndpointsCall(endpointsExp{
87 options: options,
88 authorities: []string{"emoji-svc.emojivoto.svc.cluster.local:8080", "voting-svc.emojivoto.svc.cluster.local:8080"},
89 endpoints: []util.AuthorityEndpoints{
90 {
91 Namespace: "emojivoto",
92 ServiceID: "emoji-svc",
93 Pods: []util.PodDetails{
94 {
95 Name: "emoji-6bf9f47bd5-jjcrl",
96 IP: 16909060,
97 Port: 8080,
98 },
99 },
100 },
101 {
102 Namespace: "emojivoto",
103 ServiceID: "voting-svc",
104 Pods: []util.PodDetails{
105 {
106 Name: "voting-7bf9f47bd5-jjdrl",
107 IP: 84281096,
108 Port: 8080,
109 },
110 },
111 },
112 },
113 file: "endpoints_one_output_json.golden",
114 }, t)
115 })
116 }
117
118 func testEndpointsCall(exp endpointsExp, t *testing.T) {
119 t.Helper()
120
121 updates := make([]pb.Update, 0)
122 for _, endpoint := range exp.endpoints {
123 addrSet := util.BuildAddrSet(endpoint)
124 updates = append(updates, pb.Update{Update: &pb.Update_Add{Add: addrSet}})
125 }
126
127 mockClient := &util.MockAPIClient{
128 DestinationGetClientToReturn: &util.MockDestinationGetClient{
129 UpdatesToReturn: updates,
130 },
131 }
132
133 endpoints, err := requestEndpointsFromAPI(mockClient, "", exp.authorities)
134 if err != nil {
135 t.Fatalf("Unexpected error: %v", err)
136 }
137
138 output := renderEndpoints(endpoints, exp.options)
139
140 testDataDiffer.DiffTestdata(t, exp.file, output)
141 }
142
View as plain text