-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathglobal_test.go
147 lines (132 loc) · 3.01 KB
/
global_test.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
135
136
137
138
139
140
141
142
143
144
145
146
147
// SPDX-FileCopyrightText: Pachyderm, Inc. <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package helmtest
import (
"bytes"
"fmt"
"io"
"reflect"
"testing"
"github.com/gruntwork-io/terratest/modules/logger"
"gopkg.in/yaml.v3"
goyaml "gopkg.in/yaml.v3"
v1 "k8s.io/api/core/v1"
"k8s.io/kubectl/pkg/scheme"
)
func init() {
logger.Default = logger.Discard
}
// adapted from https://play.golang.org/p/MZNwxdUzxPo
func splitYAML(manifest string) ([]string, error) {
dec := goyaml.NewDecoder(bytes.NewReader([]byte(manifest)))
var res []string
for {
var value yaml.Node
if err := dec.Decode(&value); err == io.EOF {
break
} else if err != nil {
return nil, err
}
//fmt.Printf("%+v\n", value.Content[0].Content[0].HeadComment)
b, err := goyaml.Marshal(&value)
if err != nil {
return nil, err
}
res = append(res, string(b))
}
return res, nil
}
func manifestToObjects(manifest string) ([]interface{}, error) {
files, err := splitYAML(manifest)
if err != nil {
return nil, fmt.Errorf("couldn’t split YAML: %w", err)
}
var objects []interface{}
for i, f := range files {
object, _, err := scheme.Codecs.UniversalDeserializer().Decode([]byte(f), nil, nil)
if err != nil {
return nil, fmt.Errorf("couldn’t decode file %d: %w", i, err)
}
objects = append(objects, object)
}
return objects, nil
}
func TestEnsureVolumeMountPresent(t *testing.T) {
volumeMounts := []v1.VolumeMount{
{
Name: "volmount1",
MountPath: "/my/mount",
},
{
Name: "volmount2",
MountPath: "/my/other/mount",
},
}
want := v1.VolumeMount{
Name: "volmount1",
MountPath: "/my/mount",
}
if !ensureVolumeMountPresent(want, volumeMounts) {
t.Error("Volume mount not found")
}
}
func ensureVolumeMountPresent(matchMount v1.VolumeMount, mounts []v1.VolumeMount) bool {
present := false
for _, vm := range mounts {
if reflect.DeepEqual(matchMount, vm) {
present = true
break
}
}
return present
}
func TestEnsureVolumePresent(t *testing.T) {
volumes := []v1.Volume{
{
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: "A Fine Secret",
},
},
Name: "pachd-tls-cert",
},
{
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: "Another Secret",
},
},
Name: "another-secret",
},
}
want := v1.Volume{
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: "A Fine Secret",
},
},
Name: "pachd-tls-cert",
}
if !ensureVolumePresent(want, volumes) {
t.Error("Volume mount not found")
}
}
func ensureVolumePresent(matchVol v1.Volume, volumes []v1.Volume) bool {
present := false
for _, v := range volumes {
if reflect.DeepEqual(matchVol, v) {
present = true
break
}
}
return present
}
// GetContainerByName returns container or nil
func GetContainerByName(name string, containers []v1.Container) (*v1.Container, bool) {
for _, c := range containers {
if c.Name == name {
return &c, true
}
}
return &v1.Container{}, false
}