-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance_test.go
101 lines (96 loc) · 2.38 KB
/
instance_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
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
// Copyright Datajin Technologies, Inc. 2015,2016. All rights reserved.
// Use of this source code is governed by an Artistic-2
// license that can be found in the LICENSE file.
package mktmpio
import (
"os"
"testing"
)
func TestLoadEnv(t *testing.T) {
instance := Instance{
ID: "someId",
Host: "some-host",
Port: 1234,
Type: "mktmpdb",
Username: "user",
Password: "pass",
}
err := instance.LoadEnv()
if err != nil {
t.Error("LoadEnv returned an error:", err)
}
val := os.Getenv("MKTMPDB_HOST")
if val != "some-host" {
t.Error("host env var not 'some-host':", val)
}
val = os.Getenv("MKTMPDB_PORT")
if val != "1234" {
t.Error("port env var not '1234':", val)
}
val = os.Getenv("MKTMPDB_USERNAME")
if val != "user" {
t.Error("user env var not 'user':", val)
}
val = os.Getenv("MKTMPDB_PASSWORD")
if val != "pass" {
t.Error("pssword env var not 'pass':", val)
}
}
func TestCmdNoEnv(t *testing.T) {
instance := Instance{
ID: "someId",
Host: "some-host",
Port: 1234,
Type: "mktmpdb",
Username: "user",
Password: "pass",
RemoteShell: shell{
Cmd: []string{"tmpdbcli", "-h", "some-host", "-p", "1234"},
},
}
cmd := instance.Cmd()
if cmd.Path != "tmpdbcli" {
t.Error("cmd.Path incorrect:", cmd.Path)
}
if cmd.Args[0] != cmd.Path {
t.Error("cmd.Path and cmd.Args[0] should be the same", cmd.Args[0])
}
if len(cmd.Args) != 5 {
t.Error("cmd.Args wrong length:", len(cmd.Args))
}
if cmd.Args[4] != "1234" {
t.Error("int argument was not stringified correctly:", cmd.Args[4])
}
if len(cmd.Env) != 0 {
t.Error("no env variables should be set", cmd.Env)
}
}
func TestCmdWithEnv(t *testing.T) {
instance := Instance{
ID: "someId",
Host: "some-host",
Port: 1234,
Type: "mktmpdb",
Username: "user",
Password: "pass",
RemoteShell: shell{
Cmd: []string{"tmpdbcli", "-h", "some-host", "-p", "1234"},
Env: map[string]string{
"MKTMPIO-DBPASS": "pass",
},
},
}
cmd := instance.Cmd()
if len(cmd.Args) != 5 {
t.Error("cmd.Args wrong length:", len(cmd.Args))
}
if len(cmd.Env) < 1 {
t.Error("cmd.Env should be populated:", cmd.Env)
}
if os.Getenv("MKTMPIO-DBPASS") != "" {
t.Error("real environment should not already contain MKTMPIO-DBPASS")
}
if cmd.Env[len(cmd.Env)-1] != "MKTMPIO-DBPASS=pass" {
t.Error("required shell env var not set:", cmd.Env[len(cmd.Env)-1])
}
}