github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/api/uniter/relation_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package uniter_test
     5  
     6  import (
     7  	gc "launchpad.net/gocheck"
     8  
     9  	"launchpad.net/juju-core/charm"
    10  	"launchpad.net/juju-core/state/api/params"
    11  	"launchpad.net/juju-core/state/api/uniter"
    12  	jc "launchpad.net/juju-core/testing/checkers"
    13  )
    14  
    15  type relationSuite struct {
    16  	uniterSuite
    17  	commonRelationSuiteMixin
    18  
    19  	apiRelation *uniter.Relation
    20  }
    21  
    22  var _ = gc.Suite(&relationSuite{})
    23  
    24  func (s *relationSuite) SetUpTest(c *gc.C) {
    25  	s.uniterSuite.SetUpTest(c)
    26  	s.commonRelationSuiteMixin.SetUpTest(c, s.uniterSuite)
    27  
    28  	var err error
    29  	s.apiRelation, err = s.uniter.Relation(s.stateRelation.Tag())
    30  	c.Assert(err, gc.IsNil)
    31  }
    32  
    33  func (s *relationSuite) TearDownTest(c *gc.C) {
    34  	s.uniterSuite.TearDownTest(c)
    35  }
    36  
    37  func (s *relationSuite) TestString(c *gc.C) {
    38  	c.Assert(s.apiRelation.String(), gc.Equals, "wordpress:db mysql:server")
    39  }
    40  
    41  func (s *relationSuite) TestId(c *gc.C) {
    42  	c.Assert(s.apiRelation.Id(), gc.Equals, s.stateRelation.Id())
    43  }
    44  
    45  func (s *relationSuite) TestRefresh(c *gc.C) {
    46  	c.Assert(s.apiRelation.Life(), gc.Equals, params.Alive)
    47  
    48  	// EnterScope with mysqlUnit, so the relation will be set to dying
    49  	// when destroyed later.
    50  	myRelUnit, err := s.stateRelation.Unit(s.mysqlUnit)
    51  	c.Assert(err, gc.IsNil)
    52  	err = myRelUnit.EnterScope(nil)
    53  	c.Assert(err, gc.IsNil)
    54  	s.assertInScope(c, myRelUnit, true)
    55  
    56  	// Destroy it - should set it to dying.
    57  	err = s.stateRelation.Destroy()
    58  	c.Assert(err, gc.IsNil)
    59  	c.Assert(s.apiRelation.Life(), gc.Equals, params.Alive)
    60  
    61  	err = s.apiRelation.Refresh()
    62  	c.Assert(err, gc.IsNil)
    63  	c.Assert(s.apiRelation.Life(), gc.Equals, params.Dying)
    64  
    65  	// Leave scope with mysqlUnit, so the relation will be removed.
    66  	err = myRelUnit.LeaveScope()
    67  	c.Assert(err, gc.IsNil)
    68  
    69  	c.Assert(s.apiRelation.Life(), gc.Equals, params.Dying)
    70  	err = s.apiRelation.Refresh()
    71  	c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized)
    72  }
    73  
    74  func (s *relationSuite) TestEndpoint(c *gc.C) {
    75  	apiEndpoint, err := s.apiRelation.Endpoint()
    76  	c.Assert(err, gc.IsNil)
    77  	c.Assert(apiEndpoint, gc.DeepEquals, &uniter.Endpoint{
    78  		charm.Relation{
    79  			Name:      "db",
    80  			Role:      "requirer",
    81  			Interface: "mysql",
    82  			Optional:  false,
    83  			Limit:     1,
    84  			Scope:     "global",
    85  		},
    86  	})
    87  }
    88  
    89  func (s *relationSuite) TestUnit(c *gc.C) {
    90  	_, err := s.apiRelation.Unit(nil)
    91  	c.Assert(err, gc.ErrorMatches, "unit is nil")
    92  
    93  	apiUnit, err := s.uniter.Unit("unit-wordpress-0")
    94  	c.Assert(err, gc.IsNil)
    95  	apiRelUnit, err := s.apiRelation.Unit(apiUnit)
    96  	c.Assert(err, gc.IsNil)
    97  	c.Assert(apiRelUnit, gc.NotNil)
    98  	// We just ensure we get the correct type, more tests
    99  	// are done in relationunit_test.go.
   100  	c.Assert(apiRelUnit, gc.FitsTypeOf, (*uniter.RelationUnit)(nil))
   101  }
   102  
   103  func (s *relationSuite) TestRelationById(c *gc.C) {
   104  	apiRel, err := s.uniter.RelationById(s.stateRelation.Id())
   105  	c.Assert(err, gc.IsNil)
   106  	c.Assert(apiRel, gc.DeepEquals, s.apiRelation)
   107  
   108  	// Add a relation to mysql service, which cannot be retrived.
   109  	otherRel, _, _ := s.addRelatedService(c, "mysql", "logging", s.mysqlUnit)
   110  
   111  	// Test some invalid cases.
   112  	for _, relId := range []int{-1, 42, otherRel.Id()} {
   113  		apiRel, err = s.uniter.RelationById(relId)
   114  		c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized)
   115  		c.Assert(apiRel, gc.IsNil)
   116  	}
   117  }