github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	// TODO(babbageclunk): Without the config changes and (waiting for
   116  	// the hook) this test sometimes panics. It seems like a race in
   117  	// the quickStart steps but haven't worked it out properly yet.
   118  	s.runUniterTests(c, []uniterTest{
   119  		ut(
   120  			"run commands: model",
   121  			quickStart{},
   122  			runCommands{echoUnitNameToFile("run.output")},
   123  			verifyFile{filepath.Join(testDir, "run.output"), "juju run u/0\n"},
   124  		), ut(
   125  			"run commands: jujuc commands",
   126  			quickStartRelation{},
   127  			changeConfig{"blog-title": "this is a hack"},
   128  			waitHooks{"config-changed"},
   129  			runCommands{
   130  				fmt.Sprintf("unit-get private-address >> %s", testFile("jujuc.output")),
   131  				fmt.Sprintf("unit-get public-address >> %s", testFile("jujuc.output")),
   132  			},
   133  			verifyFile{
   134  				testFile("jujuc.output"),
   135  				"private.address.example.com\npublic.address.example.com\n",
   136  			},
   137  		), ut(
   138  			"run commands: jujuc model",
   139  			quickStartRelation{},
   140  			changeConfig{"blog-title": "this is a hack"},
   141  			waitHooks{"config-changed"},
   142  			relationRunCommands{
   143  				fmt.Sprintf("echo $JUJU_RELATION_ID > %s", testFile("jujuc-env.output")),
   144  				fmt.Sprintf("echo $JUJU_REMOTE_UNIT >> %s", testFile("jujuc-env.output")),
   145  			},
   146  			verifyFile{
   147  				testFile("jujuc-env.output"),
   148  				"db:0\nmysql/0\n",
   149  			},
   150  		), ut(
   151  			"run commands: proxy settings set",
   152  			quickStartRelation{},
   153  			changeConfig{"blog-title": "this is a hack"},
   154  			waitHooks{"config-changed"},
   155  			setProxySettings{Http: "http", Https: "https", Ftp: "ftp", NoProxy: "localhost"},
   156  			runCommands{
   157  				fmt.Sprintf("echo $http_proxy > %s", testFile("proxy.output")),
   158  				fmt.Sprintf("echo $HTTP_PROXY >> %s", testFile("proxy.output")),
   159  				fmt.Sprintf("echo $https_proxy >> %s", testFile("proxy.output")),
   160  				fmt.Sprintf("echo $HTTPS_PROXY >> %s", testFile("proxy.output")),
   161  				fmt.Sprintf("echo $ftp_proxy >> %s", testFile("proxy.output")),
   162  				fmt.Sprintf("echo $FTP_PROXY >> %s", testFile("proxy.output")),
   163  				fmt.Sprintf("echo $no_proxy >> %s", testFile("proxy.output")),
   164  				fmt.Sprintf("echo $NO_PROXY >> %s", testFile("proxy.output")),
   165  			},
   166  			verifyFile{
   167  				testFile("proxy.output"),
   168  				"http\nhttp\nhttps\nhttps\nftp\nftp\nlocalhost\nlocalhost\n",
   169  			},
   170  		), ut(
   171  			"run commands: async using rpc client",
   172  			quickStart{},
   173  			asyncRunCommands{echoUnitNameToFile("run.output")},
   174  			verifyFile{testFile("run.output"), "juju run u/0\n"},
   175  			waitContextWaitGroup{},
   176  		), ut(
   177  			"run commands: waits for lock",
   178  			quickStart{},
   179  			lock.acquire(),
   180  			asyncRunCommands{echoUnitNameToFile("wait.output")},
   181  			verifyNoFile{testFile("wait.output")},
   182  			lock.release(),
   183  			verifyFile{testFile("wait.output"), "juju run u/0\n"},
   184  			waitContextWaitGroup{},
   185  		),
   186  	})
   187  }