1 package cmd
2
3 import (
4 "bytes"
5 "errors"
6 "fmt"
7 "testing"
8
9 "github.com/linkerd/linkerd2/controller/gen/apis/serviceprofile/v1alpha2"
10 "github.com/linkerd/linkerd2/pkg/profiles"
11 "sigs.k8s.io/yaml"
12 )
13
14 func TestParseProfile(t *testing.T) {
15 var buf bytes.Buffer
16
17 err := profiles.RenderProfileTemplate("myns", "mysvc", "mycluster.local", &buf, "yaml")
18 if err != nil {
19 t.Fatalf("Error rendering service profile template: %v", err)
20 }
21
22 var serviceProfile v1alpha2.ServiceProfile
23 err = yaml.Unmarshal(buf.Bytes(), &serviceProfile)
24 if err != nil {
25 t.Fatalf("Error parsing service profile: %v", err)
26 }
27
28 expectedServiceProfile := profiles.GenServiceProfile("mysvc", "myns", "mycluster.local")
29
30 err = profiles.ServiceProfileYamlEquals(serviceProfile, expectedServiceProfile)
31 if err != nil {
32 t.Fatalf("ServiceProfiles are not equal: %v", err)
33 }
34 }
35
36 func TestValidateOptions(t *testing.T) {
37 options := newProfileOptions()
38 exp := errors.New("You must specify exactly one of --template or --open-api or --proto")
39 err := options.validate()
40 if err == nil || err.Error() != exp.Error() {
41 t.Fatalf("validateOptions returned unexpected error: %s (expected: %s) for options: %+v", err, exp, options)
42 }
43
44 options = newProfileOptions()
45 options.template = true
46 options.openAPI = "openAPI"
47 exp = errors.New("You must specify exactly one of --template or --open-api or --proto")
48 err = options.validate()
49 if err == nil || err.Error() != exp.Error() {
50 t.Fatalf("validateOptions returned unexpected error: %s (expected: %s) for options: %+v", err, exp, options)
51 }
52
53 options = newProfileOptions()
54 options.template = true
55 exp = errors.New("invalid service \"\": [a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')]")
56 err = options.validate()
57 if err == nil || err.Error() != exp.Error() {
58 t.Fatalf("validateOptions returned unexpected error: %s (expected: %s) for options: %+v", err, exp, options)
59 }
60
61 options = newProfileOptions()
62 options.template = true
63 options.name = "template-name"
64 options.namespace = "default"
65 err = options.validate()
66 if err != nil {
67 t.Fatalf("validateOptions returned unexpected error (%s) for options: %+v", err, options)
68 }
69
70 options = newProfileOptions()
71 options.template = true
72 options.name = "template-name"
73 options.namespace = "namespace-name"
74 err = options.validate()
75 if err != nil {
76 t.Fatalf("validateOptions returned unexpected error (%s) for options: %+v", err, options)
77 }
78
79 options = newProfileOptions()
80 options.openAPI = "openAPI"
81 options.name = "openapi-name"
82 options.namespace = "default"
83 err = options.validate()
84 if err != nil {
85 t.Fatalf("validateOptions returned unexpected error (%s) for options: %+v", err, options)
86 }
87
88 options = newProfileOptions()
89 options.template = true
90 options.name = "service.name"
91 exp = fmt.Errorf("invalid service \"%s\": [a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')]", options.name)
92 err = options.validate()
93 if err == nil || err.Error() != exp.Error() {
94 t.Fatalf("validateOptions returned unexpected error: %s (expected: %s) for options: %+v", err, exp, options)
95 }
96
97 options = newProfileOptions()
98 options.template = true
99 options.name = "invalid/name"
100 exp = fmt.Errorf("invalid service \"%s\": [a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')]", options.name)
101 err = options.validate()
102 if err == nil || err.Error() != exp.Error() {
103 t.Fatalf("validateOptions returned unexpected error: %s (expected: %s) for options: %+v", err, exp, options)
104 }
105
106 serviceName := "service-name"
107
108 options = newProfileOptions()
109 options.template = true
110 options.name = serviceName
111 options.namespace = ""
112 exp = fmt.Errorf("invalid namespace \"%s\": [a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')]", options.namespace)
113 err = options.validate()
114 if err == nil || err.Error() != exp.Error() {
115 t.Fatalf("validateOptions returned unexpected error: %s (expected: %s) for options: %+v", err, exp, options)
116 }
117
118 options = newProfileOptions()
119 options.template = true
120 options.name = serviceName
121 options.namespace = "invalid/namespace"
122 exp = fmt.Errorf("invalid namespace \"%s\": [a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')]", options.namespace)
123 err = options.validate()
124 if err == nil || err.Error() != exp.Error() {
125 t.Fatalf("validateOptions returned unexpected error: %s (expected: %s) for options: %+v", err, exp, options)
126 }
127
128 options = newProfileOptions()
129 options.template = true
130 options.name = serviceName
131 options.namespace = "7eet-ns"
132 err = options.validate()
133 if err != nil {
134 t.Fatalf("validateOptions returned unexpected error (%s) for options: %+v", err, options)
135 }
136
137 options = newProfileOptions()
138 options.template = true
139 options.name = "7eet-svc"
140 exp = fmt.Errorf("invalid service \"%s\": [a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')]", options.name)
141 err = options.validate()
142 if err == nil || err.Error() != exp.Error() {
143 t.Fatalf("validateOptions returned unexpected error: %s (expected: %s) for options: %+v", err, exp, options)
144 }
145 }
146
View as plain text