github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runlistener_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package uniter_test
     5  
     6  import (
     7  	"path/filepath"
     8  	"runtime"
     9  
    10  	jc "github.com/juju/testing/checkers"
    11  	"github.com/juju/utils/exec"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/juju/sockets"
    15  	"github.com/juju/juju/testing"
    16  	"github.com/juju/juju/worker/uniter"
    17  	"github.com/juju/juju/worker/uniter/runcommands"
    18  )
    19  
    20  type ListenerSuite struct {
    21  	testing.BaseSuite
    22  	socketPath string
    23  }
    24  
    25  var _ = gc.Suite(&ListenerSuite{})
    26  
    27  func sockPath(c *gc.C) string {
    28  	sockPath := filepath.Join(c.MkDir(), "test.listener")
    29  	if runtime.GOOS == "windows" {
    30  		return `\\.\pipe` + sockPath[2:]
    31  	}
    32  	return sockPath
    33  }
    34  
    35  func (s *ListenerSuite) SetUpTest(c *gc.C) {
    36  	s.BaseSuite.SetUpTest(c)
    37  	s.socketPath = sockPath(c)
    38  }
    39  
    40  // Mirror the params to uniter.NewRunListener, but add cleanup to close it.
    41  func (s *ListenerSuite) NewRunListener(c *gc.C) *uniter.RunListener {
    42  	listener, err := uniter.NewRunListener(uniter.RunListenerConfig{
    43  		SocketPath:    s.socketPath,
    44  		CommandRunner: &mockRunner{c},
    45  	})
    46  	c.Assert(err, jc.ErrorIsNil)
    47  	c.Assert(listener, gc.NotNil)
    48  	s.AddCleanup(func(*gc.C) {
    49  		c.Assert(listener.Close(), jc.ErrorIsNil)
    50  	})
    51  	return listener
    52  }
    53  
    54  func (s *ListenerSuite) TestNewRunListenerOnExistingSocketRemovesItAndSucceeds(c *gc.C) {
    55  	if runtime.GOOS == "windows" {
    56  		c.Skip("bug 1403084: Current named pipes implementation does not support this")
    57  	}
    58  	s.NewRunListener(c)
    59  	s.NewRunListener(c)
    60  }
    61  
    62  func (s *ListenerSuite) TestClientCall(c *gc.C) {
    63  	s.NewRunListener(c)
    64  
    65  	client, err := sockets.Dial(s.socketPath)
    66  	c.Assert(err, jc.ErrorIsNil)
    67  	defer client.Close()
    68  
    69  	var result exec.ExecResponse
    70  	args := uniter.RunCommandsArgs{
    71  		Commands:        "some-command",
    72  		RelationId:      -1,
    73  		RemoteUnitName:  "",
    74  		ForceRemoteUnit: false,
    75  	}
    76  	err = client.Call(uniter.JujuRunEndpoint, args, &result)
    77  	c.Assert(err, jc.ErrorIsNil)
    78  
    79  	c.Assert(string(result.Stdout), gc.Equals, "some-command stdout")
    80  	c.Assert(string(result.Stderr), gc.Equals, "some-command stderr")
    81  	c.Assert(result.Code, gc.Equals, 42)
    82  }
    83  
    84  type ChannelCommandRunnerSuite struct {
    85  	testing.BaseSuite
    86  	abort          chan struct{}
    87  	commands       runcommands.Commands
    88  	commandChannel chan string
    89  	runner         *uniter.ChannelCommandRunner
    90  }
    91  
    92  var _ = gc.Suite(&ChannelCommandRunnerSuite{})
    93  
    94  func (s *ChannelCommandRunnerSuite) SetUpTest(c *gc.C) {
    95  	s.BaseSuite.SetUpTest(c)
    96  	s.abort = make(chan struct{}, 1)
    97  	s.commands = runcommands.NewCommands()
    98  	s.commandChannel = make(chan string, 1)
    99  	runner, err := uniter.NewChannelCommandRunner(uniter.ChannelCommandRunnerConfig{
   100  		Abort:          s.abort,
   101  		Commands:       s.commands,
   102  		CommandChannel: s.commandChannel,
   103  	})
   104  	c.Assert(err, jc.ErrorIsNil)
   105  	s.runner = runner
   106  }
   107  
   108  func (s *ChannelCommandRunnerSuite) TestCommandsAborted(c *gc.C) {
   109  	close(s.abort)
   110  	_, err := s.runner.RunCommands(uniter.RunCommandsArgs{
   111  		Commands: "some-command",
   112  	})
   113  	c.Assert(err, gc.ErrorMatches, "command execution aborted")
   114  }
   115  
   116  type mockRunner struct {
   117  	c *gc.C
   118  }
   119  
   120  var _ uniter.CommandRunner = (*mockRunner)(nil)
   121  
   122  func (r *mockRunner) RunCommands(args uniter.RunCommandsArgs) (results *exec.ExecResponse, err error) {
   123  	r.c.Log("mock runner: " + args.Commands)
   124  	return &exec.ExecResponse{
   125  		Code:   42,
   126  		Stdout: []byte(args.Commands + " stdout"),
   127  		Stderr: []byte(args.Commands + " stderr"),
   128  	}, nil
   129  }