-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlinux_test.go
76 lines (67 loc) · 1.99 KB
/
linux_test.go
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
package linux
import (
"context"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"strings"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var testAccProviders map[string]*schema.Provider
var testAccProvider = tfmap{
attrProviderHost: `"127.0.0.1"`,
attrProviderPort: `22`,
attrProviderUser: `"root"`,
attrProviderPassword: `"root"`,
}
var testAccOverridenProvider = tfmap{
attrProviderHost: `"8.8.8.8"`,
attrProviderPort: `2222`,
attrProviderUser: `"something"`,
attrProviderPassword: `"else"`,
attrProviderTimeout: "1",
}
func testAccInit() {
testAccProviders = map[string]*schema.Provider{
"linux": Provider(),
}
if f, err := ioutil.ReadFile("testdata/id_rsa"); err == nil {
testAccProvider.With(attrProviderPrivateKey, string(f))
}
if v, ok := os.LookupEnv("TEST_ACC_LINUX_PROVIDER_HOST"); ok {
testAccProvider.With(attrProviderHost, fmt.Sprintf(`"%s"`, v))
}
if v, ok := os.LookupEnv("TEST_ACC_LINUX_PROVIDER_PORT"); ok {
testAccProvider.With(attrProviderPort, v)
}
if v, ok := os.LookupEnv("TEST_ACC_LINUX_PROVIDER_USER"); ok {
testAccProvider.With(attrProviderUser, fmt.Sprintf(`"%s"`, v))
}
if v, ok := os.LookupEnv("TEST_ACC_LINUX_PROVIDER_PASSWORD"); ok {
testAccProvider.With(attrProviderPassword, fmt.Sprintf(`"%s"`, v))
}
if v, ok := os.LookupEnv("TEST_ACC_LINUX_PROVIDER_PRIVATE_KEY"); ok {
testAccProvider.With(attrProviderPrivateKey, fmt.Sprintf(`"%s"`, v))
}
}
func testAccPreCheckConnection(t *testing.T) func() {
return func() {
conf := map[string]string{
attrProviderHost: strings.ReplaceAll(testAccProvider[attrProviderHost], `"`, ``),
attrProviderPort: testAccProvider[attrProviderPort],
}
err := (&linux{connInfo: conf}).init(context.Background())
var errNet net.Error
if errors.As(err, &errNet) {
t.Fatalf("ssh connection should be available: %v", err)
}
}
}
func TestMain(m *testing.M) {
testAccInit()
resource.TestMain(m)
}