|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "gopkg.in/yaml.v3" |
| 8 | +) |
| 9 | + |
| 10 | +// parseCloudConfig strips the "#cloud-config" header and unmarshals the rest, |
| 11 | +// so tests assert on structured content rather than brittle substring matches. |
| 12 | +func parseCloudConfig(t *testing.T, s string) map[string]any { |
| 13 | + t.Helper() |
| 14 | + if !strings.HasPrefix(s, "#cloud-config\n") { |
| 15 | + t.Fatalf("vendor data must start with #cloud-config header, got:\n%s", s) |
| 16 | + } |
| 17 | + var out map[string]any |
| 18 | + if err := yaml.Unmarshal([]byte(s), &out); err != nil { |
| 19 | + t.Fatalf("rendered vendor data is not valid YAML: %v\n%s", err, s) |
| 20 | + } |
| 21 | + return out |
| 22 | +} |
| 23 | + |
| 24 | +func asStrings(t *testing.T, v any, field string) []string { |
| 25 | + t.Helper() |
| 26 | + list, ok := v.([]any) |
| 27 | + if !ok { |
| 28 | + t.Fatalf("%s: want a list, got %T", field, v) |
| 29 | + } |
| 30 | + out := make([]string, len(list)) |
| 31 | + for i, e := range list { |
| 32 | + s, ok := e.(string) |
| 33 | + if !ok { |
| 34 | + t.Fatalf("%s[%d]: want string, got %T", field, i, e) |
| 35 | + } |
| 36 | + out[i] = s |
| 37 | + } |
| 38 | + return out |
| 39 | +} |
| 40 | + |
| 41 | +func TestRenderVendorData_PackagesAndRunCmd(t *testing.T) { |
| 42 | + got, err := RenderVendorData([]string{"open-iscsi", "nfs-common"}, []string{"echo hi", "mkdir -p /data"}) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("RenderVendorData: %v", err) |
| 45 | + } |
| 46 | + cfg := parseCloudConfig(t, got) |
| 47 | + |
| 48 | + // package_update must be true whenever packages are requested, so the |
| 49 | + // index is refreshed before install. |
| 50 | + if cfg["package_update"] != true { |
| 51 | + t.Errorf("package_update = %v, want true", cfg["package_update"]) |
| 52 | + } |
| 53 | + if pkgs := asStrings(t, cfg["packages"], "packages"); !equal(pkgs, []string{"open-iscsi", "nfs-common"}) { |
| 54 | + t.Errorf("packages = %v", pkgs) |
| 55 | + } |
| 56 | + // runcmd must lead with the qemu-guest-agent enablement (IP discovery), |
| 57 | + // then the user's commands in order. |
| 58 | + want := []string{ |
| 59 | + "systemctl enable qemu-guest-agent", |
| 60 | + "systemctl start qemu-guest-agent", |
| 61 | + "echo hi", |
| 62 | + "mkdir -p /data", |
| 63 | + } |
| 64 | + if cmds := asStrings(t, cfg["runcmd"], "runcmd"); !equal(cmds, want) { |
| 65 | + t.Errorf("runcmd = %v, want %v", cmds, want) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestRenderVendorData_NoPackages(t *testing.T) { |
| 70 | + got, err := RenderVendorData(nil, []string{"touch /ready"}) |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("RenderVendorData: %v", err) |
| 73 | + } |
| 74 | + cfg := parseCloudConfig(t, got) |
| 75 | + // No packages → no package_update, no packages key (omitempty). |
| 76 | + if _, ok := cfg["package_update"]; ok { |
| 77 | + t.Errorf("package_update should be omitted when no packages") |
| 78 | + } |
| 79 | + if _, ok := cfg["packages"]; ok { |
| 80 | + t.Errorf("packages should be omitted when empty") |
| 81 | + } |
| 82 | + want := []string{ |
| 83 | + "systemctl enable qemu-guest-agent", |
| 84 | + "systemctl start qemu-guest-agent", |
| 85 | + "touch /ready", |
| 86 | + } |
| 87 | + if cmds := asStrings(t, cfg["runcmd"], "runcmd"); !equal(cmds, want) { |
| 88 | + t.Errorf("runcmd = %v, want %v", cmds, want) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestRenderVendorData_OnlyAgentWhenEmpty(t *testing.T) { |
| 93 | + got, err := RenderVendorData(nil, nil) |
| 94 | + if err != nil { |
| 95 | + t.Fatalf("RenderVendorData: %v", err) |
| 96 | + } |
| 97 | + cfg := parseCloudConfig(t, got) |
| 98 | + want := []string{ |
| 99 | + "systemctl enable qemu-guest-agent", |
| 100 | + "systemctl start qemu-guest-agent", |
| 101 | + } |
| 102 | + if cmds := asStrings(t, cfg["runcmd"], "runcmd"); !equal(cmds, want) { |
| 103 | + t.Errorf("runcmd = %v, want %v", cmds, want) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// TestRenderVendorData_EscapesSpecialChars proves arbitrary runcmd strings with |
| 108 | +// YAML-significant characters round-trip intact — the reason rendering goes |
| 109 | +// through yaml.Marshal instead of hand-concatenation. |
| 110 | +func TestRenderVendorData_EscapesSpecialChars(t *testing.T) { |
| 111 | + tricky := []string{ |
| 112 | + `sh -c "echo key: value > /etc/x.conf"`, // colon-space + quotes |
| 113 | + `printf '%s\n' "a: b"`, // embedded colon |
| 114 | + `echo '#not a comment'`, // leading hash inside quotes |
| 115 | + } |
| 116 | + got, err := RenderVendorData([]string{"pkg-with-dash"}, tricky) |
| 117 | + if err != nil { |
| 118 | + t.Fatalf("RenderVendorData: %v", err) |
| 119 | + } |
| 120 | + cfg := parseCloudConfig(t, got) |
| 121 | + cmds := asStrings(t, cfg["runcmd"], "runcmd") |
| 122 | + // The two agent commands prefix the three tricky ones. |
| 123 | + if len(cmds) != 5 { |
| 124 | + t.Fatalf("runcmd length = %d, want 5", len(cmds)) |
| 125 | + } |
| 126 | + if !equal(cmds[2:], tricky) { |
| 127 | + t.Errorf("tricky commands did not round-trip:\n got %q\n want %q", cmds[2:], tricky) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestVendorSnippetName(t *testing.T) { |
| 132 | + if got := VendorSnippetName(1234); got != "openctl-vendor-1234.yaml" { |
| 133 | + t.Errorf("VendorSnippetName(1234) = %q", got) |
| 134 | + } |
| 135 | + // Distinct per VM so contents never collide. |
| 136 | + if VendorSnippetName(1) == VendorSnippetName(2) { |
| 137 | + t.Errorf("VendorSnippetName must be unique per vmid") |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func equal(a, b []string) bool { |
| 142 | + if len(a) != len(b) { |
| 143 | + return false |
| 144 | + } |
| 145 | + for i := range a { |
| 146 | + if a[i] != b[i] { |
| 147 | + return false |
| 148 | + } |
| 149 | + } |
| 150 | + return true |
| 151 | +} |
0 commit comments