1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package testing_test
import (
"io/ioutil"
"path/filepath"
"runtime"
"os"
gc "gopkg.in/check.v1"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
)
type fakeHomeSuite struct {
testing.IsolationSuite
fakeHomeSuite testing.FakeHomeSuite
}
var _ = gc.Suite(&fakeHomeSuite{})
func (s *fakeHomeSuite) SetUpSuite(c *gc.C) {
s.IsolationSuite.SetUpSuite(c)
s.fakeHomeSuite = testing.FakeHomeSuite{}
s.fakeHomeSuite.SetUpSuite(c)
}
func (s *fakeHomeSuite) SetUpTest(c *gc.C) {
s.IsolationSuite.SetUpTest(c)
err := os.Setenv("HOME", "/tmp/tests")
c.Assert(err, jc.ErrorIsNil)
}
func (s *fakeHomeSuite) TearDownSuite(c *gc.C) {
s.fakeHomeSuite.TearDownSuite(c)
s.IsolationSuite.TearDownSuite(c)
}
func (s *fakeHomeSuite) TestHomeCreated(c *gc.C) {
// A fake home is created and set.
s.fakeHomeSuite.SetUpTest(c)
home := os.Getenv("HOME")
c.Assert(home, gc.Not(gc.Equals), "/tmp/tests")
c.Assert(home, jc.IsDirectory)
s.fakeHomeSuite.TearDownTest(c)
// The original home has been restored.
switch runtime.GOOS {
case "windows":
c.Assert(os.Getenv("HOME"), jc.SamePath, "C:/tmp/tests")
default:
c.Assert(os.Getenv("HOME"), jc.SamePath, "/tmp/tests")
}
}
func (s *fakeHomeSuite) TestSshDirSetUp(c *gc.C) {
// The SSH directory is properly created and set up.
s.fakeHomeSuite.SetUpTest(c)
sshDir := testing.HomePath(".ssh")
c.Assert(sshDir, jc.IsDirectory)
PrivKeyFile := filepath.Join(sshDir, "id_rsa")
c.Assert(PrivKeyFile, jc.IsNonEmptyFile)
PubKeyFile := filepath.Join(sshDir, "id_rsa.pub")
c.Assert(PubKeyFile, jc.IsNonEmptyFile)
s.fakeHomeSuite.TearDownTest(c)
}
type makeFakeHomeSuite struct {
testing.IsolationSuite
home *testing.FakeHome
}
var _ = gc.Suite(&makeFakeHomeSuite{})
func (s *makeFakeHomeSuite) SetUpTest(c *gc.C) {
s.IsolationSuite.SetUpTest(c)
s.home = testing.MakeFakeHome(c)
testFile := testing.TestFile{
Name: "testfile-name",
Data: "testfile-data",
}
s.home.AddFiles(c, testFile)
}
func (s *makeFakeHomeSuite) TestAddFiles(c *gc.C) {
// Files are correctly added to the fake home.
expectedPath := filepath.Join(os.Getenv("HOME"), "testfile-name")
contents, err := ioutil.ReadFile(expectedPath)
c.Assert(err, gc.IsNil)
c.Assert(string(contents), gc.Equals, "testfile-data")
}
func (s *makeFakeHomeSuite) TestFileContents(c *gc.C) {
// Files contents are returned as strings.
contents := s.home.FileContents(c, "testfile-name")
c.Assert(contents, gc.Equals, "testfile-data")
}
func (s *makeFakeHomeSuite) TestFileExists(c *gc.C) {
// It is possible to check whether a file exists in the fake home.
c.Assert(s.home.FileExists("testfile-name"), jc.IsTrue)
c.Assert(s.home.FileExists("no-such-file"), jc.IsFalse)
}
|