From 7a19a2bb9e9841c35572ef4543d177ab5c4517b4 Mon Sep 17 00:00:00 2001 From: Gabriel Adrian Samfira Date: Mon, 22 May 2023 21:56:30 +0300 Subject: [PATCH] Add tests for multipart mime in coreos-cloudinit Signed-off-by: Gabriel Adrian Samfira --- kola/tests/misc/cloudinit.go | 157 +++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) diff --git a/kola/tests/misc/cloudinit.go b/kola/tests/misc/cloudinit.go index af5070661..d0ae37de7 100644 --- a/kola/tests/misc/cloudinit.go +++ b/kola/tests/misc/cloudinit.go @@ -15,6 +15,7 @@ package misc import ( + "fmt" "strings" "github.com/flatcar/mantle/kola/cluster" @@ -22,6 +23,126 @@ import ( "github.com/flatcar/mantle/platform/conf" ) +var multipartMimeUserdata = `Content-Type: multipart/mixed; boundary="===============1598784645116016685==" +MIME-Version: 1.0 + +--===============1598784645116016685== +Content-Type: text/cloud-config; charset="us-ascii" +MIME-Version: 1.0 +Content-Transfer-Encoding: 7bit +Content-Disposition: attachment; filename="cloud-config" + +ssh_authorized_keys: + - ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEftQIHTRvUmyDCN7VGve4srz03Jmq6rPnqq+XMHMQUIL9c/b0l7B5tWfQvQecKyLte94HOPzAyMJlktWTVGQnY= + +--===============1598784645116016685== +Content-Type: text/cloud-config; charset="us-ascii" +MIME-Version: 1.0 +Content-Transfer-Encoding: 7bit +Content-Disposition: attachment; filename="cloud-config" + +hostname: "example" + +--===============1598784645116016685== +Content-Type: text/cloud-config; charset="us-ascii" +MIME-Version: 1.0 +Content-Transfer-Encoding: 7bit +Content-Disposition: attachment; filename="cloud-config" + +write_files: +- encoding: b64 + content: NDI= + path: /tmp/kola_b64 + permissions: '0644' +- encoding: base64 + content: NDI= + path: /tmp/kola_b64_1 + permissions: '0644' +- encoding: gzip + content: !!binary | + H4sIAGUfoFQC/zMxAgCIsCQyAgAAAA== + path: /tmp/kola_gzip + permissions: '0644' +- encoding: gz + content: !!binary | + H4sIAGUfoFQC/zMxAgCIsCQyAgAAAA== + path: /tmp/kola_gzip_1 + permissions: '0644' +- encoding: gz+base64 + content: H4sIAGUfoFQC/zMxAgCIsCQyAgAAAA== + path: /tmp/kola_gzip_base64 + permissions: '0644' +- encoding: gzip+base64 + content: H4sIAGUfoFQC/zMxAgCIsCQyAgAAAA== + path: /tmp/kola_gzip_base64_1 + permissions: '0644' +- encoding: gz+b64 + content: H4sIAGUfoFQC/zMxAgCIsCQyAgAAAA== + path: /tmp/kola_gzip_base64_2 + permissions: '0644' + +--===============1598784645116016685== +Content-Type: text/x-shellscript; charset="us-ascii" +MIME-Version: 1.0 +Content-Transfer-Encoding: 7bit +Content-Disposition: attachment; filename="create_file.ps1" + +#!/bin/sh +touch /coreos-cloudinit_multipart.txt + +--===============1598784645116016685== +Content-Type: text/cloud-config; charset="us-ascii" +MIME-Version: 1.0 +Content-Transfer-Encoding: 7bit +Content-Disposition: attachment; filename="cloud-config" + +#test_to_check_if_cloud_config_can_contain_a_comment + +--===============1598784645116016685== +Content-Type: text/plain; charset="us-ascii" +MIME-Version: 1.0 +Content-Transfer-Encoding: 7bit +Content-Disposition: attachment; filename="some_text.txt" + +This is just some random text. + +--===============1598784645116016685== +Content-Type: application/json; charset="us-ascii" +MIME-Version: 1.0 +Content-Transfer-Encoding: 7bit +Content-Disposition: attachment; filename="ignition.txt" + +{ + "ignitionVersion": 1, + "ignition": { + "version": "3.0.0" + }, + "systemd": { + "units": [{ + "name": "example.service", + "enabled": true, + "contents": "[Service]\nType=oneshot\nExecStart=/usr/bin/echo Hello World\n\n[Install]\nWantedBy=multi-user.target" + }] + } +} + +--===============1598784645116016685== +Content-Type: text/plain; charset="us-ascii" +MIME-Version: 1.0 +Content-Transfer-Encoding: 7bit +Content-Disposition: attachment; filename="incognito_cloud_config.txt" + +#cloud-config + +write_files: +- encoding: b64 + content: NDI= + path: /kola_undercover + permissions: '0644' + +--===============1598784645116016685==-- +` + func init() { register.Register(®ister.Test{ Run: CloudInitBasic, @@ -54,6 +175,14 @@ chmod 600 ~core/.ssh/authorized_keys`), // When cl.cloudinit.basic passed we don't need to run this on all clouds Platforms: []string{"qemu", "qemu-unpriv"}, }) + register.Register(®ister.Test{ + Run: CloudInitMultipartMime, + ClusterSize: 1, + Name: "cl.cloudinit.multipart-mime", + UserData: conf.CloudConfig(multipartMimeUserdata), + Distros: []string{"cl"}, + Platforms: []string{"qemu", "qemu-unpriv"}, + }) } func CloudInitBasic(c cluster.TestCluster) { @@ -78,3 +207,31 @@ func CloudInitScript(c cluster.TestCluster) { c.Fatalf("userdata script produced unexpected value %q", out) } } + +func CloudInitMultipartMime(c cluster.TestCluster) { + m := c.Machines()[0] + + expectKey := "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEftQIHTRvUmyDCN7VGve4srz03Jmq6rPnqq+XMHMQUIL9c/b0l7B5tWfQvQecKyLte94HOPzAyMJlktWTVGQnY=" + + // Test that the hostname was set by the first multipart mime part that declares the "hostname" + // cloud-config option. The second one at the end should be ignored. + out := c.MustSSH(m, "hostnamectl") + if !strings.Contains(string(out), "Static hostname: example") { + c.Fatalf("hostname wasn't set correctly:\n%s", out) + } + + // we can ignore the output. If the command fails, MustSSH will fail the test. + c.MustSSH(m, fmt.Sprintf("grep %s ~core/.ssh/authorized_keys", expectKey)) + + out = c.MustSSH(m, "ls -l /tmp/kola_*| wc -l") + if string(strings.Replace(string(out), "\n", "", -1)) != "7" { + c.Fatalf("expected 7 files in /tmp, found %q", out) + } + + // All files should have the same content (42). These files should have been created by the cloud-config part + // that declares the write_files option. + c.MustSSH(m, "for $file in /tmp/kola_*; do OUT=$(cat $file); if [ $OUT != 42 ]; then exit 1; fi; done") + // Check that the x-shellscript part was executed. + c.MustSSH(m, "test -f /coreos-cloudinit_multipart.txt") + c.MustSSH(m, "test -f /kola_undercover") +}