File: api_internal_test.go

package info (click to toggle)
incus 6.0.5-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,788 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (176 lines) | stat: -rw-r--r-- 5,863 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
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
package main

import (
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/lxc/incus/v6/shared/api"
)

// Test that an instance with a local root disk device just gets its pool property modified.
func TestInternalImportRootDevicePopulate_LocalDevice(t *testing.T) {
	instancePoolName := "test"
	localDevices := make(map[string]map[string]string)

	localRootDev := map[string]string{
		"type": "disk",
		"path": "/",
		"pool": "oldpool",
		"size": "15GiB",
	}

	localDevices["root"] = localRootDev

	internalImportRootDevicePopulate(instancePoolName, localDevices, nil, nil)

	assert.Equal(t, instancePoolName, localDevices["root"]["pool"])
	assert.Equal(t, localRootDev["type"], localDevices["root"]["type"])
	assert.Equal(t, localRootDev["path"], localDevices["root"]["path"])
	assert.Equal(t, localRootDev["size"], localDevices["root"]["size"])
}

// Test that an instance with no local root disk device but has a root disk from its old expanded profile devices,
// that doesn't match the root disk in the new profiles, gets it added back as a local disk, with the pool property
// modified.
func TestInternalImportRootDevicePopulate_ExpandedDeviceProfileDeviceMismatch(t *testing.T) {
	instancePoolName := "test"
	localDevices := make(map[string]map[string]string)
	expandedDevices := make(map[string]map[string]string)

	expandedRootDev := map[string]string{
		"type": "disk",
		"path": "/",
		"pool": "oldpool",
		"size": "15GiB",
	}

	expandedDevices["root"] = expandedRootDev

	profiles := []api.Profile{
		{
			Name: "default",
		},
	}

	internalImportRootDevicePopulate(instancePoolName, localDevices, expandedDevices, profiles)

	assert.Equal(t, instancePoolName, localDevices["root"]["pool"])
	assert.Equal(t, expandedRootDev["type"], localDevices["root"]["type"])
	assert.Equal(t, expandedRootDev["path"], localDevices["root"]["path"])
	assert.Equal(t, expandedRootDev["size"], localDevices["root"]["size"])
}

// Test that an instance with no local root disk device but has a root disk from its old expanded profile devices,
// that matches the new profile root disk device (excluding pool name), and the new profile root disk matches the
// target pool, then no local root disk device is added, and the instance will continue to use the profile root
// disk device.
func TestInternalImportRootDevicePopulate_ExpandedDeviceProfileDeviceMatch(t *testing.T) {
	instancePoolName := "test"
	localDevices := make(map[string]map[string]string)
	expandedDevices := make(map[string]map[string]string)

	expandedRootDev := map[string]string{
		"type": "disk",
		"path": "/",
		"pool": "oldpool",
		"size": "15GiB",
	}

	expandedDevices["root"] = expandedRootDev

	profiles := []api.Profile{
		{
			Name: "default",
			ProfilePut: api.ProfilePut{
				Devices: make(map[string]map[string]string),
			},
		},
	}

	profiles[0].Devices["root"] = map[string]string{
		"type": "disk",
		"path": "/",
		"pool": instancePoolName,
		"size": "15GiB",
	}

	internalImportRootDevicePopulate(instancePoolName, localDevices, expandedDevices, profiles)

	assert.Equal(t, len(localDevices), 0)
}

// Test that for an instance with no local root disk device, if the new profile root disk device doesn't match the
// target pool that the old expanded root device is added as a local root disk device (with the pool modified).
func TestInternalImportRootDevicePopulate_ExpandedDeviceProfileDevicePoolMismatch(t *testing.T) {
	instancePoolName := "test"
	localDevices := make(map[string]map[string]string)
	expandedDevices := make(map[string]map[string]string)

	expandedRootDev := map[string]string{
		"type": "disk",
		"path": "/",
		"pool": "oldpool",
		"size": "15GiB",
	}

	expandedDevices["root"] = expandedRootDev

	profiles := []api.Profile{
		{
			Name: "default",
			ProfilePut: api.ProfilePut{
				Devices: make(map[string]map[string]string),
			},
		},
	}

	profiles[0].Devices["root"] = map[string]string{
		"type": "disk",
		"path": "/",
		"pool": "wrongpool",
		"size": "15GiB",
	}

	internalImportRootDevicePopulate(instancePoolName, localDevices, expandedDevices, profiles)

	assert.Equal(t, instancePoolName, localDevices["root"]["pool"])
	assert.Equal(t, expandedRootDev["type"], localDevices["root"]["type"])
	assert.Equal(t, expandedRootDev["path"], localDevices["root"]["path"])
	assert.Equal(t, expandedRootDev["size"], localDevices["root"]["size"])
}

// Test that if old config has no root disk device, and neither does new profiles, then a basic local root disk
// device is added using the target pool.
func TestInternalImportRootDevicePopulate_NoExistingRootDiskDevice(t *testing.T) {
	instancePoolName := "test"
	localDevices := make(map[string]map[string]string)

	internalImportRootDevicePopulate(instancePoolName, localDevices, nil, nil)

	assert.Equal(t, instancePoolName, localDevices["root"]["pool"])
	assert.Equal(t, "disk", localDevices["root"]["type"])
	assert.Equal(t, "/", localDevices["root"]["path"])
}

// Test that if old config has no root disk device, and neither does new profiles, then a basic local root disk
// device is added using the target pool, and if there is already a local device called "root", then this new root
// disk device is added under an automatically generated name.
func TestInternalImportRootDevicePopulate_NoExistingRootDiskDeviceNameConflict(t *testing.T) {
	instancePoolName := "test"
	localDevices := make(map[string]map[string]string)

	localConflictingRootDev := map[string]string{
		"type":    "nic",
		"nictype": "bridged",
		"name":    "eth0",
	}

	localDevices["root"] = localConflictingRootDev // Conflicting device called "root".

	internalImportRootDevicePopulate(instancePoolName, localDevices, nil, nil)

	assert.Equal(t, instancePoolName, localDevices["root0"]["pool"])
	assert.Equal(t, "disk", localDevices["root0"]["type"])
	assert.Equal(t, "/", localDevices["root0"]["path"])
}