-
Notifications
You must be signed in to change notification settings - Fork 59
/
lxdprofile_test.go
164 lines (151 loc) · 4.09 KB
/
lxdprofile_test.go
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
// Copyright 2018 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package charm_test
import (
"strings"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/charm/v12"
)
type ProfileSuite struct{}
var _ = gc.Suite(&ProfileSuite{})
func (s *ProfileSuite) TestValidate(c *gc.C) {
var profileTests = []struct {
description string
profile *charm.LXDProfile
expectedError string
}{
{
description: "success",
profile: &charm.LXDProfile{
Config: map[string]string{
"security.nesting": "true",
"security.privileged": "true",
"linux.kernel_modules": "openvswitch,ip_tables,ip6_tables",
},
Description: "success",
Devices: map[string]map[string]string{
"tun": {
"path": "/dev/net/tun",
"type": "unix-char",
},
"sony": {
"type": "usb",
"vendorid": "0fce",
"productid": "51da",
},
"bdisk": {
"type": "unix-block",
"source": "/dev/loop0",
},
"gpu": {
"type": "gpu",
},
},
},
expectedError: "",
}, {
description: "fail on boot config",
profile: &charm.LXDProfile{
Config: map[string]string{
"security.privileged": "true",
"linux.kernel_modules": "openvswitch,ip_tables,ip6_tables",
"boot.autostart": "true",
},
},
expectedError: "invalid lxd-profile.yaml: contains config value \"boot.autostart\"",
}, {
description: "fail on limits config",
profile: &charm.LXDProfile{
Config: map[string]string{
"security.privileged": "true",
"linux.kernel_modules": "openvswitch,ip_tables,ip6_tables",
"limits.memory": "256MB",
},
},
expectedError: "invalid lxd-profile.yaml: contains config value \"limits.memory\"",
}, {
description: "fail on migration config",
profile: &charm.LXDProfile{
Config: map[string]string{
"security.privileged": "true",
"linux.kernel_modules": "openvswitch,ip_tables,ip6_tables",
"migration.incremental.memory": "true",
},
},
expectedError: "invalid lxd-profile.yaml: contains config value \"migration.incremental.memory\"",
}, {
description: "fail on unix-disk device",
profile: &charm.LXDProfile{
Config: map[string]string{
"security.privileged": "true",
"linux.kernel_modules": "openvswitch,ip_tables,ip6_tables",
},
Devices: map[string]map[string]string{
"bdisk": {
"type": "unix-disk",
"source": "/dev/loop0",
},
},
},
expectedError: "invalid lxd-profile.yaml: contains device type \"unix-disk\"",
},
}
for i, test := range profileTests {
c.Logf("test %d: %s", i, test.description)
err := test.profile.ValidateConfigDevices()
if err != nil {
c.Assert(err.Error(), gc.Equals, test.expectedError)
} else {
c.Assert(err, jc.ErrorIsNil)
}
}
}
func (s *ProfileSuite) TestReadLXDProfile(c *gc.C) {
profile, err := charm.ReadLXDProfile(strings.NewReader(`
config:
security.nesting: "true"
security.privileged: "true"
linux.kernel_modules: openvswitch,nbd,ip_tables,ip6_tables
devices:
kvm:
path: /dev/kvm
type: unix-char
mem:
path: /dev/mem
type: unix-char
tun:
path: /dev/net/tun
type: unix-char
`))
c.Assert(err, jc.ErrorIsNil)
c.Assert(profile, gc.NotNil)
}
func (s *ProfileSuite) TestLXDProfileEmptyFile(c *gc.C) {
profile, err := charm.ReadLXDProfile(strings.NewReader(`
`))
c.Assert(profile, gc.DeepEquals, charm.NewLXDProfile())
c.Assert(err, jc.ErrorIsNil)
c.Assert(profile.Empty(), jc.IsTrue)
c.Assert(profile.ValidateConfigDevices(), jc.ErrorIsNil)
}
func (s *ProfileSuite) TestReadLXDProfileFailUnmarshall(c *gc.C) {
profile, err := charm.ReadLXDProfile(strings.NewReader(`
config:
security.nesting: "true"
security.privileged: "true"
linux.kernel_modules: openvswitch,nbd,ip_tables,ip6_tables
devices:
kvm:
path: /dev/kvm
type: unix-char
mem:
path: /dev/mem
type: unix-char
tun:
path: /dev/net/tun
type: unix-char
`))
c.Assert(err, gc.ErrorMatches, "failed to unmarshall lxd-profile.yaml: yaml: .*")
c.Assert(profile, gc.IsNil)
}