Skip to content

Commit 912e9f1

Browse files
jschwinger233CMGS
authored andcommitted
fix testcases
1 parent 02d0656 commit 912e9f1

File tree

9 files changed

+68
-41
lines changed

9 files changed

+68
-41
lines changed

cluster/calcium/copy_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ func TestCopy(t *testing.T) {
4848
workload.Engine = engine
4949
store.On("GetWorkload", mock.Anything, mock.Anything).Return(workload, nil)
5050
// failed by VirtualizationCopyFrom
51-
engine.On("VirtualizationCopyFrom", mock.Anything, mock.Anything, mock.Anything).Return(nil, "", types.ErrNilEngine).Twice()
51+
engine.On("VirtualizationCopyFrom", mock.Anything, mock.Anything, mock.Anything).Return(nil, 0, 0, int64(0), types.ErrNilEngine).Twice()
5252
ch, err = c.Copy(ctx, opts)
5353
assert.NoError(t, err)
5454
for r := range ch {
5555
assert.Error(t, r.Error)
5656
}
57-
engine.On("VirtualizationCopyFrom", mock.Anything, mock.Anything, mock.Anything).Return(nil, "omg", nil)
57+
engine.On("VirtualizationCopyFrom", mock.Anything, mock.Anything, mock.Anything).Return([]byte("omg"), 0, 0, int64(0), nil)
5858
// success
5959
ch, err = c.Copy(ctx, opts)
6060
assert.NoError(t, err)

cluster/calcium/replace_test.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package calcium
22

33
import (
4-
"bytes"
54
"context"
6-
"io/ioutil"
75
"testing"
86

97
enginemocks "github.com/projecteru2/core/engine/mocks"
@@ -141,7 +139,7 @@ func TestReplaceWorkload(t *testing.T) {
141139
store.On("GetNode", mock.Anything, mock.Anything).Return(node, nil)
142140
// failed by VirtualizationCopyFrom
143141
opts.Copy = map[string]string{"src": "dst"}
144-
engine.On("VirtualizationCopyFrom", mock.Anything, mock.Anything, mock.Anything).Return(nil, "", types.ErrBadWorkloadID).Once()
142+
engine.On("VirtualizationCopyFrom", mock.Anything, mock.Anything, mock.Anything).Return(nil, 0, 0, int64(0), types.ErrBadWorkloadID).Once()
145143
ch, err = c.ReplaceWorkload(ctx, opts)
146144
assert.NoError(t, err)
147145
for r := range ch {
@@ -152,8 +150,7 @@ func TestReplaceWorkload(t *testing.T) {
152150
store.AssertExpectations(t)
153151
engine.AssertExpectations(t)
154152

155-
engine.On("VirtualizationCopyFrom", mock.Anything, mock.Anything, mock.Anything).Return(ioutil.NopCloser(bytes.NewReader([]byte{})), "", nil)
156-
opts.DeployOptions.Data = map[string]types.ReaderManager{}
153+
engine.On("VirtualizationCopyFrom", mock.Anything, mock.Anything, mock.Anything).Return([]byte{}, 0, 0, int64(0), nil)
157154
// failed by Stop
158155
engine.On("VirtualizationStop", mock.Anything, mock.Anything, mock.Anything).Return(types.ErrCannotGetEngine).Once()
159156
engine.On("VirtualizationStart", mock.Anything, mock.Anything).Return(types.ErrCannotGetEngine).Once()
@@ -186,7 +183,7 @@ func TestReplaceWorkload(t *testing.T) {
186183

187184
engine.On("VirtualizationCreate", mock.Anything, mock.Anything).Return(&enginetypes.VirtualizationCreated{ID: "new"}, nil)
188185
engine.On("VirtualizationStart", mock.Anything, mock.Anything).Return(nil)
189-
engine.On("VirtualizationCopyTo", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
186+
engine.On("VirtualizationCopyTo", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
190187
engine.On("VirtualizationInspect", mock.Anything, mock.Anything).Return(&enginetypes.VirtualizationInfo{User: "test"}, nil)
191188
store.On("AddWorkload", mock.Anything, mock.Anything, mock.Anything).Return(nil)
192189
// failed by remove workload

cluster/calcium/send_test.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ func TestSend(t *testing.T) {
1919
c := NewTestCluster()
2020
ctx := context.Background()
2121

22-
_, err := c.Send(ctx, &types.SendOptions{IDs: []string{}, Data: map[string][]byte{"xxx": {}}})
22+
_, err := c.Send(ctx, &types.SendOptions{IDs: []string{}, Files: []types.LinuxFile{{Content: []byte("xxx")}}})
2323
assert.Error(t, err)
24-
_, err = c.Send(ctx, &types.SendOptions{IDs: []string{"id"}, Data: map[string][]byte{}})
24+
_, err = c.Send(ctx, &types.SendOptions{IDs: []string{"id"}})
2525
assert.Error(t, err)
2626

2727
tmpfile, err := ioutil.TempFile("", "example")
@@ -30,8 +30,11 @@ func TestSend(t *testing.T) {
3030
defer tmpfile.Close()
3131
opts := &types.SendOptions{
3232
IDs: []string{"cid"},
33-
Data: map[string][]byte{
34-
"/tmp/1": {},
33+
Files: []types.LinuxFile{
34+
{
35+
Filename: "/tmp/1",
36+
Content: []byte{},
37+
},
3538
},
3639
}
3740
store := &storemocks.Store{}
@@ -53,10 +56,11 @@ func TestSend(t *testing.T) {
5356
)
5457
// failed by engine
5558
content, _ := ioutil.ReadAll(tmpfile)
56-
opts.Data["/tmp/1"] = content
59+
opts.Files[0].Content = content
5760
engine.On("VirtualizationCopyTo",
5861
mock.Anything, mock.Anything, mock.Anything,
5962
mock.Anything, mock.Anything, mock.Anything,
63+
mock.Anything,
6064
).Return(types.ErrCannotGetEngine).Once()
6165
ch, err = c.Send(ctx, opts)
6266
assert.NoError(t, err)
@@ -67,6 +71,7 @@ func TestSend(t *testing.T) {
6771
engine.On("VirtualizationCopyTo",
6872
mock.Anything, mock.Anything, mock.Anything,
6973
mock.Anything, mock.Anything, mock.Anything,
74+
mock.Anything,
7075
).Return(nil)
7176
ch, err = c.Send(ctx, opts)
7277
assert.NoError(t, err)

engine/mocks/fakeengine/mock.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func MakeClient(ctx context.Context, config coretypes.Config, nodename, endpoint
9292
ch <- enginetypes.VirtualizationRemapMessage{ID: ID}
9393
close(ch)
9494
e.On("VirtualizationResourceRemap", mock.Anything, mock.Anything).Return((<-chan enginetypes.VirtualizationRemapMessage)(ch), nil)
95-
e.On("VirtualizationCopyTo", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
95+
e.On("VirtualizationCopyTo", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
9696
e.On("VirtualizationStart", mock.Anything, mock.Anything).Return(nil)
9797
e.On("VirtualizationStop", mock.Anything, mock.Anything, mock.Anything).Return(nil)
9898
e.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
@@ -106,8 +106,8 @@ func MakeClient(ctx context.Context, config coretypes.Config, nodename, endpoint
106106
e.On("VirtualizationResize", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
107107
e.On("VirtualizationWait", mock.Anything, mock.Anything, mock.Anything).Return(&enginetypes.VirtualizationWaitResult{Message: "", Code: 0}, nil)
108108
e.On("VirtualizationUpdateResource", mock.Anything, mock.Anything, mock.Anything).Return(nil)
109-
copyData := ioutil.NopCloser(bytes.NewBufferString("d1...\nd2...\n"))
110-
e.On("VirtualizationCopyFrom", mock.Anything, mock.Anything, mock.Anything).Return(copyData, "", nil)
109+
110+
e.On("VirtualizationCopyFrom", mock.Anything, mock.Anything, mock.Anything).Return([]byte("d1...\nd2...\n"), 0, 0, int64(0), nil)
111111
e.On("ResourceValidate", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
112112
return e, nil
113113
}

store/etcdv3/node_test.go

+30-5
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,23 @@ func TestSetNodeStatus(t *testing.T) {
234234
node := &types.Node{
235235
NodeMeta: types.NodeMeta{
236236
Name: "testname",
237-
Endpoint: "ep",
237+
Endpoint: "mock://",
238238
Podname: "testpod",
239239
},
240240
}
241+
_, err := m.AddPod(context.TODO(), node.Podname, "")
242+
assert.NoError(err)
243+
_, err = m.AddNode(context.TODO(), &types.AddNodeOptions{
244+
Nodename: node.Name,
245+
Endpoint: node.Endpoint,
246+
Podname: node.Podname,
247+
})
248+
assert.NoError(err)
241249
assert.NoError(m.SetNodeStatus(context.TODO(), node, 1))
242250
key := filepath.Join(nodeStatusPrefix, node.Name)
243251

244252
// not expired yet
245-
_, err := m.GetOne(context.TODO(), key)
253+
_, err = m.GetOne(context.TODO(), key)
246254
assert.NoError(err)
247255
// expired
248256
time.Sleep(2000 * time.Millisecond)
@@ -257,10 +265,18 @@ func TestGetNodeStatus(t *testing.T) {
257265
node := &types.Node{
258266
NodeMeta: types.NodeMeta{
259267
Name: "testname",
260-
Endpoint: "ep",
268+
Endpoint: "mock://",
261269
Podname: "testpod",
262270
},
263271
}
272+
_, err := m.AddPod(context.TODO(), node.Podname, "")
273+
assert.NoError(err)
274+
_, err = m.AddNode(context.TODO(), &types.AddNodeOptions{
275+
Nodename: node.Name,
276+
Endpoint: node.Endpoint,
277+
Podname: node.Podname,
278+
})
279+
assert.NoError(err)
264280
assert.NoError(m.SetNodeStatus(context.TODO(), node, 1))
265281

266282
// not expired yet
@@ -269,7 +285,7 @@ func TestGetNodeStatus(t *testing.T) {
269285
assert.Equal(ns.Nodename, node.Name)
270286
assert.True(ns.Alive)
271287
// expired
272-
time.Sleep(2000 * time.Millisecond)
288+
time.Sleep(2 * time.Second)
273289
ns1, err := m.GetNodeStatus(context.TODO(), node.Name)
274290
assert.Error(err)
275291
assert.Nil(ns1)
@@ -282,11 +298,20 @@ func TestNodeStatusStream(t *testing.T) {
282298
node := &types.Node{
283299
NodeMeta: types.NodeMeta{
284300
Name: "testname",
285-
Endpoint: "ep",
301+
Endpoint: "mock://",
286302
Podname: "testpod",
287303
},
288304
}
289305

306+
_, err := m.AddPod(context.TODO(), node.Podname, "")
307+
assert.NoError(err)
308+
_, err = m.AddNode(context.TODO(), &types.AddNodeOptions{
309+
Nodename: node.Name,
310+
Endpoint: node.Endpoint,
311+
Podname: node.Podname,
312+
})
313+
assert.NoError(err)
314+
290315
go func() {
291316
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
292317
defer cancel()

types/helper_test.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
package types
22

33
import (
4-
"strconv"
54
"testing"
65

76
"github.com/stretchr/testify/assert"
87
)
98

109
func TestRound(t *testing.T) {
11-
f := func(f float64) string {
12-
return strconv.FormatFloat(f, 'f', -1, 64)
13-
}
1410
a := 0.0199999998
15-
assert.Equal(t, f(Round(a)), "0.02")
11+
assert.InDelta(t, Round(a), 0.02, 1e-5)
1612
a = 0.1999998
17-
assert.Equal(t, f(Round(a)), "0.2")
13+
assert.InDelta(t, Round(a), 0.2, 1e-5)
1814
a = 1.999998
19-
assert.Equal(t, f(Round(a)), "1.999998")
15+
assert.InDelta(t, Round(a), 1.999998, 1e-6)
2016
a = 19.99998
21-
assert.Equal(t, f(Round(a)), "19.99998")
17+
assert.InDelta(t, (Round(a)), 19.99998, 1e-6)
2218
}

types/options_test.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,15 @@ func TestSendOptions(t *testing.T) {
8989
o.IDs = []string{"workload_id1", "workload_id2"}
9090
assert.Equal(ErrNoFilesToSend, errors.Unwrap(o.Validate()))
9191

92-
o.Data = map[string][]byte{
93-
"filepath1": []byte("filecontent1"),
94-
"filepath2": []byte("filecontent2"),
92+
o.Files = []LinuxFile{
93+
{
94+
Filename: "filepath1",
95+
Content: []byte("filecontent1"),
96+
},
97+
{
98+
Filename: "filepath2",
99+
Content: []byte("filecontent2"),
100+
},
95101
}
96102
assert.NoError(o.Validate())
97103
}

utils/utils.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"io"
1010
"io/ioutil"
11+
"math"
1112
"math/big"
1213
"os"
1314
"reflect"
@@ -193,7 +194,7 @@ func TempFile(stream io.ReadCloser) (string, error) {
193194

194195
// Round for float64 to int
195196
func Round(f float64) float64 {
196-
return types.Round(f)
197+
return math.Round(f*1000000000) / 1000000000
197198
}
198199

199200
// MergeHookOutputs merge hooks output

utils/utils_test.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func TestShortID(t *testing.T) {
148148
r1 := ShortID("1234")
149149
assert.Equal(t, r1, "1234")
150150
r2 := ShortID("12345678")
151-
assert.Equal(t, r2, "1234567")
151+
assert.Equal(t, r2, "2345678")
152152
}
153153

154154
func TestFilterWorkload(t *testing.T) {
@@ -178,17 +178,14 @@ func TestTempFile(t *testing.T) {
178178
}
179179

180180
func TestRound(t *testing.T) {
181-
f := func(f float64) string {
182-
return strconv.FormatFloat(f, 'f', -1, 64)
183-
}
184181
a := 0.0199999998
185-
assert.Equal(t, f(Round(a)), "0.02")
182+
assert.InDelta(t, Round(a), 0.02, 1e-5)
186183
a = 0.1999998
187-
assert.Equal(t, f(Round(a)), "0.2")
184+
assert.InDelta(t, Round(a), 0.2, 1e-5)
188185
a = 1.999998
189-
assert.Equal(t, f(Round(a)), "1.999998")
186+
assert.InDelta(t, Round(a), 1.999998, 1e-6)
190187
a = 19.99998
191-
assert.Equal(t, f(Round(a)), "19.99998")
188+
assert.InDelta(t, (Round(a)), 19.99998, 1e-6)
192189
}
193190

194191
func TestMergeHookOutputs(t *testing.T) {

0 commit comments

Comments
 (0)