forked from getporter/porter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependenciesv1_test.go
243 lines (200 loc) · 11 KB
/
dependenciesv1_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//go:build integration
package integration
import (
"context"
"os"
"path/filepath"
"testing"
"get.porter.sh/porter/pkg/cnab"
"get.porter.sh/porter/pkg/porter"
"get.porter.sh/porter/pkg/secrets"
"get.porter.sh/porter/pkg/storage"
"github.com/cnabio/cnab-go/secrets/host"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDependenciesLifecycle(t *testing.T) {
t.Parallel()
p := porter.NewTestPorter(t)
defer p.Close()
ctx := p.SetupIntegrationTest()
namespace := installWordpressBundle(ctx, p)
defer cleanupWordpressBundle(ctx, p, namespace)
upgradeWordpressBundle(ctx, p, namespace)
invokeWordpressBundle(ctx, p, namespace)
uninstallWordpressBundle(ctx, p, namespace)
}
func publishMySQLBundle(ctx context.Context, p *porter.TestPorter) {
bunDir, err := os.MkdirTemp("", "porter-mysql")
require.NoError(p.T(), err, "could not create temp directory to publish the mysql bundle")
defer os.RemoveAll(bunDir)
// Rebuild the bundle from a temp directory so that we don't modify the source directory
// and leave modified files around.
p.TestConfig.TestContext.AddTestDirectory(filepath.Join(p.RepoRoot, "build/testdata/bundles/mysql"), bunDir)
pwd := p.Getwd()
p.Chdir(bunDir)
defer p.Chdir(pwd)
publishOpts := porter.PublishOptions{}
publishOpts.Force = true
err = publishOpts.Validate(p.Config)
require.NoError(p.T(), err, "validation of publish opts for dependent bundle failed")
err = p.Publish(ctx, publishOpts)
require.NoError(p.T(), err, "publish of dependent bundle failed")
}
func installWordpressBundle(ctx context.Context, p *porter.TestPorter) (namespace string) {
// Publish the mysql bundle that we depend upon
publishMySQLBundle(ctx, p)
// Install the bundle that has dependencies
p.CopyDirectory(filepath.Join(p.RepoRoot, "build/testdata/bundles/wordpress"), ".", false)
namespace = p.RandomString(10)
installOpts := porter.NewInstallOptions()
installOpts.Namespace = namespace
installOpts.CredentialIdentifiers = []string{"ci"} // Use the ci credential set, porter should remember this for later
installOpts.Params = []string{
"wordpress-password=mypassword",
"namespace=" + namespace,
"mysql#namespace=" + namespace,
}
// Add a supplemental parameter set to vet dep param resolution
installOpts.ParameterSets = []string{"myparam"}
testParamSets := storage.NewParameterSet(namespace, "myparam", secrets.SourceMap{
Name: "mysql#probe-timeout",
Source: secrets.Source{
Strategy: host.SourceValue,
Hint: "2",
},
})
p.TestParameters.InsertParameterSet(ctx, testParamSets)
err := installOpts.Validate(ctx, []string{}, p.Porter)
require.NoError(p.T(), err, "validation of install opts for root bundle failed")
err = p.InstallBundle(ctx, installOpts)
require.NoError(p.T(), err, "install of root bundle failed namespace %s", namespace)
// Verify that the dependency claim is present
i, err := p.Installations.GetInstallation(ctx, namespace, "wordpress-mysql")
require.NoError(p.T(), err, "could not fetch installation status for the dependency")
assert.Equal(p.T(), cnab.StatusSucceeded, i.Status.ResultStatus, "the dependency wasn't recorded as being installed successfully")
c, err := p.Installations.GetLastRun(ctx, namespace, i.Name)
require.NoError(p.T(), err, "GetLastRun failed")
resolvedParameters, err := p.Sanitizer.RestoreParameterSet(ctx, c.Parameters, cnab.ExtendedBundle{c.Bundle})
require.NoError(p.T(), err, "Resolve run failed")
assert.Equal(p.T(), "porter-ci-mysql", resolvedParameters["mysql-name"], "the dependency param value for 'mysql-name' is incorrect")
assert.Equal(p.T(), 2, resolvedParameters["probe-timeout"], "the dependency param value for 'probe-timeout' is incorrect")
// TODO(carolynvs): compare with last parameters set on the installation once we start tracking that
// Verify that the bundle claim is present
i, err = p.Installations.GetInstallation(ctx, namespace, "wordpress")
require.NoError(p.T(), err, "could not fetch claim for the root bundle")
assert.Equal(p.T(), cnab.StatusSucceeded, i.Status.ResultStatus, "the root bundle wasn't recorded as being installed successfully")
return namespace
}
func cleanupWordpressBundle(ctx context.Context, p *porter.TestPorter, namespace string) {
uninstallOptions := porter.NewUninstallOptions()
uninstallOptions.Namespace = namespace
uninstallOptions.CredentialIdentifiers = []string{"ci"}
uninstallOptions.Delete = true
err := uninstallOptions.Validate(ctx, []string{}, p.Porter)
require.NoError(p.T(), err, "validation of uninstall opts for root bundle failed")
err = p.UninstallBundle(ctx, uninstallOptions)
require.NoError(p.T(), err, "uninstall of root bundle failed")
// Verify that the dependency installation is deleted
i, err := p.Installations.GetInstallation(ctx, namespace, "wordpress-mysql")
require.ErrorIs(p.T(), err, storage.ErrNotFound{})
require.Equal(p.T(), storage.Installation{}, i)
// Verify that the root installation is deleted
i, err = p.Installations.GetInstallation(ctx, namespace, "wordpress")
require.ErrorIs(p.T(), err, storage.ErrNotFound{})
require.Equal(p.T(), storage.Installation{}, i)
}
func upgradeWordpressBundle(ctx context.Context, p *porter.TestPorter, namespace string) {
upgradeOpts := porter.NewUpgradeOptions()
upgradeOpts.Namespace = namespace
// do not specify credential sets, porter should reuse what was specified from install
upgradeOpts.Params = []string{
"wordpress-password=mypassword",
"namespace=" + namespace,
"mysql#namespace=" + namespace,
}
err := upgradeOpts.Validate(ctx, []string{}, p.Porter)
require.NoError(p.T(), err, "validation of upgrade opts for root bundle failed")
err = p.UpgradeBundle(ctx, upgradeOpts)
require.NoError(p.T(), err, "upgrade of root bundle failed")
// Verify that the dependency claim is upgraded
i, err := p.Installations.GetInstallation(ctx, namespace, "wordpress-mysql")
require.NoError(p.T(), err, "could not fetch claim for the dependency")
c, err := p.Installations.GetLastRun(ctx, i.Namespace, i.Name)
require.NoError(p.T(), err, "GetLastClaim failed")
assert.Equal(p.T(), cnab.ActionUpgrade, c.Action, "the dependency wasn't recorded as being upgraded")
assert.Equal(p.T(), cnab.StatusSucceeded, i.Status.ResultStatus, "the dependency wasn't recorded as being upgraded successfully")
// Verify that the bundle claim is upgraded
i, err = p.Installations.GetInstallation(ctx, namespace, "wordpress")
require.NoError(p.T(), err, "could not fetch claim for the root bundle")
c, err = p.Installations.GetLastRun(ctx, i.Namespace, i.Name)
require.NoError(p.T(), err, "GetLastClaim failed")
assert.Equal(p.T(), cnab.ActionUpgrade, c.Action, "the root bundle wasn't recorded as being upgraded")
assert.Equal(p.T(), cnab.StatusSucceeded, i.Status.ResultStatus, "the root bundle wasn't recorded as being upgraded successfully")
// Check that we are using the original credential set specified during install
require.Len(p.T(), i.CredentialSets, 1, "expected only one credential set associated to the installation")
assert.Equal(p.T(), "ci", i.CredentialSets[0], "expected to use the alternate credential set")
}
func invokeWordpressBundle(ctx context.Context, p *porter.TestPorter, namespace string) {
invokeOpts := porter.NewInvokeOptions()
invokeOpts.Namespace = namespace
invokeOpts.Action = "ping"
// Use a different set of creds to run this rando command
invokeOpts.CredentialIdentifiers = []string{"ci2"}
invokeOpts.Params = []string{
"wordpress-password=mypassword",
"namespace=" + namespace,
}
err := invokeOpts.Validate(ctx, []string{}, p.Porter)
require.NoError(p.T(), err, "validation of invoke opts for root bundle failed")
err = p.InvokeBundle(ctx, invokeOpts)
require.NoError(p.T(), err, "invoke of root bundle failed")
// Verify that the dependency claim is invoked
i, err := p.Installations.GetInstallation(ctx, namespace, "wordpress-mysql")
require.NoError(p.T(), err, "could not fetch claim for the dependency")
c, err := p.Installations.GetLastRun(ctx, i.Namespace, i.Name)
require.NoError(p.T(), err, "GetLastClaim failed")
assert.Equal(p.T(), "ping", c.Action, "the dependency wasn't recorded as being invoked")
assert.Equal(p.T(), cnab.StatusSucceeded, i.Status.ResultStatus, "the dependency wasn't recorded as being invoked successfully")
// Verify that the bundle claim is invoked
i, err = p.Installations.GetInstallation(ctx, namespace, "wordpress")
require.NoError(p.T(), err, "could not fetch claim for the root bundle")
c, err = p.Installations.GetLastRun(ctx, i.Namespace, i.Name)
require.NoError(p.T(), err, "GetLastClaim failed")
assert.Equal(p.T(), "ping", c.Action, "the root bundle wasn't recorded as being invoked")
assert.Equal(p.T(), cnab.StatusSucceeded, i.Status.ResultStatus, "the root bundle wasn't recorded as being invoked successfully")
// Check that we are now using the alternate credentials with the bundle
require.Len(p.T(), i.CredentialSets, 1, "expected only one credential set associated to the installation")
assert.Equal(p.T(), "ci2", i.CredentialSets[0], "expected to use the alternate credential set")
}
func uninstallWordpressBundle(ctx context.Context, p *porter.TestPorter, namespace string) {
uninstallOptions := porter.NewUninstallOptions()
uninstallOptions.Namespace = namespace
// Now go back to using the original set of credentials
uninstallOptions.CredentialIdentifiers = []string{"ci"}
uninstallOptions.Params = []string{
"namespace=" + namespace,
"mysql#namespace=" + namespace,
}
err := uninstallOptions.Validate(ctx, []string{}, p.Porter)
require.NoError(p.T(), err, "validation of uninstall opts for root bundle failed")
err = p.UninstallBundle(ctx, uninstallOptions)
require.NoError(p.T(), err, "uninstall of root bundle failed")
// Verify that the dependency claim is uninstalled
i, err := p.Installations.GetInstallation(ctx, namespace, "wordpress-mysql")
require.NoError(p.T(), err, "could not fetch installation for the dependency")
c, err := p.Installations.GetLastRun(ctx, i.Namespace, i.Name)
require.NoError(p.T(), err, "GetLastClaim failed")
assert.Equal(p.T(), cnab.ActionUninstall, c.Action, "the dependency wasn't recorded as being uninstalled")
assert.Equal(p.T(), cnab.StatusSucceeded, i.Status.ResultStatus, "the dependency wasn't recorded as being uninstalled successfully")
// Verify that the bundle claim is uninstalled
i, err = p.Installations.GetInstallation(ctx, namespace, "wordpress")
require.NoError(p.T(), err, "could not fetch installation for the root bundle")
c, err = p.Installations.GetLastRun(ctx, i.Namespace, i.Name)
require.NoError(p.T(), err, "GetLastClaim failed")
assert.Equal(p.T(), cnab.ActionUninstall, c.Action, "the root bundle wasn't recorded as being uninstalled")
assert.Equal(p.T(), cnab.StatusSucceeded, i.Status.ResultStatus, "the root bundle wasn't recorded as being uninstalled successfully")
// Check that we are now using the original credentials with the bundle
require.Len(p.T(), i.CredentialSets, 1, "expected only one credential set associated to the installation")
assert.Equal(p.T(), "ci", i.CredentialSets[0], "expected to use the alternate credential set")
}