github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/jujud/reboot/reboot_nix_test.go (about)

     1  // +build !windows
     2  
     3  package reboot_test
     4  
     5  import (
     6  	"time"
     7  
     8  	"github.com/juju/testing"
     9  	jc "github.com/juju/testing/checkers"
    10  	ft "github.com/juju/testing/filetesting"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/apiserver/params"
    14  	"github.com/juju/juju/cmd/jujud/reboot"
    15  )
    16  
    17  // on linux we use the "at" command to schedule a reboot
    18  var rebootBin = "at"
    19  
    20  var expectedRebootScript = `#!/bin/bash
    21  sleep 15
    22  shutdown -r now`
    23  
    24  var expectedShutdownScript = `#!/bin/bash
    25  sleep 15
    26  shutdown -h now`
    27  
    28  var lxcLsScript = `#!/bin/bash
    29  echo juju-machine-1-lxc-0
    30  `
    31  
    32  var lxcInfoScriptMissbehave = `#!/bin/bash
    33  echo '
    34  Name:           juju-machine-1-lxc-0
    35  State:          RUNNING
    36  PID:            13955
    37  IP:             192.168.200.85
    38  CPU use:        186.37 seconds
    39  BlkIO use:      175.29 MiB
    40  Memory use:     202.45 MiB
    41  Link:           vethXUAOWB
    42   TX bytes:      516.81 KiB
    43   RX bytes:      12.31 MiB
    44   Total bytes:   12.82 MiB
    45  '
    46  `
    47  
    48  var lxcInfoScript = `#!/bin/bash
    49  LINE_COUNT=$(wc -l "$TEMP/empty-lxc-response" 2>/dev/null | awk '{print $1}')
    50  RAN=${LINE_COUNT:-0}
    51  
    52  if [ "$RAN" -ge 3 ]
    53  then
    54      echo ""
    55  else
    56      echo '
    57  Name:           juju-machine-1-lxc-0
    58  State:          RUNNING
    59  PID:            13955
    60  IP:             192.168.200.85
    61  CPU use:        186.37 seconds
    62  BlkIO use:      175.29 MiB
    63  Memory use:     202.45 MiB
    64  Link:           vethXUAOWB
    65   TX bytes:      516.81 KiB
    66   RX bytes:      12.31 MiB
    67   Total bytes:   12.82 MiB
    68  '
    69      echo 1 >> "$TEMP/empty-lxc-response"
    70  fi
    71  `
    72  
    73  func (s *RebootSuite) rebootCommandParams(c *gc.C) []string {
    74  	return []string{
    75  		"-f",
    76  		s.rebootScript(c),
    77  		"now",
    78  	}
    79  }
    80  
    81  func (s *RebootSuite) shutdownCommandParams(c *gc.C) []string {
    82  	return []string{
    83  		"-f",
    84  		s.rebootScript(c),
    85  		"now",
    86  	}
    87  }
    88  
    89  func (s *RebootSuite) TestRebootWithContainers(c *gc.C) {
    90  	testing.PatchExecutable(c, s, "lxc-ls", lxcLsScript)
    91  	testing.PatchExecutable(c, s, "lxc-info", lxcInfoScript)
    92  	expectedRebootParams := s.rebootCommandParams(c)
    93  
    94  	s.PatchValue(reboot.Timeout, time.Duration(5*time.Second))
    95  	w, err := reboot.NewRebootWaiter(s.st, s.acfg)
    96  	c.Assert(err, jc.ErrorIsNil)
    97  
    98  	err = w.ExecuteReboot(params.ShouldReboot)
    99  	c.Assert(err, jc.ErrorIsNil)
   100  	testing.AssertEchoArgs(c, rebootBin, expectedRebootParams...)
   101  	ft.File{s.rebootScriptName, expectedRebootScript, 0755}.Check(c, s.tmpDir)
   102  }
   103  
   104  func (s *RebootSuite) TestRebootWithMissbehavingContainers(c *gc.C) {
   105  	testing.PatchExecutable(c, s, "lxc-ls", lxcLsScript)
   106  	testing.PatchExecutable(c, s, "lxc-info", lxcInfoScriptMissbehave)
   107  
   108  	s.PatchValue(reboot.Timeout, time.Duration(1*time.Second))
   109  	w, err := reboot.NewRebootWaiter(s.st, s.acfg)
   110  	c.Assert(err, jc.ErrorIsNil)
   111  
   112  	err = w.ExecuteReboot(params.ShouldReboot)
   113  	c.Assert(err, gc.ErrorMatches, "Timeout reached waiting for containers to shutdown")
   114  }
   115  
   116  func (s *RebootSuite) TestRebootNoContainers(c *gc.C) {
   117  	w, err := reboot.NewRebootWaiter(s.st, s.acfg)
   118  	c.Assert(err, jc.ErrorIsNil)
   119  	expectedRebootParams := s.rebootCommandParams(c)
   120  
   121  	err = w.ExecuteReboot(params.ShouldReboot)
   122  	c.Assert(err, jc.ErrorIsNil)
   123  	testing.AssertEchoArgs(c, rebootBin, expectedRebootParams...)
   124  	ft.File{s.rebootScriptName, expectedRebootScript, 0755}.Check(c, s.tmpDir)
   125  }
   126  
   127  func (s *RebootSuite) TestShutdownNoContainers(c *gc.C) {
   128  	w, err := reboot.NewRebootWaiter(s.st, s.acfg)
   129  	c.Assert(err, jc.ErrorIsNil)
   130  	expectedShutdownParams := s.shutdownCommandParams(c)
   131  
   132  	err = w.ExecuteReboot(params.ShouldShutdown)
   133  	c.Assert(err, jc.ErrorIsNil)
   134  	testing.AssertEchoArgs(c, rebootBin, expectedShutdownParams...)
   135  	ft.File{s.rebootScriptName, expectedShutdownScript, 0755}.Check(c, s.tmpDir)
   136  }