Skip to content

Commit

Permalink
refactor exitCode
Browse files Browse the repository at this point in the history
  • Loading branch information
Aceralon committed Oct 7, 2021
1 parent 23b8ba3 commit b54686e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 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
4 changes: 2 additions & 2 deletions engine/virt/virt.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ func (v *Virt) Execute(ctx context.Context, ID string, config *enginetypes.ExecC

// ExecExitCode get return code of a specific execution.
func (v *Virt) ExecExitCode(ctx context.Context, ID, pid string) (code int, err error) {
iPid, err := strconv.Atoi(pid)
intPid, err := strconv.Atoi(pid)
if err != nil {
return -1, err
}
code, err = v.client.ExecExitCode(ctx, ID, iPid)
code, err = v.client.ExecExitCode(ctx, ID, intPid)
if err != nil {
return -1, err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/opencontainers/runc v1.0.0-rc95 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/projecteru2/libyavirt v0.0.0-20210927061827-61332cb449e6
github.com/projecteru2/libyavirt v0.0.0-20211005075821-cf93676926ff
github.com/prometheus/client_golang v1.11.0
github.com/sanity-io/litter v1.5.1
github.com/sirupsen/logrus v1.7.0
Expand Down
Loading

0 comments on commit b54686e

Please sign in to comment.