github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/worker/uniter/debug/client_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package debug_test
     5  
     6  import (
     7  	"fmt"
     8  	"regexp"
     9  
    10  	gc "launchpad.net/gocheck"
    11  
    12  	"github.com/juju/juju/worker/uniter/debug"
    13  )
    14  
    15  type DebugHooksClientSuite struct{}
    16  
    17  var _ = gc.Suite(&DebugHooksClientSuite{})
    18  
    19  func (*DebugHooksClientSuite) TestClientScript(c *gc.C) {
    20  	ctx := debug.NewHooksContext("foo/8")
    21  
    22  	// Test the variable substitutions.
    23  	result := debug.ClientScript(ctx, nil)
    24  	// No variables left behind.
    25  	c.Assert(result, gc.Matches, "[^{}]*")
    26  	// tmux new-session -d -s {unit_name}
    27  	c.Assert(result, gc.Matches, fmt.Sprintf("(.|\n)*tmux new-session -s %s(.|\n)*", regexp.QuoteMeta(ctx.Unit)))
    28  	//) 9>{exit_flock}
    29  	c.Assert(result, gc.Matches, fmt.Sprintf("(.|\n)*\\) 9>%s(.|\n)*", regexp.QuoteMeta(ctx.ClientExitFileLock())))
    30  	//) 8>{entry_flock}
    31  	c.Assert(result, gc.Matches, fmt.Sprintf("(.|\n)*\\) 8>%s(.|\n)*", regexp.QuoteMeta(ctx.ClientFileLock())))
    32  
    33  	// nil is the same as empty slice is the same as "*".
    34  	// Also, if "*" is present as well as a named hook,
    35  	// it is equivalent to "*".
    36  	c.Assert(debug.ClientScript(ctx, nil), gc.Equals, debug.ClientScript(ctx, []string{}))
    37  	c.Assert(debug.ClientScript(ctx, []string{"*"}), gc.Equals, debug.ClientScript(ctx, nil))
    38  	c.Assert(debug.ClientScript(ctx, []string{"*", "something"}), gc.Equals, debug.ClientScript(ctx, []string{"*"}))
    39  
    40  	// debug.ClientScript does not validate hook names, as it doesn't have
    41  	// a full state API connection to determine valid relation hooks.
    42  	expected := fmt.Sprintf(
    43  		`(.|\n)*echo "aG9va3M6Ci0gc29tZXRoaW5nIHNvbWV0aGluZ2Vsc2UK" | base64 -d > %s(.|\n)*`,
    44  		regexp.QuoteMeta(ctx.ClientFileLock()),
    45  	)
    46  	c.Assert(debug.ClientScript(ctx, []string{"something somethingelse"}), gc.Matches, expected)
    47  }