File: blockio_test.go

package info (click to toggle)
golang-github-intel-goresctrl 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,064 kB
  • sloc: makefile: 19; sh: 15
file content (398 lines) | stat: -rw-r--r-- 12,258 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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright 2019-2021 Intel Corporation. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package blockio

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"github.com/intel/goresctrl/pkg/cgroups"
	"github.com/intel/goresctrl/pkg/testutils"
)

var knownIOSchedulers map[string]bool = map[string]bool{
	"bfq":         true,
	"cfq":         true,
	"deadline":    true,
	"kyber":       true,
	"mq-deadline": true,
	"none":        true,
	"noop":        true,
}

// TestSetConfig: unit tests for SetConfigFromFile(), SetConfigFromData(), and SetConfig().
func TestSetConfig(t *testing.T) {
	initialConf := map[string]cgroups.BlockIOParameters{
		"classname": cgroups.BlockIOParameters{},
	}
	emptyConf := map[string]cgroups.BlockIOParameters{}
	goodConf := map[string]cgroups.BlockIOParameters{
		"goodclass": cgroups.NewBlockIOParameters(),
	}
	classBlockIO = copyConf(initialConf)

	err := SetConfigFromFile("/blockio-test/non-existent-file", true)
	testutils.VerifyError(t, err, 1, []string{"/blockio-test/non-existent-file", "failed to read"})
	testutils.VerifyDeepEqual(t, "effective configuration 1", initialConf, classBlockIO)

	badConfFile := testutils.CreateTempFile(t, "bad config contents.\n")
	emptyConfFile := testutils.CreateTempFile(t, "")
	goodConfFile := testutils.CreateTempFile(t, "Classes:\n  goodclass:\n")
	defer os.Remove(badConfFile)
	defer os.Remove(emptyConfFile)
	defer os.Remove(goodConfFile)

	for syntaxerror := 0; syntaxerror < 4; syntaxerror++ {
		classBlockIO, err = copyConf(initialConf), nil
		switch syntaxerror {
		case 0:
			err = SetConfigFromFile(badConfFile, false)
		case 1:
			err = SetConfigFromFile(badConfFile, true)
		case 2:
			err = SetConfigFromData([]byte("bad config."), false)
		case 3:
			err = SetConfigFromData([]byte("bad config."), true)
		}
		if syntaxerror < 2 {
			testutils.VerifyError(t, err, 1, []string{badConfFile})
		}
		testutils.VerifyError(t, err, 1, []string{"error unmarshaling"})
		testutils.VerifyDeepEqual(t,
			fmt.Sprintf("syntax error configuration %d", syntaxerror),
			initialConf, classBlockIO)
	}

	// Test valid ways to clear (reset) all classes
	for clear := 0; clear < 8; clear++ {
		classBlockIO, err = copyConf(initialConf), nil
		switch clear {
		case 0:
			err = SetConfigFromFile(emptyConfFile, false)
		case 1:
			err = SetConfigFromFile(emptyConfFile, true)
		case 2:
			err = SetConfigFromData([]byte(""), false)
		case 3:
			err = SetConfigFromData([]byte(""), true)
		case 4:
			err = SetConfig(nil, false)
		case 5:
			err = SetConfig(nil, true)
		case 6:
			err = SetConfig(&Config{}, false)
		case 7:
			err = SetConfig(&Config{}, true)
		}
		testutils.VerifyNoError(t, err)
		testutils.VerifyDeepEqual(t,
			fmt.Sprintf("clear conf %d", clear),
			emptyConf, classBlockIO)
	}

	err = SetConfigFromFile(goodConfFile, true)
	testutils.VerifyNoError(t, err)
	testutils.VerifyDeepEqual(t, "ok conf", goodConf, classBlockIO)
}

// copyConf returns a shallow copy of blockio class configuration.
func copyConf(orig map[string]cgroups.BlockIOParameters) map[string]cgroups.BlockIOParameters {
	result := map[string]cgroups.BlockIOParameters{}
	for key, value := range orig {
		result[key] = value
	}
	return result
}

func TestClassNames(t *testing.T) {
	classBlockIO = map[string]cgroups.BlockIOParameters{
		"a": cgroups.BlockIOParameters{},
		"z": cgroups.BlockIOParameters{},
		"b": cgroups.BlockIOParameters{},
		"x": cgroups.BlockIOParameters{},
		"c": cgroups.BlockIOParameters{},
		"d": cgroups.BlockIOParameters{},
	}
	classes := GetClasses()
	testutils.VerifyStringSlices(t,
		[]string{"a", "b", "c", "d", "x", "z"},
		classes)
	classBlockIO = map[string]cgroups.BlockIOParameters{}
	classes = GetClasses()
	testutils.VerifyStringSlices(t,
		[]string{},
		classes)
}

// TestGetCurrentIOSchedulers: unit test for getCurrentIOSchedulers().
func TestGetCurrentIOSchedulers(t *testing.T) {
	currentIOSchedulers, err := getCurrentIOSchedulers()
	testutils.VerifyError(t, err, 0, nil)
	for blockDev, ioScheduler := range currentIOSchedulers {
		s, ok := knownIOSchedulers[ioScheduler]
		if !ok || !s {
			t.Errorf("unknown io scheduler %#v on block device %#v", ioScheduler, blockDev)
		}
	}
}

// TestConfigurableBlockDevices: unit tests for configurableBlockDevices().
func TestConfigurableBlockDevices(t *testing.T) {
	sysfsBlockDevs, err := filepath.Glob("/sys/block/*")
	if err != nil {
		sysfsBlockDevs = []string{}
	}
	devBlockDevs := []string{}
	for _, sysfsBlockDev := range sysfsBlockDevs {
		if strings.HasPrefix(sysfsBlockDev, "/sys/block/sd") || strings.HasPrefix(sysfsBlockDev, "/sys/block/vd") {
			devBlockDevs = append(devBlockDevs, strings.Replace(sysfsBlockDev, "/sys/block/", "/dev/", 1))
		}
	}
	devPartitions := []string{}
	for _, devBlockDev := range devBlockDevs {
		devPartitions, _ = filepath.Glob(devBlockDev + "[0-9]")
		if len(devPartitions) > 0 {
			break
		}
	}
	t.Logf("test real block devices: %v", devBlockDevs)
	t.Logf("test partitions: %v", devPartitions)
	tcases := []struct {
		name                    string
		devWildcards            []string
		expectedErrorCount      int
		expectedErrorSubstrings []string
		expectedMatches         int
		disabled                bool
		disabledReason          string
	}{
		{
			name:               "no device wildcards",
			devWildcards:       nil,
			expectedErrorCount: 0,
		},
		{
			name:                    "bad wildcard",
			devWildcards:            []string{"/[-/verybadwildcard]"},
			expectedErrorCount:      1,
			expectedErrorSubstrings: []string{"verybadwildcard", "syntax error"},
		},
		{
			name:                    "not matching wildcard",
			devWildcards:            []string{"/dev/path that should not exist/*"},
			expectedErrorCount:      1,
			expectedErrorSubstrings: []string{"does not match any"},
		},
		{
			name:                    "two wildcards: empty string and a character device",
			devWildcards:            []string{"/dev/null", ""},
			expectedErrorCount:      2,
			expectedErrorSubstrings: []string{"\"/dev/null\" is a character device", "\"\" does not match any"},
		},
		{
			name:                    "not a device or even a file",
			devWildcards:            []string{"/proc", "/proc/meminfo", "/proc/notexistingfile"},
			expectedErrorCount:      3,
			expectedErrorSubstrings: []string{"\"/proc\" is not a device", "\"/proc/meminfo\" is not a device"},
		},
		{
			name:                    "partition",
			devWildcards:            devPartitions,
			expectedErrorCount:      len(devPartitions),
			expectedErrorSubstrings: []string{"cannot weight/throttle partitions"},
			disabled:                len(devPartitions) == 0,
			disabledReason:          "no block device partitions found",
		},
	}
	for _, tc := range tcases {
		t.Run(tc.name, func(t *testing.T) {
			if tc.disabled {
				t.Skip(tc.disabledReason)
			}
			realPlatform := defaultPlatform{}
			bdis, err := realPlatform.configurableBlockDevices(tc.devWildcards)
			testutils.VerifyError(t, err, tc.expectedErrorCount, tc.expectedErrorSubstrings)
			if len(bdis) != tc.expectedMatches {
				t.Errorf("expected %d matching block devices, got %d", tc.expectedMatches, len(bdis))
			}
		})
	}
}

// TestDevicesParametersToCgBlockIO: unit tests for devicesParametersToCgBlockIO().
func TestDevicesParametersToCgBlockIO(t *testing.T) {
	// switch real devicesParametersToCgBlockIO to call mockPlatform.configurableBlockDevices
	currentPlatform = mockPlatform{}
	tcases := []struct {
		name                    string
		dps                     []DevicesParameters
		iosched                 map[string]string
		expectedOci             *cgroups.BlockIOParameters
		expectedErrorCount      int
		expectedErrorSubstrings []string
	}{
		{
			name: "all OCI fields",
			dps: []DevicesParameters{
				{
					Weight: "144",
				},
				{
					Devices:           []string{"/dev/sda"},
					ThrottleReadBps:   "1G",
					ThrottleWriteBps:  "2M",
					ThrottleReadIOPS:  "3k",
					ThrottleWriteIOPS: "4",
					Weight:            "50",
				},
			},
			iosched: map[string]string{"/dev/sda": "bfq"},
			expectedOci: &cgroups.BlockIOParameters{
				Weight: 144,
				WeightDevice: cgroups.DeviceWeights{
					{Major: 11, Minor: 12, Weight: 50},
				},
				ThrottleReadBpsDevice: cgroups.DeviceRates{
					{Major: 11, Minor: 12, Rate: 1000000000},
				},
				ThrottleWriteBpsDevice: cgroups.DeviceRates{
					{Major: 11, Minor: 12, Rate: 2000000},
				},
				ThrottleReadIOPSDevice: cgroups.DeviceRates{
					{Major: 11, Minor: 12, Rate: 3000},
				},
				ThrottleWriteIOPSDevice: cgroups.DeviceRates{
					{Major: 11, Minor: 12, Rate: 4},
				},
			},
		},
		{
			name: "later match overrides value",
			dps: []DevicesParameters{
				{
					Devices:         []string{"/dev/sda", "/dev/sdb", "/dev/sdc"},
					ThrottleReadBps: "100",
					Weight:          "110",
				},
				{
					Devices:         []string{"/dev/sdb", "/dev/sdc"},
					ThrottleReadBps: "300",
					Weight:          "330",
				},
				{
					Devices:         []string{"/dev/sdb"},
					ThrottleReadBps: "200",
					Weight:          "220",
				},
			},
			iosched: map[string]string{"/dev/sda": "bfq", "/dev/sdb": "bfq", "/dev/sdc": "cfq"},
			expectedOci: &cgroups.BlockIOParameters{
				Weight: -1,
				WeightDevice: cgroups.DeviceWeights{
					{Major: 11, Minor: 12, Weight: 110},
					{Major: 21, Minor: 22, Weight: 220},
					{Major: 31, Minor: 32, Weight: 330},
				},
				ThrottleReadBpsDevice: cgroups.DeviceRates{
					{Major: 11, Minor: 12, Rate: 100},
					{Major: 21, Minor: 22, Rate: 200},
					{Major: 31, Minor: 32, Rate: 300},
				},
			},
		},
		{
			name: "invalid weights, many errors in different parameter sets",
			dps: []DevicesParameters{
				{
					Weight: "99999",
				},
				{
					Devices: []string{"/dev/sda"},
					Weight:  "1",
				},
				{
					Devices: []string{"/dev/sdb"},
					Weight:  "-2",
				},
			},
			expectedErrorCount: 3,
			expectedErrorSubstrings: []string{
				"(99999) bigger than maximum",
				"(1) smaller than minimum",
				"(-2) smaller than minimum",
			},
		},
		{
			name: "throttling without listing Devices",
			dps: []DevicesParameters{
				{
					ThrottleReadBps:   "100M",
					ThrottleWriteIOPS: "20k",
				},
			},
			expectedErrorCount: 1,
			expectedErrorSubstrings: []string{
				"Devices not listed",
				"\"100M\"",
				"\"20k\"",
			},
		},
	}
	for _, tc := range tcases {
		t.Run(tc.name, func(t *testing.T) {
			oci, err := devicesParametersToCgBlockIO(tc.dps, tc.iosched)
			testutils.VerifyError(t, err, tc.expectedErrorCount, tc.expectedErrorSubstrings)
			if tc.expectedOci != nil {
				testutils.VerifyDeepEqual(t, "OCI parameters", *tc.expectedOci, oci)
			}
		})
	}
}

// mockPlatform implements mock versions of platformInterface functions.
type mockPlatform struct{}

// configurableBlockDevices mock always returns a set of block devices.
func (mpf mockPlatform) configurableBlockDevices(devWildcards []string) ([]tBlockDeviceInfo, error) {
	blockDevices := []tBlockDeviceInfo{}
	for _, devWildcard := range devWildcards {
		if devWildcard == "/dev/sda" {
			blockDevices = append(blockDevices, tBlockDeviceInfo{
				Major:   11,
				Minor:   12,
				DevNode: devWildcard,
				Origin:  fmt.Sprintf("from wildcards %v", devWildcard),
			})
		} else if devWildcard == "/dev/sdb" {
			blockDevices = append(blockDevices, tBlockDeviceInfo{
				Major:   21,
				Minor:   22,
				DevNode: devWildcard,
				Origin:  fmt.Sprintf("from wildcards %v", devWildcard),
			})
		} else if devWildcard == "/dev/sdc" {
			blockDevices = append(blockDevices, tBlockDeviceInfo{
				Major:   31,
				Minor:   32,
				DevNode: devWildcard,
				Origin:  fmt.Sprintf("from wildcards %v", devWildcard),
			})
		}
	}
	return blockDevices, nil
}