github.com/jkawamoto/roadie-azure@v0.3.5/roadie/docker_test.go (about)

     1  // +build docker
     2  //
     3  // roadie/docker_test.go
     4  //
     5  // Copyright (c) 2017 Junpei Kawamoto
     6  //
     7  // This file is part of Roadie Azure.
     8  //
     9  // Roadie Azure is free software: you can redistribute it and/or modify
    10  // it under the terms of the GNU General Public License as published by
    11  // the Free Software Foundation, either version 3 of the License, or
    12  // (at your option) any later version.
    13  //
    14  // Roadie Azure is distributed in the hope that it will be useful,
    15  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  // GNU General Public License for more details.
    18  //
    19  // You should have received a copy of the GNU General Public License
    20  // along with Roadie Azure. If not, see <http://www.gnu.org/licenses/>.
    21  //
    22  
    23  package roadie
    24  
    25  import (
    26  	"archive/tar"
    27  	"bytes"
    28  	"compress/gzip"
    29  	"context"
    30  	"io"
    31  	"log"
    32  	"strings"
    33  	"testing"
    34  
    35  	"golang.org/x/sync/errgroup"
    36  )
    37  
    38  func TestDocker(t *testing.T) {
    39  
    40  	var err error
    41  	buf := bytes.NewBuffer(nil)
    42  	logger := log.New(buf, "", 0)
    43  	ctx := context.Background()
    44  
    45  	cli, err := NewDockerClient(logger)
    46  	if err != nil {
    47  		t.Fatal(err.Error())
    48  	}
    49  	defer cli.Close()
    50  
    51  	opt := DockerBuildOpt{
    52  		ImageName: "test-image",
    53  		Dockerfile: []byte(`FROM ubuntu:latest
    54  WORKDIR /root
    55  ADD .roadie/entrypoint.sh /root/entrypoint.sh
    56  ADD . /root
    57  ENTRYPOINT /root/entrypoint.sh`),
    58  		Entrypoint: []byte(`#!/bin/bash
    59  /root/cmd.sh
    60  echo "test output"
    61  `),
    62  		ContextRoot: "../data",
    63  	}
    64  
    65  	err = cli.Build(ctx, &opt)
    66  	if err != nil {
    67  		t.Fatal(err.Error())
    68  	}
    69  	err = cli.Start(ctx, "test-image")
    70  	if err != nil {
    71  		t.Fatal(err.Error())
    72  	}
    73  
    74  	output := string(buf.Bytes())
    75  	if !strings.Contains(output, "abc") {
    76  		t.Error("Outputs doesn't have a message from cmd.sh")
    77  	}
    78  	if !strings.Contains(output, "test output") {
    79  		t.Error("Outputs doesn't have a message from entrypoint.sh")
    80  	}
    81  	t.Log(output)
    82  
    83  }
    84  
    85  func TestArchiveContext(t *testing.T) {
    86  
    87  	var err error
    88  	reader, writer := io.Pipe()
    89  	eg, ctx := errgroup.WithContext(context.Background())
    90  
    91  	opt := DockerBuildOpt{
    92  		ContextRoot: "../data",
    93  	}
    94  
    95  	eg.Go(func() error {
    96  		defer writer.Close()
    97  		return archiveContext(ctx, writer, &opt)
    98  	})
    99  
   100  	res := make(map[string]struct{})
   101  	eg.Go(func() (err error) {
   102  		zipReader, err := gzip.NewReader(reader)
   103  		if err != nil {
   104  			return
   105  		}
   106  		tarReader := tar.NewReader(zipReader)
   107  
   108  		var header *tar.Header
   109  		for {
   110  			header, err = tarReader.Next()
   111  			if err == io.EOF {
   112  				break
   113  			} else if err != nil {
   114  				return
   115  			}
   116  			res[header.Name] = struct{}{}
   117  		}
   118  		return nil
   119  	})
   120  
   121  	err = eg.Wait()
   122  	if err != nil {
   123  		t.Error(err.Error())
   124  	}
   125  
   126  	if _, exist := res[".roadie/entrypoint.sh"]; !exist {
   127  		t.Error("entrypoint.sh does not exist in the context stream")
   128  	}
   129  	if _, exist := res[".roadie/Dockerfile"]; !exist {
   130  		t.Error("Dockerfile does not exist in the context stream")
   131  	}
   132  	if _, exist := res["abc.txt"]; !exist {
   133  		t.Error("abc.txt does not exist in the context stream")
   134  	}
   135  	if _, exist := res["folder/def.txt"]; !exist {
   136  		t.Error("folder/def.txt does not exist in the context stream")
   137  	}
   138  
   139  }