github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/uniter/util_unix_test.go (about)

     1  // Copyright 2012-2014 Canonical Ltd.
     2  // Copyright 2014 Cloudbase Solutions SRL
     3  // Licensed under the AGPLv3, see LICENCE file for details.
     4  
     5  // +build !windows
     6  
     7  package uniter_test
     8  
     9  import (
    10  	"fmt"
    11  	"path/filepath"
    12  
    13  	gc "gopkg.in/check.v1"
    14  )
    15  
    16  // Command suffix for the hooks
    17  var cmdSuffix = ""
    18  
    19  var (
    20  	// Variables for changed hooks. These are used in uniter_test
    21  	appendConfigChanged            = "config-get --format yaml --output config.out"
    22  	uniterRelationsCustomizeScript = "relation-ids db > relations.out && chmod 644 relations.out"
    23  )
    24  
    25  var (
    26  	// Used in TestLeadership
    27  	leadershipScript = `
    28  if [ $(is-leader) != "False" ]; then exit -1; fi
    29  `[1:]
    30  
    31  	// Different hook file contents. These are used in util_test
    32  	goodHook = `
    33  #!/bin/bash --norc
    34  juju-log $JUJU_MODEL_UUID %s $JUJU_REMOTE_UNIT
    35  `[1:]
    36  
    37  	badHook = `
    38  #!/bin/bash --norc
    39  juju-log $JUJU_MODEL_UUID fail-%s $JUJU_REMOTE_UNIT
    40  exit 1
    41  `[1:]
    42  
    43  	rebootHook = `
    44  #!/bin/bash --norc
    45  juju-reboot
    46  `[1:]
    47  
    48  	badRebootHook = `
    49  #!/bin/bash --norc
    50  juju-reboot
    51  exit 1
    52  `[1:]
    53  
    54  	rebootNowHook = `
    55  #!/bin/bash --norc
    56  
    57  if [ -f "i_have_risen" ]
    58  then
    59      exit 0
    60  fi
    61  touch i_have_risen
    62  juju-reboot --now
    63  `[1:]
    64  
    65  	// Map of action files contents. These are used in util_test
    66  	actions = map[string]string{
    67  		"action-log": `
    68  #!/bin/bash --norc
    69  juju-log $JUJU_MODEL_UUID action-log
    70  `[1:],
    71  		"snapshot": `
    72  #!/bin/bash --norc
    73  action-set outfile.name="snapshot-01.tar" outfile.size="10.3GB"
    74  action-set outfile.size.magnitude="10.3" outfile.size.units="GB"
    75  action-set completion.status="yes" completion.time="5m"
    76  action-set completion="yes"
    77  `[1:],
    78  		"action-log-fail": `
    79  #!/bin/bash --norc
    80  action-fail "I'm afraid I can't let you do that, Dave."
    81  action-set foo="still works"
    82  `[1:],
    83  		"action-log-fail-error": `
    84  #!/bin/bash --norc
    85  action-fail too many arguments
    86  action-set foo="still works"
    87  action-fail "A real message"
    88  `[1:],
    89  		"action-reboot": `
    90  #!/bin/bash --norc
    91  juju-reboot || action-set reboot-delayed="good"
    92  juju-reboot --now || action-set reboot-now="good"
    93  `[1:],
    94  	}
    95  )
    96  
    97  func echoUnitNameToFileHelper(testDir, name string) string {
    98  	path := filepath.Join(testDir, name)
    99  	template := "echo juju run ${JUJU_UNIT_NAME} > %s.tmp; mv %s.tmp %s"
   100  	return fmt.Sprintf(template, path, path, path)
   101  }
   102  
   103  func (s *UniterSuite) TestRunCommand(c *gc.C) {
   104  	testDir := c.MkDir()
   105  	testFile := func(name string) string {
   106  		return filepath.Join(testDir, name)
   107  	}
   108  	echoUnitNameToFile := func(name string) string {
   109  		return echoUnitNameToFileHelper(testDir, name)
   110  	}
   111  
   112  	s.runUniterTests(c, []uniterTest{
   113  		ut(
   114  			"run commands: model",
   115  			quickStart{},
   116  			runCommands{echoUnitNameToFile("run.output")},
   117  			verifyFile{filepath.Join(testDir, "run.output"), "juju run u/0\n"},
   118  		), ut(
   119  			"run commands: jujuc commands",
   120  			quickStartRelation{},
   121  			runCommands{
   122  				fmt.Sprintf("unit-get private-address >> %s", testFile("jujuc.output")),
   123  				fmt.Sprintf("unit-get public-address >> %s", testFile("jujuc.output")),
   124  			},
   125  			verifyFile{
   126  				testFile("jujuc.output"),
   127  				"private.address.example.com\npublic.address.example.com\n",
   128  			},
   129  		), ut(
   130  			"run commands: jujuc model",
   131  			quickStartRelation{},
   132  			relationRunCommands{
   133  				fmt.Sprintf("echo $JUJU_RELATION_ID > %s", testFile("jujuc-env.output")),
   134  				fmt.Sprintf("echo $JUJU_REMOTE_UNIT >> %s", testFile("jujuc-env.output")),
   135  			},
   136  			verifyFile{
   137  				testFile("jujuc-env.output"),
   138  				"db:0\nmysql/0\n",
   139  			},
   140  		), ut(
   141  			"run commands: proxy settings set",
   142  			quickStartRelation{},
   143  			setProxySettings{Http: "http", Https: "https", Ftp: "ftp", NoProxy: "localhost"},
   144  			runCommands{
   145  				fmt.Sprintf("echo $http_proxy > %s", testFile("proxy.output")),
   146  				fmt.Sprintf("echo $HTTP_PROXY >> %s", testFile("proxy.output")),
   147  				fmt.Sprintf("echo $https_proxy >> %s", testFile("proxy.output")),
   148  				fmt.Sprintf("echo $HTTPS_PROXY >> %s", testFile("proxy.output")),
   149  				fmt.Sprintf("echo $ftp_proxy >> %s", testFile("proxy.output")),
   150  				fmt.Sprintf("echo $FTP_PROXY >> %s", testFile("proxy.output")),
   151  				fmt.Sprintf("echo $no_proxy >> %s", testFile("proxy.output")),
   152  				fmt.Sprintf("echo $NO_PROXY >> %s", testFile("proxy.output")),
   153  			},
   154  			verifyFile{
   155  				testFile("proxy.output"),
   156  				"http\nhttp\nhttps\nhttps\nftp\nftp\nlocalhost\nlocalhost\n",
   157  			},
   158  		), ut(
   159  			"run commands: async using rpc client",
   160  			quickStart{},
   161  			asyncRunCommands{echoUnitNameToFile("run.output")},
   162  			verifyFile{testFile("run.output"), "juju run u/0\n"},
   163  			waitContextWaitGroup{},
   164  		), ut(
   165  			"run commands: waits for lock",
   166  			quickStart{},
   167  			acquireHookSyncLock{},
   168  			asyncRunCommands{echoUnitNameToFile("wait.output")},
   169  			verifyNoFile{testFile("wait.output")},
   170  			releaseHookSyncLock,
   171  			verifyFile{testFile("wait.output"), "juju run u/0\n"},
   172  			waitContextWaitGroup{},
   173  		),
   174  	})
   175  }