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