1 package cmd
2
3 import (
4 "testing"
5
6 api "github.com/linkerd/linkerd2/viz/metrics-api"
7 )
8
9 type routesParamsExp struct {
10 options *routesOptions
11 routes []string
12 counts []uint64
13 file string
14 }
15
16 func TestRoutes(t *testing.T) {
17 options := newRoutesOptions()
18 t.Run("Returns route stats", func(t *testing.T) {
19 testRoutesCall(routesParamsExp{
20 routes: []string{"/a", "/b", "/c"},
21 counts: []uint64{90, 60, 0, 30},
22 options: options,
23 file: "routes_one_output.golden",
24 }, t)
25 })
26
27 options.outputFormat = jsonOutput
28 t.Run("Returns route stats (json)", func(t *testing.T) {
29 testRoutesCall(routesParamsExp{
30 routes: []string{"/a", "/b", "/c"},
31 counts: []uint64{90, 60, 0, 30},
32 options: options,
33 file: "routes_one_output_json.golden",
34 }, t)
35 })
36
37 wideOptions := newRoutesOptions()
38 wideOptions.toResource = "deploy/bar"
39 wideOptions.outputFormat = wideOutput
40 t.Run("Returns wider route stats", func(t *testing.T) {
41 testRoutesCall(routesParamsExp{
42 routes: []string{"/a", "/b", "/c"},
43 counts: []uint64{90, 60, 0, 30},
44 options: wideOptions,
45 file: "routes_one_output_wide.golden",
46 }, t)
47 })
48 }
49
50 func testRoutesCall(exp routesParamsExp, t *testing.T) {
51 mockClient := &api.MockAPIClient{}
52
53 response := api.GenTopRoutesResponse(exp.routes, exp.counts, exp.options.toResource != "", "foobar")
54
55 mockClient.TopRoutesResponseToReturn = response
56
57 req, err := buildTopRoutesRequest("deploy/foobar", exp.options)
58 if err != nil {
59 t.Fatalf("Unexpected error: %v", err)
60 }
61
62 output, err := requestRouteStatsFromAPI(mockClient, req, exp.options)
63 if err != nil {
64 t.Fatalf("Unexpected error: %v", err)
65 }
66
67 testDataDiffer.DiffTestdata(t, exp.file, output)
68 }
69
View as plain text