Skip to content

Commit 3866678

Browse files
committed
Refactor
- Pass most of the funcs to TestHelper Signed-off-by: Ulysses Souza <[email protected]>
1 parent 576ff62 commit 3866678

File tree

2 files changed

+62
-62
lines changed

2 files changed

+62
-62
lines changed

compliance_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,47 @@ const (
1818

1919
func TestSimpleLifecycle(t *testing.T) {
2020
h := TestHelper{T: t, testDir: "simple_lifecycle"}
21-
h.testUpDown(func(t *testing.T) {
21+
h.TestUpDown(func(t *testing.T) {
2222
time.Sleep(time.Second)
2323
})
2424
}
2525

2626
func TestSimpleNetwork(t *testing.T) {
2727
h := TestHelper{T: t, testDir: "simple_network"}
28-
h.testUpDown(func(t *testing.T) {
29-
actual := getHttpBody(t, pingUrl)
28+
h.TestUpDown(func(t *testing.T) {
29+
actual := h.getHttpBody(pingUrl)
3030
assert.Assert(t, actual == "{\"response\":\"PONG FROM TARGET\"}\n")
3131
})
3232
}
3333

3434
func TestSimpleNetworkFail(t *testing.T) {
3535
h := TestHelper{T: t, testDir: "simple_network"}
36-
h.testUpDown(func(t *testing.T) {
37-
actual := getHttpBody(t, "http://localhost:8080/ping?address=notatarget:8080/ping")
36+
h.TestUpDown(func(t *testing.T) {
37+
actual := h.getHttpBody("http://localhost:8080/ping?address=notatarget:8080/ping")
3838
assert.Assert(t, actual == "{\"response\":\"Could not reach address: notatarget:8080/ping\"}\n")
3939
})
4040
}
4141

4242
func TestDifferentNetworks(t *testing.T) {
4343
h := TestHelper{T: t, testDir: "different_networks"}
44-
h.testUpDown(func(t *testing.T) {
45-
actual := getHttpBody(t, pingUrl)
44+
h.TestUpDown(func(t *testing.T) {
45+
actual := h.getHttpBody(pingUrl)
4646
assert.Assert(t, actual == "{\"response\":\"Could not reach address: target:8080/ping\"}\n")
4747
})
4848
}
4949

5050
func TestVolumeFile(t *testing.T) {
5151
h := TestHelper{T: t, testDir: "simple_volume"}
52-
h.testUpDown(func(t *testing.T) {
53-
actual := getHttpBody(t, volumeUrl+"test_volume.txt")
52+
h.TestUpDown(func(t *testing.T) {
53+
actual := h.getHttpBody(volumeUrl + "test_volume.txt")
5454
assert.Assert(t, actual == "{\"response\":\"MYVOLUME\"}\n")
5555
})
5656
}
5757

5858
func TestSecretFile(t *testing.T) {
5959
h := TestHelper{T: t, testDir: "simple_secretfile"}
60-
h.testUpDown(func(t *testing.T) {
61-
actual := getHttpBody(t, volumeUrl+"test_secret.txt")
60+
h.TestUpDown(func(t *testing.T) {
61+
actual := h.getHttpBody(volumeUrl + "test_secret.txt")
6262
assert.Assert(t, actual == "{\"response\":\"MYSECRET\"}\n")
6363
})
6464
}
@@ -69,8 +69,8 @@ func TestConfigFile(t *testing.T) {
6969
testDir: "simple_configfile",
7070
skipCommands: []string{"docker-composeV1"},
7171
}
72-
h.testUpDown(func(t *testing.T) {
73-
actual := getHttpBody(t, volumeUrl+"test_config.txt")
72+
h.TestUpDown(func(t *testing.T) {
73+
actual := h.getHttpBody(volumeUrl + "test_config.txt")
7474
assert.Assert(t, actual == "{\"response\":\"MYCONFIG\"}\n")
7575
})
7676
}

tests_helper.go

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,37 @@ type Opt struct {
3838
Value string `yaml:"value,omitempty"`
3939
}
4040

41-
func readConfig(t *testing.T, configPath string) (*Config, error) {
41+
type TestHelper struct {
42+
*testing.T
43+
testDir string
44+
skipCommands []string
45+
}
46+
47+
func (h TestHelper) TestUpDown(fun func(t *testing.T)) {
48+
assert.Assert(h, fun != nil, "Test function cannot be `nil`")
49+
for _, f := range h.listFiles(commandsDir) {
50+
h.Run(f, func(t *testing.T) {
51+
c, err := h.readConfig(filepath.Join(commandsDir, f))
52+
assert.NilError(t, err)
53+
for _, v := range h.skipCommands {
54+
if v == c.Name {
55+
t.SkipNow()
56+
}
57+
}
58+
h.executeUp(c)
59+
fun(t)
60+
h.executeDown(c)
61+
h.checkDown()
62+
})
63+
}
64+
}
65+
66+
func (h TestHelper) readConfig(configPath string) (*Config, error) {
4267
b, err := ioutil.ReadFile(configPath)
43-
assert.NilError(t, err)
68+
assert.NilError(h.T, err)
4469
c := Config{}
4570
err = yaml.Unmarshal(b, &c)
46-
assert.NilError(t, err)
71+
assert.NilError(h.T, err)
4772
return &c, nil
4873
}
4974

@@ -65,27 +90,27 @@ func verbWithOptions(c *Config, v Verb) []string {
6590
return vOpts
6691
}
6792

68-
func executeUp(t *testing.T, c *Config, configName string) {
93+
func (h TestHelper) executeUp(c *Config) {
6994
upOpts := verbWithOptions(c, c.Up)
70-
execCmd(t, c.Command, configName, upOpts)
95+
h.execCmd(c, upOpts)
7196
}
7297

73-
func executeDown(t *testing.T, c *Config, configName string) {
98+
func (h TestHelper) executeDown(c *Config) {
7499
downOpts := verbWithOptions(c, c.Down)
75-
execCmd(t, c.Command, configName, downOpts)
100+
h.execCmd(c, downOpts)
76101
}
77102

78-
func execCmd(t *testing.T, command string, configName string, opts []string) {
79-
cmd := icmd.Command(command, opts...)
80-
cmd.Dir = filepath.Join("tests", configName)
81-
icmd.RunCmd(cmd).Assert(t, icmd.Success)
103+
func (h TestHelper) execCmd(c *Config, opts []string) {
104+
cmd := icmd.Command(c.Command, opts...)
105+
cmd.Dir = filepath.Join("tests", h.testDir)
106+
icmd.RunCmd(cmd).Assert(h.T, icmd.Success)
82107
}
83108

84-
func listDirs(t *testing.T, testDir string) []string {
109+
func (h TestHelper) listDirs(testDir string) []string {
85110
currDir, err := os.Getwd()
86-
assert.NilError(t, err)
111+
assert.NilError(h.T, err)
87112
files, err := ioutil.ReadDir(filepath.Join(currDir, testDir))
88-
assert.NilError(t, err)
113+
assert.NilError(h.T, err)
89114
var dirs []string
90115
for _, f := range files {
91116
if f.IsDir() && !strings.HasPrefix(f.Name(), ".") {
@@ -95,11 +120,11 @@ func listDirs(t *testing.T, testDir string) []string {
95120
return dirs
96121
}
97122

98-
func listFiles(t *testing.T, dir string) []string {
123+
func (h TestHelper) listFiles(dir string) []string {
99124
currDir, err := os.Getwd()
100-
assert.NilError(t, err)
125+
assert.NilError(h.T, err)
101126
content, err := ioutil.ReadDir(filepath.Join(currDir, dir))
102-
assert.NilError(t, err)
127+
assert.NilError(h.T, err)
103128
var configFiles []string
104129
for _, f := range content {
105130
if !f.IsDir() && strings.HasSuffix(f.Name(), ".yml") {
@@ -109,46 +134,21 @@ func listFiles(t *testing.T, dir string) []string {
109134
return configFiles
110135
}
111136

112-
func checkDown(t *testing.T) {
137+
func (h TestHelper) checkDown() {
113138
cli, err := client.NewEnvClient()
114-
assert.NilError(t, err)
139+
assert.NilError(h.T, err)
115140
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{
116141
All: true,
117142
})
118-
assert.NilError(t, err)
119-
assert.Assert(t, len(containers) == 0)
143+
assert.NilError(h.T, err)
144+
assert.Assert(h.T, len(containers) == 0)
120145
}
121146

122-
func getHttpBody(t *testing.T, address string) string {
147+
func (h TestHelper) getHttpBody(address string) string {
123148
resp, err := http.Get(address)
124-
assert.NilError(t, err)
149+
assert.NilError(h.T, err)
125150
defer resp.Body.Close()
126151
body, err := ioutil.ReadAll(resp.Body)
127-
assert.NilError(t, err)
152+
assert.NilError(h.T, err)
128153
return string(body)
129154
}
130-
131-
type TestHelper struct {
132-
*testing.T
133-
testDir string
134-
skipCommands []string
135-
}
136-
137-
func (h TestHelper) testUpDown(fun func(t *testing.T)) {
138-
assert.Assert(h, fun != nil, "Test function cannot be `nil`")
139-
for _, f := range listFiles(h.T, commandsDir) {
140-
h.Run(f, func(t *testing.T) {
141-
c, err := readConfig(t, filepath.Join(commandsDir, f))
142-
assert.NilError(t, err)
143-
for _, v := range h.skipCommands {
144-
if v == c.Name {
145-
t.SkipNow()
146-
}
147-
}
148-
executeUp(t, c, h.testDir)
149-
fun(t)
150-
executeDown(t, c, h.testDir)
151-
checkDown(t)
152-
})
153-
}
154-
}

0 commit comments

Comments
 (0)