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