forked from vmware-tanzu/tanzu-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory_plugin_test.go
244 lines (211 loc) · 9.84 KB
/
inventory_plugin_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
// 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-plugin-runtime/config/types"
"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 add", func() {
manifestFile, err := createTestManifestFile()
Expect(err).ToNot(HaveOccurred())
var referencedDBFile string
fakeImgpkgWrapper := &fakes.ImgpkgWrapper{}
iip := InventoryPluginUpdateOptions{
Repository: "test-repo.com",
InventoryImageTag: "latest",
ImgpkgOptions: fakeImgpkgWrapper,
Vendor: "fakevendor",
Publisher: "fakepublisher",
ManifestFile: manifestFile,
}
// 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 { //nolint:unparam
dbFile := filepath.Join(path, plugininventory.SQliteDBFileName)
db := plugininventory.NewSQLiteInventory(dbFile, "")
err := db.CreateSchema()
Expect(err).ToNot(HaveOccurred())
artifacts := make(map[string]distribution.ArtifactList)
artifacts["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",
},
}
entry := &plugininventory.PluginInventoryEntry{
Name: "foo",
Target: "global",
Description: "Foo plugin",
Publisher: "fakepublisher",
Vendor: "fakevendor",
Hidden: false,
Artifacts: artifacts,
}
err = db.InsertPlugin(entry)
Expect(err).ToNot(HaveOccurred())
referencedDBFile = dbFile
return nil
}
var _ = Context("tests for the inventory plugin add function", func() {
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(nil)
fakeImgpkgWrapper.PullImageReturnsOnCall(0, errors.New("unable to pull inventory database"))
err := iip.PluginAdd()
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 plugin inventory database can be pulled from the repository but calculating plugin binary digest fails", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(errors.New("image not found"))
fakeImgpkgWrapper.PullImageCalls(pullDBImageStub)
fakeImgpkgWrapper.GetFileDigestFromImageReturns("", errors.New("error while getting digest"))
err := iip.PluginAdd()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error while getting digest"))
Expect(err.Error()).To(ContainSubstring("error while getting plugin binary digest"))
})
var _ = It("when plugin inventory database can be pulled, plugin binary digest can be calculated by publishing image fails", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(errors.New("image not found"))
fakeImgpkgWrapper.PullImageCalls(pullDBImageStub)
fakeImgpkgWrapper.GetFileDigestFromImageReturns("fake-digest", nil)
err := iip.PluginAdd()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("image not found"))
Expect(err.Error()).To(ContainSubstring("error while publishing inventory database to the repository"))
})
var _ = It("when all configuration are correct and inserting plugin with DeactivatePlugins=false", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(nil)
fakeImgpkgWrapper.PullImageCalls(pullDBImageStub)
fakeImgpkgWrapper.GetFileDigestFromImageReturns("fake-digest", nil)
iip.DeactivatePlugins = false
err := iip.PluginAdd()
Expect(err).NotTo(HaveOccurred())
// verify that the local db file was updated before publishing the database to remote repository
db := plugininventory.NewSQLiteInventory(referencedDBFile, "")
pluginInventoryEntries, err := db.GetAllPlugins()
Expect(err).NotTo(HaveOccurred())
Expect(pluginInventoryEntries).NotTo(BeNil())
Expect(len(pluginInventoryEntries)).To(Equal(1))
Expect(pluginInventoryEntries[0].Name).To(Equal("foo"))
Expect(pluginInventoryEntries[0].Target).To(Equal(types.TargetGlobal))
Expect(pluginInventoryEntries[0].Description).To(Equal("Foo plugin"))
Expect(pluginInventoryEntries[0].Hidden).To(Equal(false))
Expect(pluginInventoryEntries[0].Publisher).To(Equal("fakepublisher"))
Expect(pluginInventoryEntries[0].Vendor).To(Equal("fakevendor"))
Expect(pluginInventoryEntries[0].Artifacts["v0.0.2"]).NotTo(BeNil())
})
var _ = It("when all configuration are correct and inserting plugin with DeactivatePlugins=true", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(nil)
fakeImgpkgWrapper.PullImageCalls(pullDBImageStub)
fakeImgpkgWrapper.GetFileDigestFromImageReturns("fake-digest", nil)
iip.DeactivatePlugins = true
err := iip.PluginAdd()
Expect(err).NotTo(HaveOccurred())
// verify that the local db file was updated before publishing the database to remote repository
db := plugininventory.NewSQLiteInventory(referencedDBFile, "")
pluginInventoryEntries, err := db.GetPlugins(&plugininventory.PluginInventoryFilter{IncludeHidden: true})
Expect(err).NotTo(HaveOccurred())
Expect(pluginInventoryEntries).NotTo(BeNil())
Expect(len(pluginInventoryEntries)).To(Equal(1))
Expect(pluginInventoryEntries[0].Name).To(Equal("foo"))
Expect(pluginInventoryEntries[0].Target).To(Equal(types.TargetGlobal))
Expect(pluginInventoryEntries[0].Description).To(Equal("Foo plugin"))
Expect(pluginInventoryEntries[0].Hidden).To(Equal(true))
Expect(pluginInventoryEntries[0].Artifacts["v0.0.2"]).NotTo(BeNil())
})
})
var _ = Context("tests for the inventory plugin UpdatePluginActivationState function", func() {
var _ = It("when specified pluginInventoryEntry doesn't exist in database", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(nil)
fakeImgpkgWrapper.PullImageCalls(pullDBImageStub)
fakeImgpkgWrapper.GetFileDigestFromImageReturns("fake-digest", nil)
iip.DeactivatePlugins = false
err := iip.UpdatePluginActivationState()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error while updating plugin"))
})
var _ = It("when all configuration are correct and DeactivatePlugins=false", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(nil)
fakeImgpkgWrapper.PullImageCalls(pullDBImageStubWithPlugins)
fakeImgpkgWrapper.GetFileDigestFromImageReturns("fake-digest", nil)
iip.DeactivatePlugins = false
err := iip.UpdatePluginActivationState()
Expect(err).NotTo(HaveOccurred())
// verify that the local db file was updated before publishing the database to remote repository
db := plugininventory.NewSQLiteInventory(referencedDBFile, "")
pluginInventoryEntries, err := db.GetAllPlugins()
Expect(err).NotTo(HaveOccurred())
Expect(pluginInventoryEntries).NotTo(BeNil())
Expect(len(pluginInventoryEntries)).To(Equal(1))
Expect(pluginInventoryEntries[0].Name).To(Equal("foo"))
Expect(pluginInventoryEntries[0].Target).To(Equal(types.TargetGlobal))
Expect(pluginInventoryEntries[0].Description).To(Equal("Foo plugin"))
Expect(pluginInventoryEntries[0].Hidden).To(Equal(false))
Expect(pluginInventoryEntries[0].Artifacts["v0.0.2"]).NotTo(BeNil())
})
var _ = It("when all configuration are correct and inserting plugin with DeactivatePlugins=true", func() {
fakeImgpkgWrapper.ResolveImageReturns(nil)
fakeImgpkgWrapper.PushImageReturns(nil)
fakeImgpkgWrapper.PullImageCalls(pullDBImageStubWithPlugins)
fakeImgpkgWrapper.GetFileDigestFromImageReturns("fake-digest", nil)
iip.DeactivatePlugins = true
err := iip.UpdatePluginActivationState()
Expect(err).NotTo(HaveOccurred())
// verify that the local db file was updated before publishing the database to remote repository
db := plugininventory.NewSQLiteInventory(referencedDBFile, "")
pluginInventoryEntries, err := db.GetPlugins(&plugininventory.PluginInventoryFilter{IncludeHidden: true})
Expect(err).NotTo(HaveOccurred())
Expect(pluginInventoryEntries).NotTo(BeNil())
Expect(len(pluginInventoryEntries)).To(Equal(1))
Expect(pluginInventoryEntries[0].Name).To(Equal("foo"))
Expect(pluginInventoryEntries[0].Target).To(Equal(types.TargetGlobal))
Expect(pluginInventoryEntries[0].Description).To(Equal("Foo plugin"))
Expect(pluginInventoryEntries[0].Hidden).To(Equal(true))
Expect(pluginInventoryEntries[0].Artifacts["v0.0.2"]).NotTo(BeNil())
})
})
})
func createTestManifestFile() (string, error) {
manifestBytes := `created: 2023-02-24T10:10:59.093382-08:00
plugins:
- name: foo
target: global
description: Foo plugin
versions:
- v0.0.2
`
tempManifestFile := filepath.Join(os.TempDir(), "plugin_manifets.yaml")
return filepath.Join(os.TempDir(), "plugin_manifets.yaml"), utils.SaveFile(tempManifestFile, []byte(manifestBytes))
}