github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/highavailability/client_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package highavailability_test
     5  
     6  import (
     7  	stdtesting "testing"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/api/highavailability"
    13  	"github.com/juju/juju/core/constraints"
    14  	jujutesting "github.com/juju/juju/juju/testing"
    15  	"github.com/juju/juju/network"
    16  	"github.com/juju/juju/state"
    17  	"github.com/juju/juju/state/presence"
    18  	coretesting "github.com/juju/juju/testing"
    19  )
    20  
    21  func TestAll(t *stdtesting.T) {
    22  	coretesting.MgoTestPackage(t)
    23  }
    24  
    25  type clientSuite struct {
    26  	jujutesting.JujuConnSuite
    27  }
    28  
    29  var _ = gc.Suite(&clientSuite{})
    30  
    31  type KillerForTesting interface {
    32  	KillForTesting() error
    33  }
    34  
    35  func assertKill(c *gc.C, killer KillerForTesting) {
    36  	c.Assert(killer.KillForTesting(), gc.IsNil)
    37  }
    38  
    39  func setAgentPresence(c *gc.C, s *jujutesting.JujuConnSuite, machineId string) *presence.Pinger {
    40  	m, err := s.BackingState.Machine(machineId)
    41  	c.Assert(err, jc.ErrorIsNil)
    42  	pinger, err := m.SetAgentPresence()
    43  	c.Assert(err, jc.ErrorIsNil)
    44  	s.BackingState.StartSync()
    45  	err = m.WaitAgentPresence(coretesting.LongWait)
    46  	c.Assert(err, jc.ErrorIsNil)
    47  	return pinger
    48  }
    49  
    50  func assertEnableHA(c *gc.C, s *jujutesting.JujuConnSuite) {
    51  	m, err := s.State.AddMachine("quantal", state.JobManageModel)
    52  	c.Assert(err, jc.ErrorIsNil)
    53  	// We have to ensure the agents are alive, or EnableHA will
    54  	// create more to replace them.
    55  	pingerA := setAgentPresence(c, s, "0")
    56  	defer assertKill(c, pingerA)
    57  
    58  	err = m.SetMachineAddresses(
    59  		network.NewScopedAddress("127.0.0.1", network.ScopeMachineLocal),
    60  		network.NewScopedAddress("cloud-local0.internal", network.ScopeCloudLocal),
    61  		network.NewScopedAddress("fc00::1", network.ScopePublic),
    62  	)
    63  	c.Assert(err, jc.ErrorIsNil)
    64  
    65  	emptyCons := constraints.Value{}
    66  	client := highavailability.NewClient(s.APIState)
    67  	result, err := client.EnableHA(3, emptyCons, nil)
    68  	c.Assert(err, jc.ErrorIsNil)
    69  
    70  	c.Assert(result.Maintained, gc.DeepEquals, []string{"machine-0"})
    71  	c.Assert(result.Added, gc.DeepEquals, []string{"machine-1", "machine-2"})
    72  	c.Assert(result.Removed, gc.HasLen, 0)
    73  
    74  	machines, err := s.State.AllMachines()
    75  	c.Assert(err, jc.ErrorIsNil)
    76  	c.Assert(machines, gc.HasLen, 3)
    77  	c.Assert(machines[0].Series(), gc.Equals, "quantal")
    78  	c.Assert(machines[1].Series(), gc.Equals, "quantal")
    79  	c.Assert(machines[2].Series(), gc.Equals, "quantal")
    80  }
    81  
    82  func (s *clientSuite) TestClientEnableHA(c *gc.C) {
    83  	assertEnableHA(c, &s.JujuConnSuite)
    84  }
    85  
    86  func (s *clientSuite) TestClientEnableHAVersion(c *gc.C) {
    87  	client := highavailability.NewClient(s.APIState)
    88  	c.Assert(client.BestAPIVersion(), gc.Equals, 2)
    89  }