github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/apiserver/agent/agent_v0_test.go (about)

     1  package agent_test
     2  
     3  import (
     4  	"github.com/juju/names"
     5  	jc "github.com/juju/testing/checkers"
     6  	gc "gopkg.in/check.v1"
     7  
     8  	"github.com/juju/juju/apiserver/agent"
     9  	"github.com/juju/juju/apiserver/common"
    10  	"github.com/juju/juju/apiserver/params"
    11  	apiservertesting "github.com/juju/juju/apiserver/testing"
    12  	"github.com/juju/juju/instance"
    13  	"github.com/juju/juju/state"
    14  	"github.com/juju/juju/state/multiwatcher"
    15  )
    16  
    17  // Definition of reusable V0 tests.
    18  
    19  type factoryV0 func(st *state.State, resources *common.Resources, auth common.Authorizer) (interface{}, error)
    20  
    21  func (s *baseSuite) testAgentFailsWithNonAgentV0(c *gc.C, factory factoryV0) {
    22  	auth := s.authorizer
    23  	auth.Tag = names.NewUserTag("admin")
    24  	api, err := factory(s.State, s.resources, auth)
    25  	c.Assert(err, gc.NotNil)
    26  	c.Assert(api, gc.IsNil)
    27  	c.Assert(err, gc.ErrorMatches, "permission denied")
    28  }
    29  
    30  func (s *baseSuite) testAgentSucceedsWithUnitAgentV0(c *gc.C, factory factoryV0) {
    31  	auth := s.authorizer
    32  	auth.Tag = names.NewUnitTag("foosball/1")
    33  	_, err := factory(s.State, s.resources, auth)
    34  	c.Assert(err, jc.ErrorIsNil)
    35  }
    36  
    37  type getEntitiesV0 interface {
    38  	GetEntities(args params.Entities) params.AgentGetEntitiesResults
    39  }
    40  
    41  func (s *baseSuite) testGetEntitiesV0(c *gc.C, api getEntitiesV0) {
    42  	err := s.container.Destroy()
    43  	c.Assert(err, jc.ErrorIsNil)
    44  	args := params.Entities{
    45  		Entities: []params.Entity{
    46  			{Tag: "machine-1"},
    47  			{Tag: "machine-0"},
    48  			{Tag: "machine-1-lxc-0"},
    49  			{Tag: "machine-42"},
    50  		},
    51  	}
    52  	results := api.GetEntities(args)
    53  	c.Assert(results, gc.DeepEquals, params.AgentGetEntitiesResults{
    54  		Entities: []params.AgentGetEntitiesResult{
    55  			{
    56  				Life: "alive",
    57  				Jobs: []multiwatcher.MachineJob{multiwatcher.JobHostUnits},
    58  			},
    59  			{Error: apiservertesting.ErrUnauthorized},
    60  			{Error: apiservertesting.ErrUnauthorized},
    61  			{Error: apiservertesting.ErrUnauthorized},
    62  		},
    63  	})
    64  }
    65  
    66  func (s *baseSuite) testGetEntitiesContainerV0(c *gc.C, api getEntitiesV0) {
    67  	err := s.container.Destroy()
    68  	c.Assert(err, jc.ErrorIsNil)
    69  	args := params.Entities{
    70  		Entities: []params.Entity{
    71  			{Tag: "machine-1"},
    72  			{Tag: "machine-0"},
    73  			{Tag: "machine-1-lxc-0"},
    74  			{Tag: "machine-42"},
    75  		},
    76  	}
    77  	results := api.GetEntities(args)
    78  	c.Assert(results, gc.DeepEquals, params.AgentGetEntitiesResults{
    79  		Entities: []params.AgentGetEntitiesResult{
    80  			{Error: apiservertesting.ErrUnauthorized},
    81  			{Error: apiservertesting.ErrUnauthorized},
    82  			{
    83  				Life:          "dying",
    84  				Jobs:          []multiwatcher.MachineJob{multiwatcher.JobHostUnits},
    85  				ContainerType: instance.LXC,
    86  			},
    87  			{Error: apiservertesting.ErrUnauthorized},
    88  		},
    89  	})
    90  }
    91  
    92  func (s *baseSuite) testGetEntitiesNotFoundV0(c *gc.C, api getEntitiesV0) {
    93  	// Destroy the container first, so we can destroy its parent.
    94  	err := s.container.Destroy()
    95  	c.Assert(err, jc.ErrorIsNil)
    96  	err = s.container.EnsureDead()
    97  	c.Assert(err, jc.ErrorIsNil)
    98  	err = s.container.Remove()
    99  	c.Assert(err, jc.ErrorIsNil)
   100  
   101  	err = s.machine1.Destroy()
   102  	c.Assert(err, jc.ErrorIsNil)
   103  	err = s.machine1.EnsureDead()
   104  	c.Assert(err, jc.ErrorIsNil)
   105  	err = s.machine1.Remove()
   106  	c.Assert(err, jc.ErrorIsNil)
   107  	results := api.GetEntities(params.Entities{
   108  		Entities: []params.Entity{{Tag: "machine-1"}},
   109  	})
   110  	c.Assert(err, jc.ErrorIsNil)
   111  	c.Assert(results, gc.DeepEquals, params.AgentGetEntitiesResults{
   112  		Entities: []params.AgentGetEntitiesResult{{
   113  			Error: &params.Error{
   114  				Code:    params.CodeNotFound,
   115  				Message: "machine 1 not found",
   116  			},
   117  		}},
   118  	})
   119  }
   120  
   121  type setPasswordsV0 interface {
   122  	SetPasswords(args params.EntityPasswords) (params.ErrorResults, error)
   123  }
   124  
   125  func (s *baseSuite) testSetPasswordsV0(c *gc.C, api setPasswordsV0) {
   126  	results, err := api.SetPasswords(params.EntityPasswords{
   127  		Changes: []params.EntityPassword{
   128  			{Tag: "machine-0", Password: "xxx-12345678901234567890"},
   129  			{Tag: "machine-1", Password: "yyy-12345678901234567890"},
   130  			{Tag: "machine-42", Password: "zzz-12345678901234567890"},
   131  		},
   132  	})
   133  	c.Assert(err, jc.ErrorIsNil)
   134  	c.Assert(results, gc.DeepEquals, params.ErrorResults{
   135  		Results: []params.ErrorResult{
   136  			{apiservertesting.ErrUnauthorized},
   137  			{nil},
   138  			{apiservertesting.ErrUnauthorized},
   139  		},
   140  	})
   141  	err = s.machine1.Refresh()
   142  	c.Assert(err, jc.ErrorIsNil)
   143  	changed := s.machine1.PasswordValid("yyy-12345678901234567890")
   144  	c.Assert(changed, jc.IsTrue)
   145  }
   146  
   147  func (s *baseSuite) testSetPasswordsShortV0(c *gc.C, api setPasswordsV0) {
   148  	results, err := api.SetPasswords(params.EntityPasswords{
   149  		Changes: []params.EntityPassword{
   150  			{Tag: "machine-1", Password: "yyy"},
   151  		},
   152  	})
   153  	c.Assert(err, jc.ErrorIsNil)
   154  	c.Assert(results.Results, gc.HasLen, 1)
   155  	c.Assert(results.Results[0].Error, gc.ErrorMatches,
   156  		"password is only 3 bytes long, and is not a valid Agent password")
   157  }
   158  
   159  // V0 test suite.
   160  
   161  func factoryWrapperV0(st *state.State, resources *common.Resources, auth common.Authorizer) (interface{}, error) {
   162  	return agent.NewAgentAPIV0(st, resources, auth)
   163  }
   164  
   165  type agentSuiteV0 struct {
   166  	baseSuite
   167  }
   168  
   169  var _ = gc.Suite(&agentSuiteV0{})
   170  
   171  func (s *agentSuiteV0) TestAgentFailsWithNonAgent(c *gc.C) {
   172  	s.testAgentFailsWithNonAgentV0(c, factoryWrapperV0)
   173  }
   174  
   175  func (s *agentSuiteV0) TestAgentSucceedsWithUnitAgent(c *gc.C) {
   176  	s.testAgentSucceedsWithUnitAgentV0(c, factoryWrapperV0)
   177  }
   178  
   179  func (s *agentSuiteV0) TestGetEntities(c *gc.C) {
   180  	s.testGetEntitiesV0(c, s.newAPI(c))
   181  }
   182  
   183  func (s *agentSuiteV0) TestGetEntitiesContainer(c *gc.C) {
   184  	auth := s.authorizer
   185  	auth.Tag = s.container.Tag()
   186  	api, err := agent.NewAgentAPIV0(s.State, s.resources, auth)
   187  	c.Assert(err, jc.ErrorIsNil)
   188  	s.testGetEntitiesContainerV0(c, api)
   189  }
   190  
   191  func (s *agentSuiteV0) TestGetEntitiesNotFound(c *gc.C) {
   192  	s.testGetEntitiesNotFoundV0(c, s.newAPI(c))
   193  }
   194  
   195  func (s *agentSuiteV0) TestSetPasswords(c *gc.C) {
   196  	s.testSetPasswordsV0(c, s.newAPI(c))
   197  }
   198  
   199  func (s *agentSuiteV0) TestSetPasswordsShort(c *gc.C) {
   200  	s.testSetPasswordsShortV0(c, s.newAPI(c))
   201  }
   202  
   203  func (s *agentSuiteV0) newAPI(c *gc.C) *agent.AgentAPIV0 {
   204  	api, err := agent.NewAgentAPIV0(s.State, s.resources, s.authorizer)
   205  	c.Assert(err, jc.ErrorIsNil)
   206  	return api
   207  }