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

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/names/v5"
     9  	jc "github.com/juju/testing/checkers"
    10  	"github.com/juju/utils/v3"
    11  	gc "gopkg.in/check.v1"
    12  )
    13  
    14  type RemoteEntitiesSuite struct {
    15  	ConnSuite
    16  }
    17  
    18  var _ = gc.Suite(&RemoteEntitiesSuite{})
    19  
    20  func (s *RemoteEntitiesSuite) assertExportLocalEntity(c *gc.C, entity names.Tag) string {
    21  	re := s.State.RemoteEntities()
    22  	token, err := re.ExportLocalEntity(entity)
    23  	c.Assert(err, jc.ErrorIsNil)
    24  	c.Assert(token, gc.Not(gc.Equals), "")
    25  	return token
    26  }
    27  
    28  func (s *RemoteEntitiesSuite) TestAllRemoteEntities(c *gc.C) {
    29  	entity := names.NewApplicationTag("mysql")
    30  	token := s.assertExportLocalEntity(c, entity)
    31  
    32  	expected, err := s.State.AllRemoteEntities()
    33  	c.Assert(err, jc.ErrorIsNil)
    34  	c.Assert(expected, gc.HasLen, 1)
    35  
    36  	remoteEntity := expected[0]
    37  	c.Assert(entity.String(), gc.Equals, remoteEntity.ID())
    38  	c.Assert(token, gc.Equals, remoteEntity.Token())
    39  }
    40  
    41  func (s *RemoteEntitiesSuite) TestExportLocalEntity(c *gc.C) {
    42  	entity := names.NewApplicationTag("mysql")
    43  	token := s.assertExportLocalEntity(c, entity)
    44  
    45  	re := s.State.RemoteEntities()
    46  	expected, err := re.GetToken(entity)
    47  	c.Assert(err, jc.ErrorIsNil)
    48  	c.Assert(token, gc.Equals, expected)
    49  }
    50  
    51  func (s *RemoteEntitiesSuite) TestExportLocalEntityTwice(c *gc.C) {
    52  	entity := names.NewApplicationTag("mysql")
    53  	expected := s.assertExportLocalEntity(c, entity)
    54  	re := s.State.RemoteEntities()
    55  	token, err := re.ExportLocalEntity(entity)
    56  	c.Assert(err, jc.Satisfies, errors.IsAlreadyExists)
    57  	c.Assert(token, gc.Equals, expected)
    58  }
    59  
    60  func (s *RemoteEntitiesSuite) TestGetRemoteEntity(c *gc.C) {
    61  	entity := names.NewApplicationTag("mysql")
    62  	token := s.assertExportLocalEntity(c, entity)
    63  
    64  	re := s.State.RemoteEntities()
    65  	expected, err := re.GetRemoteEntity(token)
    66  	c.Assert(err, jc.ErrorIsNil)
    67  	c.Assert(entity, gc.Equals, expected)
    68  }
    69  
    70  func (s *RemoteEntitiesSuite) TestMacaroon(c *gc.C) {
    71  	entity := names.NewRelationTag("mysql:db wordpress:db")
    72  	s.assertExportLocalEntity(c, entity)
    73  
    74  	re := s.State.RemoteEntities()
    75  	mac, err := newMacaroon("id")
    76  	c.Assert(err, jc.ErrorIsNil)
    77  
    78  	err = re.SaveMacaroon(names.NewApplicationTag("foo"), mac)
    79  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
    80  
    81  	err = re.SaveMacaroon(entity, mac)
    82  	c.Assert(err, jc.ErrorIsNil)
    83  
    84  	re = s.State.RemoteEntities()
    85  	expected, err := re.GetMacaroon(entity)
    86  	c.Assert(err, jc.ErrorIsNil)
    87  	assertMacaroonEquals(c, mac, expected)
    88  }
    89  
    90  func (s *RemoteEntitiesSuite) TestRemoveRemoteEntity(c *gc.C) {
    91  	entity := names.NewApplicationTag("mysql")
    92  	token := s.assertExportLocalEntity(c, entity)
    93  
    94  	re := s.State.RemoteEntities()
    95  	err := re.RemoveRemoteEntity(entity)
    96  	c.Assert(err, jc.ErrorIsNil)
    97  	re = s.State.RemoteEntities()
    98  	_, err = re.GetRemoteEntity(token)
    99  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
   100  }
   101  
   102  func (s *RemoteEntitiesSuite) TestImportRemoteEntity(c *gc.C) {
   103  	re := s.State.RemoteEntities()
   104  	entity := names.NewApplicationTag("mysql")
   105  	token := utils.MustNewUUID().String()
   106  	err := re.ImportRemoteEntity(entity, token)
   107  	c.Assert(err, jc.ErrorIsNil)
   108  
   109  	re = s.State.RemoteEntities()
   110  	expected, err := re.GetRemoteEntity(token)
   111  	c.Assert(err, jc.ErrorIsNil)
   112  	c.Assert(entity, gc.Equals, expected)
   113  }
   114  
   115  func (s *RemoteEntitiesSuite) TestImportRemoteEntityOverwrites(c *gc.C) {
   116  	re := s.State.RemoteEntities()
   117  	entity := names.NewApplicationTag("mysql")
   118  	token := utils.MustNewUUID().String()
   119  	err := re.ImportRemoteEntity(entity, token)
   120  	c.Assert(err, jc.ErrorIsNil)
   121  
   122  	anotherToken := utils.MustNewUUID().String()
   123  	err = re.ImportRemoteEntity(entity, anotherToken)
   124  	c.Assert(err, jc.ErrorIsNil)
   125  
   126  	re = s.State.RemoteEntities()
   127  	_, err = re.GetRemoteEntity(token)
   128  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
   129  	expected, err := re.GetRemoteEntity(anotherToken)
   130  	c.Assert(err, jc.ErrorIsNil)
   131  	c.Assert(entity, gc.Equals, expected)
   132  }
   133  
   134  func (s *RemoteEntitiesSuite) TestImportRemoteEntityEmptyToken(c *gc.C) {
   135  	re := s.State.RemoteEntities()
   136  	entity := names.NewApplicationTag("mysql")
   137  	err := re.ImportRemoteEntity(entity, "")
   138  	c.Assert(err, jc.Satisfies, errors.IsNotValid)
   139  }