-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathbootstrap_test.go
More file actions
177 lines (159 loc) · 5.01 KB
/
Copy pathbootstrap_test.go
File metadata and controls
177 lines (159 loc) · 5.01 KB
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
package baremetal
import (
"testing"
"github.com/stretchr/testify/assert"
"libvirt.org/go/libvirtxml"
)
func TestNewDomain(t *testing.T) {
dom := newDomain("test-bootstrap")
assert.Equal(t, "test-bootstrap", dom.Name)
assert.Equal(t, "kvm", dom.Type)
assert.Equal(t, "hvm", dom.OS.Type.Type)
assert.Empty(t, dom.OS.Type.Arch, "arch should not be set by newDomain")
assert.Empty(t, dom.OS.Type.Machine, "machine should not be set by newDomain")
assert.Equal(t, uint(6), dom.Memory.Value)
assert.Equal(t, "GiB", dom.Memory.Unit)
assert.Equal(t, uint(4), dom.VCPU.Value)
assert.Equal(t, "host-passthrough", dom.CPU.Mode)
assert.NotEmpty(t, dom.Devices.RNGs, "RNG device should be configured")
assert.NotEmpty(t, dom.Devices.Graphics, "graphics should be configured")
assert.NotEmpty(t, dom.Devices.Consoles, "console should be configured")
assert.NotEmpty(t, dom.Devices.Serials, "serial device should be configured")
assert.NotNil(t, dom.Devices.Serials[0].Log, "serial device should have a log file configured")
assert.Equal(t, "/var/log/libvirt/qemu/test-bootstrap-serial0.log", dom.Devices.Serials[0].Log.File)
assert.Equal(t, "on", dom.Devices.Serials[0].Log.Append)
assert.Nil(t, dom.Devices.Serials[0].Target, "serial target should not be set by newDomain")
}
func TestConfigureDomainArch(t *testing.T) {
tests := []struct {
name string
arch string
expectMachine string
expectFirmware string
expectGraphics bool
}{
{
name: "x86_64 sets q35 and efi",
arch: "x86_64",
expectMachine: "q35",
expectFirmware: "efi",
expectGraphics: true,
},
{
name: "aarch64 sets efi and removes graphics",
arch: "aarch64",
expectMachine: "",
expectFirmware: "efi",
expectGraphics: false,
},
{
name: "s390x removes graphics",
arch: "s390x",
expectMachine: "",
expectFirmware: "",
expectGraphics: false,
},
{
name: "ppc64le removes graphics",
arch: "ppc64le",
expectMachine: "",
expectFirmware: "",
expectGraphics: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
dom := newDomain("test-bootstrap")
configureDomainArch(&dom, tc.arch)
assert.Equal(t, tc.arch, dom.OS.Type.Arch)
assert.Equal(t, tc.expectMachine, dom.OS.Type.Machine)
assert.Equal(t, tc.expectFirmware, dom.OS.Firmware)
if tc.expectGraphics {
assert.NotNil(t, dom.Devices.Graphics, "graphics should be present for %s", tc.arch)
} else {
assert.Nil(t, dom.Devices.Graphics, "graphics should be removed for %s", tc.arch)
}
assert.NotEmpty(t, dom.Devices.Serials, "serial device should be present for %s", tc.arch)
})
}
}
func TestConfigureDomainArchPreservesOtherFields(t *testing.T) {
dom := newDomain("test-bootstrap")
dom.Devices.Interfaces = []libvirtxml.DomainInterface{
{
Model: &libvirtxml.DomainInterfaceModel{Type: "virtio"},
},
}
configureDomainArch(&dom, "x86_64")
assert.Len(t, dom.Devices.Interfaces, 1, "interfaces should not be modified")
assert.Equal(t, "hvm", dom.OS.Type.Type, "OS type should remain hvm")
assert.Equal(t, "kvm", dom.Type, "domain type should remain kvm")
}
func TestBuildBootstrapKargs(t *testing.T) {
tests := []struct {
name string
arch string
fips bool
expectContains []string
expectMissing []string
}{
{
name: "x86_64 default",
arch: "x86_64",
fips: false,
expectContains: []string{"console=ttyS0,115200n8"},
expectMissing: []string{"fips=1"},
},
{
name: "x86_64 with FIPS",
arch: "x86_64",
fips: true,
expectContains: []string{"console=ttyS0,115200n8", "fips=1"},
},
{
name: "aarch64 default",
arch: "aarch64",
fips: false,
expectContains: []string{"console=ttyAMA0,115200"},
expectMissing: []string{"fips=1"},
},
{
name: "s390x default",
arch: "s390x",
fips: false,
expectContains: []string{"console=ttysclp0"},
expectMissing: []string{"fips=1"},
},
{
name: "ppc64le default",
arch: "ppc64le",
fips: false,
expectContains: []string{"console=hvc0"},
expectMissing: []string{"fips=1"},
},
{
name: "unknown arch returns nil",
arch: "riscv64",
fips: false,
expectMissing: []string{"console="},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := buildBootstrapKargs(tc.arch, tc.fips)
if len(tc.expectContains) == 0 && len(tc.expectMissing) > 0 {
assert.Nil(t, result, "kargs should be nil for unknown arch without FIPS")
return
}
assert.NotNil(t, result, "kargs should not be nil")
kargs := string(result)
assert.True(t, kargs[len(kargs)-1] == '\n', "kargs should end with newline")
for _, s := range tc.expectContains {
assert.Contains(t, kargs, s)
}
for _, s := range tc.expectMissing {
assert.NotContains(t, kargs, s)
}
})
}
}