@@ -17,7 +17,8 @@ limitations under the License.
17
17
package sidecarmetricscollector
18
18
19
19
import (
20
- "path"
20
+ "os"
21
+ "path/filepath"
21
22
"reflect"
22
23
"testing"
23
24
"time"
@@ -27,14 +28,20 @@ import (
27
28
"github.com/kubeflow/katib/pkg/controller.v1beta1/consts"
28
29
)
29
30
31
+ var testJsonDataPath = filepath .Join ("testdata" , "JSON" )
32
+
30
33
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 ))
32
39
33
40
// TODO (tenzen-y): We should add tests for logs in TEXT format.
34
41
// Ref: https://github.com/kubeflow/katib/issues/1756
35
42
testCases := []struct {
36
43
description string
37
- fileName string
44
+ filePath string
38
45
metrics []string
39
46
filters []string
40
47
fileFormat commonv1beta1.FileSystemFileFormat
@@ -43,7 +50,7 @@ func TestCollectObservationLog(t *testing.T) {
43
50
}{
44
51
{
45
52
description : "Positive case for logs in JSON format" ,
46
- fileName : path .Join (testJsonDataPath , "good.json" ),
53
+ filePath : filepath .Join (testJsonDataPath , "good.json" ),
47
54
metrics : []string {"acc" , "loss" },
48
55
fileFormat : commonv1beta1 .JsonFormat ,
49
56
expected : & v1beta1.ObservationLog {
@@ -88,24 +95,24 @@ func TestCollectObservationLog(t *testing.T) {
88
95
},
89
96
{
90
97
description : "Invalid file name" ,
91
- fileName : "invalid" ,
98
+ filePath : "invalid" ,
92
99
err : true ,
93
100
},
94
101
{
95
102
description : "Invalid file format" ,
96
- fileName : path .Join (testJsonDataPath , "good.json" ),
103
+ filePath : filepath .Join (testJsonDataPath , "good.json" ),
97
104
fileFormat : "invalid" ,
98
105
err : true ,
99
106
},
100
107
{
101
108
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" ),
103
110
fileFormat : commonv1beta1 .JsonFormat ,
104
111
err : true ,
105
112
},
106
113
{
107
114
description : "Invalid timestamp for logs in JSON format" ,
108
- fileName : path .Join (testJsonDataPath , "invalid-timestamp.json" ),
115
+ filePath : filepath .Join (testJsonDataPath , "invalid-timestamp.json" ),
109
116
fileFormat : commonv1beta1 .JsonFormat ,
110
117
metrics : []string {"acc" , "loss" },
111
118
expected : & v1beta1.ObservationLog {
@@ -129,7 +136,7 @@ func TestCollectObservationLog(t *testing.T) {
129
136
},
130
137
{
131
138
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" ),
133
140
fileFormat : commonv1beta1 .JsonFormat ,
134
141
metrics : []string {"acc" , "loss" },
135
142
expected : & v1beta1.ObservationLog {
@@ -148,7 +155,7 @@ func TestCollectObservationLog(t *testing.T) {
148
155
149
156
for _ , test := range testCases {
150
157
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 )
152
159
if (err != nil ) != test .err {
153
160
t .Errorf ("\n GOT: \n %v\n WANT: %v\n " , err , test .err )
154
161
} else {
@@ -159,3 +166,52 @@ func TestCollectObservationLog(t *testing.T) {
159
166
})
160
167
}
161
168
}
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
+ }
0 commit comments