github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/container/lxd/connection_test.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package lxd_test 5 6 import ( 7 "os" 8 "path/filepath" 9 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/container/lxd" 13 coretesting "github.com/juju/juju/testing" 14 ) 15 16 type connectionSuite struct { 17 coretesting.BaseSuite 18 } 19 20 var _ = gc.Suite(&connectionSuite{}) 21 22 func (s *connectionSuite) TestLxdSocketPathLxdDirSet(c *gc.C) { 23 os.Setenv("LXD_DIR", "foobar") 24 path := lxd.SocketPath(nil) 25 c.Check(path, gc.Equals, filepath.Join("foobar", "unix.socket")) 26 } 27 28 func (s *connectionSuite) TestLxdSocketPathSnapSocketAndDebianSocketExists(c *gc.C) { 29 os.Setenv("LXD_DIR", "") 30 isSocket := func(path string) bool { 31 return path == filepath.FromSlash("/var/snap/lxd/common/lxd/unix.socket") || 32 path == filepath.FromSlash("/var/lib/lxd/unix.socket") 33 } 34 path := lxd.SocketPath(isSocket) 35 c.Check(path, gc.Equals, filepath.FromSlash("/var/snap/lxd/common/lxd/unix.socket")) 36 } 37 38 func (s *connectionSuite) TestLxdSocketPathNoSnapSocket(c *gc.C) { 39 os.Setenv("LXD_DIR", "") 40 isSocket := func(path string) bool { 41 return path == filepath.FromSlash("/var/lib/lxd/unix.socket") 42 } 43 path := lxd.SocketPath(isSocket) 44 c.Check(path, gc.Equals, filepath.FromSlash("/var/lib/lxd/unix.socket")) 45 } 46 47 func (s *connectionSuite) TestConnectRemoteBadProtocol(c *gc.C) { 48 svr, err := lxd.ConnectImageRemote(lxd.ServerSpec{Host: "wrong-protocol-server", Protocol: "FOOBAR"}) 49 c.Check(svr, gc.IsNil) 50 c.Check(err, gc.ErrorMatches, "bad protocol supplied for connection: FOOBAR") 51 } 52 53 func (s *connectionSuite) TestEnsureHTTPSUnchangedWhenCorrect(c *gc.C) { 54 addr := "https://somewhere" 55 c.Check(lxd.EnsureHTTPS(addr), gc.Equals, addr) 56 } 57 58 func (s *connectionSuite) TestEnsureHTTPS(c *gc.C) { 59 for _, t := range []struct { 60 Input string 61 Output string 62 }{ 63 { 64 Input: "http://somewhere", 65 Output: "https://somewhere", 66 }, 67 { 68 Input: "https://somewhere", 69 Output: "https://somewhere", 70 }, 71 { 72 Input: "somewhere", 73 Output: "https://somewhere", 74 }, 75 } { 76 got := lxd.EnsureHTTPS(t.Input) 77 c.Assert(got, gc.Equals, t.Output) 78 } 79 } 80 81 func (s *connectionSuite) TestEnsureHostPort(c *gc.C) { 82 for _, t := range []struct { 83 Input string 84 Output string 85 }{ 86 { 87 Input: "https://somewhere", 88 Output: "https://somewhere:8443", 89 }, 90 { 91 Input: "somewhere", 92 Output: "https://somewhere:8443", 93 }, 94 { 95 Input: "http://somewhere:0", 96 Output: "https://somewhere:0", 97 }, 98 { 99 Input: "https://somewhere:123", 100 Output: "https://somewhere:123", 101 }, 102 } { 103 got, err := lxd.EnsureHostPort(t.Input) 104 c.Assert(err, gc.IsNil) 105 c.Assert(got, gc.Equals, t.Output) 106 } 107 }