1
16 package chart
17
18 import (
19 "testing"
20 )
21
22 func TestValidate(t *testing.T) {
23 tests := []struct {
24 name string
25 md *Metadata
26 err error
27 }{
28 {
29 "chart without metadata",
30 nil,
31 ValidationError("chart.metadata is required"),
32 },
33 {
34 "chart without apiVersion",
35 &Metadata{Name: "test", Version: "1.0"},
36 ValidationError("chart.metadata.apiVersion is required"),
37 },
38 {
39 "chart without name",
40 &Metadata{APIVersion: "v2", Version: "1.0"},
41 ValidationError("chart.metadata.name is required"),
42 },
43 {
44 "chart without name",
45 &Metadata{Name: "../../test", APIVersion: "v2", Version: "1.0"},
46 ValidationError("chart.metadata.name \"../../test\" is invalid"),
47 },
48 {
49 "chart without version",
50 &Metadata{Name: "test", APIVersion: "v2"},
51 ValidationError("chart.metadata.version is required"),
52 },
53 {
54 "chart with bad type",
55 &Metadata{Name: "test", APIVersion: "v2", Version: "1.0", Type: "test"},
56 ValidationError("chart.metadata.type must be application or library"),
57 },
58 {
59 "chart without dependency",
60 &Metadata{Name: "test", APIVersion: "v2", Version: "1.0", Type: "application"},
61 nil,
62 },
63 {
64 "dependency with valid alias",
65 &Metadata{
66 Name: "test",
67 APIVersion: "v2",
68 Version: "1.0",
69 Type: "application",
70 Dependencies: []*Dependency{
71 {Name: "dependency", Alias: "legal-alias"},
72 },
73 },
74 nil,
75 },
76 {
77 "dependency with bad characters in alias",
78 &Metadata{
79 Name: "test",
80 APIVersion: "v2",
81 Version: "1.0",
82 Type: "application",
83 Dependencies: []*Dependency{
84 {Name: "bad", Alias: "illegal alias"},
85 },
86 },
87 ValidationError("dependency \"bad\" has disallowed characters in the alias"),
88 },
89 {
90 "same dependency twice",
91 &Metadata{
92 Name: "test",
93 APIVersion: "v2",
94 Version: "1.0",
95 Type: "application",
96 Dependencies: []*Dependency{
97 {Name: "foo", Alias: ""},
98 {Name: "foo", Alias: ""},
99 },
100 },
101 ValidationError("more than one dependency with name or alias \"foo\""),
102 },
103 {
104 "two dependencies with alias from second dependency shadowing first one",
105 &Metadata{
106 Name: "test",
107 APIVersion: "v2",
108 Version: "1.0",
109 Type: "application",
110 Dependencies: []*Dependency{
111 {Name: "foo", Alias: ""},
112 {Name: "bar", Alias: "foo"},
113 },
114 },
115 ValidationError("more than one dependency with name or alias \"foo\""),
116 },
117 {
118
119
120 "same dependency twice with different version",
121 &Metadata{
122 Name: "test",
123 APIVersion: "v2",
124 Version: "1.0",
125 Type: "application",
126 Dependencies: []*Dependency{
127 {Name: "foo", Alias: "", Version: "1.2.3"},
128 {Name: "foo", Alias: "", Version: "1.0.0"},
129 },
130 },
131 ValidationError("more than one dependency with name or alias \"foo\""),
132 },
133 {
134
135
136 "two dependencies with same name but different repos",
137 &Metadata{
138 Name: "test",
139 APIVersion: "v2",
140 Version: "1.0",
141 Type: "application",
142 Dependencies: []*Dependency{
143 {Name: "foo", Repository: "repo-0"},
144 {Name: "foo", Repository: "repo-1"},
145 },
146 },
147 ValidationError("more than one dependency with name or alias \"foo\""),
148 },
149 {
150 "dependencies has nil",
151 &Metadata{
152 Name: "test",
153 APIVersion: "v2",
154 Version: "1.0",
155 Type: "application",
156 Dependencies: []*Dependency{
157 nil,
158 },
159 },
160 ValidationError("dependencies must not contain empty or null nodes"),
161 },
162 {
163 "maintainer not empty",
164 &Metadata{
165 Name: "test",
166 APIVersion: "v2",
167 Version: "1.0",
168 Type: "application",
169 Maintainers: []*Maintainer{
170 nil,
171 },
172 },
173 ValidationError("maintainers must not contain empty or null nodes"),
174 },
175 {
176 "version invalid",
177 &Metadata{APIVersion: "v2", Name: "test", Version: "1.2.3.4"},
178 ValidationError("chart.metadata.version \"1.2.3.4\" is invalid"),
179 },
180 }
181
182 for _, tt := range tests {
183 result := tt.md.Validate()
184 if result != tt.err {
185 t.Errorf("expected %q, got %q in test %q", tt.err, result, tt.name)
186 }
187 }
188 }
189
190 func TestValidate_sanitize(t *testing.T) {
191 md := &Metadata{APIVersion: "v2", Name: "test", Version: "1.0", Description: "\adescr\u0081iption\rtest", Maintainers: []*Maintainer{{Name: "\r"}}}
192 if err := md.Validate(); err != nil {
193 t.Fatalf("unexpected error: %s", err)
194 }
195 if md.Description != "description test" {
196 t.Fatalf("description was not sanitized: %q", md.Description)
197 }
198 if md.Maintainers[0].Name != " " {
199 t.Fatal("maintainer name was not sanitized")
200 }
201 }
202
View as plain text