github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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  	// Timeout after 5 seconds
    95  	s.PatchValue(reboot.Timeout, time.Duration(5*time.Second))
    96  	w, err := reboot.NewRebootWaiter(s.st, s.acfg)
    97  	c.Assert(err, jc.ErrorIsNil)
    98  
    99  	err = w.ExecuteReboot(params.ShouldReboot)
   100  	c.Assert(err, jc.ErrorIsNil)
   101  	testing.AssertEchoArgs(c, rebootBin, expectedRebootParams...)
   102  	ft.File{s.rebootScriptName, expectedRebootScript, 0755}.Check(c, s.tmpDir)
   103  }
   104  
   105  func (s *RebootSuite) TestRebootWithMissbehavingContainers(c *gc.C) {
   106  	testing.PatchExecutable(c, s, "lxc-ls", lxcLsScript)
   107  	testing.PatchExecutable(c, s, "lxc-info", lxcInfoScriptMissbehave)
   108  	expectedRebootParams := s.rebootCommandParams(c)
   109  
   110  	// Timeout after 5 seconds
   111  	s.PatchValue(reboot.Timeout, time.Duration(5*time.Second))
   112  	w, err := reboot.NewRebootWaiter(s.st, s.acfg)
   113  	c.Assert(err, jc.ErrorIsNil)
   114  
   115  	err = w.ExecuteReboot(params.ShouldReboot)
   116  	c.Assert(err, jc.ErrorIsNil)
   117  	testing.AssertEchoArgs(c, rebootBin, expectedRebootParams...)
   118  	ft.File{s.rebootScriptName, expectedRebootScript, 0755}.Check(c, s.tmpDir)
   119  }
   120  
   121  func (s *RebootSuite) TestRebootNoContainers(c *gc.C) {
   122  	w, err := reboot.NewRebootWaiter(s.st, s.acfg)
   123  	c.Assert(err, jc.ErrorIsNil)
   124  	expectedRebootParams := s.rebootCommandParams(c)
   125  
   126  	err = w.ExecuteReboot(params.ShouldReboot)
   127  	c.Assert(err, jc.ErrorIsNil)
   128  	testing.AssertEchoArgs(c, rebootBin, expectedRebootParams...)
   129  	ft.File{s.rebootScriptName, expectedRebootScript, 0755}.Check(c, s.tmpDir)
   130  }
   131  
   132  func (s *RebootSuite) TestShutdownNoContainers(c *gc.C) {
   133  	w, err := reboot.NewRebootWaiter(s.st, s.acfg)
   134  	c.Assert(err, jc.ErrorIsNil)
   135  	expectedShutdownParams := s.shutdownCommandParams(c)
   136  
   137  	err = w.ExecuteReboot(params.ShouldShutdown)
   138  	c.Assert(err, jc.ErrorIsNil)
   139  	testing.AssertEchoArgs(c, rebootBin, expectedShutdownParams...)
   140  	ft.File{s.rebootScriptName, expectedShutdownScript, 0755}.Check(c, s.tmpDir)
   141  }