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

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package model_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/testing"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  	"gopkg.in/juju/names.v2"
    12  
    13  	"github.com/juju/juju/agent"
    14  	"github.com/juju/juju/api"
    15  	"github.com/juju/juju/cmd/jujud/agent/model"
    16  	coretesting "github.com/juju/juju/testing"
    17  )
    18  
    19  type WrapAgentSuite struct {
    20  	testing.IsolationSuite
    21  }
    22  
    23  var _ = gc.Suite(&WrapAgentSuite{})
    24  
    25  func (s *WrapAgentSuite) TestRequiresControllerUUID(c *gc.C) {
    26  	agent, err := model.WrapAgent(&mockAgent{}, "lol-nope-no-hope", coretesting.ModelTag.Id())
    27  	c.Check(err, gc.ErrorMatches, `controller uuid "lol-nope-no-hope" not valid`)
    28  	c.Check(err, jc.Satisfies, errors.IsNotValid)
    29  	c.Check(agent, gc.IsNil)
    30  }
    31  
    32  func (s *WrapAgentSuite) TestRequiresModelUUID(c *gc.C) {
    33  	agent, err := model.WrapAgent(&mockAgent{}, coretesting.ControllerTag.Id(), "lol-nope-no-hope")
    34  	c.Check(err, gc.ErrorMatches, `model uuid "lol-nope-no-hope" not valid`)
    35  	c.Check(err, jc.Satisfies, errors.IsNotValid)
    36  	c.Check(agent, gc.IsNil)
    37  }
    38  
    39  func (s *WrapAgentSuite) TestWraps(c *gc.C) {
    40  	agent, err := model.WrapAgent(&mockAgent{}, coretesting.ControllerTag.Id(), coretesting.ModelTag.Id())
    41  	c.Assert(err, jc.ErrorIsNil)
    42  	config := agent.CurrentConfig()
    43  
    44  	c.Check(config.Model(), gc.Equals, coretesting.ModelTag)
    45  	c.Check(config.Controller(), gc.Equals, coretesting.ControllerTag)
    46  	c.Check(config.OldPassword(), gc.Equals, "")
    47  
    48  	apiInfo, ok := config.APIInfo()
    49  	c.Assert(ok, jc.IsTrue)
    50  	c.Check(apiInfo, gc.DeepEquals, &api.Info{
    51  		Addrs:    []string{"here", "there"},
    52  		CACert:   "trust-me",
    53  		ModelTag: coretesting.ModelTag,
    54  		Tag:      names.NewMachineTag("123"),
    55  		Password: "12345",
    56  		Nonce:    "11111",
    57  	})
    58  }
    59  
    60  type mockAgent struct{ agent.Agent }
    61  
    62  func (mock *mockAgent) CurrentConfig() agent.Config {
    63  	return &mockConfig{}
    64  }
    65  
    66  type mockConfig struct{ agent.Config }
    67  
    68  func (mock *mockConfig) Tag() names.Tag {
    69  	return names.NewMachineTag("123")
    70  }
    71  
    72  func (mock *mockConfig) Model() names.ModelTag {
    73  	return names.NewModelTag("mock-model-uuid")
    74  }
    75  
    76  func (mock *mockConfig) Controller() names.ControllerTag {
    77  	return names.NewControllerTag("mock-controller-uuid")
    78  }
    79  
    80  func (mock *mockConfig) APIInfo() (*api.Info, bool) {
    81  	return &api.Info{
    82  		Addrs:    []string{"here", "there"},
    83  		CACert:   "trust-me",
    84  		ModelTag: names.NewModelTag("mock-model-uuid"),
    85  		Tag:      names.NewMachineTag("123"),
    86  		Password: "12345",
    87  		Nonce:    "11111",
    88  	}, true
    89  }
    90  
    91  func (mock *mockConfig) OldPassword() string {
    92  	return "do-not-use"
    93  }