-
Notifications
You must be signed in to change notification settings - Fork 19
/
training_job_test.go
131 lines (91 loc) · 3.54 KB
/
training_job_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package elasticthought
import (
"fmt"
"io/ioutil"
"os/exec"
"path"
"testing"
"github.com/couchbaselabs/go.assert"
"github.com/tleyden/fakehttp"
)
func TestSaveCmdOutputToFiles(t *testing.T) {
j := NewTrainingJob(*NewDefaultConfiguration())
j.Id = "training-job"
j.createWorkDirectory()
// build command args
cmdArgs := []string{}
cmdPath := "pwd"
cmd := exec.Command(cmdPath, cmdArgs...)
stdout, err := cmd.StdoutPipe()
assert.True(t, err == nil)
stderr, err := cmd.StderrPipe()
assert.True(t, err == nil)
err = cmd.Start()
assert.True(t, err == nil)
// read from stdout, stderr and write to temp files
err = saveCmdOutputToFiles(stdout, stderr, j.getStdOutPath(), j.getStdErrPath())
assert.True(t, err == nil)
// wait for the command to complete
err = cmd.Wait()
assert.True(t, err == nil)
// we should have a non-empty stdout/stderr files
stdoutFile := path.Join(j.getWorkDirectory(), "stdout")
stderrFile := path.Join(j.getWorkDirectory(), "stderr")
stdoutBytes, err := ioutil.ReadFile(stdoutFile)
assert.True(t, err == nil)
assert.True(t, len(stdoutBytes) > 0)
stderrBytes, err := ioutil.ReadFile(stderrFile)
assert.True(t, err == nil)
assert.True(t, len(stderrBytes) == 0)
}
func TestUpdateProcessingState(t *testing.T) {
testServer := fakehttp.NewHTTPServerWithPort(NextPort())
testServer.Start()
// response when go-couch tries to see that the server is up
testServer.Response(200, jsonHeaders(), `{"version": "fake"}`)
// response when go-couch check is db exists
testServer.Response(200, jsonHeaders(), `{"db_name": "db"}`)
// first update returns 409
testServer.Response(409, jsonHeaders(), "")
// response to GET to refresh
testServer.Response(200, jsonHeaders(), `{"_id": "training_job", "_rev": "bar", "processing-state": "pending"}`)
// second update succeeds
testServer.Response(200, jsonHeaders(), `{"id": "training_job", "rev": "bar"}`)
configuration := NewDefaultConfiguration()
configuration.DbUrl = fmt.Sprintf("%v/db", testServer.URL)
trainingJob := NewTrainingJob(*configuration)
trainingJob.ElasticThoughtDoc.Id = "training_job"
trainingJob.ElasticThoughtDoc.Revision = "rev"
trainingJob.ProcessingState = Pending
ok, err := trainingJob.UpdateProcessingState(Processing)
assert.True(t, err == nil)
assert.True(t, ok)
}
func TestUpdateModelUrl(t *testing.T) {
docId := "training_job"
expectedTrainedModelUrl := fmt.Sprintf("%v%v/trained.caffemodel", CBFS_URI_PREFIX, docId)
testServer := fakehttp.NewHTTPServerWithPort(NextPort())
testServer.Start()
// response when go-couch tries to see that the server is up
testServer.Response(200, jsonHeaders(), `{"version": "fake"}`)
// response when go-couch check is db exists
testServer.Response(200, jsonHeaders(), `{"db_name": "db"}`)
// first update returns 409
testServer.Response(409, jsonHeaders(), "")
// response to GET to refresh
testServer.Response(200, jsonHeaders(), `{"_id": "training_job", "_rev": "bar"}`)
// second update succeeds
testServer.Response(200, jsonHeaders(), `{"id": "training_job", "rev": "bar"}`)
configuration := NewDefaultConfiguration()
configuration.DbUrl = fmt.Sprintf("%v/db", testServer.URL)
trainingJob := NewTrainingJob(*configuration)
trainingJob.ElasticThoughtDoc.Id = docId
trainingJob.ElasticThoughtDoc.Revision = "rev"
trainingJob.ProcessingState = Pending
err := trainingJob.updateCaffeModelUrl()
assert.True(t, err == nil)
assert.Equals(t, trainingJob.TrainedModelUrl, expectedTrainedModelUrl)
}
func jsonHeaders() map[string]string {
return map[string]string{"Content-Type": "application/json"}
}