This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
/
test_util.go
134 lines (117 loc) · 2.79 KB
/
test_util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package helm
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"testing"
"helm.sh/helm/v3/pkg/chart"
"github.com/keptn-contrib/helm-service/pkg/objectutils"
"github.com/kinbiko/jsonassert"
"github.com/stretchr/testify/assert"
kyaml "k8s.io/apimachinery/pkg/util/yaml"
)
func check(e error, t *testing.T) {
if e != nil {
t.Error(e)
}
}
type GeneratedResource struct {
URI string
FileContent []string
}
func Equals(actual *chart.Chart, valuesExpected GeneratedResource, templatesExpected []GeneratedResource, t *testing.T) {
// Compare values
jsonData, err := json.Marshal(actual.Values)
if err != nil {
t.Error(err)
}
ja := jsonassert.New(t)
ja.Assertf(string(jsonData), valuesExpected.FileContent[0])
for _, resource := range templatesExpected {
reader := ioutil.NopCloser(bytes.NewReader(GetTemplateByName(actual, resource.URI).Data))
decoder := kyaml.NewDocumentDecoder(reader)
for i := 0; ; i++ {
b1 := make([]byte, 4096)
n1, err := decoder.Read(b1)
if err == io.EOF {
break
}
assert.Nil(t, err, "")
jsonData, err := objectutils.ToJSON(b1[:n1])
if err != nil {
t.Error(err)
}
ja := jsonassert.New(t)
ja.Assertf(string(jsonData), resource.FileContent[i])
}
}
}
func GetTemplateByName(chart *chart.Chart, templateName string) *chart.File {
for _, template := range chart.Templates {
if template.Name == templateName {
return template
}
}
return nil
}
// GetTestGeneratedChart returns a sample chart representing a "generated-chart"
func GetTestGeneratedChart() chart.Chart {
return chart.Chart{
Raw: nil,
Metadata: &chart.Metadata{
Name: "carts-generated",
Version: "0.1.0",
APIVersion: "v2",
},
Lock: nil,
Templates: []*chart.File{
{
Name: "carts-canary-istio-destinationrule.yaml",
Data: []byte(GeneratedCanaryDestinationRule),
},
{
Name: "carts-canary-service.yaml",
Data: []byte(GeneratedCanaryService),
},
{
Name: "carts-istio-virtualservice.yaml",
Data: []byte(GeneratedVirtualService),
},
{
Name: "carts-primary-deployment.yaml",
Data: []byte(GeneratedPrimaryDeployment),
},
{
Name: "carts-primary-istio-destinationrule.yaml",
Data: []byte(GeneratedPrimaryDestinationRule),
},
{
Name: "carts-primary-service.yaml",
Data: []byte(GeneratedPrimaryService),
},
},
}
}
// GetTestUserChart returns a sample chart representing a chart provided by the user
func GetTestUserChart() chart.Chart {
return chart.Chart{
Raw: nil,
Metadata: &chart.Metadata{
Name: "carts",
Version: "0.1.0",
APIVersion: "v2",
},
Lock: nil,
Templates: []*chart.File{
{
Name: "carts-service.yaml",
Data: []byte(userService),
},
{
Name: "carts-deployment.yaml",
Data: []byte(userDeployment),
},
},
}
}