-
Notifications
You must be signed in to change notification settings - Fork 42
/
run_test_case.go
111 lines (91 loc) · 2.77 KB
/
run_test_case.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
102
103
104
105
106
107
108
109
110
111
package pkg
import (
"fmt"
"os/exec"
"strings"
"github.com/cyberark/secretless-broker/test/util/testutil"
"github.com/smartystreets/goconvey/convey"
)
const jdbcJARPath = "/secretless/test/util/jdbc/jdbc.jar"
// RunQuery runs a simply test query for the given client configuration and port.
func RunQuery(
clientConfig testutil.ClientConfiguration,
connectPort testutil.ConnectionPort,
) (string, error) {
args := []string{"-c", "select count(*) from test.test"}
connectionParams := []string{"dbname=postgres"}
sslmode := "disable"
if clientConfig.SSL {
sslmode = "require"
}
connectionParams = append(connectionParams, fmt.Sprintf("sslmode=%s", sslmode))
args = append(args, fmt.Sprintf("--username=%s", clientConfig.Username))
connectionParams = append(connectionParams, fmt.Sprintf("password=%s", clientConfig.Password))
args = append(args, fmt.Sprintf("--port=%s", connectPort.ToPortString()))
var host string
switch connectPort.SocketType {
case testutil.TCP:
host = connectPort.Host()
case testutil.Socket:
host = connectPort.ToSocketDir()
default:
panic("Listener Type can only be TCP or Socket")
}
args = append(args, fmt.Sprintf("--host=%s", host))
// join connection params
args = append(args, strings.Join(connectionParams, " "))
// Pre command logs
convey.Println("")
convey.Println("---<< EXECUTED")
convey.Println(strings.Join(append([]string{"psql"}, args...), " "))
cmdOut, err := exec.Command("psql", args...).CombinedOutput()
// Post command logs
//TODO: deal with verbose
if testutil.Verbose {
if err != nil {
convey.Println("--->> RESULTS")
convey.Println("----- ERROR: ")
convey.Println(err.Error())
}
convey.Println("----- OUTPUT: ")
convey.Println(string(cmdOut))
}
convey.Println("---<< END")
convey.Println("")
return string(cmdOut), err
}
// RunQuery runs a simply test query for the given client configuration and port.
func RunJDBCQuery(
clientConfig testutil.ClientConfiguration,
connectPort testutil.ConnectionPort,
) (string, error) {
args := []string{
"-jar", jdbcJARPath,
"-d", "postgres",
"-m", "postgresql",
"-h", fmt.Sprintf("%s:%d", connectPort.Host(), connectPort.Port),
"-U", clientConfig.Username,
"-P", clientConfig.Password,
"select count(*) from test.test",
}
// Pre command logs
convey.Println("")
convey.Println("---->> ARGS")
fmt.Println(args)
convey.Println("---<< EXECUTED")
cmdOut, err := exec.Command("java", args...).CombinedOutput()
// Post command logs
//TODO: deal with verbose
if testutil.Verbose {
if err != nil {
convey.Println("--->> RESULTS")
convey.Println("----- ERROR: ")
convey.Println(err.Error())
}
convey.Println("----- OUTPUT: ")
convey.Println(string(cmdOut))
}
convey.Println("---<< END")
convey.Println("")
return string(cmdOut), err
}