File: error_details_tracker_test.go

package info (click to toggle)
gitlab-agent 16.11.5-1
  • links: PTS, VCS
  • area: contrib
  • in suites: experimental
  • size: 7,072 kB
  • sloc: makefile: 193; sh: 55; ruby: 3
file content (252 lines) | stat: -rw-r--r-- 7,958 bytes parent folder | download
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
package agent

import (
	"context"
	"errors"
	"sync"
	"testing"

	"github.com/stretchr/testify/suite"
	rdutil "gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/remote_development/agent/util"
)

type ErrorTrackerTestSuite struct {
	suite.Suite

	tracker *errorDetailsTracker

	testError        error
	testErrorDetails *ErrorDetails
	testWorkspace    string
	testNamespace    string
	testTrackerKey   errorTrackerKey
}

func TestRemoteDevModuleErrorTracker(t *testing.T) {
	suite.Run(t, new(ErrorTrackerTestSuite))
}

func (e *ErrorTrackerTestSuite) TestErrorTracking_ErrorDetailsPublished() {
	ctx := context.Background()

	asyncErrorDetails, unblockPublish := e.newBlockedAsyncErrorDetails(e.testErrorDetails)
	e.NotContains(e.tracker.createSnapshot(), e.testTrackerKey)

	v1 := uint64(1)
	e.tracker.watchForLatestErrors(ctx, e.testWorkspace, e.testNamespace, v1, asyncErrorDetails)

	// since publishing of error is blocked, verify state of tracker
	e.ensureTrackerIsWatchingForErrors(e.testTrackerKey, v1)

	// resume error publish, wait for it to be received and verify state of tracker again
	unblockPublish()
	e.tracker.waitForErrors()

	e.ensureTrackerReceivedErrorDetails(e.testTrackerKey, v1, e.testErrorDetails)
}

func (e *ErrorTrackerTestSuite) TestErrorTracking_NilErrorDetails() {
	ctx := context.Background()

	// blocked value is created so that tracker state can be verified before publish
	asyncErrorDetails, unblockPublish := e.newBlockedAsyncErrorDetails(nil)

	e.NotContains(e.tracker.createSnapshot(), e.testTrackerKey)

	v1 := uint64(1)
	e.tracker.watchForLatestErrors(ctx, e.testWorkspace, e.testNamespace, v1, asyncErrorDetails)

	// since publishing of nil is blocked, verify state of tracker
	e.ensureTrackerIsWatchingForErrors(e.testTrackerKey, v1)

	// unblock the async nil and verify final state of tracker
	unblockPublish()
	e.tracker.waitForErrors()

	e.ensureTrackerIsNotWatchingKey(e.testTrackerKey)
}

func (e *ErrorTrackerTestSuite) TestErrorTracking_NoErrorDetails() {
	ctx := context.Background()

	asyncOp1 := rdutil.RunWithAsyncResult(func(_ chan<- *ErrorDetails) {
		// no errors are published to the write-only channel
	})

	e.NotContains(e.tracker.createSnapshot(), e.testTrackerKey)

	v1 := uint64(1)
	e.tracker.watchForLatestErrors(ctx, e.testWorkspace, e.testNamespace, v1, asyncOp1)

	// wait for errors(if any) and verify final state of tracker
	e.tracker.waitForErrors()

	e.ensureTrackerIsNotWatchingKey(e.testTrackerKey)
}

func (e *ErrorTrackerTestSuite) TestErrorTracking_MultipleErrorDetails() {
	ctx := context.Background()

	err1 := errors.New("applier error 1")
	errDetails1 := newErrorDetails(ErrorTypeApplier, err1.Error())

	err2 := errors.New("applier error 2")
	errDetails2 := newErrorDetails(ErrorTypeApplier, err2.Error())

	asyncOp1 := rdutil.RunWithAsyncResult(func(ch chan<- *ErrorDetails) {
		ch <- errDetails1
		ch <- errDetails2
	})

	v1 := uint64(1)
	e.tracker.watchForLatestErrors(ctx, e.testWorkspace, e.testNamespace, v1, asyncOp1)
	e.tracker.waitForErrors()

	// verify that the received error is the first error
	e.ensureTrackerReceivedErrorDetails(e.testTrackerKey, v1, errDetails1)
}

func (e *ErrorTrackerTestSuite) TestErrorTracking_MultipleNilErrors() {
	ctx := context.Background()

	asyncOp1 := rdutil.RunWithAsyncResult(func(ch chan<- *ErrorDetails) {
		ch <- nil
		ch <- nil
	})

	v1 := uint64(1)

	e.tracker.watchForLatestErrors(ctx, e.testWorkspace, e.testNamespace, v1, asyncOp1)
	e.tracker.waitForErrors()

	e.ensureTrackerIsNotWatchingKey(e.testTrackerKey)
}

// This verifies cases where the tracker watches for errors using a lower version
// after initiating a watch with a higher version for the same workspace / namespace combination
func (e *ErrorTrackerTestSuite) TestErrorTracking_OutOfOrderVersions() {
	ctx := context.Background()

	latestErrorDetails := newErrorDetails(ErrorTypeApplier, "latest error")
	latestAsyncOp, unblockLatest := e.newBlockedAsyncErrorDetails(latestErrorDetails)
	v2 := uint64(2)

	e.tracker.watchForLatestErrors(ctx, e.testWorkspace, e.testNamespace, v2, latestAsyncOp)

	oldErrorDetails := newErrorDetails(ErrorTypeKubernetes, "older error")
	oldAsyncOp, unblockOld := e.newBlockedAsyncErrorDetails(oldErrorDetails)
	v1 := uint64(1)

	e.tracker.watchForLatestErrors(ctx, e.testWorkspace, e.testNamespace, v1, oldAsyncOp)

	// at this point, verify that only entry for version=2 is being tracked
	e.ensureTrackerIsWatchingForErrors(e.testTrackerKey, v2)

	// unblock the publishing of async errors
	unblockOld()
	unblockLatest()
	e.tracker.waitForErrors()

	// ensure that only error details for the latest operation are received
	e.ensureTrackerReceivedErrorDetails(e.testTrackerKey, v2, latestErrorDetails)
}

func (e *ErrorTrackerTestSuite) TestErrorTracking_ConflictingErrorDetails() {
	ctx := context.Background()

	asyncOp1, unblockOp1 := e.newBlockedAsyncErrorDetails(e.testErrorDetails)

	v1 := uint64(1)

	e.tracker.watchForLatestErrors(ctx, e.testWorkspace, e.testNamespace, v1, asyncOp1)

	testErrorDetails2 := newErrorDetails(ErrorTypeKubernetes, "some other error")
	asyncOp2, unblockOp2 := e.newBlockedAsyncErrorDetails(testErrorDetails2)

	v2 := uint64(2)
	e.tracker.watchForLatestErrors(ctx, e.testWorkspace, e.testNamespace, v2, asyncOp2)

	// at this point, the tracker should stop tracking version=1
	e.ensureTrackerIsWatchingForErrors(e.testTrackerKey, v2)

	// unblock the publishing of async errors
	unblockOp1()
	unblockOp2()
	e.tracker.waitForErrors()

	e.ensureTrackerReceivedErrorDetails(e.testTrackerKey, v2, testErrorDetails2)
}

func (e *ErrorTrackerTestSuite) TestErrorTracking_ConflictsWithEventualSuccess() {
	ctx := context.Background()

	failingOp, unblockingFailingOp := e.newBlockedAsyncErrorDetails(e.testErrorDetails)

	v1 := uint64(1)
	e.tracker.watchForLatestErrors(ctx, e.testWorkspace, e.testNamespace, v1, failingOp)

	successfulOp, unblockSuccessfulOp := e.newBlockedAsyncErrorDetails(nil)

	v2 := uint64(2)
	e.tracker.watchForLatestErrors(ctx, e.testWorkspace, e.testNamespace, v2, successfulOp)

	// at this point, the track should stop tracking version=1
	e.ensureTrackerIsWatchingForErrors(e.testTrackerKey, v2)

	// unblock the publishing of async errors
	unblockingFailingOp()
	unblockSuccessfulOp()
	e.tracker.waitForErrors()

	e.ensureTrackerIsNotWatchingKey(e.testTrackerKey)
}

// newBlockedAsyncErrorDetails returns a blocked channel that returns the passed error details once the
// returned unblock function is invoked. The channel is closed automatically after.
func (e *ErrorTrackerTestSuite) newBlockedAsyncErrorDetails(details *ErrorDetails) (result <-chan *ErrorDetails, unblock func()) {
	var wg sync.WaitGroup

	wg.Add(1)

	return rdutil.RunWithAsyncResult[*ErrorDetails](func(ch chan<- *ErrorDetails) {
		wg.Wait()

		ch <- details
	}), wg.Done
}

func (e *ErrorTrackerTestSuite) SetupTest() {
	e.tracker = newErrorDetailsTracker()

	e.testError = errors.New("applier error")
	e.testErrorDetails = newErrorDetails(ErrorTypeApplier, e.testError.Error())
	e.testWorkspace = "test-workspace"
	e.testNamespace = "test-namespace"
	e.testTrackerKey = errorTrackerKey{
		name:      e.testWorkspace,
		namespace: e.testNamespace,
	}
}

func (e *ErrorTrackerTestSuite) ensureTrackerIsWatchingForErrors(key errorTrackerKey, version uint64) {
	snapshot := e.tracker.createSnapshot()
	e.Contains(snapshot, key)
	e.Equal(operationState{
		version:      version,
		errorDetails: nil,
	}, snapshot[key])
}

func (e *ErrorTrackerTestSuite) ensureTrackerReceivedErrorDetails(key errorTrackerKey, version uint64, details *ErrorDetails) {
	snapshot := e.tracker.createSnapshot()
	e.Contains(snapshot, key)
	e.Equal(operationState{
		version:      version,
		errorDetails: details,
	}, snapshot[key])
}

func (e *ErrorTrackerTestSuite) ensureTrackerIsNotWatchingKey(key errorTrackerKey) {
	snapshot := e.tracker.createSnapshot()
	e.NotContains(snapshot, key)
}