Skip to content

Commit

Permalink
ciao-launcher: Add more docker unit tests
Browse files Browse the repository at this point in the history
This commit adds the following new docker unit tests:

- TestDockerCreateImageFail
- TestDockerCreateImageWithVolumes
- TestDockerCreateImageWithResources
- TestDockerMonitorVM
- TestDockerMonitorVMClose
- TestDockerStats

It improves launcher's unit test coverage to 52.6%.

Partial fix for issue ciao-project#1

Signed-off-by: Mark Ryan <[email protected]>
  • Loading branch information
Mark Ryan committed Jan 5, 2017
1 parent 10e7261 commit b5dc87c
Show file tree
Hide file tree
Showing 3 changed files with 342 additions and 29 deletions.
3 changes: 3 additions & 0 deletions ciao-launcher/container_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type containerManager interface {
*network.NetworkingConfig, string) (types.ContainerCreateResponse, error)
ContainerRemove(context.Context, types.ContainerRemoveOptions) error
ContainerStart(context.Context, string) error
ContainerInspect(context.Context, string) (types.ContainerJSON, error)
ContainerInspectWithRaw(context.Context, string, bool) (types.ContainerJSON, []byte, error)
ContainerStats(context.Context, string, bool) (io.ReadCloser, error)
ContainerKill(context.Context, string, string) error
ContainerWait(context.Context, string) (int, error)
}
40 changes: 20 additions & 20 deletions ciao-launcher/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ func (d *docker) createImage(bridge string, userData, metaData []byte) error {
err = ioutil.WriteFile(idPath, []byte(resp.ID), 0600)
if err != nil {
glog.Errorf("Unable to store docker container ID %v", err)
_ = dockerDeleteContainer(d.cli, resp.ID, d.cfg.Instance)
return err
}

Expand All @@ -333,6 +334,19 @@ func (d *docker) createImage(bridge string, userData, metaData []byte) error {
return nil
}

func dockerDeleteContainer(cli containerManager, dockerID, instanceUUID string) error {
err := cli.ContainerRemove(context.Background(),
types.ContainerRemoveOptions{
ContainerID: dockerID,
Force: true})
if err != nil {
glog.Warningf("Unable to delete docker instance %s:%s err %v",
instanceUUID, dockerID, err)
}

return err
}

func (d *docker) deleteImage() error {
if d.dockerID == "" {
return nil
Expand All @@ -343,17 +357,7 @@ func (d *docker) deleteImage() error {
return err
}

err = d.cli.ContainerRemove(context.Background(),
types.ContainerRemoveOptions{
ContainerID: d.dockerID,
Force: true})
if err != nil {
glog.Warningf("Unable to delete docker instance %s:%s err %v",
d.cfg.Instance, d.dockerID, err)
return err
}

return nil
return dockerDeleteContainer(d.cli, d.dockerID, d.cfg.Instance)
}

func (d *docker) startVM(vnicName, ipAddress, cephID string) error {
Expand All @@ -378,7 +382,7 @@ func (d *docker) startVM(vnicName, ipAddress, cephID string) error {
return nil
}

func dockerCommandLoop(cli *client.Client, dockerChannel chan interface{}, instance, dockerID string) {
func dockerCommandLoop(cli containerManager, dockerChannel chan interface{}, instance, dockerID string) {
ctx, cancelFunc := context.WithCancel(context.Background())
lostContainerCh := make(chan struct{})
go func() {
Expand Down Expand Up @@ -420,8 +424,9 @@ DONE:
glog.Infof("Docker Instance %s:%s shut down", instance, dockerID)
}

func dockerConnect(dockerChannel chan interface{}, instance, dockerID string, closedCh chan struct{},
connectedCh chan struct{}, wg *sync.WaitGroup, boot bool) {
func dockerConnect(cli containerManager, dockerChannel chan interface{}, instance,
dockerID string, closedCh chan struct{}, connectedCh chan struct{},
wg *sync.WaitGroup, boot bool) {

defer func() {
if closedCh != nil {
Expand All @@ -431,11 +436,6 @@ func dockerConnect(dockerChannel chan interface{}, instance, dockerID string, cl
wg.Done()
}()

cli, err := getDockerClient()
if err != nil {
return
}

// BUG(markus): Need a way to cancel this. Can't do this until we have contexts

con, err := cli.ContainerInspect(context.Background(), dockerID)
Expand Down Expand Up @@ -470,7 +470,7 @@ func (d *docker) monitorVM(closedCh chan struct{}, connectedCh chan struct{},
}
dockerChannel := make(chan interface{})
wg.Add(1)
go dockerConnect(dockerChannel, d.cfg.Instance, d.dockerID, closedCh, connectedCh, wg, boot)
go dockerConnect(d.cli, dockerChannel, d.cfg.Instance, d.dockerID, closedCh, connectedCh, wg, boot)
return dockerChannel
}

Expand Down
Loading

0 comments on commit b5dc87c

Please sign in to comment.