Merge "Move mockContainer implementation to testutil"

This commit is contained in:
Zuul 2020-12-14 19:38:41 +00:00 committed by Gerrit Code Review
commit 56e632687e
4 changed files with 100 additions and 69 deletions

1
go.sum
View File

@ -902,6 +902,7 @@ golang.org/x/tools v0.0.0-20190920225731-5eefd052ad72/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20190930201159-7c411dea38b0/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191010075000-0337d82405ff h1:XdBG6es/oFDr1HwaxkxgVve7NB281QhxgK/i4voubFs=
golang.org/x/tools v0.0.0-20191010075000-0337d82405ff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e h1:aZzprAO9/8oim3qStq3wc1Xuxx4QmAGriC4VU4ojemQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=

View File

@ -27,50 +27,12 @@ import (
api "opendev.org/airship/airshipctl/pkg/api/v1alpha1"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/container"
"opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/log"
"opendev.org/airship/airshipctl/testutil"
testcontainer "opendev.org/airship/airshipctl/testutil/container"
)
type mockContainer struct {
imagePull func() error
runCommand func() error
getContainerLogs func() (io.ReadCloser, error)
rmContainer func() error
getID func() string
waitUntilFinished func() error
inspectContainer func() (container.State, error)
}
func (mc *mockContainer) ImagePull() error {
return mc.imagePull()
}
func (mc *mockContainer) RunCommand([]string, io.Reader, []string, []string) error {
return mc.runCommand()
}
func (mc *mockContainer) GetContainerLogs() (io.ReadCloser, error) {
return mc.getContainerLogs()
}
func (mc *mockContainer) RmContainer() error {
return mc.rmContainer()
}
func (mc *mockContainer) GetID() string {
return mc.getID()
}
func (mc *mockContainer) WaitUntilFinished() error {
return mc.waitUntilFinished()
}
func (mc *mockContainer) InspectContainer() (container.State, error) {
return mc.inspectContainer()
}
const testID = "TESTID"
func TestBootstrapIso(t *testing.T) {
@ -95,10 +57,10 @@ func TestBootstrapIso(t *testing.T) {
testDoc := &MockDocument{
MockAsYAML: func() ([]byte, error) { return []byte("TESTDOC"), nil },
}
testBuilder := &mockContainer{
runCommand: func() error { return nil },
getID: func() string { return testID },
rmContainer: func() error { return nil },
testBuilder := &testcontainer.MockContainer{
MockRunCommand: func() error { return nil },
MockGetID: func() string { return testID },
MockRmContainer: func() error { return nil },
}
expOut := []string{
@ -110,7 +72,7 @@ func TestBootstrapIso(t *testing.T) {
}
tests := []struct {
builder *mockContainer
builder *testcontainer.MockContainer
cfg *api.ImageConfiguration
doc *MockDocument
debug bool
@ -118,10 +80,10 @@ func TestBootstrapIso(t *testing.T) {
expectedErr error
}{
{
builder: &mockContainer{
runCommand: func() error { return testErr },
waitUntilFinished: func() error { return nil },
rmContainer: func() error { return nil },
builder: &testcontainer.MockContainer{
MockRunCommand: func() error { return testErr },
MockWaitUntilFinished: func() error { return nil },
MockRmContainer: func() error { return nil },
},
cfg: testCfg,
doc: testDoc,
@ -130,12 +92,12 @@ func TestBootstrapIso(t *testing.T) {
expectedErr: testErr,
},
{
builder: &mockContainer{
runCommand: func() error { return nil },
getID: func() string { return "TESTID" },
waitUntilFinished: func() error { return nil },
rmContainer: func() error { return nil },
getContainerLogs: func() (io.ReadCloser, error) { return ioutil.NopCloser(strings.NewReader("")), nil },
builder: &testcontainer.MockContainer{
MockRunCommand: func() error { return nil },
MockGetID: func() string { return "TESTID" },
MockWaitUntilFinished: func() error { return nil },
MockRmContainer: func() error { return nil },
MockGetContainerLogs: func() (io.ReadCloser, error) { return ioutil.NopCloser(strings.NewReader("")), nil },
},
cfg: testCfg,
doc: testDoc,
@ -144,11 +106,11 @@ func TestBootstrapIso(t *testing.T) {
expectedErr: nil,
},
{
builder: &mockContainer{
runCommand: func() error { return nil },
getID: func() string { return "TESTID" },
rmContainer: func() error { return testErr },
waitUntilFinished: func() error { return nil },
builder: &testcontainer.MockContainer{
MockRunCommand: func() error { return nil },
MockGetID: func() string { return "TESTID" },
MockRmContainer: func() error { return testErr },
MockWaitUntilFinished: func() error { return nil },
},
cfg: testCfg,
doc: testDoc,

View File

@ -29,6 +29,7 @@ import (
"opendev.org/airship/airshipctl/pkg/events"
"opendev.org/airship/airshipctl/pkg/phase/ifc"
"opendev.org/airship/airshipctl/testutil"
testcontainer "opendev.org/airship/airshipctl/testutil/container"
)
var (
@ -98,16 +99,16 @@ func TestExecutorRun(t *testing.T) {
testCases := []struct {
name string
builder *mockContainer
builder *testcontainer.MockContainer
expectedEvt []events.Event
}{
{
name: "Run isogen successfully",
builder: &mockContainer{
runCommand: func() error { return nil },
getID: func() string { return "TESTID" },
rmContainer: func() error { return nil },
waitUntilFinished: func() error { return nil },
builder: &testcontainer.MockContainer{
MockRunCommand: func() error { return nil },
MockGetID: func() string { return "TESTID" },
MockRmContainer: func() error { return nil },
MockWaitUntilFinished: func() error { return nil },
},
expectedEvt: []events.Event{
events.NewEvent().WithIsogenEvent(events.IsogenEvent{
@ -123,12 +124,12 @@ func TestExecutorRun(t *testing.T) {
},
{
name: "Fail on container command",
builder: &mockContainer{
runCommand: func() error {
builder: &testcontainer.MockContainer{
MockRunCommand: func() error {
return container.ErrRunContainerCommand{Cmd: "super fail"}
},
getID: func() string { return "TESTID" },
rmContainer: func() error { return nil },
MockGetID: func() string { return "TESTID" },
MockRmContainer: func() error { return nil },
},
expectedEvt: []events.Event{

67
testutil/container/container.go Executable file
View File

@ -0,0 +1,67 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package container
import (
"io"
"opendev.org/airship/airshipctl/pkg/container"
)
// MockContainer implements Container for unit test purposes
type MockContainer struct {
MockImagePull func() error
MockRunCommand func() error
MockGetContainerLogs func() (io.ReadCloser, error)
MockRmContainer func() error
MockGetID func() string
MockWaitUntilFinished func() error
MockInspectContainer func() (container.State, error)
}
// ImagePull Container interface implementation for unit test purposes
func (mc *MockContainer) ImagePull() error {
return mc.MockImagePull()
}
// RunCommand Container interface implementation for unit test purposes
func (mc *MockContainer) RunCommand([]string, io.Reader, []string, []string) error {
return mc.MockRunCommand()
}
// GetContainerLogs Container interface implementation for unit test purposes
func (mc *MockContainer) GetContainerLogs() (io.ReadCloser, error) {
return mc.MockGetContainerLogs()
}
// RmContainer Container interface implementation for unit test purposes
func (mc *MockContainer) RmContainer() error {
return mc.MockRmContainer()
}
// GetID Container interface implementation for unit test purposes
func (mc *MockContainer) GetID() string {
return mc.MockGetID()
}
// WaitUntilFinished Container interface implementation for unit test purposes
func (mc *MockContainer) WaitUntilFinished() error {
return mc.MockWaitUntilFinished()
}
// InspectContainer Container interface implementation for unit test purposes
func (mc *MockContainer) InspectContainer() (container.State, error) {
return mc.MockInspectContainer()
}