Skip to content

Commit 26f04ef

Browse files
committed
review: generate test files with go test command
1 parent 35cfcff commit 26f04ef

File tree

6 files changed

+67
-22
lines changed

6 files changed

+67
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ bin
2222
*.dll
2323
*.so
2424
*.dylib
25+
pkg/metricscollector/v1beta1/file-metricscollector/testdata
2526

2627
## Test binary, build with `go test -c`
2728
*.test

pkg/metricscollector/v1beta1/file-metricscollector/file-metricscollector_test.go

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ limitations under the License.
1717
package sidecarmetricscollector
1818

1919
import (
20-
"path"
20+
"os"
21+
"path/filepath"
2122
"reflect"
2223
"testing"
2324
"time"
@@ -27,14 +28,20 @@ import (
2728
"github.com/kubeflow/katib/pkg/controller.v1beta1/consts"
2829
)
2930

31+
var testJsonDataPath = filepath.Join("testdata", "JSON")
32+
3033
func TestCollectObservationLog(t *testing.T) {
31-
const testJsonDataPath = "testdata/JSON"
34+
35+
if err := generateTestFiles(); err != nil {
36+
t.Fatal(err)
37+
}
38+
defer os.RemoveAll(filepath.Dir(testJsonDataPath))
3239

3340
// TODO (tenzen-y): We should add tests for logs in TEXT format.
3441
// Ref: https://github.com/kubeflow/katib/issues/1756
3542
testCases := []struct {
3643
description string
37-
fileName string
44+
filePath string
3845
metrics []string
3946
filters []string
4047
fileFormat commonv1beta1.FileSystemFileFormat
@@ -43,7 +50,7 @@ func TestCollectObservationLog(t *testing.T) {
4350
}{
4451
{
4552
description: "Positive case for logs in JSON format",
46-
fileName: path.Join(testJsonDataPath, "good.json"),
53+
filePath: filepath.Join(testJsonDataPath, "good.json"),
4754
metrics: []string{"acc", "loss"},
4855
fileFormat: commonv1beta1.JsonFormat,
4956
expected: &v1beta1.ObservationLog{
@@ -88,24 +95,24 @@ func TestCollectObservationLog(t *testing.T) {
8895
},
8996
{
9097
description: "Invalid file name",
91-
fileName: "invalid",
98+
filePath: "invalid",
9299
err: true,
93100
},
94101
{
95102
description: "Invalid file format",
96-
fileName: path.Join(testJsonDataPath, "good.json"),
103+
filePath: filepath.Join(testJsonDataPath, "good.json"),
97104
fileFormat: "invalid",
98105
err: true,
99106
},
100107
{
101108
description: "Invalid formatted file for logs in JSON format",
102-
fileName: path.Join(testJsonDataPath, "invalid-format.json"),
109+
filePath: filepath.Join(testJsonDataPath, "invalid-format.json"),
103110
fileFormat: commonv1beta1.JsonFormat,
104111
err: true,
105112
},
106113
{
107114
description: "Invalid timestamp for logs in JSON format",
108-
fileName: path.Join(testJsonDataPath, "invalid-timestamp.json"),
115+
filePath: filepath.Join(testJsonDataPath, "invalid-timestamp.json"),
109116
fileFormat: commonv1beta1.JsonFormat,
110117
metrics: []string{"acc", "loss"},
111118
expected: &v1beta1.ObservationLog{
@@ -129,7 +136,7 @@ func TestCollectObservationLog(t *testing.T) {
129136
},
130137
{
131138
description: "Missing objective metric in training logs",
132-
fileName: path.Join(testJsonDataPath, "missing-objective-metric.json"),
139+
filePath: filepath.Join(testJsonDataPath, "missing-objective-metric.json"),
133140
fileFormat: commonv1beta1.JsonFormat,
134141
metrics: []string{"acc", "loss"},
135142
expected: &v1beta1.ObservationLog{
@@ -148,7 +155,7 @@ func TestCollectObservationLog(t *testing.T) {
148155

149156
for _, test := range testCases {
150157
t.Run(test.description, func(t *testing.T) {
151-
actual, err := CollectObservationLog(test.fileName, test.metrics, test.filters, test.fileFormat)
158+
actual, err := CollectObservationLog(test.filePath, test.metrics, test.filters, test.fileFormat)
152159
if (err != nil) != test.err {
153160
t.Errorf("\nGOT: \n%v\nWANT: %v\n", err, test.err)
154161
} else {
@@ -159,3 +166,52 @@ func TestCollectObservationLog(t *testing.T) {
159166
})
160167
}
161168
}
169+
170+
func generateTestFiles() error {
171+
if _, err := os.Stat(testJsonDataPath); err != nil {
172+
if err = os.MkdirAll(testJsonDataPath, 0700); err != nil {
173+
return err
174+
}
175+
}
176+
177+
testData := []struct {
178+
fileName string
179+
data string
180+
}{
181+
{
182+
fileName: "good.json",
183+
data: `{"checkpoint_path": "", "global_step": "0", "loss": "0.22082142531871796", "timestamp": 1638422847.28721, "trial": "0"}
184+
{"acc": "0.9349666833877563", "checkpoint_path": "", "global_step": "0", "timestamp": 1638422847.287801, "trial": "0"}
185+
{"checkpoint_path": "", "global_step": "1", "loss": "0.1414974331855774", "timestamp": "2021-12-02T14:27:50.000035161Z", "trial": "0"}
186+
{"acc": "0.9586416482925415", "checkpoint_path": "", "global_step": "1", "timestamp": "2021-12-02T14:27:50.000037459Z", "trial": "0"}
187+
{"checkpoint_path": "", "global_step": "2", "loss": "0.10683439671993256", "trial": "0"}
188+
`,
189+
},
190+
{
191+
fileName: "invalid-format.json",
192+
data: `"checkpoint_path": "", "global_step": "0", "loss": "0.22082142531871796", "timestamp": 1638422847.28721, "trial": "0"
193+
{"acc": "0.9349666833877563", "checkpoint_path": "", "global_step": "0", "timestamp": 1638422847.287801, "trial": "0
194+
`,
195+
},
196+
{
197+
fileName: "invalid-timestamp.json",
198+
data: `{"checkpoint_path": "", "global_step": "0", "loss": "0.22082142531871796", "timestamp": "invalid", "trial": "0"}
199+
{"acc": "0.9349666833877563", "checkpoint_path": "", "global_step": "0", "timestamp": 1638422847, "trial": "0"}
200+
`,
201+
}, {
202+
fileName: "missing-objective-metric.json",
203+
data: `{"checkpoint_path": "", "global_step": "0", "loss": "0.22082142531871796", "timestamp": 1638422847.28721, "trial": "0"}
204+
{"checkpoint_path": "", "global_step": "1", "loss": "0.1414974331855774", "timestamp": "2021-12-02T14:27:50.000035161+09:00", "trial": "0"}
205+
{"checkpoint_path": "", "global_step": "2", "loss": "0.10683439671993256", "trial": "0"}`,
206+
},
207+
}
208+
209+
for _, td := range testData {
210+
filePath := filepath.Join(testJsonDataPath, td.fileName)
211+
if err := os.WriteFile(filePath, []byte(td.data), 0600); err != nil {
212+
return err
213+
}
214+
}
215+
216+
return nil
217+
}

pkg/metricscollector/v1beta1/file-metricscollector/testdata/JSON/good.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

pkg/metricscollector/v1beta1/file-metricscollector/testdata/JSON/invalid-format.json

Lines changed: 0 additions & 2 deletions
This file was deleted.

pkg/metricscollector/v1beta1/file-metricscollector/testdata/JSON/invalid-timestamp.json

Lines changed: 0 additions & 2 deletions
This file was deleted.

pkg/metricscollector/v1beta1/file-metricscollector/testdata/JSON/missing-objective-metric.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)