@@ -38,12 +38,37 @@ type Opt struct {
38
38
Value string `yaml:"value,omitempty"`
39
39
}
40
40
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 ) {
42
67
b , err := ioutil .ReadFile (configPath )
43
- assert .NilError (t , err )
68
+ assert .NilError (h . T , err )
44
69
c := Config {}
45
70
err = yaml .Unmarshal (b , & c )
46
- assert .NilError (t , err )
71
+ assert .NilError (h . T , err )
47
72
return & c , nil
48
73
}
49
74
@@ -65,27 +90,27 @@ func verbWithOptions(c *Config, v Verb) []string {
65
90
return vOpts
66
91
}
67
92
68
- func executeUp ( t * testing. T , c * Config , configName string ) {
93
+ func ( h TestHelper ) executeUp ( c * Config ) {
69
94
upOpts := verbWithOptions (c , c .Up )
70
- execCmd (t , c . Command , configName , upOpts )
95
+ h . execCmd (c , upOpts )
71
96
}
72
97
73
- func executeDown ( t * testing. T , c * Config , configName string ) {
98
+ func ( h TestHelper ) executeDown ( c * Config ) {
74
99
downOpts := verbWithOptions (c , c .Down )
75
- execCmd (t , c . Command , configName , downOpts )
100
+ h . execCmd (c , downOpts )
76
101
}
77
102
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 )
82
107
}
83
108
84
- func listDirs ( t * testing. T , testDir string ) []string {
109
+ func ( h TestHelper ) listDirs ( testDir string ) []string {
85
110
currDir , err := os .Getwd ()
86
- assert .NilError (t , err )
111
+ assert .NilError (h . T , err )
87
112
files , err := ioutil .ReadDir (filepath .Join (currDir , testDir ))
88
- assert .NilError (t , err )
113
+ assert .NilError (h . T , err )
89
114
var dirs []string
90
115
for _ , f := range files {
91
116
if f .IsDir () && ! strings .HasPrefix (f .Name (), "." ) {
@@ -95,11 +120,11 @@ func listDirs(t *testing.T, testDir string) []string {
95
120
return dirs
96
121
}
97
122
98
- func listFiles ( t * testing. T , dir string ) []string {
123
+ func ( h TestHelper ) listFiles ( dir string ) []string {
99
124
currDir , err := os .Getwd ()
100
- assert .NilError (t , err )
125
+ assert .NilError (h . T , err )
101
126
content , err := ioutil .ReadDir (filepath .Join (currDir , dir ))
102
- assert .NilError (t , err )
127
+ assert .NilError (h . T , err )
103
128
var configFiles []string
104
129
for _ , f := range content {
105
130
if ! f .IsDir () && strings .HasSuffix (f .Name (), ".yml" ) {
@@ -109,46 +134,21 @@ func listFiles(t *testing.T, dir string) []string {
109
134
return configFiles
110
135
}
111
136
112
- func checkDown ( t * testing. T ) {
137
+ func ( h TestHelper ) checkDown ( ) {
113
138
cli , err := client .NewEnvClient ()
114
- assert .NilError (t , err )
139
+ assert .NilError (h . T , err )
115
140
containers , err := cli .ContainerList (context .Background (), types.ContainerListOptions {
116
141
All : true ,
117
142
})
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 )
120
145
}
121
146
122
- func getHttpBody ( t * testing. T , address string ) string {
147
+ func ( h TestHelper ) getHttpBody ( address string ) string {
123
148
resp , err := http .Get (address )
124
- assert .NilError (t , err )
149
+ assert .NilError (h . T , err )
125
150
defer resp .Body .Close ()
126
151
body , err := ioutil .ReadAll (resp .Body )
127
- assert .NilError (t , err )
152
+ assert .NilError (h . T , err )
128
153
return string (body )
129
154
}
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