File: idle_limit_strategy_test.go

package info (click to toggle)
gitlab-ci-multi-runner 14.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 31,248 kB
  • sloc: sh: 1,694; makefile: 384; asm: 79; ruby: 68
file content (302 lines) | stat: -rw-r--r-- 12,780 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
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
//go:build !integration
// +build !integration

package machine

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"

	"gitlab.com/gitlab-org/gitlab-runner/common"
)

type ilsRunnerConfig struct {
	limit           int
	idleCount       int
	idleCountMin    int
	idleScaleFactor float64
	idleTime        int
	maxGrowthRate   int
	maxBuilds       int
}

type ilsMachinesData struct {
	creating int
	idle     int
	used     int
}

type ilsMachineDetails struct {
	state     machineState
	usedCount int
	used      time.Time
}

func ilsNewRunnerConfig(c ilsRunnerConfig) *common.RunnerConfig {
	config := &common.RunnerConfig{
		Limit: c.limit,
	}
	config.Machine = &common.DockerMachine{
		IdleCount:       c.idleCount,
		IdleCountMin:    c.idleCountMin,
		IdleScaleFactor: c.idleScaleFactor,
		IdleTime:        c.idleTime,
		MaxGrowthRate:   c.maxGrowthRate,
		MaxBuilds:       c.maxBuilds,
	}

	return config
}

func ilsNewMachinesData(d ilsMachinesData) *machinesData {
	return &machinesData{
		Creating: d.creating,
		Idle:     d.idle,
		Used:     d.used,
	}
}

func ilsNewMachineDetails(d ilsMachineDetails) *machineDetails {
	return &machineDetails{
		State:     d.state,
		UsedCount: d.usedCount,
		Used:      d.used,
	}
}

func TestCanCreateIdle(t *testing.T) {
	tests := map[string]struct {
		config            ilsRunnerConfig
		data              ilsMachinesData
		expectedCanCreate bool
	}{
		"MaxMachinesGrowth exceeded": {
			config:            ilsRunnerConfig{maxGrowthRate: 10},
			data:              ilsMachinesData{creating: 10},
			expectedCanCreate: false,
		},
		"MaxMachinesGrowth not reached": {
			config:            ilsRunnerConfig{maxGrowthRate: 10},
			data:              ilsMachinesData{creating: 1},
			expectedCanCreate: false,
		},
		"limit exceeded": {
			config:            ilsRunnerConfig{limit: 10},
			data:              ilsMachinesData{creating: 5, idle: 3, used: 2},
			expectedCanCreate: false,
		},
		"limit not reached": {
			config:            ilsRunnerConfig{limit: 10, idleCount: 10},
			data:              ilsMachinesData{idle: 3, used: 2},
			expectedCanCreate: true,
		},
		"IdleCountMin not fulfilled and IdleScaleFactor evaluated to 0": {
			config:            ilsRunnerConfig{idleCount: 100, idleCountMin: 10, idleScaleFactor: 1},
			data:              ilsMachinesData{idle: 0, used: 0},
			expectedCanCreate: true,
		},
		"IdleCountMin not fulfilled and IdleScaleFactor evaluated to non 0 and not reached": {
			config:            ilsRunnerConfig{idleCount: 100, idleCountMin: 10, idleScaleFactor: 1},
			data:              ilsMachinesData{idle: 0, used: 1},
			expectedCanCreate: true,
		},
		"IdleCountMin not fulfilled and IdleScaleFactor evaluated to non 0 and exceeded": {
			config:            ilsRunnerConfig{idleCount: 100, idleCountMin: 10, idleScaleFactor: 1},
			data:              ilsMachinesData{idle: 5, used: 1},
			expectedCanCreate: true,
		},
		"IdleCountMin fulfilled and IdleScaleFactor evaluated to non 0 and not reached": {
			config:            ilsRunnerConfig{idleCount: 100, idleCountMin: 10, idleScaleFactor: 1},
			data:              ilsMachinesData{idle: 10, used: 15},
			expectedCanCreate: true,
		},
		"IdleCountMin fulfilled and IdleScaleFactor evaluated to non 0 and exceeded": {
			config:            ilsRunnerConfig{idleCount: 100, idleCountMin: 10, idleScaleFactor: 1},
			data:              ilsMachinesData{idle: 10, used: 1},
			expectedCanCreate: false,
		},
		"IdleCountMin not set and IdleScaleFactor evaluated to 0 and exceeded": {
			config:            ilsRunnerConfig{idleCount: 100, idleScaleFactor: 1},
			data:              ilsMachinesData{idle: 0, used: 0},
			expectedCanCreate: true,
		},
		"IdleScaleFactor evaluated to non 0 and not reached but IdleCount exceeded": {
			config:            ilsRunnerConfig{idleCount: 10, idleScaleFactor: 1},
			data:              ilsMachinesData{idle: 10, used: 100},
			expectedCanCreate: false,
		},
		"IdleCount exceeded": {
			config:            ilsRunnerConfig{idleCount: 10},
			data:              ilsMachinesData{idle: 10},
			expectedCanCreate: false,
		},
		"IdleCount not reached": {
			config:            ilsRunnerConfig{idleCount: 10},
			data:              ilsMachinesData{idle: 1},
			expectedCanCreate: true,
		},
		// It makes no sense to have the IdleCountMin at the same or higher level than IdleCount.
		// Preparing such configuration would practically remove the functionality added by IdleScaleFactor
		// and revert the IdleCount behavior to be "static number of idle machines to maintain".
		// As preventing that from happening and adding warnings or even errors for such case would
		// complicate the code, I think we can assume that user should understand how IdleCount, IdleCountMin
		// and IdleScaleFactor work together and that this case doesn't make sense.
		// The following two test cases are added to ensure that scaling still works, even when IdleCount and
		// IdleCountMin are messed up.
		"IdleCount exceeded and IdleCountMin not fulfilled": {
			config:            ilsRunnerConfig{idleCount: 5, idleCountMin: 10},
			data:              ilsMachinesData{idle: 8},
			expectedCanCreate: false,
		},
		"IdleCount exceeded and IdleCountMin not fulfilled and IdleScaleFactor evaluated to non 0 and not reached": {
			config:            ilsRunnerConfig{idleCount: 5, idleCountMin: 10, idleScaleFactor: 1},
			data:              ilsMachinesData{idle: 8, used: 9},
			expectedCanCreate: false,
		},
	}

	for tn, tt := range tests {
		t.Run(tn, func(t *testing.T) {
			result := canCreateIdle(ilsNewRunnerConfig(tt.config), ilsNewMachinesData(tt.data))
			assert.Equal(t, tt.expectedCanCreate, result)
		})
	}
}

func TestShouldRemoveIdle(t *testing.T) {
	stubUsedTime := func(seconds int) time.Time {
		return time.Now().Add(time.Duration(seconds) * time.Second)
	}

	tests := map[string]struct {
		config         ilsRunnerConfig
		data           ilsMachinesData
		details        ilsMachineDetails
		expectedReason removeIdleReason
	}{
		"machine not in Idle state": {
			config:         ilsRunnerConfig{},
			data:           ilsMachinesData{},
			details:        ilsMachineDetails{state: machineStateCreating},
			expectedReason: dontRemoveIdleMachine,
		},
		"MaxBuilds exceeded": {
			config:         ilsRunnerConfig{idleCount: 10, maxBuilds: 1},
			data:           ilsMachinesData{idle: 1},
			details:        ilsMachineDetails{state: machineStateIdle, usedCount: 1},
			expectedReason: removeIdleReasonTooManyBuilds,
		},
		"MaxBuilds not reached": {
			config:         ilsRunnerConfig{idleCount: 10, maxBuilds: 10},
			data:           ilsMachinesData{idle: 1},
			details:        ilsMachineDetails{state: machineStateIdle, usedCount: 1},
			expectedReason: dontRemoveIdleMachine,
		},
		"limit exceeded": {
			config:         ilsRunnerConfig{idleCount: 10, limit: 15},
			data:           ilsMachinesData{creating: 5, idle: 5, used: 5},
			details:        ilsMachineDetails{state: machineStateIdle},
			expectedReason: removeIdleReasonTooManyMachines,
		},
		"IdleTime not exceeded and IdleCount not exceeded": {
			config:         ilsRunnerConfig{idleCount: 10, idleTime: 3600},
			data:           ilsMachinesData{idle: 5},
			details:        ilsMachineDetails{state: machineStateIdle, used: stubUsedTime(-60)},
			expectedReason: dontRemoveIdleMachine,
		},
		"IdleTime not exceeded and IdleCount exceeded": {
			config:         ilsRunnerConfig{idleCount: 10, idleTime: 3600},
			data:           ilsMachinesData{idle: 10},
			details:        ilsMachineDetails{state: machineStateIdle, used: stubUsedTime(-60)},
			expectedReason: dontRemoveIdleMachine,
		},
		"IdleTime exceeded and IdleCount not exceeded": {
			config:         ilsRunnerConfig{idleCount: 10, idleTime: 10},
			data:           ilsMachinesData{idle: 5},
			details:        ilsMachineDetails{state: machineStateIdle, used: stubUsedTime(-60)},
			expectedReason: dontRemoveIdleMachine,
		},
		"IdleTime exceeded and IdleCount exceeded": {
			config:         ilsRunnerConfig{idleCount: 10, idleTime: 10},
			data:           ilsMachinesData{idle: 10},
			details:        ilsMachineDetails{state: machineStateIdle, used: stubUsedTime(-60)},
			expectedReason: removeIdleReasonTooManyIdleMachines,
		},
		"IdleTime exceeded and IdleCountMin not fulfilled and IdleScaleFactor evaluated to 0": {
			config:         ilsRunnerConfig{idleCount: 100, idleCountMin: 10, idleScaleFactor: 1, idleTime: 10},
			data:           ilsMachinesData{idle: 0, used: 0},
			details:        ilsMachineDetails{state: machineStateIdle, used: stubUsedTime(-60)},
			expectedReason: dontRemoveIdleMachine,
		},
		"IdleTime exceeded and IdleCountMin not fulfilled and IdleScaleFactor evaluated to non 0 and not reached": {
			config:         ilsRunnerConfig{idleCount: 100, idleCountMin: 10, idleScaleFactor: 1, idleTime: 10},
			data:           ilsMachinesData{idle: 0, used: 1},
			details:        ilsMachineDetails{state: machineStateIdle, used: stubUsedTime(-60)},
			expectedReason: dontRemoveIdleMachine,
		},
		"IdleTime exceeded and IdleCountMin not fulfilled and IdleScaleFactor evaluated to non 0 and exceeded": {
			config:         ilsRunnerConfig{idleCount: 100, idleCountMin: 10, idleScaleFactor: 1, idleTime: 10},
			data:           ilsMachinesData{idle: 5, used: 1},
			details:        ilsMachineDetails{state: machineStateIdle, used: stubUsedTime(-60)},
			expectedReason: dontRemoveIdleMachine,
		},
		"IdleTime exceeded and IdleCountMin fulfilled and IdleScaleFactor evaluated to non 0 and not reached": {
			config:         ilsRunnerConfig{idleCount: 100, idleCountMin: 10, idleScaleFactor: 1, idleTime: 10},
			data:           ilsMachinesData{idle: 10, used: 15},
			details:        ilsMachineDetails{state: machineStateIdle, used: stubUsedTime(-60)},
			expectedReason: dontRemoveIdleMachine,
		},
		"IdleTime exceeded and IdleCountMin fulfilled and IdleScaleFactor evaluated to non 0 and exceeded": {
			config:         ilsRunnerConfig{idleCount: 100, idleCountMin: 10, idleScaleFactor: 1, idleTime: 10},
			data:           ilsMachinesData{idle: 10, used: 1},
			details:        ilsMachineDetails{state: machineStateIdle, used: stubUsedTime(-60)},
			expectedReason: removeIdleReasonTooManyIdleMachines,
		},
		"IdleTime exceeded and IdleCountMin not set and IdleScaleFactor evaluated to 0 and exceeded": {
			config:         ilsRunnerConfig{idleCount: 100, idleScaleFactor: 1, idleTime: 10},
			data:           ilsMachinesData{idle: 0, used: 0},
			details:        ilsMachineDetails{state: machineStateIdle, used: stubUsedTime(-60)},
			expectedReason: dontRemoveIdleMachine,
		},
		"IdleTime exceeded and IdleScaleFactor evaluated to non 0 and not reached but IdleCount exceeded": {
			config:         ilsRunnerConfig{idleCount: 10, idleScaleFactor: 1, idleTime: 10},
			data:           ilsMachinesData{idle: 10, used: 100},
			details:        ilsMachineDetails{state: machineStateIdle, used: stubUsedTime(-60)},
			expectedReason: removeIdleReasonTooManyIdleMachines,
		},
		// It makes no sense to have the IdleCountMin at the same or higher level than IdleCount.
		// Preparing such configuration would practically remove the functionality added by IdleScaleFactor
		// and revert the IdleCount behavior to be "static number of idle machines to maintain".
		// As preventing that from happening and adding warnings or even errors for such case would
		// complicate the code, I think we can assume that user should understand how IdleCount, IdleCountMin
		// and IdleScaleFactor work together and that this case doesn't make sense.
		// The following two test cases are added to ensure that scaling still works, even when IdleCount and
		// IdleCountMin are messed up.
		"IdleTime exceeded and IdleCount exceeded and IdleCountMin not fulfilled": {
			config:         ilsRunnerConfig{idleCount: 5, idleCountMin: 10, idleTime: 10},
			data:           ilsMachinesData{idle: 8},
			details:        ilsMachineDetails{state: machineStateIdle, used: stubUsedTime(-60)},
			expectedReason: removeIdleReasonTooManyIdleMachines,
		},
		"IdleTime exceeded and IdleCount exceeded and IdleCountMin not fulfilled " +
			"and IdleScaleFactor evaluated to non 0 and not reached": {
			config:         ilsRunnerConfig{idleCount: 5, idleCountMin: 10, idleScaleFactor: 1, idleTime: 10},
			data:           ilsMachinesData{idle: 8, used: 9},
			details:        ilsMachineDetails{state: machineStateIdle, used: stubUsedTime(-60)},
			expectedReason: removeIdleReasonTooManyIdleMachines,
		},
	}

	for tn, tt := range tests {
		t.Run(tn, func(t *testing.T) {
			result := shouldRemoveIdle(
				ilsNewRunnerConfig(tt.config),
				ilsNewMachinesData(tt.data),
				ilsNewMachineDetails(tt.details),
			)
			assert.Equal(t, tt.expectedReason, result)
		})
	}
}