github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/block/client_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package block_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/apiserver/block"
    11  	"github.com/juju/juju/apiserver/common"
    12  	"github.com/juju/juju/apiserver/params"
    13  	"github.com/juju/juju/apiserver/testing"
    14  	jujutesting "github.com/juju/juju/juju/testing"
    15  	"github.com/juju/juju/state"
    16  )
    17  
    18  type blockSuite struct {
    19  	// TODO(anastasiamac) mock to remove JujuConnSuite
    20  	jujutesting.JujuConnSuite
    21  	api *block.API
    22  }
    23  
    24  var _ = gc.Suite(&blockSuite{})
    25  
    26  func (s *blockSuite) SetUpTest(c *gc.C) {
    27  	s.JujuConnSuite.SetUpTest(c)
    28  
    29  	var err error
    30  	auth := testing.FakeAuthorizer{
    31  		Tag:            s.AdminUserTag(c),
    32  		EnvironManager: true,
    33  	}
    34  	s.api, err = block.NewAPI(s.State, common.NewResources(), auth)
    35  	c.Assert(err, jc.ErrorIsNil)
    36  }
    37  
    38  func (s *blockSuite) TestListBlockNoneExistent(c *gc.C) {
    39  	s.assertBlockList(c, 0)
    40  }
    41  
    42  func (s *blockSuite) assertBlockList(c *gc.C, length int) {
    43  	all, err := s.api.List()
    44  	c.Assert(err, jc.ErrorIsNil)
    45  	c.Assert(all.Results, gc.HasLen, length)
    46  }
    47  
    48  func (s *blockSuite) TestSwitchValidBlockOn(c *gc.C) {
    49  	s.assertSwitchBlockOn(c, state.DestroyBlock.String(), "for TestSwitchValidBlockOn")
    50  }
    51  
    52  func (s *blockSuite) assertSwitchBlockOn(c *gc.C, blockType, msg string) {
    53  	on := params.BlockSwitchParams{
    54  		Type:    blockType,
    55  		Message: msg,
    56  	}
    57  	err := s.api.SwitchBlockOn(on)
    58  	c.Assert(err.Error, gc.IsNil)
    59  	s.assertBlockList(c, 1)
    60  }
    61  
    62  func (s *blockSuite) TestSwitchInvalidBlockOn(c *gc.C) {
    63  	on := params.BlockSwitchParams{
    64  		Type:    "invalid_block_type",
    65  		Message: "for TestSwitchInvalidBlockOn",
    66  	}
    67  
    68  	c.Assert(func() { s.api.SwitchBlockOn(on) }, gc.PanicMatches, ".*unknown block type.*")
    69  }
    70  
    71  func (s *blockSuite) TestSwitchBlockOff(c *gc.C) {
    72  	valid := state.DestroyBlock
    73  	s.assertSwitchBlockOn(c, valid.String(), "for TestSwitchBlockOff")
    74  
    75  	off := params.BlockSwitchParams{
    76  		Type: valid.String(),
    77  	}
    78  	err := s.api.SwitchBlockOff(off)
    79  	c.Assert(err.Error, gc.IsNil)
    80  	s.assertBlockList(c, 0)
    81  }