1
1
package main
2
2
3
3
import (
4
+ "encoding/json"
4
5
"io/ioutil"
5
6
"os"
6
7
"path/filepath"
7
8
"sort"
9
+ "strings"
8
10
"testing"
9
11
10
12
"github.com/docker/infrakit/pkg/spi/instance"
@@ -13,10 +15,12 @@ import (
13
15
)
14
16
15
17
func TestUsage (t * testing.T ) {
18
+ // Test a softlayer_virtual_guest with an @hostname_prefix
16
19
run (t , "softlayer_virtual_guest" , `
17
20
{
18
21
"type": "softlayer_virtual_guest",
19
22
"value": {
23
+ "@hostname_prefix": "softlayer-hostname",
20
24
"cores": 2,
21
25
"memory": 2048,
22
26
"tags": [
@@ -39,6 +43,61 @@ func TestUsage(t *testing.T) {
39
43
}
40
44
` )
41
45
46
+ // Test a softlayer_virtual_guest without an @hostname_prefix
47
+ run (t , "softlayer_virtual_guest" , `
48
+ {
49
+ "type": "softlayer_virtual_guest",
50
+ "value": {
51
+ "cores": 2,
52
+ "memory": 2048,
53
+ "tags": [
54
+ "terraform_demo_swarm_mgr_sl"
55
+ ],
56
+ "connection": {
57
+ "user": "root",
58
+ "private_key": "${file(\"~/.ssh/id_rsa_de\")}"
59
+ },
60
+ "hourly_billing": true,
61
+ "local_disk": true,
62
+ "network_speed": 100,
63
+ "datacenter": "dal10",
64
+ "os_reference_code": "UBUNTU_14_64",
65
+ "domain": "softlayer.com",
66
+ "ssh_key_ids": [
67
+ "${data.softlayer_ssh_key.public_key.id}"
68
+ ]
69
+ }
70
+ }
71
+ ` )
72
+
73
+ // Test a softlayer_virtual_guest with an empty @hostname_prefix
74
+ run (t , "softlayer_virtual_guest" , `
75
+ {
76
+ "type": "softlayer_virtual_guest",
77
+ "value": {
78
+ "@hostname_prefix": " ",
79
+ "cores": 2,
80
+ "memory": 2048,
81
+ "tags": [
82
+ "terraform_demo_swarm_mgr_sl"
83
+ ],
84
+ "connection": {
85
+ "user": "root",
86
+ "private_key": "${file(\"~/.ssh/id_rsa_de\")}"
87
+ },
88
+ "hourly_billing": true,
89
+ "local_disk": true,
90
+ "network_speed": 100,
91
+ "datacenter": "dal10",
92
+ "os_reference_code": "UBUNTU_14_64",
93
+ "domain": "softlayer.com",
94
+ "ssh_key_ids": [
95
+ "${data.softlayer_ssh_key.public_key.id}"
96
+ ]
97
+ }
98
+ }
99
+ ` )
100
+
42
101
run (t , "aws_instance" , `
43
102
{
44
103
"type" : "aws_instance",
@@ -113,6 +172,24 @@ func run(t *testing.T, resourceType, properties string) {
113
172
114
173
props := parsed .Resource [resourceType ][string (* id )]
115
174
175
+ // Unmarshal json for easy access
176
+ var testingData interface {}
177
+ json .Unmarshal ([]byte (properties ), & testingData )
178
+ m := testingData .(map [string ]interface {})
179
+ value , _ := m ["value" ].(map [string ]interface {})
180
+
181
+ // If a hostname was specified, the expectation is that the hostname is appended with the timestamp from the ID
182
+ if value ["@hostname_prefix" ] != nil && strings .Trim (value ["@hostname_prefix" ].(string ), " " ) != "" {
183
+ newID := strings .Replace (string (* id ), "instance-" , "" , - 1 )
184
+ expectedHostname := "softlayer-hostname-" + newID
185
+ require .Equal (t , expectedHostname , props ["hostname" ])
186
+ } else {
187
+ // If no hostname was specified, the hostname should equal the ID
188
+ require .Equal (t , string (* id ), props ["hostname" ])
189
+ }
190
+ // Verify the hostname prefix key/value is no longer in the props
191
+ require .Nil (t , props ["@hostname_prefix" ])
192
+
116
193
switch resourceType {
117
194
case "softlayer_virtual_guest" :
118
195
require .Equal (t , conv ([]interface {}{
0 commit comments