github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/apiserver/facades/client/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/common"
    11  	"github.com/juju/juju/apiserver/facade/facadetest"
    12  	"github.com/juju/juju/apiserver/facades/client/block"
    13  	"github.com/juju/juju/apiserver/testing"
    14  	jujutesting "github.com/juju/juju/juju/testing"
    15  	"github.com/juju/juju/rpc/params"
    16  	"github.com/juju/juju/state"
    17  )
    18  
    19  type blockSuite struct {
    20  	// TODO(anastasiamac) mock to remove JujuConnSuite
    21  	jujutesting.JujuConnSuite
    22  	api *block.API
    23  }
    24  
    25  var _ = gc.Suite(&blockSuite{})
    26  
    27  func (s *blockSuite) SetUpTest(c *gc.C) {
    28  	s.JujuConnSuite.SetUpTest(c)
    29  
    30  	var err error
    31  	auth := testing.FakeAuthorizer{
    32  		Tag:        s.AdminUserTag(c),
    33  		Controller: true,
    34  	}
    35  	s.api, err = block.NewAPI(facadetest.Context{
    36  		State_:     s.State,
    37  		Resources_: common.NewResources(),
    38  		Auth_:      auth,
    39  	})
    40  	c.Assert(err, jc.ErrorIsNil)
    41  }
    42  
    43  func (s *blockSuite) TestListBlockNoneExistent(c *gc.C) {
    44  	s.assertBlockList(c, 0)
    45  }
    46  
    47  func (s *blockSuite) assertBlockList(c *gc.C, length int) {
    48  	all, err := s.api.List()
    49  	c.Assert(err, jc.ErrorIsNil)
    50  	c.Assert(all.Results, gc.HasLen, length)
    51  }
    52  
    53  func (s *blockSuite) TestSwitchValidBlockOn(c *gc.C) {
    54  	s.assertSwitchBlockOn(c, state.DestroyBlock.String(), "for TestSwitchValidBlockOn")
    55  }
    56  
    57  func (s *blockSuite) assertSwitchBlockOn(c *gc.C, blockType, msg string) {
    58  	on := params.BlockSwitchParams{
    59  		Type:    blockType,
    60  		Message: msg,
    61  	}
    62  	err := s.api.SwitchBlockOn(on)
    63  	c.Assert(err.Error, gc.IsNil)
    64  	s.assertBlockList(c, 1)
    65  }
    66  
    67  func (s *blockSuite) TestSwitchInvalidBlockOn(c *gc.C) {
    68  	on := params.BlockSwitchParams{
    69  		Type:    "invalid_block_type",
    70  		Message: "for TestSwitchInvalidBlockOn",
    71  	}
    72  
    73  	c.Assert(func() { s.api.SwitchBlockOn(on) }, gc.PanicMatches, ".*unknown block type.*")
    74  }
    75  
    76  func (s *blockSuite) TestSwitchBlockOff(c *gc.C) {
    77  	valid := state.DestroyBlock
    78  	s.assertSwitchBlockOn(c, valid.String(), "for TestSwitchBlockOff")
    79  
    80  	off := params.BlockSwitchParams{
    81  		Type: valid.String(),
    82  	}
    83  	err := s.api.SwitchBlockOff(off)
    84  	c.Assert(err.Error, gc.IsNil)
    85  	s.assertBlockList(c, 0)
    86  }