github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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_ENV_UUID %s $JUJU_REMOTE_UNIT
    35  `[1:]
    36  
    37  	badHook = `
    38  #!/bin/bash --norc
    39  juju-log $JUJU_ENV_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_ENV_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  	adminTag := s.AdminUserTag(c)
   109  	echoUnitNameToFile := func(name string) string {
   110  		return echoUnitNameToFileHelper(testDir, name)
   111  	}
   112  
   113  	s.runUniterTests(c, []uniterTest{
   114  		ut(
   115  			"run commands: environment",
   116  			quickStart{},
   117  			runCommands{echoUnitNameToFile("run.output")},
   118  			verifyFile{filepath.Join(testDir, "run.output"), "juju run u/0\n"},
   119  		), ut(
   120  			"run commands: jujuc commands",
   121  			quickStartRelation{},
   122  			runCommands{
   123  				fmt.Sprintf("owner-get tag > %s", testFile("jujuc.output")),
   124  				fmt.Sprintf("unit-get private-address >> %s", testFile("jujuc.output")),
   125  				fmt.Sprintf("unit-get public-address >> %s", testFile("jujuc.output")),
   126  			},
   127  			verifyFile{
   128  				testFile("jujuc.output"),
   129  				adminTag.String() + "\nprivate.address.example.com\npublic.address.example.com\n",
   130  			},
   131  		), ut(
   132  			"run commands: jujuc environment",
   133  			quickStartRelation{},
   134  			relationRunCommands{
   135  				fmt.Sprintf("echo $JUJU_RELATION_ID > %s", testFile("jujuc-env.output")),
   136  				fmt.Sprintf("echo $JUJU_REMOTE_UNIT >> %s", testFile("jujuc-env.output")),
   137  			},
   138  			verifyFile{
   139  				testFile("jujuc-env.output"),
   140  				"db:0\nmysql/0\n",
   141  			},
   142  		), ut(
   143  			"run commands: proxy settings set",
   144  			quickStartRelation{},
   145  			setProxySettings{Http: "http", Https: "https", Ftp: "ftp", NoProxy: "localhost"},
   146  			runCommands{
   147  				fmt.Sprintf("echo $http_proxy > %s", testFile("proxy.output")),
   148  				fmt.Sprintf("echo $HTTP_PROXY >> %s", testFile("proxy.output")),
   149  				fmt.Sprintf("echo $https_proxy >> %s", testFile("proxy.output")),
   150  				fmt.Sprintf("echo $HTTPS_PROXY >> %s", testFile("proxy.output")),
   151  				fmt.Sprintf("echo $ftp_proxy >> %s", testFile("proxy.output")),
   152  				fmt.Sprintf("echo $FTP_PROXY >> %s", testFile("proxy.output")),
   153  				fmt.Sprintf("echo $no_proxy >> %s", testFile("proxy.output")),
   154  				fmt.Sprintf("echo $NO_PROXY >> %s", testFile("proxy.output")),
   155  			},
   156  			verifyFile{
   157  				testFile("proxy.output"),
   158  				"http\nhttp\nhttps\nhttps\nftp\nftp\nlocalhost\nlocalhost\n",
   159  			},
   160  		), ut(
   161  			"run commands: async using rpc client",
   162  			quickStart{},
   163  			asyncRunCommands{echoUnitNameToFile("run.output")},
   164  			verifyFile{testFile("run.output"), "juju run u/0\n"},
   165  			waitContextWaitGroup{},
   166  		), ut(
   167  			"run commands: waits for lock",
   168  			quickStart{},
   169  			acquireHookSyncLock{},
   170  			asyncRunCommands{echoUnitNameToFile("wait.output")},
   171  			verifyNoFile{testFile("wait.output")},
   172  			releaseHookSyncLock,
   173  			verifyFile{testFile("wait.output"), "juju run u/0\n"},
   174  			waitContextWaitGroup{},
   175  		),
   176  	})
   177  }