github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/gce/google/raw_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package google
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	jc "github.com/juju/testing/checkers"
     9  	"github.com/juju/utils"
    10  	"google.golang.org/api/compute/v1"
    11  	gc "gopkg.in/check.v1"
    12  )
    13  
    14  type rawConnSuite struct {
    15  	BaseSuite
    16  
    17  	op      *compute.Operation
    18  	rawConn *rawConn
    19  	// TODO(katco): 2016-08-09: lp:1611427
    20  	strategy utils.AttemptStrategy
    21  
    22  	callCount int
    23  	opCallErr error
    24  }
    25  
    26  var _ = gc.Suite(&rawConnSuite{})
    27  
    28  func (s *rawConnSuite) SetUpTest(c *gc.C) {
    29  	s.BaseSuite.SetUpTest(c)
    30  
    31  	s.op = &compute.Operation{
    32  		Name:   "some_op",
    33  		Status: StatusDone,
    34  	}
    35  	service := &compute.Service{}
    36  	service.ZoneOperations = compute.NewZoneOperationsService(service)
    37  	service.RegionOperations = compute.NewRegionOperationsService(service)
    38  	service.GlobalOperations = compute.NewGlobalOperationsService(service)
    39  	s.rawConn = &rawConn{service}
    40  	s.strategy.Min = 4
    41  
    42  	s.callCount = 0
    43  	s.opCallErr = nil
    44  	s.PatchValue(&doOpCall, func(call opDoer) (*compute.Operation, error) {
    45  		s.callCount++
    46  		return s.op, s.opCallErr
    47  	})
    48  }
    49  
    50  func (s *rawConnSuite) TestConnectionCheckOperationError(c *gc.C) {
    51  	s.opCallErr = errors.New("<unknown>")
    52  
    53  	_, err := s.rawConn.checkOperation("proj", s.op)
    54  
    55  	c.Check(err, gc.ErrorMatches, ".*<unknown>")
    56  	c.Check(s.callCount, gc.Equals, 1)
    57  }
    58  
    59  func (s *rawConnSuite) TestConnectionCheckOperationZone(c *gc.C) {
    60  	original := &compute.Operation{Zone: "a-zone"}
    61  	op, err := s.rawConn.checkOperation("proj", original)
    62  
    63  	c.Check(err, jc.ErrorIsNil)
    64  	c.Check(op, gc.NotNil)
    65  	c.Check(op, gc.Not(gc.Equals), original)
    66  	c.Check(s.callCount, gc.Equals, 1)
    67  }
    68  
    69  func (s *rawConnSuite) TestConnectionCheckOperationRegion(c *gc.C) {
    70  	original := &compute.Operation{Region: "a"}
    71  	op, err := s.rawConn.checkOperation("proj", original)
    72  
    73  	c.Check(err, jc.ErrorIsNil)
    74  	c.Check(op, gc.NotNil)
    75  	c.Check(op, gc.Not(gc.Equals), original)
    76  	c.Check(s.callCount, gc.Equals, 1)
    77  }
    78  
    79  func (s *rawConnSuite) TestConnectionCheckOperationGlobal(c *gc.C) {
    80  	original := &compute.Operation{}
    81  	op, err := s.rawConn.checkOperation("proj", original)
    82  
    83  	c.Check(err, jc.ErrorIsNil)
    84  	c.Check(op, gc.NotNil)
    85  	c.Check(op, gc.Not(gc.Equals), original)
    86  	c.Check(s.callCount, gc.Equals, 1)
    87  }
    88  
    89  func (s *rawConnSuite) TestConnectionWaitOperation(c *gc.C) {
    90  	original := &compute.Operation{}
    91  	err := s.rawConn.waitOperation("proj", original, s.strategy)
    92  
    93  	c.Check(err, jc.ErrorIsNil)
    94  	c.Check(s.callCount, gc.Equals, 1)
    95  }
    96  
    97  func (s *rawConnSuite) TestConnectionWaitOperationAlreadyDone(c *gc.C) {
    98  	original := &compute.Operation{
    99  		Status: StatusDone,
   100  	}
   101  	err := s.rawConn.waitOperation("proj", original, s.strategy)
   102  
   103  	c.Check(err, jc.ErrorIsNil)
   104  	c.Check(s.callCount, gc.Equals, 0)
   105  }
   106  
   107  func (s *rawConnSuite) TestConnectionWaitOperationWaiting(c *gc.C) {
   108  	s.op.Status = StatusRunning
   109  	s.PatchValue(&doOpCall, func(call opDoer) (*compute.Operation, error) {
   110  		s.callCount++
   111  		if s.callCount > 1 {
   112  			s.op.Status = StatusDone
   113  		}
   114  		return s.op, s.opCallErr
   115  	})
   116  
   117  	original := &compute.Operation{}
   118  	err := s.rawConn.waitOperation("proj", original, s.strategy)
   119  
   120  	c.Check(err, jc.ErrorIsNil)
   121  	c.Check(s.callCount, gc.Equals, 2)
   122  }
   123  
   124  func (s *rawConnSuite) TestConnectionWaitOperationTimeout(c *gc.C) {
   125  	s.op.Status = StatusRunning
   126  	err := s.rawConn.waitOperation("proj", s.op, s.strategy)
   127  
   128  	c.Check(err, gc.ErrorMatches, ".* timed out .*")
   129  	c.Check(s.callCount, gc.Equals, 4)
   130  }
   131  
   132  func (s *rawConnSuite) TestConnectionWaitOperationFailure(c *gc.C) {
   133  	s.opCallErr = errors.New("<unknown>")
   134  
   135  	original := &compute.Operation{}
   136  	err := s.rawConn.waitOperation("proj", original, s.strategy)
   137  
   138  	c.Check(err, gc.ErrorMatches, ".*<unknown>")
   139  	c.Check(s.callCount, gc.Equals, 1)
   140  }
   141  
   142  func (s *rawConnSuite) TestConnectionWaitOperationError(c *gc.C) {
   143  	s.op.Error = &compute.OperationError{}
   144  	s.op.Name = "testing-wait-operation-error"
   145  
   146  	original := &compute.Operation{}
   147  	err := s.rawConn.waitOperation("proj", original, s.strategy)
   148  
   149  	c.Check(err, gc.ErrorMatches, `.* "testing-wait-operation-error" .*`)
   150  	c.Check(s.callCount, gc.Equals, 1)
   151  }