github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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  	jc "github.com/juju/testing/checkers"
    17  	gc "gopkg.in/check.v1"
    18  
    19  	"github.com/juju/juju/environs/tools"
    20  	"github.com/juju/juju/testing"
    21  )
    22  
    23  type buildSuite struct {
    24  	testing.BaseSuite
    25  	restore  func()
    26  	cwd      string
    27  	filePath string
    28  }
    29  
    30  var _ = gc.Suite(&buildSuite{})
    31  
    32  func (b *buildSuite) SetUpTest(c *gc.C) {
    33  	b.BaseSuite.SetUpTest(c)
    34  
    35  	suffix := ""
    36  	if runtime.GOOS == "windows" {
    37  		suffix = ".bat"
    38  	}
    39  
    40  	dir1 := c.MkDir()
    41  	dir2 := c.MkDir()
    42  
    43  	c.Log(dir1)
    44  	c.Log(dir2)
    45  
    46  	path := os.Getenv("PATH")
    47  	os.Setenv("PATH", strings.Join([]string{dir1, dir2, path}, string(filepath.ListSeparator)))
    48  
    49  	// Make an executable file called "juju-test" in dir2.
    50  	b.filePath = filepath.Join(dir2, "juju-test"+suffix)
    51  	err := ioutil.WriteFile(
    52  		b.filePath,
    53  		[]byte("doesn't matter, we don't execute it"),
    54  		0755)
    55  	c.Assert(err, jc.ErrorIsNil)
    56  
    57  	cwd, err := os.Getwd()
    58  	c.Assert(err, jc.ErrorIsNil)
    59  
    60  	b.cwd = c.MkDir()
    61  	err = os.Chdir(b.cwd)
    62  	c.Assert(err, jc.ErrorIsNil)
    63  
    64  	b.restore = func() {
    65  		os.Setenv("PATH", path)
    66  		os.Chdir(cwd)
    67  	}
    68  }
    69  
    70  func (b *buildSuite) TearDownTest(c *gc.C) {
    71  	b.restore()
    72  	b.BaseSuite.TearDownTest(c)
    73  }
    74  
    75  func (b *buildSuite) TestFindExecutable(c *gc.C) {
    76  
    77  	for _, test := range []struct {
    78  		execFile   string
    79  		expected   string
    80  		errorMatch string
    81  	}{{
    82  		execFile: filepath.Join("/", "some", "absolute", "path"),
    83  		expected: filepath.Join("/", "some", "absolute", "path"),
    84  	}, {
    85  		execFile: "./foo",
    86  		expected: filepath.Join(b.cwd, "foo"),
    87  	}, {
    88  		execFile: "juju-test",
    89  		expected: b.filePath,
    90  	}, {
    91  		execFile:   "non-existent-exec-file",
    92  		errorMatch: `could not find "non-existent-exec-file" in the path`,
    93  	}} {
    94  		result, err := tools.FindExecutable(test.execFile)
    95  		if test.errorMatch == "" {
    96  			c.Assert(err, jc.ErrorIsNil)
    97  			c.Assert(result, gc.Equals, test.expected)
    98  		} else {
    99  			c.Assert(err, gc.ErrorMatches, test.errorMatch)
   100  			c.Assert(result, gc.Equals, "")
   101  		}
   102  	}
   103  }
   104  
   105  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"
   106  
   107  func (b *buildSuite) TestEmptyArchive(c *gc.C) {
   108  	var buf bytes.Buffer
   109  	dir := c.MkDir()
   110  	err := tools.Archive(&buf, dir)
   111  	c.Assert(err, jc.ErrorIsNil)
   112  	c.Assert(buf.String(), gc.Equals, emptyArchive)
   113  }
   114  
   115  func (b *buildSuite) TestArchiveAndSHA256(c *gc.C) {
   116  	var buf bytes.Buffer
   117  	dir := c.MkDir()
   118  	sha256hash, err := tools.ArchiveAndSHA256(&buf, dir)
   119  	c.Assert(err, jc.ErrorIsNil)
   120  	c.Assert(buf.String(), gc.Equals, emptyArchive)
   121  
   122  	h := sha256.New()
   123  	h.Write([]byte(emptyArchive))
   124  	c.Assert(sha256hash, gc.Equals, fmt.Sprintf("%x", h.Sum(nil)))
   125  }