Skip to content

Commit d2f9ca4

Browse files
committed
Refactor checks and messages
Signed-off-by: Ulysses Souza <[email protected]>
1 parent 3866678 commit d2f9ca4

File tree

2 files changed

+80
-24
lines changed

2 files changed

+80
-24
lines changed

compliance_test.go

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package main
22

33
import (
4+
"fmt"
45
"testing"
56
"time"
6-
7-
"gotest.tools/v3/assert"
87
)
98

109
const (
@@ -17,49 +16,78 @@ const (
1716
)
1817

1918
func TestSimpleLifecycle(t *testing.T) {
20-
h := TestHelper{T: t, testDir: "simple_lifecycle"}
21-
h.TestUpDown(func(t *testing.T) {
19+
h := TestHelper{
20+
T: t,
21+
testDir: "simple_lifecycle",
22+
}
23+
h.TestUpDown(func() {
2224
time.Sleep(time.Second)
2325
})
2426
}
2527

2628
func TestSimpleNetwork(t *testing.T) {
27-
h := TestHelper{T: t, testDir: "simple_network"}
28-
h.TestUpDown(func(t *testing.T) {
29+
h := TestHelper{
30+
T: t,
31+
testDir: "simple_network",
32+
specRef: "Networks-top-level-element",
33+
}
34+
h.TestUpDown(func() {
2935
actual := h.getHttpBody(pingUrl)
30-
assert.Assert(t, actual == "{\"response\":\"PONG FROM TARGET\"}\n")
36+
expected := jsonResponse("PONG FROM TARGET")
37+
h.Check(expected, actual)
3138
})
3239
}
3340

3441
func TestSimpleNetworkFail(t *testing.T) {
35-
h := TestHelper{T: t, testDir: "simple_network"}
36-
h.TestUpDown(func(t *testing.T) {
42+
h := TestHelper{
43+
T: t,
44+
testDir: "simple_network",
45+
specRef: "Networks-top-level-element",
46+
}
47+
h.TestUpDown(func() {
3748
actual := h.getHttpBody("http://localhost:8080/ping?address=notatarget:8080/ping")
38-
assert.Assert(t, actual == "{\"response\":\"Could not reach address: notatarget:8080/ping\"}\n")
49+
expected := jsonResponse("Could not reach address: notatarget:8080/ping")
50+
h.Check(expected, actual)
3951
})
4052
}
4153

4254
func TestDifferentNetworks(t *testing.T) {
43-
h := TestHelper{T: t, testDir: "different_networks"}
44-
h.TestUpDown(func(t *testing.T) {
55+
h := TestHelper{
56+
T: t,
57+
testDir: "different_networks",
58+
specRef: "Networks-top-level-element",
59+
}
60+
h.TestUpDown(func() {
4561
actual := h.getHttpBody(pingUrl)
46-
assert.Assert(t, actual == "{\"response\":\"Could not reach address: target:8080/ping\"}\n")
62+
expected := jsonResponse("Could not reach address: target:8080/ping")
63+
h.Check(expected, actual)
4764
})
4865
}
4966

5067
func TestVolumeFile(t *testing.T) {
51-
h := TestHelper{T: t, testDir: "simple_volume"}
52-
h.TestUpDown(func(t *testing.T) {
68+
h := TestHelper{
69+
T: t,
70+
testDir: "simple_volume",
71+
specRef: "volumes-top-level-element",
72+
}
73+
h.TestUpDown(func() {
5374
actual := h.getHttpBody(volumeUrl + "test_volume.txt")
54-
assert.Assert(t, actual == "{\"response\":\"MYVOLUME\"}\n")
75+
expected := jsonResponse("MYVOLUME")
76+
h.Check(expected, actual)
77+
5578
})
5679
}
5780

5881
func TestSecretFile(t *testing.T) {
59-
h := TestHelper{T: t, testDir: "simple_secretfile"}
60-
h.TestUpDown(func(t *testing.T) {
82+
h := TestHelper{
83+
T: t,
84+
testDir: "simple_secretfile",
85+
specRef: "secrets-top-level-element",
86+
}
87+
h.TestUpDown(func() {
6188
actual := h.getHttpBody(volumeUrl + "test_secret.txt")
62-
assert.Assert(t, actual == "{\"response\":\"MYSECRET\"}\n")
89+
expected := jsonResponse("MYSECRET")
90+
h.Check(expected, actual)
6391
})
6492
}
6593

@@ -68,9 +96,15 @@ func TestConfigFile(t *testing.T) {
6896
T: t,
6997
testDir: "simple_configfile",
7098
skipCommands: []string{"docker-composeV1"},
99+
specRef: "configs-top-level-element",
71100
}
72-
h.TestUpDown(func(t *testing.T) {
101+
h.TestUpDown(func() {
73102
actual := h.getHttpBody(volumeUrl + "test_config.txt")
74-
assert.Assert(t, actual == "{\"response\":\"MYCONFIG\"}\n")
103+
expected := jsonResponse("MYCONFIG")
104+
h.Check(expected, actual)
75105
})
76106
}
107+
108+
func jsonResponse(content string) string {
109+
return fmt.Sprintf("{\"response\":\"%s\"}\n", content)
110+
}

tests_helper.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"fmt"
56
"io/ioutil"
67
"net/http"
78
"os"
@@ -17,7 +18,8 @@ import (
1718
)
1819

1920
const (
20-
commandsDir = "commands"
21+
commandsDir = "commands"
22+
baseSpecReference = "https://github.com/compose-spec/compose-spec/blob/master/spec.md"
2123
)
2224

2325
type Config struct {
@@ -42,9 +44,10 @@ type TestHelper struct {
4244
*testing.T
4345
testDir string
4446
skipCommands []string
47+
specRef string
4548
}
4649

47-
func (h TestHelper) TestUpDown(fun func(t *testing.T)) {
50+
func (h TestHelper) TestUpDown(fun func()) {
4851
assert.Assert(h, fun != nil, "Test function cannot be `nil`")
4952
for _, f := range h.listFiles(commandsDir) {
5053
h.Run(f, func(t *testing.T) {
@@ -56,13 +59,32 @@ func (h TestHelper) TestUpDown(fun func(t *testing.T)) {
5659
}
5760
}
5861
h.executeUp(c)
59-
fun(t)
62+
fun()
6063
h.executeDown(c)
6164
h.checkDown()
6265
})
6366
}
6467
}
6568

69+
func (h TestHelper) Check(expected, actual string) {
70+
assert.Check(h.T, expected == actual, h.assertSpecReferenceMessage(expected, actual))
71+
}
72+
73+
func (h TestHelper) assertSpecReferenceMessage(expected, actual string) string {
74+
return fmt.Sprintf("\n- expected: %q\n+ actual: %q\n%s", expected, actual, h.specReferenceMessage())
75+
}
76+
77+
func (h TestHelper) specReferenceMessage() string {
78+
return "Please refer to: " + h.getSpecReference()
79+
}
80+
81+
func (h TestHelper) getSpecReference() string {
82+
if h.specRef != "" {
83+
return baseSpecReference + "#" + h.specRef
84+
}
85+
return baseSpecReference
86+
}
87+
6688
func (h TestHelper) readConfig(configPath string) (*Config, error) {
6789
b, err := ioutil.ReadFile(configPath)
6890
assert.NilError(h.T, err)

0 commit comments

Comments
 (0)