github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/offerconnections_test.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/errors"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/core/status"
    14  	"github.com/juju/juju/state"
    15  	"github.com/juju/juju/testing"
    16  )
    17  
    18  type offerConnectionsSuite struct {
    19  	ConnSuite
    20  
    21  	suspendedRel *state.Relation
    22  	activeRel    *state.Relation
    23  }
    24  
    25  var _ = gc.Suite(&offerConnectionsSuite{})
    26  
    27  func (s *offerConnectionsSuite) SetUpTest(c *gc.C) {
    28  	s.ConnSuite.SetUpTest(c)
    29  	s.AddTestingApplication(c, "mysql", s.AddTestingCharm(c, "mysql"))
    30  	wpCh := s.AddTestingCharm(c, "wordpress")
    31  	s.AddTestingApplication(c, "wordpress", wpCh)
    32  	s.AddTestingApplication(c, "wordpress2", wpCh)
    33  
    34  	eps, err := s.State.InferEndpoints("wordpress", "mysql")
    35  	c.Assert(err, jc.ErrorIsNil)
    36  	s.activeRel, err = s.State.AddRelation(eps...)
    37  	c.Assert(err, jc.ErrorIsNil)
    38  	err = s.activeRel.SetStatus(status.StatusInfo{Status: status.Joined})
    39  	c.Assert(err, jc.ErrorIsNil)
    40  
    41  	eps, err = s.State.InferEndpoints("wordpress2", "mysql")
    42  	c.Assert(err, jc.ErrorIsNil)
    43  	s.suspendedRel, err = s.State.AddRelation(eps...)
    44  	c.Assert(err, jc.ErrorIsNil)
    45  	err = s.suspendedRel.SetStatus(status.StatusInfo{Status: status.Suspended})
    46  	c.Assert(err, jc.ErrorIsNil)
    47  }
    48  
    49  func (s *offerConnectionsSuite) TestAddOfferConnection(c *gc.C) {
    50  	oc, err := s.State.AddOfferConnection(state.AddOfferConnectionParams{
    51  		SourceModelUUID: testing.ModelTag.Id(),
    52  		RelationId:      s.suspendedRel.Id(),
    53  		RelationKey:     s.suspendedRel.Tag().Id(),
    54  		Username:        "fred",
    55  		OfferUUID:       "offer-uuid",
    56  	})
    57  	c.Assert(err, jc.ErrorIsNil)
    58  	c.Assert(oc.SourceModelUUID(), gc.Equals, testing.ModelTag.Id())
    59  	c.Assert(oc.RelationId(), gc.Equals, s.suspendedRel.Id())
    60  	c.Assert(oc.RelationKey(), gc.Equals, s.suspendedRel.Tag().Id())
    61  	c.Assert(oc.OfferUUID(), gc.Equals, "offer-uuid")
    62  	c.Assert(oc.UserName(), gc.Equals, "fred")
    63  
    64  	_, err = s.State.AddOfferConnection(state.AddOfferConnectionParams{
    65  		SourceModelUUID: testing.ModelTag.Id(),
    66  		RelationId:      s.activeRel.Id(),
    67  		RelationKey:     s.activeRel.Tag().Id(),
    68  		Username:        "fred",
    69  		OfferUUID:       "offer-uuid",
    70  	})
    71  	c.Assert(err, jc.ErrorIsNil)
    72  
    73  	rc, err := s.State.RemoteConnectionStatus("offer-uuid")
    74  	c.Assert(err, jc.ErrorIsNil)
    75  	c.Assert(rc.TotalConnectionCount(), gc.Equals, 2)
    76  	c.Assert(rc.ActiveConnectionCount(), gc.Equals, 1)
    77  
    78  	all, err := s.State.OfferConnections("offer-uuid")
    79  	c.Assert(err, jc.ErrorIsNil)
    80  	c.Assert(all, gc.HasLen, 2)
    81  	c.Assert(all[0].SourceModelUUID(), gc.Equals, testing.ModelTag.Id())
    82  	c.Assert(all[0].RelationId(), gc.Equals, s.suspendedRel.Id())
    83  	c.Assert(all[0].RelationKey(), gc.Equals, s.suspendedRel.Tag().Id())
    84  	c.Assert(all[0].OfferUUID(), gc.Equals, "offer-uuid")
    85  	c.Assert(all[0].UserName(), gc.Equals, "fred")
    86  	c.Assert(all[0].String(),
    87  		gc.Equals, fmt.Sprintf(`connection to "offer-uuid" by "fred" for relation %d`, s.suspendedRel.Id()))
    88  	c.Assert(all[1].SourceModelUUID(), gc.Equals, testing.ModelTag.Id())
    89  	c.Assert(all[1].RelationId(), gc.Equals, s.activeRel.Id())
    90  	c.Assert(all[1].RelationKey(), gc.Equals, s.activeRel.Tag().Id())
    91  	c.Assert(all[1].OfferUUID(), gc.Equals, "offer-uuid")
    92  	c.Assert(all[1].UserName(), gc.Equals, "fred")
    93  	c.Assert(all[1].String(),
    94  		gc.Equals, fmt.Sprintf(`connection to "offer-uuid" by "fred" for relation %d`, s.activeRel.Id()))
    95  }
    96  
    97  func (s *offerConnectionsSuite) TestAddOfferConnectionNotFound(c *gc.C) {
    98  	// Note: missing RelationKey to trigger a not-found error.
    99  	_, err := s.State.AddOfferConnection(state.AddOfferConnectionParams{
   100  		SourceModelUUID: testing.ModelTag.Id(),
   101  		RelationId:      s.activeRel.Id(),
   102  		Username:        "fred",
   103  		OfferUUID:       "offer-uuid",
   104  	})
   105  	c.Assert(err, jc.ErrorIsNil)
   106  
   107  	rc, err := s.State.RemoteConnectionStatus("offer-uuid")
   108  	c.Assert(err, jc.ErrorIsNil)
   109  	c.Assert(rc.TotalConnectionCount(), gc.Equals, 1)
   110  	c.Assert(rc.ActiveConnectionCount(), gc.Equals, 0)
   111  }
   112  
   113  func (s *offerConnectionsSuite) TestAddOfferConnectionTwice(c *gc.C) {
   114  	_, err := s.State.AddOfferConnection(state.AddOfferConnectionParams{
   115  		SourceModelUUID: testing.ModelTag.Id(),
   116  		RelationId:      s.activeRel.Id(),
   117  		RelationKey:     s.activeRel.Tag().Id(),
   118  		Username:        "fred",
   119  		OfferUUID:       "offer-uuid",
   120  	})
   121  	c.Assert(err, jc.ErrorIsNil)
   122  
   123  	_, err = s.State.AddOfferConnection(state.AddOfferConnectionParams{
   124  		SourceModelUUID: testing.ModelTag.Id(),
   125  		RelationId:      s.activeRel.Id(),
   126  		RelationKey:     s.activeRel.Tag().Id(),
   127  		Username:        "fred",
   128  		OfferUUID:       "offer-uuid",
   129  	})
   130  	c.Assert(err, jc.Satisfies, errors.IsAlreadyExists)
   131  }
   132  
   133  func (s *offerConnectionsSuite) TestOfferConnectionForRelation(c *gc.C) {
   134  	oc, err := s.State.AddOfferConnection(state.AddOfferConnectionParams{
   135  		SourceModelUUID: testing.ModelTag.Id(),
   136  		RelationId:      s.activeRel.Id(),
   137  		RelationKey:     s.activeRel.Tag().Id(),
   138  		Username:        "fred",
   139  		OfferUUID:       "offer-uuid",
   140  	})
   141  	c.Assert(err, jc.ErrorIsNil)
   142  
   143  	_, err = s.State.OfferConnectionForRelation("some-key")
   144  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
   145  	obtained, err := s.State.OfferConnectionForRelation(s.activeRel.Tag().Id())
   146  	c.Assert(err, jc.ErrorIsNil)
   147  	c.Assert(obtained.RelationId(), gc.Equals, oc.RelationId())
   148  	c.Assert(obtained.RelationKey(), gc.Equals, oc.RelationKey())
   149  	c.Assert(obtained.OfferUUID(), gc.Equals, oc.OfferUUID())
   150  }
   151  
   152  func (s *offerConnectionsSuite) TestOfferConnectionsForUser(c *gc.C) {
   153  	oc, err := s.State.AddOfferConnection(state.AddOfferConnectionParams{
   154  		SourceModelUUID: testing.ModelTag.Id(),
   155  		RelationId:      s.activeRel.Id(),
   156  		RelationKey:     s.activeRel.Tag().Id(),
   157  		Username:        "fred",
   158  		OfferUUID:       "offer-uuid",
   159  	})
   160  	c.Assert(err, jc.ErrorIsNil)
   161  
   162  	obtained, err := s.State.OfferConnectionsForUser("mary")
   163  	c.Assert(err, jc.ErrorIsNil)
   164  	c.Assert(obtained, gc.HasLen, 0)
   165  	obtained, err = s.State.OfferConnectionsForUser("fred")
   166  	c.Assert(err, jc.ErrorIsNil)
   167  	c.Assert(obtained, gc.HasLen, 1)
   168  	c.Assert(obtained[0].OfferUUID(), gc.Equals, oc.OfferUUID())
   169  	c.Assert(obtained[0].UserName(), gc.Equals, oc.UserName())
   170  }
   171  
   172  func (s *offerConnectionsSuite) TestAllOfferConnections(c *gc.C) {
   173  	obtained, err := s.State.AllOfferConnections()
   174  	c.Assert(err, jc.ErrorIsNil)
   175  	c.Assert(obtained, gc.HasLen, 0)
   176  
   177  	oc1, err := s.State.AddOfferConnection(state.AddOfferConnectionParams{
   178  		SourceModelUUID: testing.ModelTag.Id(),
   179  		RelationId:      s.activeRel.Id(),
   180  		RelationKey:     s.activeRel.Tag().Id(),
   181  		Username:        "fred",
   182  		OfferUUID:       "offer-uuid1",
   183  	})
   184  	c.Assert(err, jc.ErrorIsNil)
   185  
   186  	oc2, err := s.State.AddOfferConnection(state.AddOfferConnectionParams{
   187  		SourceModelUUID: testing.ModelTag.Id(),
   188  		RelationId:      s.suspendedRel.Id(),
   189  		RelationKey:     s.suspendedRel.Tag().Id(),
   190  		Username:        "mary",
   191  		OfferUUID:       "offer-uuid2",
   192  	})
   193  	c.Assert(err, jc.ErrorIsNil)
   194  
   195  	obtained, err = s.State.AllOfferConnections()
   196  	c.Assert(err, jc.ErrorIsNil)
   197  
   198  	// Get strings for comparison. Comparing pointers is no good.
   199  	obtainedStr := make([]string, len(obtained))
   200  	for i, v := range obtained {
   201  		obtainedStr[i] = v.String()
   202  	}
   203  	c.Assert(obtainedStr, jc.SameContents, []string{oc1.String(), oc2.String()})
   204  
   205  }