github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/commands/debughooks_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     5  
     6  import (
     7  	"runtime"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	coretesting "github.com/juju/juju/testing"
    13  )
    14  
    15  var _ = gc.Suite(&DebugHooksSuite{})
    16  
    17  type DebugHooksSuite struct {
    18  	SSHCommonSuite
    19  }
    20  
    21  var debugHooksTests = []struct {
    22  	info     string
    23  	args     []string
    24  	error    string
    25  	proxy    bool
    26  	expected *argsSpec
    27  }{{
    28  	info:  "unit name without hook",
    29  	args:  []string{"mysql/0"},
    30  	proxy: true,
    31  	expected: &argsSpec{
    32  		hostKeyChecking: "yes",
    33  		knownHosts:      "0",
    34  		enablePty:       true,
    35  		args:            "ubuntu@0.public sudo /bin/bash -c 'F=$(mktemp); echo IyEvYmluL2Jhc2gKKApjbGVhbnVwX29uX2V4aXQoKSAKeyAKCWVjaG8gIkNsZWFuaW5nIHVwIHRoZSBkZWJ1ZyBzZXNzaW9uIgoJdG11eCBraWxsLXNlc3Npb24gLXQgbXlzcWwvMDsgCn0KdHJhcCBjbGVhbnVwX29uX2V4aXQgRVhJVAoKIyBMb2NrIHRoZSBqdWp1LTx1bml0Pi1kZWJ1ZyBsb2NrZmlsZS4KZmxvY2sgLW4gOCB8fCAoCgllY2hvICJGb3VuZCBleGlzdGluZyBkZWJ1ZyBzZXNzaW9ucywgYXR0ZW1wdGluZyB0byByZWNvbm5lY3QiIDI+JjEKCWV4ZWMgdG11eCBhdHRhY2gtc2Vzc2lvbiAtdCBteXNxbC8wCglleGl0ICQ/CgkpCigKIyBDbG9zZSB0aGUgaW5oZXJpdGVkIGxvY2sgRkQsIG9yIHRtdXggd2lsbCBrZWVwIGl0IG9wZW4uCmV4ZWMgOD4mLQoKIyBXcml0ZSBvdXQ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XhlYyB0bXV4IG5ldy1zZXNzaW9uIC1kIC10IG15c3FsLzAgLXMgJHNlc3Npb25fbmFtZQoJCWV4ZWMgdG11eCBhdHRhY2gtc2Vzc2lvbiAtdCAkc2Vzc2lvbl9uYW1lIFw7IHNldC1vcHRpb24gZGVzdHJveS11bmF0dGFjaGVkCgllbHNlCgkgICAgZXhlYyB0bXV4IGF0dGFjaC1zZXNzaW9uIC10IG15c3FsLzAKCWZpCikKKSA5Pi90bXAvanVqdS11bml0LW15c3FsLTAtZGVidWctaG9va3MtZXhpdAopIDg+L3RtcC9qdWp1LXVuaXQtbXlzcWwtMC1kZWJ1Zy1ob29rcwpleGl0ICQ/Cg== | base64 -d > $F; . $F'",
    36  	},
    37  }, {
    38  	info: "proxy",
    39  	args: []string{"--proxy=true", "mysql/0"},
    40  	expected: &argsSpec{
    41  		hostKeyChecking: "yes",
    42  		knownHosts:      "0",
    43  		enablePty:       true,
    44  		withProxy:       true,
    45  		args:            "ubuntu@0.private sudo /bin/bash -c 'F=$(mktemp); echo IyEvYmluL2Jhc2gKKApjbGVhbnVwX29uX2V4aXQoKSAKeyAKCWVjaG8gIkNsZWFuaW5nIHVwIHRoZSBkZWJ1ZyBzZXNzaW9uIgoJdG11eCBraWxsLXNlc3Npb24gLXQgbXlzcWwvMDsgCn0KdHJhcCBjbGVhbnVwX29uX2V4aXQgRVhJVAoKIyBMb2NrIHRoZSBqdWp1LTx1bml0Pi1kZWJ1ZyBsb2NrZmlsZS4KZmxvY2sgLW4gOCB8fCAoCgllY2hvICJGb3VuZCBleGlzdGluZyBkZWJ1ZyBzZXNzaW9ucywgYXR0ZW1wdGluZyB0byByZWNvbm5lY3QiIDI+JjEKCWV4ZWMgdG11eCBhdHRhY2gtc2Vzc2lvbiAtdCBteXNxbC8wCglleGl0ICQ/CgkpCigKIyBDbG9zZSB0aGUgaW5oZXJpdGVkIGxvY2sgRkQsIG9yIHRtdXggd2lsbCBrZWVwIGl0IG9wZW4uCmV4ZWMgOD4mL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IiRjbGllbnRfY250CgkJZXhlYyB0bXV4IG5ldy1zZXNzaW9uIC1kIC10IG15c3FsLzAgLXMgJHNlc3Npb25fbmFtZQoJCWV4ZWMgdG11eCBhdHRhY2gtc2Vzc2lvbiAtdCAkc2Vzc2lvbl9uYW1lIFw7IHNldC1vcHRpb24gZGVzdHJveS11bmF0dGFjaGVkCgllbHNlCgkgICAgZXhlYyB0bXV4IGF0dGFjaC1zZXNzaW9uIC10IG15c3FsLzAKCWZpCikKKSA5Pi90bXAvanVqdS11bml0LW15c3FsLTAtZGVidWctaG9va3MtZXhpdAopIDg+L3RtcC9qdWp1LXVuaXQtbXlzcWwtMC1kZWJ1Zy1ob29rcwpleGl0ICQ/Cg== | base64 -d > $F; . $F'",
    46  	},
    47  }, {
    48  	info:     `"*" is a valid hook name: it means hook everything`,
    49  	args:     []string{"mysql/0", "*"},
    50  	expected: nil,
    51  }, {
    52  	info:     `"*" mixed with named hooks is equivalent to "*"`,
    53  	args:     []string{"mysql/0", "*", "relation-get"},
    54  	expected: nil,
    55  }, {
    56  	info:     `multiple named hooks may be specified`,
    57  	args:     []string{"mysql/0", "start", "stop"},
    58  	expected: nil,
    59  }, {
    60  	info:     `relation hooks have the relation name prefixed`,
    61  	args:     []string{"mysql/0", "juju-info-relation-joined"},
    62  	expected: nil,
    63  }, {
    64  	info:  `invalid unit syntax`,
    65  	args:  []string{"mysql"},
    66  	error: `"mysql" is not a valid unit name`,
    67  }, {
    68  	info:  `invalid unit`,
    69  	args:  []string{"nonexistent/123"},
    70  	error: `unit "nonexistent/123" not found`,
    71  }, {
    72  	info:  `invalid hook`,
    73  	args:  []string{"mysql/0", "invalid-hook"},
    74  	error: `unit "mysql/0" does not contain hook "invalid-hook"`,
    75  }}
    76  
    77  func (s *DebugHooksSuite) TestDebugHooksCommand(c *gc.C) {
    78  	//TODO(bogdanteleaga): Fix once debughooks are supported on windows
    79  	if runtime.GOOS == "windows" {
    80  		c.Skip("bug 1403084: Skipping on windows for now")
    81  	}
    82  
    83  	s.setupModel(c)
    84  
    85  	for i, t := range debugHooksTests {
    86  		c.Logf("test %d: %s\n\t%s\n", i, t.info, t.args)
    87  
    88  		ctx, err := coretesting.RunCommand(c, newDebugHooksCommand(), t.args...)
    89  		if t.error != "" {
    90  			c.Check(err, gc.ErrorMatches, t.error)
    91  		} else {
    92  			c.Check(err, jc.ErrorIsNil)
    93  			if t.expected != nil {
    94  				t.expected.check(c, coretesting.Stdout(ctx))
    95  			}
    96  		}
    97  	}
    98  }