github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/provider/lxd/environ_raw_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 // +build go1.3 5 6 package lxd 7 8 import ( 9 "os" 10 11 "github.com/juju/errors" 12 "github.com/juju/testing" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 16 "github.com/juju/juju/tools/lxdclient" 17 ) 18 19 type environRawSuite struct { 20 testing.IsolationSuite 21 testing.Stub 22 readFile readFileFunc 23 runCommand runCommandFunc 24 } 25 26 var _ = gc.Suite(&environRawSuite{}) 27 28 func (s *environRawSuite) SetUpTest(c *gc.C) { 29 s.IsolationSuite.SetUpTest(c) 30 s.Stub.ResetCalls() 31 s.readFile = func(path string) ([]byte, error) { 32 s.AddCall("readFile", path) 33 if err := s.NextErr(); err != nil { 34 return nil, err 35 } 36 return []byte("content:" + path), nil 37 } 38 s.runCommand = func(command string, args ...string) (string, error) { 39 s.AddCall("runCommand", command, args) 40 if err := s.NextErr(); err != nil { 41 return "", err 42 } 43 return "default via 10.0.8.1 dev eth0", nil 44 } 45 } 46 47 func (s *environRawSuite) TestGetRemoteConfig(c *gc.C) { 48 cfg, err := getRemoteConfig(s.readFile, s.runCommand) 49 c.Assert(err, jc.ErrorIsNil) 50 c.Assert(cfg, jc.DeepEquals, &lxdclient.Config{ 51 Remote: lxdclient.Remote{ 52 Name: "remote", 53 Host: "10.0.8.1", 54 Protocol: "lxd", 55 Cert: &lxdclient.Cert{ 56 CertPEM: []byte("content:/etc/juju/lxd-client.crt"), 57 KeyPEM: []byte("content:/etc/juju/lxd-client.key"), 58 }, 59 ServerPEMCert: "content:/etc/juju/lxd-server.crt", 60 }, 61 }) 62 s.Stub.CheckCalls(c, []testing.StubCall{ 63 {"readFile", []interface{}{"/etc/juju/lxd-client.crt"}}, 64 {"readFile", []interface{}{"/etc/juju/lxd-client.key"}}, 65 {"readFile", []interface{}{"/etc/juju/lxd-server.crt"}}, 66 {"runCommand", []interface{}{"ip", []string{"route", "list", "match", "0/0"}}}, 67 }) 68 } 69 70 func (s *environRawSuite) TestGetRemoteConfigFileNotExist(c *gc.C) { 71 s.SetErrors(os.ErrNotExist) 72 _, err := getRemoteConfig(s.readFile, s.runCommand) 73 // os.IsNotExist is translated to errors.IsNotFound 74 c.Assert(err, jc.Satisfies, errors.IsNotFound) 75 c.Assert(err, gc.ErrorMatches, "reading client certificate: /etc/juju/lxd-client.crt not found") 76 } 77 78 func (s *environRawSuite) TestGetRemoteConfigFileError(c *gc.C) { 79 s.SetErrors(nil, errors.New("i/o error")) 80 _, err := getRemoteConfig(s.readFile, s.runCommand) 81 c.Assert(err, gc.ErrorMatches, "reading client key: i/o error") 82 } 83 84 func (s *environRawSuite) TestGetRemoteConfigIPRouteFormatError(c *gc.C) { 85 s.runCommand = func(string, ...string) (string, error) { 86 return "this is not the prefix you're looking for", nil 87 } 88 _, err := getRemoteConfig(s.readFile, s.runCommand) 89 c.Assert(err, gc.ErrorMatches, 90 `getting gateway address: unexpected output from "ip route": this is not the prefix you're looking for`) 91 } 92 93 func (s *environRawSuite) TestGetRemoteConfigIPRouteCommandError(c *gc.C) { 94 s.SetErrors(nil, nil, nil, errors.New("buh bow")) 95 _, err := getRemoteConfig(s.readFile, s.runCommand) 96 c.Assert(err, gc.ErrorMatches, `getting gateway address: buh bow`) 97 }