github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/tools/lxdclient/client_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 // +build go1.3 5 6 package lxdclient 7 8 import ( 9 "io/ioutil" 10 "os" 11 12 "github.com/juju/errors" 13 "github.com/juju/testing" 14 jc "github.com/juju/testing/checkers" 15 "github.com/lxc/lxd" 16 gc "gopkg.in/check.v1" 17 ) 18 19 type ConnectSuite struct { 20 testing.IsolationSuite 21 } 22 23 var _ = gc.Suite(&ConnectSuite{}) 24 25 func (cs *ConnectSuite) TestLocalConnectError(c *gc.C) { 26 f, err := ioutil.TempFile("", "juju-lxd-remote-test") 27 c.Assert(err, jc.ErrorIsNil) 28 defer os.RemoveAll(f.Name()) 29 30 cfg, err := Config{ 31 Remote: Remote{ 32 Name: "local", 33 Host: "unix://" + f.Name(), 34 }, 35 }.WithDefaults() 36 c.Assert(err, jc.ErrorIsNil) 37 38 /* ECONNREFUSED because it's not a socket (mimics behavior of a socket 39 * with nobody listening) 40 */ 41 _, err = Connect(cfg) 42 c.Assert(err.Error(), gc.Equals, `can't connect to the local LXD server: LXD refused connections; is LXD running? 43 44 Please configure LXD by running: 45 $ newgrp lxd 46 $ lxd init 47 `) 48 49 /* EACCESS because we can't read/write */ 50 c.Assert(f.Chmod(0400), jc.ErrorIsNil) 51 _, err = Connect(cfg) 52 c.Assert(err.Error(), gc.Equals, `can't connect to the local LXD server: Permisson denied, are you in the lxd group? 53 54 Please configure LXD by running: 55 $ newgrp lxd 56 $ lxd init 57 `) 58 59 /* ENOENT because it doesn't exist */ 60 c.Assert(os.RemoveAll(f.Name()), jc.ErrorIsNil) 61 _, err = Connect(cfg) 62 c.Assert(err.Error(), gc.Equals, `can't connect to the local LXD server: LXD socket not found; is LXD installed & running? 63 64 Please install LXD by running: 65 $ sudo apt-get install lxd 66 and then configure it with: 67 $ newgrp lxd 68 $ lxd init 69 `) 70 71 // Yes, the error message actually matters here... this is being displayed 72 // to the user. 73 cs.PatchValue(&lxdNewClientFromInfo, fakeNewClientFromInfo) 74 _, err = Connect(cfg) 75 c.Assert(err.Error(), gc.Equals, `can't connect to the local LXD server: boo! 76 77 Please install LXD by running: 78 $ sudo apt-get install lxd 79 and then configure it with: 80 $ newgrp lxd 81 $ lxd init 82 `) 83 } 84 85 func (cs *ConnectSuite) TestCheckLXDBridgeConfiguration(c *gc.C) { 86 var err error 87 88 valid := ` 89 # Whether to setup a new bridge or use an existing one 90 USE_LXD_BRIDGE="true" 91 92 # Bridge name 93 # This is still used even if USE_LXD_BRIDGE is set to false 94 # set to an empty value to fully disable 95 LXD_BRIDGE="lxdbr0" 96 97 # Path to an extra dnsmasq configuration file 98 LXD_CONFILE="" 99 100 # DNS domain for the bridge 101 LXD_DOMAIN="lxd" 102 103 # IPv4 104 ## IPv4 address (e.g. 10.0.4.1) 105 LXD_IPV4_ADDR="10.0.4.1" 106 107 ## IPv4 netmask (e.g. 255.255.255.0) 108 LXD_IPV4_NETMASK="255.255.255.0" 109 110 ## IPv4 network (e.g. 10.0.4.0/24) 111 LXD_IPV4_NETWORK="10.0.4.1/24" 112 113 ## IPv4 DHCP range (e.g. 10.0.4.2,10.0.4.254) 114 LXD_IPV4_DHCP_RANGE="10.0.4.2,10.0.4.254" 115 116 ## IPv4 DHCP number of hosts (e.g. 250) 117 LXD_IPV4_DHCP_MAX="253" 118 119 ## NAT IPv4 traffic 120 LXD_IPV4_NAT="true" 121 122 # IPv6 123 ## IPv6 address (e.g. 2001:470:b368:4242::1) 124 LXD_IPV6_ADDR="" 125 126 ## IPv6 CIDR mask (e.g. 64) 127 LXD_IPV6_MASK="" 128 LXD_IPV6_NETWORK="" 129 ` 130 131 err = checkLXDBridgeConfiguration(valid) 132 c.Assert(err, jc.ErrorIsNil) 133 134 noBridge := ` 135 USE_LXD_BRIDGE="false" 136 ` 137 err = checkLXDBridgeConfiguration(noBridge) 138 c.Assert(err.Error(), gc.Equals, `lxdbr0 not enabled but required 139 It looks like your lxdbr0 has not yet been configured. Please configure it via: 140 141 sudo dpkg-reconfigure -p medium lxd 142 143 and then bootstrap again.`) 144 145 badName := ` 146 USE_LXD_BRIDGE="true" 147 LXD_BRIDGE="meshuggahrocks" 148 ` 149 err = checkLXDBridgeConfiguration(badName) 150 c.Assert(err.Error(), gc.Equals, LXDBridgeFile+` has a bridge named meshuggahrocks, not lxdbr0 151 It looks like your lxdbr0 has not yet been configured. Please configure it via: 152 153 sudo dpkg-reconfigure -p medium lxd 154 155 and then bootstrap again.`) 156 157 noSubnets := ` 158 USE_LXD_BRIDGE="true" 159 LXD_BRIDGE="lxdbr0" 160 ` 161 err = checkLXDBridgeConfiguration(noSubnets) 162 c.Assert(err.Error(), gc.Equals, `lxdbr0 has no ipv4 or ipv6 subnet enabled 163 It looks like your lxdbr0 has not yet been configured. Please configure it via: 164 165 sudo dpkg-reconfigure -p medium lxd 166 167 and then bootstrap again.`) 168 169 } 170 171 func (cs *ConnectSuite) TestRemoteConnectError(c *gc.C) { 172 cs.PatchValue(&lxdNewClientFromInfo, fakeNewClientFromInfo) 173 174 cfg, err := Config{ 175 Remote: Remote{ 176 Name: "foo", 177 Host: "a.b.c", 178 Cert: &Cert{ 179 Name: "really-valid", 180 CertPEM: []byte("kinda-public"), 181 KeyPEM: []byte("super-secret"), 182 }, 183 }, 184 }.WithDefaults() 185 c.Assert(err, jc.ErrorIsNil) 186 _, err = Connect(cfg) 187 188 c.Assert(errors.Cause(err), gc.Equals, testerr) 189 } 190 191 func (cs *ConnectSuite) TestVersionCheck(c *gc.C) { 192 c.Assert(isSupportedLxdVersion("2.0.0"), jc.IsTrue) 193 c.Assert(isSupportedLxdVersion("2.0.0.rc4"), jc.IsFalse) 194 c.Assert(isSupportedLxdVersion("0.19"), jc.IsFalse) 195 c.Assert(isSupportedLxdVersion("2.0.1"), jc.IsTrue) 196 } 197 198 var testerr = errors.Errorf("boo!") 199 200 func fakeNewClientFromInfo(info lxd.ConnectInfo) (*lxd.Client, error) { 201 return nil, testerr 202 }