-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathpatchenvelopes_test.go
236 lines (209 loc) · 5.91 KB
/
patchenvelopes_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
// Copyright (c) 2023 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package msrv_test
import (
"sync"
"testing"
"time"
"github.com/lf-edge/eve/pkg/pillar/base"
"github.com/lf-edge/eve/pkg/pillar/cmd/msrv"
"github.com/lf-edge/eve/pkg/pillar/pubsub"
"github.com/lf-edge/eve/pkg/pillar/types"
"github.com/onsi/gomega"
uuid "github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
)
// TestPatchEnvelopes creates PatchEnvelope then deletes
// one of them and checks of it was properly deleted
func TestPatchEnvelopes(t *testing.T) {
t.Parallel()
g := gomega.NewGomegaWithT(t)
logger := logrus.StandardLogger()
log := base.NewSourceLogObject(logger, "petypes", 1234)
ps := pubsub.New(&pubsub.EmptyDriver{}, logger, log)
peStore := msrv.NewPatchEnvelopes(log, ps)
patch1UUID := "6ba7b810-9dad-11d1-80b4-000000000000"
app1UUID := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
contentU := "6ba7b810-9dad-11d1-80b4-ffffffffffff"
u1, err := uuid.FromString(app1UUID)
g.Expect(err).To(gomega.BeNil())
u2, err := uuid.FromString(contentU)
g.Expect(err).To(gomega.BeNil())
fileSHA := "someFileSha"
f := "some/File/Location.txt"
volumeStatuses := []types.VolumeStatus{
{
VolumeID: u1,
ContentID: u2,
State: types.INSTALLED,
FileLocation: f,
},
}
contentStatuses := []types.ContentTreeStatus{
{
ContentID: u2,
ContentSha256: fileSHA,
},
}
peInfo := []types.PatchEnvelopeInfo{
{
PatchID: patch1UUID,
AllowedApps: []string{app1UUID},
BinaryBlobs: []types.BinaryBlobCompleted{
{
FileName: "TestFileName",
FileSha: "TestFileSha",
FileMetadata: "TestFileMetadata",
URL: "./testurl",
},
},
VolumeRefs: []types.BinaryBlobVolumeRef{
{
FileName: "VolTestFileName",
ImageName: "VolTestImageName",
FileMetadata: "VolTestFileMetadata",
ImageID: app1UUID,
},
},
},
}
peStoreMutex := sync.Mutex{}
go func() {
peStoreMutex.Lock()
defer peStoreMutex.Unlock()
for _, vs := range volumeStatuses {
peStore.UpdateVolumeStatus(vs, false)
peStore.UpdateStateNotificationCh() <- struct{}{}
}
}()
go func() {
peStoreMutex.Lock()
defer peStoreMutex.Unlock()
for _, ct := range contentStatuses {
peStore.UpdateContentTree(ct, false)
peStore.UpdateStateNotificationCh() <- struct{}{}
}
}()
go func() {
peStoreMutex.Lock()
defer peStoreMutex.Unlock()
peStore.UpdateEnvelopes(peInfo)
peStore.UpdateStateNotificationCh() <- struct{}{}
}()
finishedProcessing := make(chan struct{})
go func() {
for {
// Since there's no feedback mechanism to see if the work in
// peStore structure was done we need to wait for it to finish
// There are 3 goroutines changing peStore state:
// one which adds Envelopes with one BinaryBlob one VolumeRef (len(envelopes) > 0)
// one which moves VolumeRef to BinaryBlob (envelopes[0].BinaryBlobs >= 2
// one which adds SHA to BinaryBlob created from VolumeRef (finding blob and comparing SHA)
envelopes := peStore.Get(app1UUID).Envelopes
if len(envelopes) > 0 && len(envelopes[0].BinaryBlobs) >= 2 {
volBlobIdx := types.CompletedBinaryBlobIdxByName(envelopes[0].BinaryBlobs, "VolTestFileName")
if volBlobIdx != -1 && envelopes[0].BinaryBlobs[volBlobIdx].FileSha != "" {
close(finishedProcessing)
return
}
}
time.Sleep(time.Second)
}
}()
deadline := 1 * time.Minute
g.Eventually(func() bool {
select {
case <-finishedProcessing:
return true
case <-time.After(deadline):
return false
}
}, deadline, time.Second).Should(gomega.BeTrue())
g.Expect(peStore.Get(app1UUID).Envelopes).To(gomega.BeEquivalentTo(
[]types.PatchEnvelopeInfo{
{
PatchID: patch1UUID,
AllowedApps: []string{app1UUID},
State: types.PatchEnvelopeStateActive,
BinaryBlobs: []types.BinaryBlobCompleted{
{
FileName: "TestFileName",
FileSha: "TestFileSha",
FileMetadata: "TestFileMetadata",
URL: "./testurl",
},
{
FileName: "VolTestFileName",
//pragma: allowlist nextline secret
FileSha: fileSHA,
FileMetadata: "VolTestFileMetadata",
URL: f,
},
},
VolumeRefs: []types.BinaryBlobVolumeRef{
{
FileName: "VolTestFileName",
ImageName: "VolTestImageName",
FileMetadata: "VolTestFileMetadata",
ImageID: app1UUID,
},
},
},
}))
patch2UUID := "6ba7b810-9dad-11d1-80b4-111111111111"
app2UUID := "00000000-9dad-11d1-80b4-00c04fd430c8"
peInfo = []types.PatchEnvelopeInfo{
{
PatchID: patch2UUID,
AllowedApps: []string{app2UUID},
BinaryBlobs: []types.BinaryBlobCompleted{
{
FileName: "TestFileName",
FileSha: "TestFileSha",
FileMetadata: "TestFileMetadata",
URL: "./testurl",
},
},
},
}
peStore.UpdateEnvelopes(peInfo)
peStore.UpdateStateNotificationCh() <- struct{}{}
finishDeleting := make(chan struct{})
go func() {
for {
// Since there's no feedback mechanism to see if the work in
// peStore structure was done we need to wait for it to finish
envelopes1 := peStore.Get(app1UUID).Envelopes
envelopes2 := peStore.Get(app2UUID).Envelopes
if len(envelopes2) > 0 && len(envelopes1) == 0 {
close(finishDeleting)
return
}
time.Sleep(time.Second)
}
}()
g.Eventually(func() bool {
select {
case <-finishDeleting:
return true
case <-time.After(deadline):
return false
}
}, deadline, time.Second).Should(gomega.BeTrue())
g.Expect(peStore.Get(app2UUID).Envelopes).To(gomega.BeEquivalentTo(
[]types.PatchEnvelopeInfo{
{
PatchID: patch2UUID,
AllowedApps: []string{app2UUID},
State: types.PatchEnvelopeStateActive,
BinaryBlobs: []types.BinaryBlobCompleted{
{
FileName: "TestFileName",
FileSha: "TestFileSha",
FileMetadata: "TestFileMetadata",
URL: "./testurl",
},
},
},
}))
}