forked from getporter/porter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependenciesv2_test.go
235 lines (185 loc) · 10.3 KB
/
dependenciesv2_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
//go:build integration
package integration
import (
"context"
"os"
"path/filepath"
"testing"
"get.porter.sh/porter/pkg/cnab"
"get.porter.sh/porter/pkg/experimental"
"get.porter.sh/porter/pkg/porter"
"get.porter.sh/porter/pkg/storage"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSharedDependencies(t *testing.T) {
t.Parallel()
p := porter.NewTestPorter(t)
defer p.Close()
ctx := p.SetupIntegrationTest()
p.Config.SetExperimentalFlags(experimental.FlagDependenciesV2)
namespace := p.RandomString(10)
bunDir, err := os.MkdirTemp("", "porter-mysql-")
require.NoError(p.T(), err, "could not create temp directory at all")
defer os.RemoveAll(bunDir)
setupWordpress_v2(ctx, p, namespace, bunDir)
upgradeWordpressBundle_v2(ctx, p, namespace)
invokeWordpressBundle_v2(ctx, p, namespace)
uninstallWordpressBundle_v2(ctx, p, namespace)
defer cleanupWordpressBundle_v2(ctx, p, namespace)
}
func setupMysql(ctx context.Context, p *porter.TestPorter, namespace string, bunDir string) {
p.TestConfig.TestContext.AddTestDirectory(filepath.Join(p.RepoRoot, "build/testdata/bundles/mysql"), bunDir+"/mysql")
p.Chdir(bunDir + "/mysql")
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")
installOpts := porter.NewInstallOptions()
installOpts.Namespace = namespace
installOpts.CredentialIdentifiers = []string{"ci"} // Use the ci credential set, porter should remember this for later
err = installOpts.Validate(ctx, []string{}, p.Porter)
require.NoError(p.T(), err, "validation of install opts for shared mysql bundle failed")
err = p.InstallBundle(ctx, installOpts)
require.NoError(p.T(), err, "install of shared mysql bundle failed namespace %s", namespace)
mysqlinst, err := p.Installations.GetInstallation(ctx, namespace, "mysql")
require.NoError(p.T(), err, "could not fetch installation status for the dependency")
//Set the label on the installaiton so Porter knows to grab it
mysqlinst.SetLabel("sh.porter.SharingGroup", "myapp")
err = p.Installations.UpdateInstallation(ctx, mysqlinst)
require.NoError(p.T(), err, "could not add label to mysql inst")
}
func setupWordpress_v2(ctx context.Context, p *porter.TestPorter, namespace string, bunDir string) {
setupMysql(ctx, p, namespace, bunDir)
p.CopyDirectory(filepath.Join(p.RepoRoot, "build/testdata/bundles/wordpressv2"), ".", false)
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")
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,
}
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)
numInst, err := p.Installations.ListInstallations(ctx, storage.ListOptions{Namespace: namespace})
assert.Equal(p.T(), 2, len(numInst))
i, err := p.Installations.GetInstallation(ctx, namespace, "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")
// 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")
}
func cleanupWordpressBundle_v2(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")
// This shouldn't get deleted, it existed before, and it should exist after
i, err := p.Installations.GetInstallation(ctx, namespace, "mysql")
assert.Equal(p.T(), cnab.StatusSucceeded, i.Status.ResultStatus, "the dependency wasn't recorded as being installed successfully")
// 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_v2(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 still installed
// upgrade should not change our status
i, err := p.Installations.GetInstallation(ctx, namespace, "mysql")
require.NoError(p.T(), err, "could not fetch claim for the dependency")
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_v2(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, "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_v2(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 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")
}