github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/state/endpoint_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  	"gopkg.in/juju/charm.v6-unstable"
    10  
    11  	"github.com/juju/juju/state"
    12  )
    13  
    14  type EndpointSuite struct {
    15  }
    16  
    17  var _ = gc.Suite(&EndpointSuite{})
    18  
    19  var canRelateTests = []struct {
    20  	role1, role2 charm.RelationRole
    21  	success      bool
    22  }{
    23  	{charm.RoleProvider, charm.RoleRequirer, true},
    24  	{charm.RoleRequirer, charm.RolePeer, false},
    25  	{charm.RolePeer, charm.RoleProvider, false},
    26  	{charm.RoleProvider, charm.RoleProvider, false},
    27  	{charm.RoleRequirer, charm.RoleRequirer, false},
    28  	{charm.RolePeer, charm.RolePeer, false},
    29  }
    30  
    31  func (s *EndpointSuite) TestCanRelate(c *gc.C) {
    32  	for i, t := range canRelateTests {
    33  		c.Logf("test %d", i)
    34  		ep1 := state.Endpoint{
    35  			ServiceName: "one-service",
    36  			Relation: charm.Relation{
    37  				Interface: "ifce",
    38  				Name:      "foo",
    39  				Role:      t.role1,
    40  				Scope:     charm.ScopeGlobal,
    41  			},
    42  		}
    43  		ep2 := state.Endpoint{
    44  			ServiceName: "another-service",
    45  			Relation: charm.Relation{
    46  				Interface: "ifce",
    47  				Name:      "bar",
    48  				Role:      t.role2,
    49  				Scope:     charm.ScopeGlobal,
    50  			},
    51  		}
    52  		if t.success {
    53  			c.Assert(ep1.CanRelateTo(ep2), jc.IsTrue)
    54  			c.Assert(ep2.CanRelateTo(ep1), jc.IsTrue)
    55  			ep1.Interface = "different"
    56  		}
    57  		c.Assert(ep1.CanRelateTo(ep2), jc.IsFalse)
    58  		c.Assert(ep2.CanRelateTo(ep1), jc.IsFalse)
    59  	}
    60  	ep1 := state.Endpoint{
    61  		ServiceName: "same-service",
    62  		Relation: charm.Relation{
    63  			Interface: "ifce",
    64  			Name:      "foo",
    65  			Role:      charm.RoleProvider,
    66  			Scope:     charm.ScopeGlobal,
    67  		},
    68  	}
    69  	ep2 := state.Endpoint{
    70  		ServiceName: "same-service",
    71  		Relation: charm.Relation{
    72  			Interface: "ifce",
    73  			Name:      "bar",
    74  			Role:      charm.RoleRequirer,
    75  			Scope:     charm.ScopeGlobal,
    76  		},
    77  	}
    78  	c.Assert(ep1.CanRelateTo(ep2), jc.IsFalse)
    79  	c.Assert(ep2.CanRelateTo(ep1), jc.IsFalse)
    80  }