launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/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  	gc "launchpad.net/gocheck"
     9  	"regexp"
    10  
    11  	"launchpad.net/juju-core/worker/uniter/debug"
    12  )
    13  
    14  type DebugHooksClientSuite struct{}
    15  
    16  var _ = gc.Suite(&DebugHooksClientSuite{})
    17  
    18  func (*DebugHooksClientSuite) TestClientScript(c *gc.C) {
    19  	ctx := debug.NewHooksContext("foo/8")
    20  
    21  	// Test the variable substitutions.
    22  	result := debug.ClientScript(ctx, nil)
    23  	// No variables left behind.
    24  	c.Assert(result, gc.Matches, "[^{}]*")
    25  	// tmux new-session -d -s {unit_name}
    26  	c.Assert(result, gc.Matches, fmt.Sprintf("(.|\n)*tmux new-session -s %s(.|\n)*", regexp.QuoteMeta(ctx.Unit)))
    27  	//) 9>{exit_flock}
    28  	c.Assert(result, gc.Matches, fmt.Sprintf("(.|\n)*\\) 9>%s(.|\n)*", regexp.QuoteMeta(ctx.ClientExitFileLock())))
    29  	//) 8>{entry_flock}
    30  	c.Assert(result, gc.Matches, fmt.Sprintf("(.|\n)*\\) 8>%s(.|\n)*", regexp.QuoteMeta(ctx.ClientFileLock())))
    31  
    32  	// nil is the same as empty slice is the same as "*".
    33  	// Also, if "*" is present as well as a named hook,
    34  	// it is equivalent to "*".
    35  	c.Assert(debug.ClientScript(ctx, nil), gc.Equals, debug.ClientScript(ctx, []string{}))
    36  	c.Assert(debug.ClientScript(ctx, []string{"*"}), gc.Equals, debug.ClientScript(ctx, nil))
    37  	c.Assert(debug.ClientScript(ctx, []string{"*", "something"}), gc.Equals, debug.ClientScript(ctx, []string{"*"}))
    38  
    39  	// debug.ClientScript does not validate hook names, as it doesn't have
    40  	// a full state API connection to determine valid relation hooks.
    41  	expected := fmt.Sprintf(
    42  		`(.|\n)*echo "aG9va3M6Ci0gc29tZXRoaW5nIHNvbWV0aGluZ2Vsc2UK" | base64 -d > %s(.|\n)*`,
    43  		regexp.QuoteMeta(ctx.ClientFileLock()),
    44  	)
    45  	c.Assert(debug.ClientScript(ctx, []string{"something somethingelse"}), gc.Matches, expected)
    46  }