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