gopkg.in/docker/libcompose.v0@v0.4.0/integration/common_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"bytes"
     5  	"math/rand"
     6  	"os"
     7  	"os/exec"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"golang.org/x/net/context"
    13  
    14  	"github.com/Sirupsen/logrus"
    15  	"github.com/docker/docker/api/types"
    16  	"github.com/docker/docker/client"
    17  	lclient "github.com/docker/libcompose/docker/client"
    18  	"github.com/docker/libcompose/docker/container"
    19  	"github.com/docker/libcompose/labels"
    20  
    21  	. "gopkg.in/check.v1"
    22  )
    23  
    24  const (
    25  	SimpleTemplate = `
    26          hello:
    27            image: busybox
    28            stdin_open: true
    29            tty: true
    30          `
    31  	SimpleTemplateWithVols = `
    32          hello:
    33            image: busybox
    34            stdin_open: true
    35            tty: true
    36            volumes:
    37            - /root:/root
    38            - /home:/home
    39            - /var/lib/vol1
    40            - /var/lib/vol2
    41            - /var/lib/vol4
    42          `
    43  
    44  	SimpleTemplateWithVols2 = `
    45          hello:
    46            image: busybox
    47            stdin_open: true
    48            tty: true
    49            volumes:
    50            - /tmp/tmp-root:/root
    51            - /var/lib/vol1
    52            - /var/lib/vol3
    53            - /var/lib/vol4
    54          `
    55  )
    56  
    57  func Test(t *testing.T) { TestingT(t) }
    58  
    59  func init() {
    60  	Suite(&CliSuite{
    61  		command: "../bundles/libcompose-cli",
    62  	})
    63  }
    64  
    65  type CliSuite struct {
    66  	command  string
    67  	projects []string
    68  }
    69  
    70  func (s *CliSuite) TearDownTest(c *C) {
    71  	// Delete all containers
    72  	client := GetClient(c)
    73  
    74  	containers, err := client.ContainerList(context.Background(), types.ContainerListOptions{
    75  		All: true,
    76  	})
    77  	c.Assert(err, IsNil)
    78  	for _, container := range containers {
    79  		// Unpause container (if paused) and ignore error (if wasn't paused)
    80  		client.ContainerUnpause(context.Background(), container.ID)
    81  		// And remove force \o/
    82  		err := client.ContainerRemove(context.Background(), container.ID, types.ContainerRemoveOptions{
    83  			Force:         true,
    84  			RemoveVolumes: true,
    85  		})
    86  		c.Assert(err, IsNil)
    87  	}
    88  }
    89  
    90  func (s *CliSuite) CreateProjectFromText(c *C, input string) string {
    91  	return s.ProjectFromText(c, "create", input)
    92  }
    93  
    94  func (s *CliSuite) RandomProject() string {
    95  	return "testproject" + RandStr(7)
    96  }
    97  
    98  func (s *CliSuite) ProjectFromText(c *C, command, input string) string {
    99  	projectName := s.RandomProject()
   100  	return s.FromText(c, projectName, command, input)
   101  }
   102  
   103  func (s *CliSuite) FromText(c *C, projectName, command string, argsAndInput ...string) string {
   104  	command, args, input := s.createCommand(c, projectName, command, argsAndInput)
   105  
   106  	cmd := exec.Command(s.command, args...)
   107  	cmd.Stdin = bytes.NewBufferString(strings.Replace(input, "\t", "  ", -1))
   108  	if os.Getenv("TESTVERBOSE") != "" {
   109  		cmd.Stdout = os.Stdout
   110  		cmd.Stderr = os.Stdout
   111  	}
   112  
   113  	err := cmd.Run()
   114  	c.Assert(err, IsNil, Commentf("Failed to run %s %v: %v\n with input:\n%s", s.command, err, args, input))
   115  
   116  	return projectName
   117  }
   118  
   119  // Doesn't assert that command runs successfully
   120  func (s *CliSuite) FromTextCaptureOutput(c *C, projectName, command string, argsAndInput ...string) (string, string) {
   121  	command, args, input := s.createCommand(c, projectName, command, argsAndInput)
   122  
   123  	cmd := exec.Command(s.command, args...)
   124  	cmd.Stdin = bytes.NewBufferString(strings.Replace(input, "\t", "  ", -1))
   125  
   126  	output, err := cmd.CombinedOutput()
   127  	if err != nil {
   128  		logrus.Errorf("Failed to run %s %v: %v\n with input:\n%s", s.command, err, args, input)
   129  	}
   130  
   131  	return projectName, string(output[:])
   132  }
   133  
   134  func (s *CliSuite) createCommand(c *C, projectName, command string, argsAndInput []string) (string, []string, string) {
   135  	args := []string{"--verbose", "-p", projectName, "-f", "-", command}
   136  	args = append(args, argsAndInput[0:len(argsAndInput)-1]...)
   137  
   138  	input := argsAndInput[len(argsAndInput)-1]
   139  
   140  	if command == "up" {
   141  		args = append(args, "-d")
   142  	} else if command == "restart" {
   143  		args = append(args, "--timeout", "0")
   144  	} else if command == "stop" {
   145  		args = append(args, "--timeout", "0")
   146  	}
   147  
   148  	logrus.Infof("Running %s %v", command, args)
   149  
   150  	return command, args, input
   151  }
   152  
   153  func GetClient(c *C) client.APIClient {
   154  	client, err := lclient.Create(lclient.Options{})
   155  
   156  	c.Assert(err, IsNil)
   157  
   158  	return client
   159  }
   160  
   161  func (s *CliSuite) GetContainerByName(c *C, name string) *types.ContainerJSON {
   162  	client := GetClient(c)
   163  	container, err := container.Get(context.Background(), client, name)
   164  
   165  	c.Assert(err, IsNil)
   166  
   167  	return container
   168  }
   169  
   170  func (s *CliSuite) GetVolumeByName(c *C, name string) *types.Volume {
   171  	client := GetClient(c)
   172  	volume, err := client.VolumeInspect(context.Background(), name)
   173  
   174  	c.Assert(err, IsNil)
   175  
   176  	return &volume
   177  }
   178  
   179  func (s *CliSuite) GetContainersByProject(c *C, project string) []types.Container {
   180  	client := GetClient(c)
   181  	containers, err := container.ListByFilter(context.Background(), client, labels.PROJECT.Eq(project))
   182  
   183  	c.Assert(err, IsNil)
   184  
   185  	return containers
   186  }
   187  
   188  func asMap(items []string) map[string]bool {
   189  	result := map[string]bool{}
   190  	for _, item := range items {
   191  		result[item] = true
   192  	}
   193  	return result
   194  }
   195  
   196  var random = rand.New(rand.NewSource(time.Now().Unix()))
   197  
   198  func RandStr(n int) string {
   199  	letters := []rune("abcdefghijklmnopqrstuvwxyz")
   200  	b := make([]rune, n)
   201  	for i := range b {
   202  		b[i] = letters[random.Intn(len(letters))]
   203  	}
   204  	return string(b)
   205  }