github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/reboot/reboot_test.go (about)

     1  // Copyright 2014 Cloudbase Solutions
     2  // Copyright 2014 Canonical Ltd.
     3  // Licensed under the AGPLv3, see LICENCE file for details.
     4  
     5  // +build !ppc64
     6  
     7  package reboot_test
     8  
     9  import (
    10  	stdtesting "testing"
    11  
    12  	"github.com/juju/errors"
    13  	jc "github.com/juju/testing/checkers"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	"github.com/juju/juju/api"
    17  	"github.com/juju/juju/api/reboot"
    18  	"github.com/juju/juju/apiserver/params"
    19  	"github.com/juju/juju/juju/testing"
    20  	"github.com/juju/juju/state"
    21  	coretesting "github.com/juju/juju/testing"
    22  )
    23  
    24  func TestAll(t *stdtesting.T) {
    25  	coretesting.MgoTestPackage(t)
    26  }
    27  
    28  type machineRebootSuite struct {
    29  	testing.JujuConnSuite
    30  
    31  	machine *state.Machine
    32  	st      api.Connection
    33  	reboot  reboot.State
    34  }
    35  
    36  var _ = gc.Suite(&machineRebootSuite{})
    37  
    38  func (s *machineRebootSuite) SetUpTest(c *gc.C) {
    39  	s.JujuConnSuite.SetUpTest(c)
    40  	var err error
    41  	s.st, s.machine = s.OpenAPIAsNewMachine(c)
    42  	s.reboot, err = s.st.Reboot()
    43  	c.Assert(err, jc.ErrorIsNil)
    44  	c.Assert(s.reboot, gc.NotNil)
    45  }
    46  
    47  func (s *machineRebootSuite) TestWatchForRebootEvent(c *gc.C) {
    48  	reboot.PatchFacadeCall(s, s.reboot, func(facade string, p interface{}, resp interface{}) error {
    49  		return nil
    50  	})
    51  	_, err := s.reboot.WatchForRebootEvent()
    52  	c.Assert(err, jc.ErrorIsNil)
    53  }
    54  
    55  func (s *machineRebootSuite) TestWatchForRebootEventError(c *gc.C) {
    56  	reboot.PatchFacadeCall(s, s.reboot, func(facade string, p interface{}, resp interface{}) error {
    57  		if resp, ok := resp.(*params.NotifyWatchResult); ok {
    58  			resp.Error = &params.Error{
    59  				Message: "Some error.",
    60  				Code:    params.CodeNotAssigned,
    61  			}
    62  		}
    63  		return nil
    64  	})
    65  	_, err := s.reboot.WatchForRebootEvent()
    66  	c.Assert(err.Error(), gc.Equals, "Some error.")
    67  }
    68  
    69  func (s *machineRebootSuite) TestRequestReboot(c *gc.C) {
    70  	reboot.PatchFacadeCall(s, s.reboot, func(facade string, p interface{}, resp interface{}) error {
    71  		if entities, ok := p.(params.Entities); ok {
    72  			if len(entities.Entities) != 1 {
    73  				return errors.Errorf("Expected 1 machine, got: %d", len(entities.Entities))
    74  			}
    75  			if entities.Entities[0].Tag != s.machine.Tag().String() {
    76  				return errors.Errorf("Expecting machineTag %s, got %s", entities.Entities[0].Tag, s.machine.Tag().String())
    77  			}
    78  		}
    79  		if resp, ok := resp.(*params.ErrorResults); ok {
    80  			resp.Results = []params.ErrorResult{
    81  				{},
    82  			}
    83  		}
    84  		return nil
    85  	})
    86  	err := s.reboot.RequestReboot()
    87  	c.Assert(err, jc.ErrorIsNil)
    88  }
    89  
    90  func (s *machineRebootSuite) TestRequestRebootError(c *gc.C) {
    91  	reboot.PatchFacadeCall(s, s.reboot, func(facade string, p interface{}, resp interface{}) error {
    92  		if entities, ok := p.(params.Entities); ok {
    93  			if len(entities.Entities) != 1 {
    94  				return errors.Errorf("Expected 1 machine, got: %d", len(entities.Entities))
    95  			}
    96  			if entities.Entities[0].Tag != s.machine.Tag().String() {
    97  				return errors.Errorf("Expecting machineTag %s, got %s", entities.Entities[0].Tag, s.machine.Tag().String())
    98  			}
    99  		}
   100  		if resp, ok := resp.(*params.ErrorResults); ok {
   101  			resp.Results = []params.ErrorResult{
   102  				{
   103  					Error: &params.Error{
   104  						Message: "Some error.",
   105  						Code:    params.CodeNotAssigned,
   106  					},
   107  				},
   108  			}
   109  		}
   110  		return nil
   111  	})
   112  	err := s.reboot.RequestReboot()
   113  	c.Assert(err.Error(), gc.Equals, "Some error.")
   114  }
   115  
   116  func (s *machineRebootSuite) TestGetRebootAction(c *gc.C) {
   117  	reboot.PatchFacadeCall(s, s.reboot, func(facade string, p interface{}, resp interface{}) error {
   118  		if resp, ok := resp.(*params.RebootActionResults); ok {
   119  			resp.Results = []params.RebootActionResult{
   120  				{Result: params.ShouldDoNothing},
   121  			}
   122  		}
   123  		return nil
   124  	})
   125  	rAction, err := s.reboot.GetRebootAction()
   126  	c.Assert(err, jc.ErrorIsNil)
   127  	c.Assert(rAction, gc.Equals, params.ShouldDoNothing)
   128  }
   129  
   130  func (s *machineRebootSuite) TestGetRebootActionMultipleResults(c *gc.C) {
   131  	reboot.PatchFacadeCall(s, s.reboot, func(facade string, p interface{}, resp interface{}) error {
   132  		if resp, ok := resp.(*params.RebootActionResults); ok {
   133  			resp.Results = []params.RebootActionResult{
   134  				{Result: params.ShouldDoNothing},
   135  				{Result: params.ShouldDoNothing},
   136  			}
   137  		}
   138  		return nil
   139  	})
   140  	_, err := s.reboot.GetRebootAction()
   141  	c.Assert(err.Error(), gc.Equals, "expected 1 result, got 2")
   142  }
   143  
   144  func (s *machineRebootSuite) TestClearReboot(c *gc.C) {
   145  	reboot.PatchFacadeCall(s, s.reboot, func(facade string, p interface{}, resp interface{}) error {
   146  		if entities, ok := p.(params.Entities); ok {
   147  			if len(entities.Entities) != 1 {
   148  				return errors.Errorf("Expected 1 machine, got: %d", len(entities.Entities))
   149  			}
   150  			if entities.Entities[0].Tag != s.machine.Tag().String() {
   151  				return errors.Errorf("Expecting machineTag %s, got %s", entities.Entities[0].Tag, s.machine.Tag().String())
   152  			}
   153  		}
   154  		if resp, ok := resp.(*params.ErrorResults); ok {
   155  			resp.Results = []params.ErrorResult{
   156  				{},
   157  			}
   158  		}
   159  		return nil
   160  	})
   161  	err := s.reboot.ClearReboot()
   162  	c.Assert(err, jc.ErrorIsNil)
   163  }
   164  
   165  func (s *machineRebootSuite) TestClearRebootError(c *gc.C) {
   166  	reboot.PatchFacadeCall(s, s.reboot, func(facade string, p interface{}, resp interface{}) error {
   167  		if resp, ok := resp.(*params.ErrorResults); ok {
   168  			resp.Results = []params.ErrorResult{
   169  				{
   170  					Error: &params.Error{
   171  						Message: "Some error.",
   172  						Code:    params.CodeNotAssigned,
   173  					},
   174  				},
   175  			}
   176  		}
   177  		return nil
   178  	})
   179  	err := s.reboot.ClearReboot()
   180  	c.Assert(err.Error(), gc.Equals, "Some error.")
   181  }