@@ -5,11 +5,11 @@ import (
5
5
"fmt"
6
6
"io"
7
7
"io/ioutil"
8
+ "strings"
8
9
"testing"
9
10
10
11
"github.com/stretchr/testify/assert"
11
12
"github.com/stretchr/testify/mock"
12
- "github.com/stretchr/testify/require"
13
13
14
14
enginemocks "github.com/projecteru2/core/engine/mocks"
15
15
enginetypes "github.com/projecteru2/core/engine/types"
@@ -25,6 +25,7 @@ import (
25
25
)
26
26
27
27
func TestRunAndWaitFailedThenWALCommitted (t * testing.T ) {
28
+ assert := assert .New (t )
28
29
c , _ := newCreateWorkloadCluster (t )
29
30
c .wal = & WAL {WAL : & walmocks.WAL {}}
30
31
@@ -53,19 +54,29 @@ func TestRunAndWaitFailedThenWALCommitted(t *testing.T) {
53
54
mstore .On ("MakeDeployStatus" , mock .Anything , mock .Anything , mock .Anything ).Return (fmt .Errorf ("err" )).Once ()
54
55
55
56
ch , err := c .RunAndWait (context .Background (), opts , make (chan []byte ))
56
- require .NoError (t , err )
57
- require .NotNil (t , ch )
58
- require .False (t , walCommitted )
59
- require .Nil (t , <- ch ) // recv nil due to the ch will be closed.
57
+ assert .NoError (err )
58
+ assert .NotNil (ch )
59
+ assert .False (walCommitted )
60
+ m := <- ch
61
+ assert .Equal (m .WorkloadID , "" )
62
+ assert .True (strings .HasPrefix (string (m .Data ), "Create workload failed" ))
60
63
61
64
lambdaID , exists := opts .Labels [labelLambdaID ]
62
- require .True (t , exists )
63
- require .True (t , len (lambdaID ) > 1 )
64
- require .True (t , walCommitted )
65
+ assert .True (exists )
66
+ assert .True (len (lambdaID ) > 1 )
67
+ assert .True (walCommitted )
68
+ assert .Equal (m .StdStreamType , types .Stderr )
65
69
}
66
70
67
71
func TestLambdaWithWorkloadIDReturned (t * testing.T ) {
68
- c , _ := newLambdaCluster (t )
72
+ assert := assert .New (t )
73
+ c , nodes := newLambdaCluster (t )
74
+ engine := nodes [0 ].Engine .(* enginemocks.API )
75
+
76
+ workload := & types.Workload {ID : "workloadfortonictest" , Engine : engine }
77
+ store := c .store .(* storemocks.Store )
78
+ store .On ("GetWorkload" , mock .Anything , mock .Anything ).Return (workload , nil )
79
+ store .On ("GetWorkloads" , mock .Anything , mock .Anything ).Return ([]* types.Workload {workload }, nil )
69
80
70
81
opts := & types.DeployOptions {
71
82
Name : "zc:name" ,
@@ -79,12 +90,112 @@ func TestLambdaWithWorkloadIDReturned(t *testing.T) {
79
90
},
80
91
}
81
92
93
+ r1 , w1 := io .Pipe ()
94
+ go func () {
95
+ w1 .Write ([]byte ("stdout line1\n " ))
96
+ w1 .Write ([]byte ("stdout line2\n " ))
97
+ w1 .Close ()
98
+ }()
99
+ r2 , w2 := io .Pipe ()
100
+ go func () {
101
+ w2 .Write ([]byte ("stderr line1\n " ))
102
+ w2 .Write ([]byte ("stderr line2\n " ))
103
+ w2 .Close ()
104
+ }()
105
+ engine .On ("VirtualizationLogs" , mock .Anything , mock .Anything ).Return (ioutil .NopCloser (r1 ), ioutil .NopCloser (r2 ), nil )
106
+ engine .On ("VirtualizationWait" , mock .Anything , mock .Anything , mock .Anything ).Return (& enginetypes.VirtualizationWaitResult {Code : 0 }, nil )
107
+
82
108
ch , err := c .RunAndWait (context .Background (), opts , make (chan []byte ))
83
- assert .NoError (t , err )
84
- assert .NotNil (t , ch )
109
+ assert .NoError (err )
110
+ assert .NotNil (ch )
85
111
86
- m := <- ch
87
- require .Equal (t , m .WorkloadID , "workloadfortonictest" )
112
+ ms := []* types.AttachWorkloadMessage {}
113
+ for m := range ch {
114
+ ms = append (ms , m )
115
+ }
116
+ assert .Equal (len (ms ), 8 )
117
+ assert .Equal (ms [0 ].WorkloadID , "workloadfortonictest" )
118
+ assert .Equal (ms [0 ].Data , []byte ("" ))
119
+ assert .Equal (ms [1 ].WorkloadID , "workloadfortonictest" )
120
+ assert .Equal (ms [1 ].Data , []byte ("" ))
121
+ assert .True (strings .HasPrefix (string (ms [7 ].Data ), exitDataPrefix ))
122
+ assert .True (strings .HasPrefix (string (ms [6 ].Data ), exitDataPrefix ))
123
+ }
124
+
125
+ func TestLambdaWithError (t * testing.T ) {
126
+ assert := assert .New (t )
127
+ c , nodes := newLambdaCluster (t )
128
+ engine := nodes [0 ].Engine .(* enginemocks.API )
129
+
130
+ workload := & types.Workload {ID : "workloadfortonictest" , Engine : engine }
131
+ store := c .store .(* storemocks.Store )
132
+ store .On ("GetWorkloads" , mock .Anything , mock .Anything ).Return ([]* types.Workload {workload }, nil )
133
+
134
+ opts := & types.DeployOptions {
135
+ Name : "zc:name" ,
136
+ Count : 2 ,
137
+ DeployStrategy : strategy .Auto ,
138
+ Podname : "p1" ,
139
+ ResourceOpts : types.ResourceOptions {CPUQuotaLimit : 1 },
140
+ Image : "zc:test" ,
141
+ Entrypoint : & types.Entrypoint {
142
+ Name : "good-entrypoint" ,
143
+ },
144
+ }
145
+
146
+ store .On ("GetWorkload" , mock .Anything , mock .Anything ).Return (nil , fmt .Errorf ("error" )).Twice ()
147
+ ch0 , err := c .RunAndWait (context .Background (), opts , make (chan []byte ))
148
+ assert .NoError (err )
149
+ assert .NotNil (ch0 )
150
+ m0 := <- ch0
151
+ assert .Equal (m0 .WorkloadID , "workloadfortonictest" )
152
+ assert .True (strings .HasPrefix (string (m0 .Data ), "Get workload" ))
153
+
154
+ store .On ("GetWorkload" , mock .Anything , mock .Anything ).Return (workload , nil )
155
+
156
+ engine .On ("VirtualizationLogs" , mock .Anything , mock .Anything ).Return (nil , nil , fmt .Errorf ("error" )).Twice ()
157
+ ch1 , err := c .RunAndWait (context .Background (), opts , make (chan []byte ))
158
+ assert .NoError (err )
159
+ assert .NotNil (ch1 )
160
+ m1 := <- ch1
161
+ assert .Equal (m1 .WorkloadID , "workloadfortonictest" )
162
+ assert .True (strings .HasPrefix (string (m1 .Data ), "Fetch log for workload" ))
163
+
164
+ r1 , w1 := io .Pipe ()
165
+ go func () {
166
+ w1 .Write ([]byte ("stdout line1\n " ))
167
+ w1 .Write ([]byte ("stdout line2\n " ))
168
+ w1 .Close ()
169
+ }()
170
+ r2 , w2 := io .Pipe ()
171
+ go func () {
172
+ w2 .Write ([]byte ("stderr line1\n " ))
173
+ w2 .Write ([]byte ("stderr line2\n " ))
174
+ w2 .Close ()
175
+ }()
176
+ engine .On ("VirtualizationLogs" , mock .Anything , mock .Anything ).Return (ioutil .NopCloser (r1 ), ioutil .NopCloser (r2 ), nil )
177
+
178
+ engine .On ("VirtualizationWait" , mock .Anything , mock .Anything , mock .Anything ).Return (nil , fmt .Errorf ("error" ))
179
+ ch2 , err := c .RunAndWait (context .Background (), opts , make (chan []byte ))
180
+ assert .NoError (err )
181
+ assert .NotNil (ch2 )
182
+
183
+ ms := []* types.AttachWorkloadMessage {}
184
+ for m := range ch2 {
185
+ ms = append (ms , m )
186
+ }
187
+ assert .Equal (len (ms ), 8 )
188
+ assert .Equal (ms [0 ].WorkloadID , "workloadfortonictest" )
189
+ assert .Equal (ms [0 ].Data , []byte ("" ))
190
+ assert .Equal (ms [1 ].WorkloadID , "workloadfortonictest" )
191
+ assert .Equal (ms [1 ].Data , []byte ("" ))
192
+
193
+ m2 := ms [7 ]
194
+ assert .Equal (m2 .WorkloadID , "workloadfortonictest" )
195
+ assert .True (strings .HasPrefix (string (m2 .Data ), "Wait workload" ))
196
+ m3 := ms [6 ]
197
+ assert .Equal (m3 .WorkloadID , "workloadfortonictest" )
198
+ assert .True (strings .HasPrefix (string (m3 .Data ), "Wait workload" ))
88
199
}
89
200
90
201
func newLambdaCluster (t * testing.T ) (* Calcium , []* types.Node ) {
@@ -167,21 +278,6 @@ func newLambdaCluster(t *testing.T) (*Calcium, []*types.Node) {
167
278
engine .On ("ImageLocalDigests" , mock .Anything , mock .Anything ).Return ([]string {"" }, nil )
168
279
engine .On ("ImageRemoteDigest" , mock .Anything , mock .Anything ).Return ("" , nil )
169
280
170
- r1 , w1 := io .Pipe ()
171
- go func () {
172
- w1 .Write ([]byte ("stdout line1\n " ))
173
- w1 .Write ([]byte ("stdout line2\n " ))
174
- w1 .Close ()
175
- }()
176
- r2 , w2 := io .Pipe ()
177
- go func () {
178
- w2 .Write ([]byte ("stderr line1\n " ))
179
- w2 .Write ([]byte ("stderr line2\n " ))
180
- w2 .Close ()
181
- }()
182
- engine .On ("VirtualizationLogs" , mock .Anything , mock .Anything ).Return (ioutil .NopCloser (r1 ), ioutil .NopCloser (r2 ), nil )
183
- engine .On ("VirtualizationWait" , mock .Anything , mock .Anything , mock .Anything ).Return (& enginetypes.VirtualizationWaitResult {Code : 0 })
184
-
185
281
// doCreateAndStartWorkload fails: AddWorkload
186
282
engine .On ("VirtualizationCreate" , mock .Anything , mock .Anything ).Return (& enginetypes.VirtualizationCreated {ID : "workloadfortonictest" }, nil )
187
283
engine .On ("VirtualizationStart" , mock .Anything , mock .Anything ).Return (nil )
@@ -190,7 +286,5 @@ func newLambdaCluster(t *testing.T) (*Calcium, []*types.Node) {
190
286
engine .On ("VirtualizationInspect" , mock .Anything , mock .Anything ).Return (& enginetypes.VirtualizationInfo {}, nil )
191
287
store .On ("AddWorkload" , mock .Anything , mock .Anything ).Return (nil )
192
288
193
- workload := & types.Workload {ID : "workloadfortonictest" , Engine : engine }
194
- store .On ("GetWorkload" , mock .Anything , mock .Anything ).Return (workload , nil )
195
289
return c , nodes
196
290
}
0 commit comments