1 package skipports
2
3 import (
4 "context"
5 "fmt"
6 "os"
7 "testing"
8 "time"
9
10 "github.com/linkerd/linkerd2/testutil"
11 "github.com/linkerd/linkerd2/testutil/prommatch"
12 )
13
14 var TestHelper *testutil.TestHelper
15
16 var (
17 skipPortsNs = "skip-ports-test"
18 emojivotoDeployments = []string{"emoji", "vote-bot", "voting", "web"}
19 )
20
21 func secureRequestMatcher(dst string) *prommatch.Matcher {
22 return prommatch.NewMatcher("request_total",
23 prommatch.Labels{
24 "direction": prommatch.Equals("outbound"),
25 "tls": prommatch.Equals("true"),
26 "dst_service": prommatch.Equals(dst),
27 })
28 }
29
30 func insecureRequestMatcher(dst string) *prommatch.Matcher {
31 return prommatch.NewMatcher("request_total",
32 prommatch.Labels{
33 "direction": prommatch.Equals("outbound"),
34 "tls": prommatch.Equals("no_identity"),
35 "dst_service": prommatch.Equals(dst),
36 })
37 }
38
39 func TestMain(m *testing.M) {
40 TestHelper = testutil.NewTestHelper()
41
42 TestHelper.WaitUntilDeployReady(testutil.LinkerdDeployReplicasEdge)
43 os.Exit(m.Run())
44 }
45
46
47
48
49
50 func TestSkipInboundPorts(t *testing.T) {
51
52 if os.Getenv("RUN_ARM_TEST") != "" {
53 t.Skip("Skipping Skip Inbound Ports test. TODO: Build multi-arch emojivoto")
54 }
55
56 ctx := context.Background()
57 TestHelper.WithDataPlaneNamespace(ctx, skipPortsNs, nil, t, func(t *testing.T, ns string) {
58 out, err := TestHelper.LinkerdRun("inject", "--manual", "testdata/skip_ports_application.yaml")
59 if err != nil {
60 testutil.AnnotatedFatal(t, "'linkerd inject' command failed", err)
61 }
62 out, err = TestHelper.KubectlApply(out, ns)
63 if err != nil {
64 testutil.AnnotatedFatalf(t, "'kubectl apply' command failed",
65 "'kubectl apply' command failed\n%s", out)
66 }
67
68
69 for _, deploy := range emojivotoDeployments {
70 if err := TestHelper.CheckPods(ctx, ns, deploy, 1); err != nil {
71
72 if rce, ok := err.(*testutil.RestartCountError); ok {
73 testutil.AnnotatedWarn(t, "CheckPods timed-out", rce)
74 } else {
75 testutil.AnnotatedError(t, "CheckPods timed-out", err)
76 }
77 }
78 }
79
80 t.Run("check webapp metrics", func(t *testing.T) {
81
82
83 err := testutil.RetryFor(30*time.Second, func() error {
84 pods, err := TestHelper.GetPods(ctx, ns, map[string]string{"app": "web-svc"})
85 if err != nil {
86 return fmt.Errorf("error getting pods\n%w", err)
87 }
88
89 podName := fmt.Sprintf("pod/%s", pods[0].Name)
90 cmd := []string{"diagnostics", "proxy-metrics", "--namespace", ns, podName}
91
92 metrics, err := TestHelper.LinkerdRun(cmd...)
93 if err != nil {
94 return fmt.Errorf("error getting metrics for pod\n%w", err)
95 }
96 s := prommatch.Suite{}.
97 MustContain("secure requests to emoji-svc", secureRequestMatcher("emoji-svc")).
98 MustContain("insecure requests to voting-svc", insecureRequestMatcher("voting-svc")).
99 MustNotContain("insecure requests to emoji-svc", insecureRequestMatcher("emoji-svc")).
100 MustNotContain("secure requests to voting-svc", secureRequestMatcher("voting-svc"))
101 if err := s.CheckString(metrics); err != nil {
102 return fmt.Errorf("error matching metrics\n%w", err)
103 }
104 return nil
105 })
106
107 if err != nil {
108 testutil.AnnotatedFatalf(t, "unexpected error", "unexpected error: %v", err)
109 }
110 })
111 })
112 }
113
View as plain text