github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/api/uniter/unitstorage_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package uniter_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  	"gopkg.in/juju/names.v2"
    11  
    12  	basetesting "github.com/juju/juju/api/base/testing"
    13  	"github.com/juju/juju/api/uniter"
    14  	"github.com/juju/juju/apiserver/params"
    15  	coretesting "github.com/juju/juju/testing"
    16  )
    17  
    18  type unitStorageSuite struct {
    19  	coretesting.BaseSuite
    20  }
    21  
    22  var _ = gc.Suite(&unitStorageSuite{})
    23  
    24  func (s *unitStorageSuite) createTestUnit(c *gc.C, t string, apiCaller basetesting.APICallerFunc) *uniter.Unit {
    25  	tag := names.NewUnitTag(t)
    26  	st := uniter.NewState(apiCaller, tag)
    27  	return uniter.CreateUnit(st, tag)
    28  }
    29  
    30  func (s *unitStorageSuite) TestAddUnitStorage(c *gc.C) {
    31  	count := uint64(1)
    32  	args := map[string][]params.StorageConstraints{
    33  		"data": []params.StorageConstraints{
    34  			params.StorageConstraints{Count: &count}},
    35  	}
    36  
    37  	expected := params.StoragesAddParams{
    38  		Storages: []params.StorageAddParams{
    39  			{"unit-mysql-0", "data", params.StorageConstraints{Count: &count}},
    40  		},
    41  	}
    42  
    43  	apiCaller := basetesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    44  		c.Assert(objType, gc.Equals, "Uniter")
    45  		c.Assert(version, gc.Equals, 4)
    46  		c.Assert(id, gc.Equals, "")
    47  		c.Assert(request, gc.Equals, "AddUnitStorage")
    48  		c.Assert(arg, gc.DeepEquals, expected)
    49  		c.Assert(result, gc.FitsTypeOf, &params.ErrorResults{})
    50  		*(result.(*params.ErrorResults)) = params.ErrorResults{
    51  			Results: []params.ErrorResult{{
    52  				Error: &params.Error{Message: "yoink"},
    53  			}},
    54  		}
    55  		return nil
    56  	})
    57  	u := s.createTestUnit(c, "mysql/0", apiCaller)
    58  	err := u.AddStorage(args)
    59  	c.Assert(err, gc.ErrorMatches, "yoink")
    60  }
    61  
    62  func (s *unitStorageSuite) TestAddUnitStorageError(c *gc.C) {
    63  	count := uint64(1)
    64  	args := map[string][]params.StorageConstraints{
    65  		"data": []params.StorageConstraints{params.StorageConstraints{Count: &count}},
    66  	}
    67  
    68  	expected := params.StoragesAddParams{
    69  		Storages: []params.StorageAddParams{
    70  			{"unit-mysql-0", "data", params.StorageConstraints{Count: &count}},
    71  		},
    72  	}
    73  
    74  	var called bool
    75  	msg := "yoink"
    76  	apiCaller := basetesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    77  		c.Assert(objType, gc.Equals, "Uniter")
    78  		c.Assert(version, gc.Equals, 4)
    79  		c.Assert(id, gc.Equals, "")
    80  		c.Assert(request, gc.Equals, "AddUnitStorage")
    81  		c.Assert(arg, gc.DeepEquals, expected)
    82  		called = true
    83  
    84  		c.Assert(result, gc.FitsTypeOf, &params.ErrorResults{})
    85  		return errors.New(msg)
    86  	})
    87  
    88  	u := s.createTestUnit(c, "mysql/0", apiCaller)
    89  	err := u.AddStorage(args)
    90  	c.Assert(err, gc.ErrorMatches, msg)
    91  	c.Assert(called, jc.IsTrue)
    92  }