xref: /freebsd/libexec/nuageinit/tests/nuageinit.sh (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1atf_test_case args
2atf_test_case nocloud
3atf_test_case nocloud_userdata_script
4atf_test_case nocloud_userdata_cloudconfig
5atf_test_case nocloud_userdata_cloudconfig_users
6atf_test_case nocloud_network
7atf_test_case config2
8atf_test_case config2_pubkeys
9atf_test_case config2_network
10atf_test_case config2_network_static_v4
11
12
13args_body()
14{
15	atf_check -s exit:1 -e inline:"Usage /usr/libexec/nuageinit <cloud-init directory> [config-2|nocloud]\n" /usr/libexec/nuageinit
16	atf_check -s exit:1 -e inline:"Usage /usr/libexec/nuageinit <cloud-init directory> [config-2|nocloud]\n" /usr/libexec/nuageinit bla
17	atf_check -s exit:1 -e inline:"Usage /usr/libexec/nuageinit <cloud-init directory> [config-2|nocloud]\n" /usr/libexec/nuageinit bla meh plop
18	atf_check -s exit:1 -e inline:"Unknown cloud init type: meh\n" /usr/libexec/nuageinit bla meh
19}
20
21nocloud_body()
22{
23	here=$(pwd)
24	mkdir -p media/nuageinit
25	atf_check -s exit:1 -e match:"nuageinit: error parsing nocloud.*" /usr/libexec/nuageinit ${here}/media/nuageinit/ nocloud
26	export NUAGE_FAKE_ROOTDIR=$(pwd)
27	printf "instance-id: iid-local01\nlocal-hostname: cloudimg\n" > ${here}/media/nuageinit/meta-data
28	atf_check -s exit:0 /usr/libexec/nuageinit ${here}/media/nuageinit nocloud
29	atf_check -o inline:"hostname=\"cloudimg\"\n" cat etc/rc.conf.d/hostname
30	cat > media/nuageinit/meta-data << EOF
31instance-id: iid-local01
32hostname: myhost
33EOF
34	atf_check -s exit:0 /usr/libexec/nuageinit ${here}/media/nuageinit nocloud
35	atf_check -o inline:"hostname=\"myhost\"\n" cat etc/rc.conf.d/hostname
36}
37
38nocloud_userdata_script_body()
39{
40	here=$(pwd)
41	mkdir -p media/nuageinit
42	printf "instance-id: iid-local01\n" > ${here}/media/nuageinit/meta-data
43	printf "#!/bin/sh\necho "yeah"\n" > ${here}/media/nuageinit/user-data
44	chmod 755  ${here}/media/nuageinit/user-data
45	atf_check -s exit:0 -o inline:"yeah\n" /usr/libexec/nuageinit ${here}/media/nuageinit nocloud
46}
47
48nocloud_userdata_cloudconfig_users_body()
49{
50	here=$(pwd)
51	export NUAGE_FAKE_ROOTDIR=$(pwd)
52	if [ $(id -u) -ne 0 ]; then
53		atf_skip "root required"
54	fi
55	mkdir -p media/nuageinit
56	printf "instance-id: iid-local01\n" > ${here}/media/nuageinit/meta-data
57	mkdir -p etc
58	cat > etc/master.passwd <<EOF
59root:*:0:0::0:0:Charlie &:/root:/bin/csh
60sys:*:1:0::0:0:Sys:/home/sys:/bin/csh
61EOF
62	pwd_mkdb -d etc ${here}/etc/master.passwd
63	cat > etc/group <<EOF
64wheel:*:0:root
65users:*:1:
66EOF
67	cat > media/nuageinit/user-data <<EOF
68#cloud-config
69groups:
70  - admingroup: [root,sys]
71  - cloud-users
72users:
73  - default
74  - name: foobar
75    gecos: Foo B. Bar
76    primary_group: foobar
77    groups: users
78    passwd: $6$j212wezy$7H/1LT4f9/N3wpgNunhsIqtMj62OKiS3nyNwuizouQc3u7MbYCarYeAHWYPYb2FT.lbioDm2RrkJPb9BZMN1O/
79EOF
80	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit nocloud
81	cat > expectedgroup << EOF
82wheel:*:0:root,freebsd
83users:*:1:foobar
84admingroup:*:1001:root,sys
85cloud-users:*:1002:
86freebsd:*:1003:
87foobar:*:1004:
88EOF
89	cat > expectedpasswd << EOF
90root:*:0:0::0:0:Charlie &:/root:/bin/csh
91sys:*:1:0::0:0:Sys:/home/sys:/bin/csh
92freebsd:freebsd:1001:1003::0:0:FreeBSD User:/home/freebsd:/bin/sh
93foobar:H/1LT4f9/N3wpgNunhsIqtMj62OKiS3nyNwuizouQc3u7MbYCarYeAHWYPYb2FT.lbioDm2RrkJPb9BZMN1O/:1002:1004::0:0:Foo B. Bar:/home/foobar:/bin/sh
94EOF
95	atf_check -o file:expectedpasswd cat ${here}/etc/master.passwd
96	atf_check -o file:expectedgroup cat ${here}/etc/group
97}
98
99nocloud_network_body()
100{
101	here=$(pwd)
102	mkdir -p media/nuageinit
103	mkdir -p etc
104	cat > etc/master.passwd <<EOF
105root:*:0:0::0:0:Charlie &:/root:/bin/csh
106sys:*:1:0::0:0:Sys:/home/sys:/bin/csh
107EOF
108	pwd_mkdb -d etc ${here}/etc/master.passwd
109	cat > etc/group <<EOF
110wheel:*:0:root
111users:*:1:
112EOF
113	if [ $(id -u) -ne 0 ]; then
114		atf_skip "root required"
115	fi
116	mynetworks=$(ifconfig -l ether)
117	if [ -z "$mynetworks" ]; then
118		atf_skip "a network interface is needed"
119	fi
120	set -- $mynetworks
121	myiface=$1
122	myaddr=$(ifconfig $myiface ether | awk '/ether/ { print $2 }')
123	printf "instance-id: iid-local01\n" > ${here}/media/nuageinit/meta-data
124	cat > media/nuageinit/user-data <<EOF
125#cloud-config
126#
127network:
128  version: 2
129  ethernets:
130    # opaque ID for physical interfaces, only referred to by other stanzas
131    id0:
132      match:
133        macaddress: '${myaddr}'
134      addresses:
135        - 192.168.14.2/24
136        - 2001:1::1/64
137      gateway4: 192.168.14.1
138      gateway6: 2001:1::2
139EOF
140	export NUAGE_FAKE_ROOTDIR=$(pwd)
141	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit nocloud
142	cat > network <<EOF
143ifconfig_${myiface}="inet 192.168.14.2/24"
144ifconfig_${myiface}_ipv6="inet6 2001:1::1/64"
145ipv6_network_interfaces="${myiface}"
146ipv6_default_interface="${myiface}"
147EOF
148	cat > routing <<EOF
149defaultrouter="192.168.14.1"
150ipv6_defaultrouter="2001:1::2"
151ipv6_route_${myiface}="2001:1::2 -prefixlen 128 -interface ${myiface}"
152EOF
153	atf_check -o file:network cat ${here}/etc/rc.conf.d/network
154	atf_check -o file:routing cat ${here}/etc/rc.conf.d/routing
155}
156config2_body()
157{
158	here=$(pwd)
159	mkdir -p media/nuageinit
160	atf_check -s exit:1 -e match:"nuageinit: error parsing config-2: meta_data.json.*" /usr/libexec/nuageinit ${here}/media/nuageinit config-2
161	printf "{}" > media/nuageinit/meta_data.json
162	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit config-2
163	cat > media/nuageinit/meta_data.json << EOF
164{
165   "hostname": "cloudimg",
166}
167EOF
168	export NUAGE_FAKE_ROOTDIR=$(pwd)
169	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit config-2
170	atf_check -o inline:"hostname=\"cloudimg\"\n" cat etc/rc.conf.d/hostname
171}
172
173config2_pubkeys_body()
174{
175	here=$(pwd)
176	if [ $(id -u) -ne 0 ]; then
177		atf_skip "root required"
178	fi
179	mkdir -p media/nuageinit
180	cat > media/nuageinit/meta_data.json << EOF
181{
182   "public_keys": {
183       "mykey": "ssh-rsa AAAAB3NzaC1y...== Generated by Nova"
184   },
185}
186EOF
187	mkdir -p etc
188	cat > etc/master.passwd <<EOF
189root:*:0:0::0:0:Charlie &:/root:/bin/csh
190sys:*:1:0::0:0:Sys:/home/sys:/bin/csh
191EOF
192	pwd_mkdb -d etc ${here}/etc/master.passwd
193	cat > etc/group <<EOF
194wheel:*:0:root
195users:*:1:
196EOF
197	export NUAGE_FAKE_ROOTDIR=$(pwd)
198	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit config-2
199	atf_check -o inline:"ssh-rsa AAAAB3NzaC1y...== Generated by Nova\n" cat home/freebsd/.ssh/authorized_keys
200}
201
202config2_network_body() {
203	here=$(pwd)
204	mkdir -p media/nuageinit
205	printf "{}" > media/nuageinit/meta_data.json
206	mynetworks=$(ifconfig -l ether)
207	if [ -z "$mynetworks" ]; then
208		atf_skip "a network interface is needed"
209	fi
210	set -- $mynetworks
211	myiface=$1
212	myaddr=$(ifconfig $myiface ether | awk '/ether/ { print $2 }')
213cat > media/nuageinit/network_data.json <<EOF
214{
215    "links": [
216        {
217            "ethernet_mac_address": "$myaddr",
218            "id": "iface0",
219            "mtu": null,
220        }
221    ],
222    "networks": [
223        {
224            "id": "network0",
225            "link": "iface0",
226            "type": "ipv4_dhcp"
227        },
228	{ // IPv6
229	"id": "private-ipv4",
230	"type": "ipv6",
231	"link": "iface0",
232	// supports condensed IPv6 with CIDR netmask
233	"ip_address": "2001:cdba::3257:9652/24",
234	"gateway": "fd00::1"
235	"routes": [
236	    {
237		"network": "::",
238		"netmask": "::",
239		"gateway": "fd00::1"
240	},
241	    {
242		"network": "::",
243		"netmask": "ffff:ffff:ffff::",
244		"gateway": "fd00::1:1"
245	},
246	],
247	"network_id": "da5bb487-5193-4a65-a3df-4a0055a8c0d8"
248},
249    ],
250}
251EOF
252	export NUAGE_FAKE_ROOTDIR=$(pwd)
253	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit config-2
254	cat > network <<EOF
255ifconfig_${myiface}="DHCP"
256ifconfig_${myiface}_ipv6="inet6 2001:cdba::3257:9652/24"
257ipv6_network_interfaces="${myiface}"
258ipv6_default_interface="${myiface}"
259EOF
260	cat > routing <<EOF
261ipv6_defaultrouter="fd00::1"
262ipv6_route_${myiface}="fd00::1 -prefixlen 128 -interface ${myiface}"
263ipv6_static_routes="${myiface}"
264EOF
265	atf_check -o file:network cat ${here}/etc/rc.conf.d/network
266	atf_check -o file:routing cat ${here}/etc/rc.conf.d/routing
267}
268
269config2_network_static_v4_body() {
270	here=$(pwd)
271	mkdir -p media/nuageinit
272	printf "{}" > media/nuageinit/meta_data.json
273	mynetworks=$(ifconfig -l ether)
274	if [ -z "$mynetworks" ]; then
275		atf_skip "a network interface is needed"
276	fi
277	set -- $mynetworks
278	myiface=$1
279	myaddr=$(ifconfig $myiface ether | awk '/ether/ { print $2 }')
280cat > media/nuageinit/network_data.json <<EOF
281{
282    "links": [
283        {
284            "ethernet_mac_address": "$myaddr",
285            "id": "iface0",
286            "mtu": null,
287        }
288    ],
289    "networks": [
290        {
291            "id": "network0",
292            "link": "iface0",
293            "type": "ipv4"
294            "ip_address": "10.184.0.244",
295            "netmask": "255.255.240.0",
296            "routes": [
297            {
298                "network": "10.0.0.0",
299                "netmask": "255.0.0.0",
300                "gateway": "11.0.0.1"
301            },
302            {
303                "network": "0.0.0.0",
304                "netmask": "0.0.0.0",
305                "gateway": "23.253.157.1"
306            }
307         ]
308     }
309]
310}
311EOF
312
313	export NUAGE_FAKE_ROOTDIR=$(pwd)
314	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit config-2
315	cat > network <<EOF
316ifconfig_${myiface}="inet 10.184.0.244 netmask 255.255.240.0"
317EOF
318	cat > routing <<EOF
319route_cloudinit1_${myiface}="-net 10.0.0.0 11.0.0.1 255.0.0.0"
320defaultrouter="23.253.157.1"
321static_routes="cloudinit1_${myiface}"
322EOF
323	atf_check -o file:network cat ${here}/etc/rc.conf.d/network
324	atf_check -o file:routing cat ${here}/etc/rc.conf.d/routing
325}
326
327atf_init_test_cases()
328{
329	atf_add_test_case args
330	atf_add_test_case nocloud
331	atf_add_test_case nocloud_userdata_script
332	atf_add_test_case nocloud_userdata_cloudconfig_users
333	atf_add_test_case nocloud_network
334	atf_add_test_case config2
335	atf_add_test_case config2_pubkeys
336	atf_add_test_case config2_network
337	atf_add_test_case config2_network_static_v4
338}
339