forked from hashicorp/packer-plugin-tencentcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_config_test.go
More file actions
139 lines (112 loc) · 3.04 KB
/
run_config_test.go
File metadata and controls
139 lines (112 loc) · 3.04 KB
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package cvm
import (
"io/ioutil"
"os"
"testing"
"github.com/hashicorp/packer-plugin-sdk/communicator"
)
func testConfig() *TencentCloudRunConfig {
return &TencentCloudRunConfig{
Zone: "ap-guangzhou",
SourceImageId: "img-qwer1234",
InstanceType: "S3.SMALL2",
Comm: communicator.Config{
SSH: communicator.SSH{
SSHUsername: "tencentcloud",
},
},
}
}
func TestTencentCloudRunConfig_Prepare(t *testing.T) {
cf := testConfig()
if err := cf.Prepare(nil); err != nil {
t.Fatalf("shouldn't have err: %v", err)
}
cf.InstanceType = ""
if err := cf.Prepare(nil); err == nil {
t.Fatal("should have err")
}
cf.InstanceType = "S3.SMALL2"
cf.SourceImageId = ""
if err := cf.Prepare(nil); err == nil {
t.Fatal("should have err")
}
cf.SourceImageId = "img-qwer1234"
cf.Comm.SSHPort = 0
if err := cf.Prepare(nil); err != nil {
t.Fatalf("shouldn't have err: %v", err)
}
if cf.Comm.SSHPort != 22 {
t.Fatalf("invalid ssh port value: %v", cf.Comm.SSHPort)
}
cf.Comm.SSHPort = 44
if err := cf.Prepare(nil); err != nil {
t.Fatalf("shouldn't have err: %v", err)
}
if cf.Comm.SSHPort != 44 {
t.Fatalf("invalid ssh port value: %v", cf.Comm.SSHPort)
}
}
func TestTencentCloudRunConfigPrepare_UserData(t *testing.T) {
cf := testConfig()
tf, err := ioutil.TempFile("", "packer")
if err != nil {
t.Fatalf("new temp file failed: %v", err)
}
defer os.Remove(tf.Name())
defer tf.Close()
cf.UserData = "text user_data"
cf.UserDataFile = tf.Name()
if err := cf.Prepare(nil); err == nil {
t.Fatal("should have error")
}
}
func TestTencentCloudRunConfigPrepare_UserDataFile(t *testing.T) {
cf := testConfig()
cf.UserDataFile = "not-exist-file"
if err := cf.Prepare(nil); err == nil {
t.Fatal("should have error")
}
tf, err := ioutil.TempFile("", "packer")
if err != nil {
t.Fatalf("new temp file failed: %v", err)
}
defer os.Remove(tf.Name())
defer tf.Close()
cf.UserDataFile = tf.Name()
if err := cf.Prepare(nil); err != nil {
t.Fatalf("shouldn't have error: %v", err)
}
}
func TestTencentCloudRunConfigPrepare_TemporaryKeyPairName(t *testing.T) {
cf := testConfig()
cf.Comm.SSHTemporaryKeyPairName = ""
if err := cf.Prepare(nil); err != nil {
t.Fatalf("shouldn't have error: %v", err)
}
if cf.Comm.SSHTemporaryKeyPairName == "" {
t.Fatal("invalid ssh key pair value")
}
cf.Comm.SSHTemporaryKeyPairName = "ssh-key-123"
if err := cf.Prepare(nil); err != nil {
t.Fatalf("shouldn't have error: %v", err)
}
if cf.Comm.SSHTemporaryKeyPairName != "ssh-key-123" {
t.Fatalf("invalid ssh key pair value: %v", cf.Comm.SSHTemporaryKeyPairName)
}
}
func TestTencentCloudRunConfigPrepare_SSHPrivateIp(t *testing.T) {
cf := testConfig()
if cf.SSHPrivateIp != false {
t.Fatalf("invalid ssh_private_ip value: %v", cf.SSHPrivateIp)
}
cf.SSHPrivateIp = true
if err := cf.Prepare(nil); err != nil {
t.Fatalf("shouldn't have error: %v", err)
}
if cf.SSHPrivateIp != true {
t.Fatalf("invalud ssh_private_ip value: %v", cf.SSHPrivateIp)
}
}