Skip to content

Commit

Permalink
fix test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Aceralon committed Sep 27, 2021
1 parent c8c8fdb commit 0bbb21e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
6 changes: 3 additions & 3 deletions cluster/calcium/control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,21 @@ func TestControlStart(t *testing.T) {
data := ioutil.NopCloser(bytes.NewBufferString("output"))
engine.On("Execute", mock.Anything, mock.Anything, mock.Anything).Return("eid", data, nil, nil, nil).Times(4)
// failed by ExecExitCode
engine.On("ExecExitCode", mock.Anything, mock.Anything).Return(-1, types.ErrNilEngine).Once()
engine.On("ExecExitCode", mock.Anything, mock.Anything, mock.Anything).Return(-1, types.ErrNilEngine).Once()
ch, err = c.ControlWorkload(ctx, []string{"id1"}, cluster.WorkloadStart, false)
assert.NoError(t, err)
for r := range ch {
assert.Error(t, r.Error)
}
// exitCode is not 0
engine.On("ExecExitCode", mock.Anything, mock.Anything).Return(-1, nil).Once()
engine.On("ExecExitCode", mock.Anything, mock.Anything, mock.Anything).Return(-1, nil).Once()
ch, err = c.ControlWorkload(ctx, []string{"id1"}, cluster.WorkloadStart, false)
assert.NoError(t, err)
for r := range ch {
assert.Error(t, r.Error)
}
// exitCode is 0
engine.On("ExecExitCode", mock.Anything, mock.Anything).Return(0, nil)
engine.On("ExecExitCode", mock.Anything, mock.Anything, mock.Anything).Return(0, nil)
ch, err = c.ControlWorkload(ctx, []string{"id1"}, cluster.WorkloadStart, false)
assert.NoError(t, err)
for r := range ch {
Expand Down
15 changes: 10 additions & 5 deletions cluster/calcium/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,42 @@ func TestExecuteWorkload(t *testing.T) {
ctx := context.Background()
store := &storemocks.Store{}
c.store = store

// failed by GetWorkload
store.On("GetWorkload", mock.Anything, mock.Anything).Return(nil, types.ErrBadCount).Once()
ID := "abc"
ch := c.ExecuteWorkload(ctx, &types.ExecuteWorkloadOptions{WorkloadID: ID}, nil)
for ac := range ch {
assert.NotEmpty(t, ac.Data)
}

engine := &enginemocks.API{}
workload := &types.Workload{
ID: ID,
Engine: engine,
}
store.On("GetWorkload", mock.Anything, mock.Anything).Return(workload, nil)

// failed by Execute
engine.On("Execute", mock.Anything, mock.Anything, mock.Anything).Return(ID, nil, nil, nil, types.ErrCannotGetEngine).Once()
result := "def"
engine.On("Execute", mock.Anything, mock.Anything, mock.Anything).Return(result, nil, nil, nil, types.ErrCannotGetEngine).Once()
ch = c.ExecuteWorkload(ctx, &types.ExecuteWorkloadOptions{WorkloadID: ID}, nil)
for ac := range ch {
assert.Equal(t, ac.WorkloadID, ID)
}
buf := ioutil.NopCloser(bytes.NewBufferString(`echo 1\n`))
engine.On("Execute", mock.Anything, mock.Anything, mock.Anything).Return(ID, buf, nil, nil, nil).Twice()
engine.On("Execute", mock.Anything, mock.Anything, mock.Anything).Return(result, buf, nil, nil, nil).Twice()

// failed by ExecExitCode
engine.On("ExecExitCode", mock.Anything, mock.Anything).Return(-1, types.ErrCannotGetEngine).Once()
engine.On("ExecExitCode", mock.Anything, mock.Anything, mock.Anything).Return(-1, types.ErrCannotGetEngine).Once()
ch = c.ExecuteWorkload(ctx, &types.ExecuteWorkloadOptions{WorkloadID: ID}, nil)
data := []byte{}
for ac := range ch {
assert.Equal(t, ac.WorkloadID, ID)
data = append(data, ac.Data...)
}
assert.Contains(t, string(data), "echo")
engine.On("ExecExitCode", mock.Anything, mock.Anything).Return(0, nil)
engine.On("ExecExitCode", mock.Anything, mock.Anything, mock.Anything).Return(0, nil)
ch = c.ExecuteWorkload(ctx, &types.ExecuteWorkloadOptions{WorkloadID: ID}, nil)
for ac := range ch {
assert.Equal(t, ac.WorkloadID, ID)
Expand All @@ -72,7 +77,7 @@ func TestExecuteWorkload(t *testing.T) {
ch = c.ExecuteWorkload(ctx, &types.ExecuteWorkloadOptions{WorkloadID: ID, OpenStdin: true}, inChan)
inChan <- []byte("a")
inChan <- escapeCommand
engine.On("ExecResize", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(types.ErrAlreadyFilled)
engine.On("ExecResize", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(types.ErrAlreadyFilled)
w := &window{100, 100}
b, err := json.Marshal(w)
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion engine/docker/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestWithDumpFiles(t *testing.T) {
}
fp := []string{}
for target, content := range data {
withTarfileDump(context.TODO(), target, bytes.NewBuffer(content), func(target, tarfile string) error {
withTarfileDump(context.TODO(), target, content, 0, 0, 0, func(target, tarfile string) error {
assert.True(t, strings.HasPrefix(target, "/tmp/test"))
fp = append(fp, tarfile)
_, err := os.Stat(tarfile)
Expand Down

0 comments on commit 0bbb21e

Please sign in to comment.