This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mockcontainer: implement mockContainer
mockContainer is a mock struct to write unit tests Signed-off-by: Julio Montes <[email protected]>
- Loading branch information
Julio Montes
committed
Apr 13, 2018
1 parent
d208929
commit c394b63
Showing
2 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// | ||
// Copyright (c) 2018 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/opencontainers/runc/libcontainer" | ||
"github.com/opencontainers/runc/libcontainer/configs" | ||
) | ||
|
||
type mockContainer struct { | ||
id string | ||
status libcontainer.Status | ||
processes []int | ||
} | ||
|
||
func (m *mockContainer) ID() string { | ||
return m.id | ||
} | ||
|
||
func (m *mockContainer) Status() (libcontainer.Status, error) { | ||
return m.status, nil | ||
} | ||
|
||
func (m *mockContainer) State() (*libcontainer.State, error) { | ||
return nil, nil | ||
} | ||
|
||
func (m *mockContainer) Config() configs.Config { | ||
return configs.Config{} | ||
} | ||
|
||
func (m *mockContainer) Processes() ([]int, error) { | ||
return m.processes, nil | ||
} | ||
|
||
func (m *mockContainer) Stats() (*libcontainer.Stats, error) { | ||
return nil, nil | ||
} | ||
|
||
func (m *mockContainer) Set(config configs.Config) error { | ||
return nil | ||
} | ||
|
||
func (m *mockContainer) Start(process *libcontainer.Process) (err error) { | ||
return nil | ||
} | ||
|
||
func (m *mockContainer) Run(process *libcontainer.Process) (err error) { | ||
return nil | ||
} | ||
|
||
func (m *mockContainer) Destroy() error { | ||
return nil | ||
} | ||
|
||
func (m *mockContainer) Signal(s os.Signal, all bool) error { | ||
return nil | ||
} | ||
|
||
func (m *mockContainer) Exec() error { | ||
return nil | ||
} | ||
|
||
func (m *mockContainer) Checkpoint(criuOpts *libcontainer.CriuOpts) error { | ||
return nil | ||
} | ||
|
||
func (m *mockContainer) Restore(process *libcontainer.Process, criuOpts *libcontainer.CriuOpts) error { | ||
return nil | ||
} | ||
|
||
func (m *mockContainer) Pause() error { | ||
return nil | ||
} | ||
|
||
func (m *mockContainer) Resume() error { | ||
return nil | ||
} | ||
|
||
func (m *mockContainer) NotifyOOM() (<-chan struct{}, error) { | ||
return nil, nil | ||
} | ||
|
||
func (m *mockContainer) NotifyMemoryPressure(level libcontainer.PressureLevel) (<-chan struct{}, error) { | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// | ||
// Copyright (c) 2018 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"syscall" | ||
"testing" | ||
|
||
"github.com/opencontainers/runc/libcontainer" | ||
"github.com/opencontainers/runc/libcontainer/configs" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMockContainerID(t *testing.T) { | ||
assert := assert.New(t) | ||
expectedContID := "abc" | ||
m := &mockContainer{id: expectedContID} | ||
id := m.ID() | ||
assert.Equal(expectedContID, id) | ||
} | ||
|
||
func TestMockContainerStatus(t *testing.T) { | ||
assert := assert.New(t) | ||
expectedStatus := libcontainer.Running | ||
m := &mockContainer{status: expectedStatus} | ||
status, err := m.Status() | ||
assert.NoError(err) | ||
assert.Equal(expectedStatus, status) | ||
} | ||
|
||
func TestMockContainerState(t *testing.T) { | ||
assert := assert.New(t) | ||
m := &mockContainer{} | ||
st, err := m.State() | ||
assert.NoError(err) | ||
assert.Nil(st) | ||
} | ||
|
||
func TestMockContainerConfig(t *testing.T) { | ||
assert := assert.New(t) | ||
m := &mockContainer{} | ||
cfg := m.Config() | ||
assert.NotNil(cfg) | ||
} | ||
|
||
func TestMockContainerProcesses(t *testing.T) { | ||
assert := assert.New(t) | ||
expectedProcesses := []int{1} | ||
m := &mockContainer{processes: expectedProcesses} | ||
p, err := m.Processes() | ||
assert.NoError(err) | ||
assert.Equal(expectedProcesses, p) | ||
} | ||
|
||
func TestMockContainerStats(t *testing.T) { | ||
assert := assert.New(t) | ||
m := &mockContainer{} | ||
st, err := m.Stats() | ||
assert.NoError(err) | ||
assert.Nil(st) | ||
} | ||
|
||
func TestMockContainerSet(t *testing.T) { | ||
assert := assert.New(t) | ||
m := &mockContainer{} | ||
err := m.Set(configs.Config{}) | ||
assert.NoError(err) | ||
} | ||
|
||
func TestMockContainerStart(t *testing.T) { | ||
assert := assert.New(t) | ||
m := &mockContainer{} | ||
err := m.Start(nil) | ||
assert.NoError(err) | ||
} | ||
|
||
func TestMockContainerRun(t *testing.T) { | ||
assert := assert.New(t) | ||
m := &mockContainer{} | ||
err := m.Run(nil) | ||
assert.NoError(err) | ||
} | ||
|
||
func TestMockContainerDestroy(t *testing.T) { | ||
assert := assert.New(t) | ||
m := &mockContainer{} | ||
err := m.Destroy() | ||
assert.NoError(err) | ||
} | ||
|
||
func TestMockContainerSignal(t *testing.T) { | ||
assert := assert.New(t) | ||
m := &mockContainer{} | ||
err := m.Signal(syscall.SIGKILL, true) | ||
assert.NoError(err) | ||
} | ||
|
||
func TestMockContainerExec(t *testing.T) { | ||
assert := assert.New(t) | ||
m := &mockContainer{} | ||
err := m.Exec() | ||
assert.NoError(err) | ||
} | ||
|
||
func TestMockContainerCheckpoint(t *testing.T) { | ||
assert := assert.New(t) | ||
m := &mockContainer{} | ||
err := m.Checkpoint(nil) | ||
assert.NoError(err) | ||
} | ||
|
||
func TestMockContainerRestore(t *testing.T) { | ||
assert := assert.New(t) | ||
m := &mockContainer{} | ||
err := m.Restore(nil, nil) | ||
assert.NoError(err) | ||
} | ||
|
||
func TestMockContainerPause(t *testing.T) { | ||
assert := assert.New(t) | ||
m := &mockContainer{} | ||
err := m.Pause() | ||
assert.NoError(err) | ||
} | ||
|
||
func TestMockContainerResume(t *testing.T) { | ||
assert := assert.New(t) | ||
m := &mockContainer{} | ||
err := m.Resume() | ||
assert.NoError(err) | ||
} | ||
|
||
func TestMockContainerNotifyOOM(t *testing.T) { | ||
assert := assert.New(t) | ||
m := &mockContainer{} | ||
c, err := m.NotifyOOM() | ||
assert.NoError(err) | ||
assert.Nil(c) | ||
} | ||
|
||
func TestMockContainerNotifyMemoryPressure(t *testing.T) { | ||
assert := assert.New(t) | ||
m := &mockContainer{} | ||
c, err := m.NotifyMemoryPressure(libcontainer.LowPressure) | ||
assert.NoError(err) | ||
assert.Nil(c) | ||
} |