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