File: util_test.go

package info (click to toggle)
incus 6.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 24,428 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (66 lines) | stat: -rw-r--r-- 1,704 bytes parent folder | download | duplicates (3)
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
package drivers

import (
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/lxc/incus/v6/internal/server/instance/drivers/cfg"
)

// Test roundDownToBlockSize.
func TestRoundDownToBlockSize(t *testing.T) {
	const blockSize = 128 * 1024 * 1024

	value := roundDownToBlockSize(1073741824, blockSize)
	assert.Equal(t, int64(1073741824), value)

	value = roundDownToBlockSize(1000000000, blockSize)
	assert.Equal(t, int64(805306368), value)
}

// Test memoryConfigSectionToMap.
func TestMemoryConfigSectionToMap(t *testing.T) {
	result := memoryConfigSectionToMap(
		&cfg.Section{
			Name: "object \"mem0\"",
			Entries: map[string]string{
				"size":         "1024M",
				"host-nodes.0": "0",
				"host-nodes.1": "1",
				"policy":       "bind",
				"share":        "on",
			},
		},
	)

	expected := map[string]any{
		"size":       int64(1073741824),
		"host-nodes": []int{0, 1},
		"policy":     "bind",
		"share":      true,
	}

	assert.Equal(t, expected["size"], result["size"])
	assert.ElementsMatch(t, expected["host-nodes"], result["host-nodes"])
	assert.Equal(t, expected["policy"], result["policy"])
	assert.Equal(t, expected["share"], result["share"])
}

// Test extractTraiingNumber.
func TestExtractTrailingNumber(t *testing.T) {
	value, _ := extractTrailingNumber("mem0", "mem")
	assert.Equal(t, 0, value)

	value, _ = extractTrailingNumber("mem34", "mem")
	assert.Equal(t, 34, value)

	value, _ = extractTrailingNumber("dimm1", "dimm")
	assert.Equal(t, 1, value)

	expectedErr := "Prefix mem not found in dimm1"
	_, err := extractTrailingNumber("dimm1", "mem")
	if err.Error() != expectedErr {
		t.Errorf("unexpected error message: got %q, want %q", err.Error(), expectedErr)
	}
}