github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/provider/gce/google/conn_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package google_test 5 6 import ( 7 "github.com/juju/errors" 8 jc "github.com/juju/testing/checkers" 9 "google.golang.org/api/compute/v1" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/provider/gce/google" 13 ) 14 15 type connSuite struct { 16 google.BaseSuite 17 18 conn *google.Connection 19 } 20 21 var _ = gc.Suite(&connSuite{}) 22 23 func (s *connSuite) TestConnect(c *gc.C) { 24 google.SetRawConn(s.Conn, nil) 25 service := &compute.Service{} 26 s.PatchValue(google.NewRawConnection, func(auth *google.Credentials) (*compute.Service, error) { 27 return service, nil 28 }) 29 30 conn, err := google.Connect(s.ConnCfg, s.Credentials) 31 c.Assert(err, jc.ErrorIsNil) 32 33 c.Check(google.ExposeRawService(conn), gc.Equals, service) 34 } 35 36 func (s *connSuite) TestConnectionVerifyCredentials(c *gc.C) { 37 s.FakeConn.Project = &compute.Project{} 38 err := s.Conn.VerifyCredentials() 39 40 c.Check(err, jc.ErrorIsNil) 41 } 42 43 func (s *connSuite) TestConnectionVerifyCredentialsAPI(c *gc.C) { 44 s.FakeConn.Project = &compute.Project{} 45 err := s.Conn.VerifyCredentials() 46 c.Assert(err, jc.ErrorIsNil) 47 48 c.Check(s.FakeConn.Calls, gc.HasLen, 1) 49 c.Check(s.FakeConn.Calls[0].FuncName, gc.Equals, "GetProject") 50 c.Check(s.FakeConn.Calls[0].ProjectID, gc.Equals, "spam") 51 } 52 53 func (s *connSuite) TestConnectionVerifyCredentialsInvalid(c *gc.C) { 54 s.FakeConn.Err = errors.New("retrieving auth token for user@mail.com: Invalid Key") 55 err := s.Conn.VerifyCredentials() 56 57 c.Check(err, gc.ErrorMatches, `retrieving auth token for user@mail.com: Invalid Key`) 58 } 59 60 func (s *connSuite) TestConnectionAvailabilityZones(c *gc.C) { 61 s.FakeConn.Zones = []*compute.Zone{{ 62 Name: "a-zone", 63 Status: google.StatusUp, 64 }} 65 66 azs, err := s.Conn.AvailabilityZones("a") 67 c.Check(err, gc.IsNil) 68 69 c.Check(len(azs), gc.Equals, 1) 70 c.Check(azs[0].Name(), gc.Equals, "a-zone") 71 c.Check(azs[0].Status(), gc.Equals, google.StatusUp) 72 } 73 74 func (s *connSuite) TestConnectionAvailabilityZonesAPI(c *gc.C) { 75 _, err := s.Conn.AvailabilityZones("a") 76 c.Assert(err, gc.IsNil) 77 78 c.Check(s.FakeConn.Calls, gc.HasLen, 1) 79 c.Check(s.FakeConn.Calls[0].FuncName, gc.Equals, "ListAvailabilityZones") 80 c.Check(s.FakeConn.Calls[0].ProjectID, gc.Equals, "spam") 81 c.Check(s.FakeConn.Calls[0].Region, gc.Equals, "a") 82 } 83 84 func (s *connSuite) TestConnectionAvailabilityZonesErr(c *gc.C) { 85 s.FakeConn.Err = errors.New("<unknown>") 86 87 _, err := s.Conn.AvailabilityZones("a") 88 89 c.Check(err, gc.ErrorMatches, "<unknown>") 90 }