...
1 package srv
2
3 import (
4 "net/http"
5 "net/http/httptest"
6 "strings"
7 "testing"
8
9 "github.com/go-test/deep"
10 "github.com/julienschmidt/httprouter"
11 "github.com/linkerd/linkerd2/controller/gen/apis/serviceprofile/v1alpha2"
12 helpers "github.com/linkerd/linkerd2/pkg/profiles"
13 "sigs.k8s.io/yaml"
14 )
15
16 const releaseVersion = "0.3.3"
17
18 func TestHandleIndex(t *testing.T) {
19 server := FakeServer()
20
21 handler := &handler{
22 render: server.RenderTemplate,
23 version: releaseVersion,
24 }
25
26 recorder := httptest.NewRecorder()
27 req := httptest.NewRequest("GET", "/", nil)
28 handler.handleIndex(recorder, req, httprouter.Params{})
29
30 if recorder.Code != http.StatusOK {
31 t.Errorf("Incorrect StatusCode: %+v", recorder.Code)
32 t.Errorf("Expected %+v", http.StatusOK)
33 }
34
35 header := http.Header{
36 "Content-Type": []string{"text/html"},
37 }
38 if diff := deep.Equal(recorder.Header(), header); diff != nil {
39 t.Errorf("Unexpected header: %v", diff)
40 }
41
42 actualBody := recorder.Body.String()
43
44 expectedSubstrings := []string{
45 "<div class=\"main\" id=\"main\"",
46 "data-release-version=\"" + releaseVersion + "\"",
47 "data-controller-namespace=\"\"",
48 "data-uuid=\"\"",
49 }
50 for _, expectedSubstring := range expectedSubstrings {
51 if !strings.Contains(actualBody, expectedSubstring) {
52 t.Fatalf("Expected string [%s] to be present in [%s]", expectedSubstring, actualBody)
53 }
54 }
55 }
56
57 func TestHandleConfigDownload(t *testing.T) {
58 server := FakeServer()
59
60 handler := &handler{
61 render: server.RenderTemplate,
62 controllerNamespace: "linkerd",
63 clusterDomain: "mycluster.local",
64 }
65
66 recorder := httptest.NewRecorder()
67 req := httptest.NewRequest("GET", "/profiles/new?service=authors&namespace=booksns", nil)
68
69 handler.handleProfileDownload(recorder, req, httprouter.Params{})
70
71 if recorder.Code != http.StatusOK {
72 t.Errorf("Incorrect StatusCode: %+v", recorder.Code)
73 t.Errorf("Expected %+v", http.StatusOK)
74 }
75
76 header := http.Header{
77 "Content-Type": []string{
78 "text/yaml",
79 },
80 "Content-Disposition": []string{
81 "attachment; filename=authors-profile.yml",
82 },
83 }
84 if diff := deep.Equal(recorder.Header(), header); diff != nil {
85 t.Errorf("Unexpected header: %v", diff)
86 }
87
88 var serviceProfile v1alpha2.ServiceProfile
89 err := yaml.Unmarshal(recorder.Body.Bytes(), &serviceProfile)
90 if err != nil {
91 t.Fatalf("Error parsing service profile: %v", err)
92 }
93
94 expectedServiceProfile := helpers.GenServiceProfile("authors", "booksns", "mycluster.local")
95
96 err = helpers.ServiceProfileYamlEquals(serviceProfile, expectedServiceProfile)
97 if err != nil {
98 t.Fatalf("ServiceProfiles are not equal: %v", err)
99 }
100 }
101
View as plain text