github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/environs/tools/build_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package tools_test
     5  
     6  import (
     7  	"bytes"
     8  	"crypto/sha256"
     9  	"fmt"
    10  	"io/ioutil"
    11  	"os"
    12  	"path/filepath"
    13  	"runtime"
    14  	"strings"
    15  
    16  	exttest "github.com/juju/testing"
    17  	jc "github.com/juju/testing/checkers"
    18  	"github.com/juju/version"
    19  	gc "gopkg.in/check.v1"
    20  
    21  	"github.com/juju/juju/environs/tools"
    22  	"github.com/juju/juju/juju/names"
    23  	"github.com/juju/juju/testing"
    24  )
    25  
    26  type buildSuite struct {
    27  	testing.BaseSuite
    28  	restore  func()
    29  	cwd      string
    30  	filePath string
    31  	exttest.PatchExecHelper
    32  }
    33  
    34  var _ = gc.Suite(&buildSuite{})
    35  
    36  func (b *buildSuite) SetUpTest(c *gc.C) {
    37  	b.BaseSuite.SetUpTest(c)
    38  
    39  	suffix := ""
    40  	if runtime.GOOS == "windows" {
    41  		suffix = ".bat"
    42  	}
    43  
    44  	dir1 := c.MkDir()
    45  	dir2 := c.MkDir()
    46  
    47  	c.Log(dir1)
    48  	c.Log(dir2)
    49  
    50  	path := os.Getenv("PATH")
    51  	os.Setenv("PATH", strings.Join([]string{dir1, dir2, path}, string(filepath.ListSeparator)))
    52  
    53  	// Make an executable file called "juju-test" in dir2.
    54  	b.filePath = filepath.Join(dir2, "juju-test"+suffix)
    55  	err := ioutil.WriteFile(
    56  		b.filePath,
    57  		[]byte("doesn't matter, we don't execute it"),
    58  		0755)
    59  	c.Assert(err, jc.ErrorIsNil)
    60  
    61  	cwd, err := os.Getwd()
    62  	c.Assert(err, jc.ErrorIsNil)
    63  
    64  	b.cwd = c.MkDir()
    65  	err = os.Chdir(b.cwd)
    66  	c.Assert(err, jc.ErrorIsNil)
    67  
    68  	b.restore = func() {
    69  		os.Setenv("PATH", path)
    70  		os.Chdir(cwd)
    71  	}
    72  }
    73  
    74  func (b *buildSuite) TearDownTest(c *gc.C) {
    75  	b.restore()
    76  	b.BaseSuite.TearDownTest(c)
    77  }
    78  
    79  func (b *buildSuite) TestFindExecutable(c *gc.C) {
    80  	root := "/"
    81  	if runtime.GOOS == "windows" {
    82  		root = `C:\`
    83  	}
    84  	for _, test := range []struct {
    85  		execFile   string
    86  		expected   string
    87  		errorMatch string
    88  	}{{
    89  		execFile: filepath.Join(root, "some", "absolute", "path"),
    90  		expected: filepath.Join(root, "some", "absolute", "path"),
    91  	}, {
    92  		execFile: "./foo",
    93  		expected: filepath.Join(b.cwd, "foo"),
    94  	}, {
    95  		execFile: "juju-test",
    96  		expected: b.filePath,
    97  	}, {
    98  		execFile:   "non-existent-exec-file",
    99  		errorMatch: `could not find "non-existent-exec-file" in the path`,
   100  	}} {
   101  		result, err := tools.FindExecutable(test.execFile)
   102  		if test.errorMatch == "" {
   103  			c.Assert(err, jc.ErrorIsNil)
   104  			c.Assert(result, gc.Equals, test.expected)
   105  		} else {
   106  			c.Assert(err, gc.ErrorMatches, test.errorMatch)
   107  			c.Assert(result, gc.Equals, "")
   108  		}
   109  	}
   110  }
   111  
   112  const emptyArchive = "\x1f\x8b\b\x00\x00\tn\x88\x00\xffb\x18\x05\xa3`\x14\x8cX\x00\b\x00\x00\xff\xff.\xaf\xb5\xef\x00\x04\x00\x00"
   113  
   114  func (b *buildSuite) TestEmptyArchive(c *gc.C) {
   115  	var buf bytes.Buffer
   116  	dir := c.MkDir()
   117  	err := tools.Archive(&buf, dir)
   118  	c.Assert(err, jc.ErrorIsNil)
   119  	c.Assert(buf.String(), gc.Equals, emptyArchive)
   120  }
   121  
   122  func (b *buildSuite) TestArchiveAndSHA256(c *gc.C) {
   123  	var buf bytes.Buffer
   124  	dir := c.MkDir()
   125  	sha256hash, err := tools.ArchiveAndSHA256(&buf, dir)
   126  	c.Assert(err, jc.ErrorIsNil)
   127  	c.Assert(buf.String(), gc.Equals, emptyArchive)
   128  
   129  	h := sha256.New()
   130  	h.Write([]byte(emptyArchive))
   131  	c.Assert(sha256hash, gc.Equals, fmt.Sprintf("%x", h.Sum(nil)))
   132  }
   133  
   134  func (b *buildSuite) TestGetVersionFromJujud(c *gc.C) {
   135  	ver := version.Binary{
   136  		Number: version.Number{
   137  			Major: 1,
   138  			Minor: 2,
   139  			Tag:   "beta",
   140  			Patch: 1,
   141  		},
   142  		Series: "trusty",
   143  		Arch:   "amd64",
   144  	}
   145  
   146  	argsCh := make(chan []string, 1)
   147  	execCommand := b.GetExecCommand(exttest.PatchExecConfig{
   148  		Stderr: "hey, here's some logging you should ignore",
   149  		Stdout: ver.String(),
   150  		Args:   argsCh,
   151  	})
   152  
   153  	b.PatchValue(tools.ExecCommand, execCommand)
   154  
   155  	v, err := tools.GetVersionFromJujud("foo")
   156  	c.Assert(err, jc.ErrorIsNil)
   157  	c.Assert(v, gc.Equals, ver)
   158  
   159  	select {
   160  	case args := <-argsCh:
   161  		cmd := filepath.Join("foo", names.Jujud)
   162  		c.Assert(args, gc.DeepEquals, []string{cmd, "version"})
   163  	default:
   164  		c.Fatalf("Failed to get args sent to executable.")
   165  	}
   166  }
   167  
   168  func (b *buildSuite) TestGetVersionFromJujudWithParseError(c *gc.C) {
   169  	argsCh := make(chan []string, 1)
   170  	execCommand := b.GetExecCommand(exttest.PatchExecConfig{
   171  		Stderr: "hey, here's some logging",
   172  		Stdout: "oops, not a valid version",
   173  		Args:   argsCh,
   174  	})
   175  
   176  	b.PatchValue(tools.ExecCommand, execCommand)
   177  
   178  	_, err := tools.GetVersionFromJujud("foo")
   179  	c.Assert(err, gc.ErrorMatches, `invalid version "oops, not a valid version" printed by jujud`)
   180  
   181  	select {
   182  	case args := <-argsCh:
   183  		cmd := filepath.Join("foo", names.Jujud)
   184  		c.Assert(args, gc.DeepEquals, []string{cmd, "version"})
   185  	default:
   186  		c.Fatalf("Failed to get args sent to executable.")
   187  	}
   188  }
   189  
   190  func (b *buildSuite) TestGetVersionFromJujudWithRunError(c *gc.C) {
   191  	argsCh := make(chan []string, 1)
   192  	execCommand := b.GetExecCommand(exttest.PatchExecConfig{
   193  		Stderr:   "the stderr",
   194  		Stdout:   "the stdout",
   195  		ExitCode: 1,
   196  		Args:     argsCh,
   197  	})
   198  
   199  	b.PatchValue(tools.ExecCommand, execCommand)
   200  
   201  	_, err := tools.GetVersionFromJujud("foo")
   202  
   203  	cmd := filepath.Join("foo", names.Jujud)
   204  	msg := fmt.Sprintf("cannot get version from %q: exit status 1; the stderr\nthe stdout\n", cmd)
   205  
   206  	c.Assert(err.Error(), gc.Equals, msg)
   207  
   208  	select {
   209  	case args := <-argsCh:
   210  		c.Assert(args, gc.DeepEquals, []string{cmd, "version"})
   211  	default:
   212  		c.Fatalf("Failed to get args sent to executable.")
   213  	}
   214  }