summaryrefslogtreecommitdiff
path: root/libvirt/cloudinit_def_test.go
blob: 18c63a075cbc76200cffc9ba8e368d16296ee2b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package libvirt

import (
	"fmt"
	"github.com/hashicorp/terraform/helper/resource"
	"os"
	"path/filepath"
	"strings"
	"testing"
	"gopkg.in/yaml.v2"
)

func TestNewCloudInitDef(t *testing.T) {
	ci := newCloudInitDef()

	if ci.Metadata.InstanceID == "" {
		t.Error("Expected metadata InstanceID not to be empty")
	}
}

func TestTerraformKeyOps(t *testing.T) {
	ci := newCloudInitDef()

	volKey := "volume-key"

	terraformID := ci.buildTerraformKey(volKey)
	if terraformID == "" {
		t.Error("key should not be empty")
	}

	actualKey, _ := getCloudInitVolumeKeyFromTerraformID(terraformID)
	if actualKey != volKey {
		t.Error("wrong key returned")
	}
}

func TestCreateFiles(t *testing.T) {
	ci := newCloudInitDef()

	dir, err := ci.createFiles()
	if err != nil {
		t.Errorf("Unexpected error %v", err)
	}
	defer os.RemoveAll(dir)

	for _, file := range []string{USERDATA, METADATA} {
		check, err := exists(filepath.Join(dir, file))
		if !check {
			t.Errorf("%s not found: %v", file, err)
		}
	}
}

func TestCreateISONoExteralTool(t *testing.T) {
	path := os.Getenv("PATH")
	defer os.Setenv("PATH", path)

	os.Setenv("PATH", "/")

	ci := newCloudInitDef()

	iso, err := ci.createISO()
	if err == nil {
		t.Errorf("Expected error")
	}

	if iso != "" {
		t.Errorf("Expected iso to be empty")
	}
}

func TestConvertUserDataToMapPreservesCloudInitNames(t *testing.T) {
	ud := CloudInitUserData{
		SSHAuthorizedKeys: []string{"key1"},
	}

	actual, err := convertUserDataToMap(ud)
	if err != nil {
		t.Errorf("Unexpectd error %v", err)
	}

	_, ok := actual["ssh_authorized_keys"]
	if !ok {
		t.Error("Could not found ssh_authorized_keys key")
	}
}

func TestMergeEmptyUserDataIntoUserDataRaw(t *testing.T) {
	ud := CloudInitUserData{}

	var userDataRaw = `
new-key: new-value-set-by-extra
ssh_authorized_keys:
  - key2-from-extra-data
`

	res, err := mergeUserDataIntoUserDataRaw(ud, userDataRaw)
	if err != nil {
		t.Errorf("Unexpectd error %v", err)
	}

	actual := make(map[string]interface{})
	err = yaml.Unmarshal([]byte(res), &actual)
	if err != nil {
		t.Errorf("Unexpectd error %v", err)
	}

	if _, ok := actual["ssh_authorized_keys"]; !ok {
		t.Error("ssh_authorized_keys missing")
	}

	if _, ok := actual["new-key"]; !ok {
		t.Error("new-key missing")
	}
}

func TestMergeUserDataIntoEmptyUserDataRaw(t *testing.T) {
	ud := CloudInitUserData{
		SSHAuthorizedKeys: []string{"key1"},
	}
	var userDataRaw string

	res, err := mergeUserDataIntoUserDataRaw(ud, userDataRaw)
	if err != nil {
		t.Errorf("Unexpectd error %v", err)
	}

	actual := make(map[string]interface{})
	err = yaml.Unmarshal([]byte(res), &actual)
	if err != nil {
		t.Errorf("Unexpectd error %v", err)
	}

	if _, ok := actual["ssh_authorized_keys"]; !ok {
		t.Error("ssh_authorized_keys missing")
	}
}

func TestMergeUserDataIntoUserDataRawGivesPrecedenceToRawData(t *testing.T) {
	udKey := "user-data-key"
	ud := CloudInitUserData{
		SSHAuthorizedKeys: []string{udKey},
	}

	var userDataRaw = `
new-key: new-value-set-by-extra
ssh_authorized_keys:
  - key2-from-extra-data
`

	res, err := mergeUserDataIntoUserDataRaw(ud, userDataRaw)
	if err != nil {
		t.Errorf("Unexpectd error %v", err)
	}

	if strings.Contains(res, udKey) {
		t.Error("Should not have found string defined by user data")
	}

	if !strings.Contains(res, "key2-from-extra-data") {
		t.Error("Should have found string defined by raw data")
	}
}

func TestCreateCloudIsoViaPlugin(t *testing.T) {
	var config = fmt.Sprintf(`

   resource "libvirt_cloudinit" "test" {
   name = "commoninit.iso"
   local_hostname = "tango"
   pool =           "default"
   user_data =      "#cloud-config\nssh_authorized_keys: []\n" 
    }
        `)
	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckLibvirtIgnitionDestroy,
		Steps: []resource.TestStep{
			{
				Config: config,
				Check: resource.ComposeTestCheckFunc(
					resource.TestCheckResourceAttr(
						"libvirt_cloudinit.test", "name", "commoninit.iso"),
					resource.TestCheckResourceAttr(
						"libvirt_cloudinit.test", "local_hostname", "tango"),
				),
			},
		},
	})
}

func exists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return true, err
}