1
16 package chart
17
18 import (
19 "encoding/json"
20 "testing"
21
22 "github.com/stretchr/testify/assert"
23 )
24
25 func TestCRDs(t *testing.T) {
26 chrt := Chart{
27 Files: []*File{
28 {
29 Name: "crds/foo.yaml",
30 Data: []byte("hello"),
31 },
32 {
33 Name: "bar.yaml",
34 Data: []byte("hello"),
35 },
36 {
37 Name: "crds/foo/bar/baz.yaml",
38 Data: []byte("hello"),
39 },
40 {
41 Name: "crdsfoo/bar/baz.yaml",
42 Data: []byte("hello"),
43 },
44 {
45 Name: "crds/README.md",
46 Data: []byte("# hello"),
47 },
48 },
49 }
50
51 is := assert.New(t)
52 crds := chrt.CRDs()
53 is.Equal(2, len(crds))
54 is.Equal("crds/foo.yaml", crds[0].Name)
55 is.Equal("crds/foo/bar/baz.yaml", crds[1].Name)
56 }
57
58 func TestSaveChartNoRawData(t *testing.T) {
59 chrt := Chart{
60 Raw: []*File{
61 {
62 Name: "fhqwhgads.yaml",
63 Data: []byte("Everybody to the Limit"),
64 },
65 },
66 }
67
68 is := assert.New(t)
69 data, err := json.Marshal(chrt)
70 if err != nil {
71 t.Fatal(err)
72 }
73
74 res := &Chart{}
75 if err := json.Unmarshal(data, res); err != nil {
76 t.Fatal(err)
77 }
78
79 is.Equal([]*File(nil), res.Raw)
80 }
81
82 func TestMetadata(t *testing.T) {
83 chrt := Chart{
84 Metadata: &Metadata{
85 Name: "foo.yaml",
86 AppVersion: "1.0.0",
87 APIVersion: "v2",
88 Version: "1.0.0",
89 Type: "application",
90 },
91 }
92
93 is := assert.New(t)
94
95 is.Equal("foo.yaml", chrt.Name())
96 is.Equal("1.0.0", chrt.AppVersion())
97 is.Equal(nil, chrt.Validate())
98 }
99
100 func TestIsRoot(t *testing.T) {
101 chrt1 := Chart{
102 parent: &Chart{
103 Metadata: &Metadata{
104 Name: "foo",
105 },
106 },
107 }
108
109 chrt2 := Chart{
110 Metadata: &Metadata{
111 Name: "foo",
112 },
113 }
114
115 is := assert.New(t)
116
117 is.Equal(false, chrt1.IsRoot())
118 is.Equal(true, chrt2.IsRoot())
119 }
120
121 func TestChartPath(t *testing.T) {
122 chrt1 := Chart{
123 parent: &Chart{
124 Metadata: &Metadata{
125 Name: "foo",
126 },
127 },
128 }
129
130 chrt2 := Chart{
131 Metadata: &Metadata{
132 Name: "foo",
133 },
134 }
135
136 is := assert.New(t)
137
138 is.Equal("foo.", chrt1.ChartPath())
139 is.Equal("foo", chrt2.ChartPath())
140 }
141
142 func TestChartFullPath(t *testing.T) {
143 chrt1 := Chart{
144 parent: &Chart{
145 Metadata: &Metadata{
146 Name: "foo",
147 },
148 },
149 }
150
151 chrt2 := Chart{
152 Metadata: &Metadata{
153 Name: "foo",
154 },
155 }
156
157 is := assert.New(t)
158
159 is.Equal("foo/charts/", chrt1.ChartFullPath())
160 is.Equal("foo", chrt2.ChartFullPath())
161 }
162
163 func TestCRDObjects(t *testing.T) {
164 chrt := Chart{
165 Files: []*File{
166 {
167 Name: "crds/foo.yaml",
168 Data: []byte("hello"),
169 },
170 {
171 Name: "bar.yaml",
172 Data: []byte("hello"),
173 },
174 {
175 Name: "crds/foo/bar/baz.yaml",
176 Data: []byte("hello"),
177 },
178 {
179 Name: "crdsfoo/bar/baz.yaml",
180 Data: []byte("hello"),
181 },
182 {
183 Name: "crds/README.md",
184 Data: []byte("# hello"),
185 },
186 },
187 }
188
189 expected := []CRD{
190 {
191 Name: "crds/foo.yaml",
192 Filename: "crds/foo.yaml",
193 File: &File{
194 Name: "crds/foo.yaml",
195 Data: []byte("hello"),
196 },
197 },
198 {
199 Name: "crds/foo/bar/baz.yaml",
200 Filename: "crds/foo/bar/baz.yaml",
201 File: &File{
202 Name: "crds/foo/bar/baz.yaml",
203 Data: []byte("hello"),
204 },
205 },
206 }
207
208 is := assert.New(t)
209 crds := chrt.CRDObjects()
210 is.Equal(expected, crds)
211 }
212
View as plain text