github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/jujud/agent/checkconnection_test.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package agent_test
     5  
     6  import (
     7  	"io"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/testing"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/agent"
    15  	agentcmd "github.com/juju/juju/cmd/jujud/agent"
    16  )
    17  
    18  type checkConnectionSuite struct {
    19  	testing.IsolationSuite
    20  }
    21  
    22  var _ = gc.Suite(&checkConnectionSuite{})
    23  
    24  func (s *checkConnectionSuite) TestInitChecksTag(c *gc.C) {
    25  	cmd := agentcmd.NewCheckConnectionCommand(nil, nil)
    26  	err := cmd.Init(nil)
    27  	c.Assert(err, gc.ErrorMatches, "agent-name argument is required")
    28  	err = cmd.Init([]string{"aloy"})
    29  	c.Assert(err, gc.ErrorMatches, `agent-name: "aloy" is not a valid tag`)
    30  	err = cmd.Init([]string{"user-eleuthia"})
    31  	c.Assert(err, gc.ErrorMatches, `agent-name must be a machine or unit tag`)
    32  	err = cmd.Init([]string{"unit-demeter-0", "minerva"})
    33  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["minerva"\]`)
    34  }
    35  
    36  func (s *checkConnectionSuite) TestRunComplainsAboutConnectionErrors(c *gc.C) {
    37  	cmd := agentcmd.NewCheckConnectionCommand(newAgentConf(),
    38  		func(a agent.Agent) (io.Closer, error) {
    39  			return nil, errors.Errorf("hartz-timor swarm detected")
    40  		})
    41  	c.Assert(cmd.Init([]string{"unit-artemis-5"}), jc.ErrorIsNil)
    42  	err := cmd.Run(nil)
    43  	c.Assert(err, gc.ErrorMatches, "checking connection for unit-artemis-5: hartz-timor swarm detected")
    44  }
    45  
    46  func (s *checkConnectionSuite) TestRunClosesConnection(c *gc.C) {
    47  	cmd := agentcmd.NewCheckConnectionCommand(newAgentConf(),
    48  		func(a agent.Agent) (io.Closer, error) {
    49  			return &mockConnection{}, nil
    50  		})
    51  	c.Assert(cmd.Init([]string{"unit-artemis-5"}), jc.ErrorIsNil)
    52  	err := cmd.Run(nil)
    53  	c.Assert(err, gc.ErrorMatches, "closing connection for unit-artemis-5: seal integrity check failed")
    54  }
    55  
    56  func newAgentConf() *mockAgentConf {
    57  	return &mockAgentConf{stub: &testing.Stub{}}
    58  }
    59  
    60  type mockAgentConf struct {
    61  	agentcmd.AgentConf
    62  	stub *testing.Stub
    63  }
    64  
    65  func (c *mockAgentConf) ReadConfig(tag string) error {
    66  	c.stub.AddCall("ReadConfig", tag)
    67  	return c.stub.NextErr()
    68  }
    69  
    70  type mockConnection struct{}
    71  
    72  func (c *mockConnection) Close() error {
    73  	return errors.Errorf("seal integrity check failed")
    74  }