Skip to content

Commit

Permalink
Add tests for multipart mime in coreos-cloudinit
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Adrian Samfira <[email protected]>
  • Loading branch information
gabriel-samfira committed May 23, 2023
1 parent 5e94997 commit 7a19a2b
Showing 1 changed file with 157 additions and 0 deletions.
157 changes: 157 additions & 0 deletions kola/tests/misc/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,134 @@
package misc

import (
"fmt"
"strings"

"github.com/flatcar/mantle/kola/cluster"
"github.com/flatcar/mantle/kola/register"
"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(&register.Test{
Run: CloudInitBasic,
Expand Down Expand Up @@ -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(&register.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) {
Expand All @@ -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")
}

0 comments on commit 7a19a2b

Please sign in to comment.