github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/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 "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/worker/uniter/runner/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.Not(gc.Matches), "(.|\n)*{unit_name}(.|\n)*")
    26  	c.Assert(result, gc.Not(gc.Matches), "(.|\n)*{tmux_conf}(.|\n)*")
    27  	c.Assert(result, gc.Not(gc.Matches), "(.|\n)*{entry_flock}(.|\n)*")
    28  	c.Assert(result, gc.Not(gc.Matches), "(.|\n)*{exit_flock}(.|\n)*")
    29  	// tmux new-session -d -s {unit_name}
    30  	c.Assert(result, gc.Matches, fmt.Sprintf("(.|\n)*tmux attach-session -t %s(.|\n)*", regexp.QuoteMeta(ctx.Unit)))
    31  	//) 9>{exit_flock}
    32  	c.Assert(result, gc.Matches, fmt.Sprintf("(.|\n)*\\) 9>%s(.|\n)*", regexp.QuoteMeta(ctx.ClientExitFileLock())))
    33  	//) 8>{entry_flock}
    34  	c.Assert(result, gc.Matches, fmt.Sprintf("(.|\n)*\\) 8>%s(.|\n)*", regexp.QuoteMeta(ctx.ClientFileLock())))
    35  
    36  	// nil is the same as empty slice is the same as "*".
    37  	// Also, if "*" is present as well as a named hook,
    38  	// it is equivalent to "*".
    39  	c.Assert(debug.ClientScript(ctx, nil), gc.Equals, debug.ClientScript(ctx, []string{}))
    40  	c.Assert(debug.ClientScript(ctx, []string{"*"}), gc.Equals, debug.ClientScript(ctx, nil))
    41  	c.Assert(debug.ClientScript(ctx, []string{"*", "something"}), gc.Equals, debug.ClientScript(ctx, []string{"*"}))
    42  
    43  	// debug.ClientScript does not validate hook names, as it doesn't have
    44  	// a full state API connection to determine valid relation hooks.
    45  	expected := fmt.Sprintf(
    46  		`(.|\n)*echo "aG9va3M6Ci0gc29tZXRoaW5nIHNvbWV0aGluZ2Vsc2UK" | base64 -d > %s(.|\n)*`,
    47  		regexp.QuoteMeta(ctx.ClientFileLock()),
    48  	)
    49  	c.Assert(debug.ClientScript(ctx, []string{"something somethingelse"}), gc.Matches, expected)
    50  }