forked from vmware-tanzu/tanzu-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory_plugin_group_test.go
350 lines (308 loc) · 13.7 KB
/
inventory_plugin_group_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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright 2023 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package inventory
import (
"errors"
"os"
"path/filepath"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/vmware-tanzu/tanzu-cli/cmd/plugin/builder/fakes"
"github.com/vmware-tanzu/tanzu-cli/pkg/distribution"
"github.com/vmware-tanzu/tanzu-cli/pkg/plugininventory"
"github.com/vmware-tanzu/tanzu-cli/pkg/utils"
)
var _ = Describe("Unit tests for inventory plugin-group add", func() {
pluginGroupManifestFile, err := createTestPluginGroupManifestFile()
Expect(err).ToNot(HaveOccurred())
var referencedDBFile string
var ipgu InventoryPluginGroupUpdateOptions
fakeImgpkgWrapper := &fakes.ImgpkgWrapper{}
// pullDBImageStub create new empty database with the table schemas created
pullDBImageStub := func(image, path string) error { //nolint:unparam
dbFile := filepath.Join(path, plugininventory.SQliteDBFileName)
db := plugininventory.NewSQLiteInventory(dbFile, "")
err := db.CreateSchema()
Expect(err).ToNot(HaveOccurred())
referencedDBFile = dbFile
return nil
}
// pullDBImageStubWithPlugins create new database with the table schemas and foo plugin
pullDBImageStubWithPlugins := func(image, path string) error {
err := pullDBImageStub(image, path)
if err != nil {
return err
}
db := plugininventory.NewSQLiteInventory(referencedDBFile, "")
artifactsFoo := make(map[string]distribution.ArtifactList)
artifactsFoo["v0.0.2"] = []distribution.Artifact{
{
OS: "darwin",
Arch: "amd64",
Digest: "fake-digest",
Image: "fake-uri",
},
{
OS: "linux",
Arch: "amd64",
Digest: "fake-digest",
Image: "fake-uri",
},
}
artifactsBar := make(map[string]distribution.ArtifactList)
artifactsBar["v0.0.3"] = []distribution.Artifact{
{
OS: "darwin",
Arch: "amd64",
Digest: "fake-digest",
Image: "fake-uri",
},
{
OS: "linux",
Arch: "amd64",
Digest: "fake-digest",
Image: "fake-uri",
},
}
entryFoo := &plugininventory.PluginInventoryEntry{
Name: "foo",
Target: "global",
Description: "Foo plugin",
Publisher: "fakepublisher",
Vendor: "fakevendor",
Hidden: false,
Artifacts: artifactsFoo,
}
entryBar := &plugininventory.PluginInventoryEntry{
Name: "bar",
Target: "mission-control",
Description: "Bar plugin",
Publisher: "fakepublisher",
Vendor: "fakevendor",
Hidden: false,
Artifacts: artifactsBar,
}
err = db.InsertPlugin(entryFoo)
Expect(err).ToNot(HaveOccurred())
err = db.InsertPlugin(entryBar)
Expect(err).ToNot(HaveOccurred())
return nil
}
// pullDBImageStubWithPluginGroups create new database with the table schemas and plugin groups
pullDBImageStubWithPluginGroups := func(image, path string) error {
err := pullDBImageStubWithPlugins(image, path)
if err != nil {
return err
}
db := plugininventory.NewSQLiteInventory(referencedDBFile, "")
pgEntry := plugininventory.PluginGroup{
Vendor: "fakevendor",
Publisher: "fakepublisher",
Name: "v1.0.0",
Hidden: false,
Plugins: []*plugininventory.PluginGroupPluginEntry{
{
PluginIdentifier: plugininventory.PluginIdentifier{Name: "foo", Target: "global", Version: "v0.0.2"},
Mandatory: false,
},
},
}
err = db.InsertPluginGroup(&pgEntry, false)
Expect(err).ToNot(HaveOccurred())
return nil
}
var _ = Context("tests for the inventory plugin-group add function", func() {
BeforeEach(func() {
ipgu = InventoryPluginGroupUpdateOptions{
Repository: "test-repo.com",
InventoryImageTag: "latest",
ImgpkgOptions: fakeImgpkgWrapper,
Vendor: "fakevendor",
Publisher: "fakepublisher",
PluginGroupManifestFile: pluginGroupManifestFile,
GroupName: "v1.0.0",
DeactivatePluginGroup: false,
Override: false,
}
})
var _ = It("when plugin inventory database cannot be pulled from the repository", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(errors.New("image not found"))
fakeImgpkgWrapper.PullImageReturns(errors.New("unable to pull inventory database"))
err := ipgu.PluginGroupAdd()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error while pulling database from the image"))
Expect(err.Error()).To(ContainSubstring("unable to pull inventory database"))
})
var _ = It("when specified manifest file doesn't exists, adding plugin group should throw error", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(errors.New("image not found"))
fakeImgpkgWrapper.PullImageCalls(pullDBImageStub)
ipgu.PluginGroupManifestFile = "does-not-exists.yaml"
err := ipgu.PluginGroupAdd()
Expect(referencedDBFile).NotTo(BeEmpty())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error while reading plugin group"))
})
var _ = It("when specified plugins in the plugin-group doesn't exist in the inventory database, adding plugin group should throw error", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(nil)
fakeImgpkgWrapper.PullImageCalls(pullDBImageStub)
err := ipgu.PluginGroupAdd()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error while inserting plugin group"))
Expect(err.Error()).To(ContainSubstring("specified plugin"))
Expect(err.Error()).To(ContainSubstring("not present in the database"))
})
var _ = It("when specified plugins exists and the plugin-group doesn't exist in the inventory database, adding plugin group should be successful", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(nil)
fakeImgpkgWrapper.PullImageCalls(pullDBImageStubWithPlugins)
err := ipgu.PluginGroupAdd()
Expect(err).NotTo(HaveOccurred())
// verify that the local db file was updated correctly before publishing the database to remote repository
db := plugininventory.NewSQLiteInventory(referencedDBFile, "")
pgEntries, err := db.GetPluginGroups(plugininventory.PluginGroupFilter{})
Expect(err).NotTo(HaveOccurred())
Expect(pgEntries).NotTo(BeNil())
Expect(len(pgEntries)).To(Equal(1))
Expect(pgEntries[0].Name).To(Equal("v1.0.0"))
Expect(pgEntries[0].Publisher).To(Equal("fakepublisher"))
Expect(pgEntries[0].Vendor).To(Equal("fakevendor"))
Expect(pgEntries[0].Hidden).To(Equal(ipgu.DeactivatePluginGroup))
Expect(len(pgEntries[0].Plugins)).To(Equal(2))
})
var _ = It("when specified plugin-group already exist in the inventory database and override is not provided, adding plugin group should throw error", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(nil)
fakeImgpkgWrapper.PullImageCalls(pullDBImageStubWithPluginGroups)
err := ipgu.PluginGroupAdd()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error while inserting plugin group"))
Expect(err.Error()).To(ContainSubstring("unable to insert plugin-group"))
Expect(err.Error()).To(ContainSubstring("NIQUE constraint failed"))
})
var _ = It("when specified plugin-group already exist in the inventory database and override is provided, adding plugin group should be successful", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(nil)
fakeImgpkgWrapper.PullImageCalls(pullDBImageStubWithPluginGroups)
ipgu.Override = true
ipgu.DeactivatePluginGroup = false
err := ipgu.PluginGroupAdd()
Expect(err).NotTo(HaveOccurred())
// verify that the local db file was updated correctly before publishing the database to remote repository
db := plugininventory.NewSQLiteInventory(referencedDBFile, "")
pgEntries, err := db.GetPluginGroups(plugininventory.PluginGroupFilter{})
Expect(err).NotTo(HaveOccurred())
Expect(pgEntries).NotTo(BeNil())
Expect(len(pgEntries)).To(Equal(1))
Expect(pgEntries[0].Name).To(Equal("v1.0.0"))
Expect(pgEntries[0].Publisher).To(Equal("fakepublisher"))
Expect(pgEntries[0].Vendor).To(Equal("fakevendor"))
Expect(pgEntries[0].Hidden).To(Equal(ipgu.DeactivatePluginGroup))
Expect(len(pgEntries[0].Plugins)).To(Equal(2))
})
var _ = It("when inventory database cannot be published from the repository", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(errors.New("unable to publish image"))
fakeImgpkgWrapper.PullImageCalls(pullDBImageStubWithPlugins)
err := ipgu.PluginGroupAdd()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error while publishing inventory database to the repository as image"))
Expect(err.Error()).To(ContainSubstring("unable to publish image"))
})
})
var _ = Context("tests for the inventory plugin-group UpdatePluginGroupActivationState function", func() {
BeforeEach(func() {
ipgu = InventoryPluginGroupUpdateOptions{
Repository: "test-repo.com",
InventoryImageTag: "latest",
ImgpkgOptions: fakeImgpkgWrapper,
Vendor: "fakevendor",
Publisher: "fakepublisher",
GroupName: "v1.0.0",
DeactivatePluginGroup: false,
}
})
var _ = It("when plugin inventory database cannot be pulled from the repository", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(errors.New("image not found"))
fakeImgpkgWrapper.PullImageReturns(errors.New("unable to pull inventory database"))
err := ipgu.UpdatePluginGroupActivationState()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error while pulling database from the image"))
Expect(err.Error()).To(ContainSubstring("unable to pull inventory database"))
})
var _ = It("when specified plugin-group doesn't exist in the inventory database, updating the activation state should throw error", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(nil)
fakeImgpkgWrapper.PullImageCalls(pullDBImageStub)
err := ipgu.UpdatePluginGroupActivationState()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error while updating activation state of plugin group"))
Expect(err.Error()).To(ContainSubstring("unable to update plugin-group 'fakevendor-fakepublisher/v1.0.0'. This might be possible because the provided plugin-group doesn't exists"))
})
var _ = It("when specified plugin-group exists in the inventory database, updating the activation state with 'DeactivatePluginGroup=true' should be successful", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(nil)
fakeImgpkgWrapper.PullImageCalls(pullDBImageStubWithPluginGroups)
ipgu.DeactivatePluginGroup = true
err := ipgu.UpdatePluginGroupActivationState()
Expect(err).NotTo(HaveOccurred())
// verify that the local db file was updated correctly before publishing the database to remote repository
db := plugininventory.NewSQLiteInventory(referencedDBFile, "")
pgEntries, err := db.GetPluginGroups(plugininventory.PluginGroupFilter{IncludeHidden: true})
Expect(err).NotTo(HaveOccurred())
Expect(pgEntries).NotTo(BeNil())
Expect(len(pgEntries)).To(Equal(1))
Expect(pgEntries[0].Name).To(Equal("v1.0.0"))
Expect(pgEntries[0].Publisher).To(Equal("fakepublisher"))
Expect(pgEntries[0].Vendor).To(Equal("fakevendor"))
Expect(pgEntries[0].Hidden).To(Equal(ipgu.DeactivatePluginGroup))
Expect(len(pgEntries[0].Plugins)).To(Equal(1))
})
var _ = It("when specified plugin-group exists in the inventory database, updating the activation state with 'DeactivatePluginGroup=false' should be successful", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(nil)
fakeImgpkgWrapper.PullImageCalls(pullDBImageStubWithPluginGroups)
ipgu.DeactivatePluginGroup = false
err := ipgu.UpdatePluginGroupActivationState()
Expect(err).NotTo(HaveOccurred())
// verify that the local db file was updated correctly before publishing the database to remote repository
db := plugininventory.NewSQLiteInventory(referencedDBFile, "")
pgEntries, err := db.GetPluginGroups(plugininventory.PluginGroupFilter{})
Expect(err).NotTo(HaveOccurred())
Expect(pgEntries).NotTo(BeNil())
Expect(len(pgEntries)).To(Equal(1))
Expect(pgEntries[0].Name).To(Equal("v1.0.0"))
Expect(pgEntries[0].Publisher).To(Equal("fakepublisher"))
Expect(pgEntries[0].Vendor).To(Equal("fakevendor"))
Expect(pgEntries[0].Hidden).To(Equal(ipgu.DeactivatePluginGroup))
Expect(len(pgEntries[0].Plugins)).To(Equal(1))
})
var _ = It("when inventory database cannot be published from the repository", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(errors.New("unable to publish image"))
fakeImgpkgWrapper.PullImageCalls(pullDBImageStubWithPluginGroups)
err := ipgu.UpdatePluginGroupActivationState()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error while publishing inventory database to the repository as image"))
Expect(err.Error()).To(ContainSubstring("unable to publish image"))
})
})
})
func createTestPluginGroupManifestFile() (string, error) {
manifestBytes := `created: 2023-02-24T10:10:59.093382-08:00
plugins:
- name: foo
target: global
scope: Standalone
version: v0.0.2
- name: bar
target: mission-control
scope: Context
version: v0.0.3
`
tempManifestFile := filepath.Join(os.TempDir(), "plugin_group_manifets.yaml")
return filepath.Join(os.TempDir(), "plugin_group_manifets.yaml"), utils.SaveFile(tempManifestFile, []byte(manifestBytes))
}