github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/tools/lxdclient/config_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_test 7 8 import ( 9 "github.com/juju/errors" 10 jc "github.com/juju/testing/checkers" 11 "github.com/juju/utils/set" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/tools/lxdclient" 15 ) 16 17 var ( 18 _ = gc.Suite(&configSuite{}) 19 _ = gc.Suite(&configFunctionalSuite{}) 20 ) 21 22 type configBaseSuite struct { 23 lxdclient.BaseSuite 24 25 remote lxdclient.Remote 26 } 27 28 func (s *configBaseSuite) SetUpTest(c *gc.C) { 29 s.BaseSuite.SetUpTest(c) 30 31 s.remote = lxdclient.Remote{ 32 Name: "my-remote", 33 Host: "some-host", 34 Protocol: lxdclient.LXDProtocol, 35 Cert: s.Cert, 36 } 37 } 38 39 type configSuite struct { 40 configBaseSuite 41 } 42 43 func (s *configSuite) TestWithDefaultsOkay(c *gc.C) { 44 cfg := lxdclient.Config{ 45 Namespace: "my-ns", 46 Remote: s.remote, 47 } 48 updated, err := cfg.WithDefaults() 49 c.Assert(err, jc.ErrorIsNil) 50 51 c.Check(updated, jc.DeepEquals, cfg) 52 } 53 54 func (s *configSuite) TestWithDefaultsMissingRemote(c *gc.C) { 55 cfg := lxdclient.Config{ 56 Namespace: "my-ns", 57 } 58 updated, err := cfg.WithDefaults() 59 c.Assert(err, jc.ErrorIsNil) 60 61 c.Check(updated, jc.DeepEquals, lxdclient.Config{ 62 Namespace: "my-ns", 63 Remote: lxdclient.Local, 64 }) 65 } 66 67 func (s *configSuite) TestWithDefaultsMissingStream(c *gc.C) { 68 cfg := lxdclient.Config{ 69 Namespace: "my-ns", 70 Remote: s.remote, 71 } 72 updated, err := cfg.WithDefaults() 73 c.Assert(err, jc.ErrorIsNil) 74 75 c.Check(updated, jc.DeepEquals, lxdclient.Config{ 76 Namespace: "my-ns", 77 Remote: s.remote, 78 }) 79 } 80 81 func (s *configSuite) TestValidateOkay(c *gc.C) { 82 cfg := lxdclient.Config{ 83 Namespace: "my-ns", 84 Remote: s.remote, 85 } 86 err := cfg.Validate() 87 88 c.Check(err, jc.ErrorIsNil) 89 } 90 91 func (s *configSuite) TestValidateOnlyRemote(c *gc.C) { 92 cfg := lxdclient.Config{ 93 Remote: s.remote, 94 } 95 err := cfg.Validate() 96 97 c.Check(err, jc.ErrorIsNil) 98 } 99 100 func (s *configSuite) TestValidateMissingRemote(c *gc.C) { 101 cfg := lxdclient.Config{ 102 Namespace: "my-ns", 103 } 104 err := cfg.Validate() 105 106 c.Check(err, jc.Satisfies, errors.IsNotValid) 107 } 108 109 func (s *configSuite) TestValidateZeroValue(c *gc.C) { 110 var cfg lxdclient.Config 111 err := cfg.Validate() 112 113 c.Check(err, jc.Satisfies, errors.IsNotValid) 114 } 115 116 func (s *configSuite) TestUsingTCPRemoteNoop(c *gc.C) { 117 cfg := lxdclient.Config{ 118 Namespace: "my-ns", 119 Remote: s.remote, 120 } 121 nonlocal, err := cfg.UsingTCPRemote() 122 c.Assert(err, jc.ErrorIsNil) 123 124 c.Check(nonlocal, jc.DeepEquals, cfg) 125 } 126 127 type configFunctionalSuite struct { 128 configBaseSuite 129 130 client *lxdclient.Client 131 } 132 133 func (s *configFunctionalSuite) SetUpTest(c *gc.C) { 134 s.configBaseSuite.SetUpTest(c) 135 136 s.client = newLocalClient(c) 137 c.Logf("connected to %v", s.client) 138 139 if s.client != nil { 140 origCerts, err := s.client.ListCerts() 141 c.Assert(err, jc.ErrorIsNil) 142 s.AddCleanup(func(c *gc.C) { 143 certs, err := s.client.ListCerts() 144 c.Assert(err, jc.ErrorIsNil) 145 146 orig := set.NewStrings(origCerts...) 147 added := set.NewStrings(certs...).Difference(orig) 148 for _, fingerprint := range added.Values() { 149 err := s.client.RemoveCertByFingerprint(fingerprint) 150 if err != nil { 151 c.Logf("could not remove cert %q: %v", fingerprint, err) 152 } 153 } 154 }) 155 } 156 } 157 158 func (s *configFunctionalSuite) TestUsingTCPRemote(c *gc.C) { 159 if s.client == nil { 160 c.Skip("LXD not running locally") 161 } 162 // We can't just pass the testingCert as part of the Local connection, 163 // because Validate() doesn't like Local remotes that have 164 // Certificates. 165 lxdclient.PatchGenerateCertificate(&s.CleanupSuite, testingCert, testingKey) 166 167 cfg := lxdclient.Config{ 168 Namespace: "my-ns", 169 Remote: lxdclient.Local, 170 } 171 nonlocal, err := cfg.UsingTCPRemote() 172 c.Assert(err, jc.ErrorIsNil) 173 174 checkValidRemote(c, &nonlocal.Remote) 175 c.Check(nonlocal, jc.DeepEquals, lxdclient.Config{ 176 Namespace: "my-ns", 177 Remote: lxdclient.Remote{ 178 Name: lxdclient.Local.Name, 179 Host: nonlocal.Remote.Host, 180 Cert: nonlocal.Remote.Cert, 181 Protocol: lxdclient.LXDProtocol, 182 ServerPEMCert: nonlocal.Remote.ServerPEMCert, 183 }, 184 }) 185 c.Check(nonlocal.Remote.Host, gc.Not(gc.Equals), "") 186 c.Check(nonlocal.Remote.Cert.CertPEM, gc.Not(gc.Equals), "") 187 c.Check(nonlocal.Remote.Cert.KeyPEM, gc.Not(gc.Equals), "") 188 c.Check(nonlocal.Remote.ServerPEMCert, gc.Not(gc.Equals), "") 189 // TODO(ericsnow) Check that the server has the certs. 190 } 191 192 func newLocalClient(c *gc.C) *lxdclient.Client { 193 client, err := lxdclient.Connect(lxdclient.Config{ 194 Namespace: "my-ns", 195 Remote: lxdclient.Local, 196 }) 197 if err != nil { 198 c.Log(err) 199 return nil 200 } 201 return client 202 }