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