/
helpers.go
136 lines (111 loc) · 4.2 KB
/
helpers.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
package tester
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"get.porter.sh/porter/pkg/porter"
"get.porter.sh/porter/pkg/storage"
"get.porter.sh/porter/pkg/yaml"
"get.porter.sh/porter/tests"
"get.porter.sh/porter/tests/testdata"
"github.com/carolynvs/magex/shx"
"github.com/stretchr/testify/require"
)
// PrepareTestBundle ensures that the mybuns test bundle is ready to use.
func (t Tester) PrepareTestBundle() {
// Build and publish an interesting test bundle and its dependency
t.MakeTestBundle(testdata.MyDb, testdata.MyDbRef)
t.MakeTestBundle(testdata.MyBuns, testdata.MyBunsRef)
t.ApplyTestBundlePrerequisites()
}
// ApplyTestBundlePrerequisites ensures that anything required by the test bundle, mybuns, is ready to use.
func (t Tester) ApplyTestBundlePrerequisites() {
// These are environment variables referenced by the mybuns credential set
os.Setenv("USER", "porterci")
os.Setenv("ALT_USER", "porterci2")
t.RequirePorter("parameters", "apply", filepath.Join(t.RepoRoot, "tests/testdata/params/mybuns.yaml"), "--namespace=")
t.RequirePorter("credentials", "apply", filepath.Join(t.RepoRoot, "tests/testdata/creds/mybuns.yaml"), "--namespace=")
}
func (t Tester) MakeTestBundle(name string, ref string) {
// Skip if we've already pushed it for another test
if _, _, err := t.RunPorter("explain", ref); err == nil {
return
}
pwd, _ := os.Getwd()
defer t.Chdir(pwd)
t.Chdir(filepath.Join(t.RepoRoot, "tests/testdata/", name))
// TODO(carolynvs): porter publish detection of needing a build should do this
output, err := shx.OutputS("docker", "inspect", strings.Replace(ref, name, name+"-installer", 1))
if output == "[]" || err != nil {
t.RequirePorter("build")
}
// Rely on the auto build functionality to avoid long slow rebuilds when nothing has changed
t.RequirePorter("publish", "--reference", ref)
}
func (t Tester) ShowInstallation(namespace string, name string) (porter.DisplayInstallation, error) {
stdout, _, err := t.RunPorter("show", name, "--namespace", namespace, "--output=json")
if err != nil {
return porter.DisplayInstallation{}, err
}
var di porter.DisplayInstallation
require.NoError(t.T, json.Unmarshal([]byte(stdout), &di))
return di, nil
}
func (t Tester) RequireInstallationExists(namespace string, name string) porter.DisplayInstallation {
di, err := t.ShowInstallation(namespace, name)
require.NoError(t.T, err)
require.Equal(t.T, name, di.Name, "incorrect installation name")
require.Equal(t.T, namespace, di.Namespace, "incorrect installation namespace")
return di
}
func (t Tester) RequireInstallationNotFound(namespace string, name string) {
_, err := t.ShowInstallation(namespace, name)
t.RequireNotFoundReturned(err)
}
func (t Tester) RequireNotFoundReturned(err error) {
require.Error(t.T, err)
require.Contains(t.T, err.Error(), "not found")
}
func (t Tester) ListInstallations(allNamespaces bool, namespace string, name string, labels []string) ([]porter.DisplayInstallation, error) {
args := []string{
"list",
"--output=json",
"--name", name,
}
if allNamespaces {
args = append(args, "--all-namespaces")
} else {
args = append(args, "--namespace", namespace)
}
for _, l := range labels {
args = append(args, "--label", l)
}
stdout, _, err := t.RunPorter(args...)
if err != nil {
return nil, err
}
var installations []porter.DisplayInstallation
require.NoError(t.T, json.Unmarshal([]byte(stdout), &installations))
return installations, nil
}
func (t Tester) RequireInstallationInList(namespace, name string, list []storage.Installation) storage.Installation {
for _, i := range list {
if i.Namespace == namespace && i.Name == name {
return i
}
}
t.T.Fatalf("expected %s/%s to be in the list of installations", namespace, name)
return storage.Installation{}
}
// EditYaml applies a set of yq transformations to a file.
func (t Tester) EditYaml(path string, transformations ...func(yq *yaml.Editor) error) {
t.TestContext.EditYaml(path, transformations...)
}
// RequireFileMode checks that all files in the specified path match the specifed
// file mode. Uses a glob pattern to match.
func (t *Tester) RequireFileMode(path string, mode os.FileMode) {
if !tests.AssertDirectoryPermissionsEqual(t.T, path, mode) {
t.T.FailNow()
}
}