forked from uchimanajet7/rds-try
/
rds-try_test.go
140 lines (122 loc) · 3.59 KB
/
rds-try_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
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
140
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"
"github.com/BurntSushi/toml"
"github.com/uchimanajet7/rds-try/config"
"github.com/uchimanajet7/rds-try/utils"
)
// need to run the caller always "defer os.RemoveAll(temp_dir)"
func getTestConfig() (*config.Config, string, error) {
aws := config.AWSConfig{
Accesskey: "CF2DC307CC89F49F68F365235AA54BAB2DDD02DA",
SecretKey: "ca485e5b709eae0ceacd68b61cdb28119f13942c71bbb68066c8a3cb45185a39",
}
testName := utils.GetAppName() + "-test"
tempDir, _ := ioutil.TempDir("", testName)
out := config.OutConfig{
Root: tempDir,
File: true,
Bom: true,
}
log := config.LogConfig{
Root: tempDir,
Verbose: true,
JSON: true,
}
rds := config.RDSConfig{
MultiAz: true,
DBId: utils.GetFormatedDBDisplayName(testName),
Region: "us-west-2",
User: "test-admin",
Pass: "pass-pass",
Type: "db.m3.medium",
}
rdsMap := map[string]config.RDSConfig{
"default2": rds,
}
config := &config.Config{
Aws: aws,
Out: out,
Rds: rdsMap,
Log: log,
}
tempFile, err := ioutil.TempFile(tempDir, utils.GetAppName()+"-test")
if err != nil {
return nil, tempDir, err
}
if err := toml.NewEncoder(tempFile).Encode(config); err != nil {
return nil, tempDir, err
}
tempFile.Sync()
tempFile.Close()
os.Args = []string{utils.GetAppName(), "-c=" + tempFile.Name(), "-n=default", "ls"}
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
return config, tempDir, nil
}
func TestResolveArgs(t *testing.T) {
testConfig, tempDir, err := getTestConfig()
defer os.RemoveAll(tempDir)
if err != nil {
t.Errorf("config file for the test was not created: %s", err.Error())
}
// resolve command line
conf, exCode := resolveArgs()
if exCode != 0 || conf == nil {
t.Errorf("config file load error")
}
if !reflect.DeepEqual(testConfig, conf) {
t.Errorf("config data not match: %+v/%+v", testConfig, conf)
}
}
func TestSetLogOptions(t *testing.T) {
testConfig, tempDir, err := getTestConfig()
defer os.RemoveAll(tempDir)
if err != nil {
t.Errorf("config file for the test was not created: %s", err.Error())
}
// resolve command line
conf, exCode := resolveArgs()
if exCode != 0 || conf == nil {
t.Errorf("config file load error")
}
if !reflect.DeepEqual(testConfig, conf) {
t.Errorf("config data not match: %+v/%+v", testConfig, conf)
}
// log setting
logFile, exCode := setLogOptions(conf)
defer logFile.Close()
if exCode != 0 || logFile == nil {
t.Errorf("log setting error")
}
}
func TestGetCommandStruct(t *testing.T) {
testConfig, tempDir, err := getTestConfig()
defer os.RemoveAll(tempDir)
if err != nil {
t.Errorf("config file for the test was not created: %s", err.Error())
}
// resolve command line
conf, exCode := resolveArgs()
if exCode != 0 || conf == nil {
t.Errorf("config file load error")
}
if !reflect.DeepEqual(testConfig, conf) {
t.Errorf("config data not match: %+v/%+v", testConfig, conf)
}
// get base command
// Known error message:[The security token included in the request is invalid.]
t.Log("Known error message:[The security token included in the request is invalid.]")
fmt.Println(" ### Known error message:[The security token included in the request is invalid.]")
commandStruct, exCode := getCommandStruct(conf)
if exCode != 0 || commandStruct == nil {
return
}
if !reflect.DeepEqual(commandStruct.RDSConfig, conf.Rds) {
t.Errorf("RDS config data not match: %+v/%+v", commandStruct.RDSConfig, conf.Rds)
}
}