Skip to content
This repository has been archived by the owner on Dec 21, 2018. It is now read-only.

Commit

Permalink
Adding services parameters in methods. (#6)
Browse files Browse the repository at this point in the history
* Modify Makefile to allow branch with '/'.

* Add possibility to start/stop part of services described in docker-compose file, Add pause/unpause methods to start without creating and stop without deleting services.

* Fix comments

* Fix case in comments

* Modify pause/unpause method names to be more homogenous with Docker command names.
  • Loading branch information
nmengin authored and Vincent Demeester committed Sep 6, 2017
1 parent 2048f80 commit 6167fd3
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ dist
.gopath
*.test
vendor
.idea
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ LIBKERMIT_ENVS := \
BIND_DIR := "dist"
LIBKERMIT_MOUNT := -v "$(CURDIR)/$(BIND_DIR):/go/src/github.com/libkermit/compose/$(BIND_DIR)"

GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
LIBKERMIT_DEV_IMAGE := libkermit-compose-dev$(if $(GIT_BRANCH),:$(GIT_BRANCH))
GIT_BRANCH := $(subst heads/,,$(shell git rev-parse --abbrev-ref HEAD 2>/dev/null))
LIBKERMIT_DEV_IMAGE := libkermit-compose-dev$(if $(GIT_BRANCH),:$(subst /,-,$(GIT_BRANCH)))
REPONAME := $(shell echo $(REPO) | tr '[:upper:]' '[:lower:]')

DAEMON_VERSION := $(if $(DAEMON_VERSION),$(DAEMON_VERSION),"default")
Expand Down
30 changes: 23 additions & 7 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,20 @@ func CreateProject(name string, composeFiles ...string) (*Project, error) {
}

// Start creates and starts the compose project.
func (p *Project) Start() error {
func (p *Project) Start(services ...string) error {
ctx := context.Background()
err := p.composeProject.Create(ctx, options.Create{})
if err != nil {
return err
}
err = p.composeProject.Start(ctx)

return p.StartOnly(services...)
}

// StartOnly only starts created services which are stopped.
func (p *Project) StartOnly(services ...string) error {
ctx := context.Background()
err := p.composeProject.Start(ctx, services...)
if err != nil {
return err
}
Expand All @@ -91,18 +98,27 @@ func (p *Project) Start() error {
return nil
}

// Stop shuts down and clean the project
func (p *Project) Stop() error {
// FIXME(vdemeester) handle timeout
// StopOnly only stop services without delete them.
func (p *Project) StopOnly(services ...string) error {
ctx := context.Background()
err := p.composeProject.Stop(ctx, 10)
err := p.composeProject.Stop(ctx, 10, services...)
if err != nil {
return err
}
<-p.stopped
close(p.stopped)
return nil
}

err = p.composeProject.Delete(ctx, options.Delete{})
// Stop shuts down and clean the project
func (p *Project) Stop(services ...string) error {
// FIXME(vdemeester) handle timeout
err := p.StopOnly(services...)
if err != nil {
return err
}
ctx := context.Background()
err = p.composeProject.Delete(ctx, options.Delete{}, services...)
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions integration/assets/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
hello:
image: busybox
command: top
other:
image: busybox
command: top
22 changes: 22 additions & 0 deletions integration/testing/services_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package composeit

import (
"testing"

compose "github.com/libkermit/compose/testing"
)

func TestServicesProject(t *testing.T) {
project := compose.CreateProject(t, "services", "../assets/services.yml")
project.Start(t, "hello")

container := project.Container(t, "hello")
if container.Name != "/services_hello_1" {
t.Fatalf("expected name '/services_hello_1', got %s", container.Name)
}

//"No container found for '%s' service
project.NoContainer(t, "other")

project.Stop(t, "hello")
}
18 changes: 14 additions & 4 deletions testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ func CreateProject(t *testing.T, name string, composeFiles ...string) *Project {
}

// Start creates and starts the compose project.
func (p *Project) Start(t *testing.T) {
if err := p.project.Start(); err != nil {
func (p *Project) Start(t *testing.T, services ...string) {
if err := p.project.Start(services...); err != nil {
t.Fatalf("error while starting compose project: %s", err.Error())
}
}

// Stop shuts down and clean the project
func (p *Project) Stop(t *testing.T) {
if err := p.project.Stop(); err != nil {
func (p *Project) Stop(t *testing.T, services ...string) {
if err := p.project.Stop(services...); err != nil {
t.Fatalf("error while stopping compose project: %s", err.Error())
}
}
Expand Down Expand Up @@ -65,3 +65,13 @@ func (p *Project) Container(t *testing.T, service string) types.ContainerJSON {
}
return container
}

// NoContainer check is there is no container for the service given
// It fails if there one or more containers or if the error returned
// does not indicate an empty container list
func (p *Project) NoContainer(t *testing.T, service string) {
validErr := "No container found for '" + service + "' service"
if _, err := p.project.Container(service); err == nil || err.Error() != validErr {
t.Fatalf("error while getting the container for service '%s'", service)
}
}

0 comments on commit 6167fd3

Please sign in to comment.