github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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  	strategy utils.AttemptStrategy
    20  
    21  	callCount int
    22  	opCallErr error
    23  }
    24  
    25  var _ = gc.Suite(&rawConnSuite{})
    26  
    27  func (s *rawConnSuite) SetUpTest(c *gc.C) {
    28  	s.BaseSuite.SetUpTest(c)
    29  
    30  	s.op = &compute.Operation{
    31  		Name:   "some_op",
    32  		Status: StatusDone,
    33  	}
    34  	service := &compute.Service{}
    35  	service.ZoneOperations = compute.NewZoneOperationsService(service)
    36  	service.RegionOperations = compute.NewRegionOperationsService(service)
    37  	service.GlobalOperations = compute.NewGlobalOperationsService(service)
    38  	s.rawConn = &rawConn{service}
    39  	s.strategy.Min = 4
    40  
    41  	s.callCount = 0
    42  	s.opCallErr = nil
    43  	s.PatchValue(&doOpCall, func(call opDoer) (*compute.Operation, error) {
    44  		s.callCount++
    45  		return s.op, s.opCallErr
    46  	})
    47  }
    48  
    49  func (s *rawConnSuite) TestConnectionCheckOperationError(c *gc.C) {
    50  	s.opCallErr = errors.New("<unknown>")
    51  
    52  	_, err := s.rawConn.checkOperation("proj", s.op)
    53  
    54  	c.Check(err, gc.ErrorMatches, ".*<unknown>")
    55  	c.Check(s.callCount, gc.Equals, 1)
    56  }
    57  
    58  func (s *rawConnSuite) TestConnectionCheckOperationZone(c *gc.C) {
    59  	original := &compute.Operation{Zone: "a-zone"}
    60  	op, err := s.rawConn.checkOperation("proj", original)
    61  
    62  	c.Check(err, jc.ErrorIsNil)
    63  	c.Check(op, gc.NotNil)
    64  	c.Check(op, gc.Not(gc.Equals), original)
    65  	c.Check(s.callCount, gc.Equals, 1)
    66  }
    67  
    68  func (s *rawConnSuite) TestConnectionCheckOperationRegion(c *gc.C) {
    69  	original := &compute.Operation{Region: "a"}
    70  	op, err := s.rawConn.checkOperation("proj", original)
    71  
    72  	c.Check(err, jc.ErrorIsNil)
    73  	c.Check(op, gc.NotNil)
    74  	c.Check(op, gc.Not(gc.Equals), original)
    75  	c.Check(s.callCount, gc.Equals, 1)
    76  }
    77  
    78  func (s *rawConnSuite) TestConnectionCheckOperationGlobal(c *gc.C) {
    79  	original := &compute.Operation{}
    80  	op, err := s.rawConn.checkOperation("proj", original)
    81  
    82  	c.Check(err, jc.ErrorIsNil)
    83  	c.Check(op, gc.NotNil)
    84  	c.Check(op, gc.Not(gc.Equals), original)
    85  	c.Check(s.callCount, gc.Equals, 1)
    86  }
    87  
    88  func (s *rawConnSuite) TestConnectionWaitOperation(c *gc.C) {
    89  	original := &compute.Operation{}
    90  	err := s.rawConn.waitOperation("proj", original, s.strategy)
    91  
    92  	c.Check(err, jc.ErrorIsNil)
    93  	c.Check(s.callCount, gc.Equals, 1)
    94  }
    95  
    96  func (s *rawConnSuite) TestConnectionWaitOperationAlreadyDone(c *gc.C) {
    97  	original := &compute.Operation{
    98  		Status: StatusDone,
    99  	}
   100  	err := s.rawConn.waitOperation("proj", original, s.strategy)
   101  
   102  	c.Check(err, jc.ErrorIsNil)
   103  	c.Check(s.callCount, gc.Equals, 0)
   104  }
   105  
   106  func (s *rawConnSuite) TestConnectionWaitOperationWaiting(c *gc.C) {
   107  	s.op.Status = StatusRunning
   108  	s.PatchValue(&doOpCall, func(call opDoer) (*compute.Operation, error) {
   109  		s.callCount++
   110  		if s.callCount > 1 {
   111  			s.op.Status = StatusDone
   112  		}
   113  		return s.op, s.opCallErr
   114  	})
   115  
   116  	original := &compute.Operation{}
   117  	err := s.rawConn.waitOperation("proj", original, s.strategy)
   118  
   119  	c.Check(err, jc.ErrorIsNil)
   120  	c.Check(s.callCount, gc.Equals, 2)
   121  }
   122  
   123  func (s *rawConnSuite) TestConnectionWaitOperationTimeout(c *gc.C) {
   124  	s.op.Status = StatusRunning
   125  	err := s.rawConn.waitOperation("proj", s.op, s.strategy)
   126  
   127  	c.Check(err, gc.ErrorMatches, ".* timed out .*")
   128  	c.Check(s.callCount, gc.Equals, 4)
   129  }
   130  
   131  func (s *rawConnSuite) TestConnectionWaitOperationFailure(c *gc.C) {
   132  	s.opCallErr = errors.New("<unknown>")
   133  
   134  	original := &compute.Operation{}
   135  	err := s.rawConn.waitOperation("proj", original, s.strategy)
   136  
   137  	c.Check(err, gc.ErrorMatches, ".*<unknown>")
   138  	c.Check(s.callCount, gc.Equals, 1)
   139  }
   140  
   141  func (s *rawConnSuite) TestConnectionWaitOperationError(c *gc.C) {
   142  	s.op.Error = &compute.OperationError{}
   143  	s.op.Name = "testing-wait-operation-error"
   144  
   145  	original := &compute.Operation{}
   146  	err := s.rawConn.waitOperation("proj", original, s.strategy)
   147  
   148  	c.Check(err, gc.ErrorMatches, `.* "testing-wait-operation-error" .*`)
   149  	c.Check(s.callCount, gc.Equals, 1)
   150  }