github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/api/client/annotations/client_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package annotations_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	"github.com/kr/pretty"
     9  	"go.uber.org/mock/gomock"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	basemocks "github.com/juju/juju/api/base/mocks"
    13  	"github.com/juju/juju/api/client/annotations"
    14  	"github.com/juju/juju/rpc/params"
    15  )
    16  
    17  type annotationsMockSuite struct{}
    18  
    19  var _ = gc.Suite(&annotationsMockSuite{})
    20  
    21  func (s *annotationsMockSuite) TestSetEntitiesAnnotation(c *gc.C) {
    22  	ctrl := gomock.NewController(c)
    23  	defer ctrl.Finish()
    24  
    25  	annts := map[string]string{"annotation1": "test"}
    26  	annts2 := map[string]string{"annotation2": "test"}
    27  	setParams := map[string]map[string]string{
    28  		"charmA":       annts,
    29  		"applicationB": annts2,
    30  	}
    31  
    32  	args := params.AnnotationsSet{
    33  		Annotations: []params.EntityAnnotations{
    34  			{
    35  				EntityTag:   "charmA",
    36  				Annotations: annts,
    37  			},
    38  			{
    39  				EntityTag:   "applicationB",
    40  				Annotations: annts2,
    41  			},
    42  		},
    43  	}
    44  
    45  	result := new(params.ErrorResults)
    46  	results := params.ErrorResults{
    47  		Results: nil,
    48  	}
    49  	mockFacadeCaller := basemocks.NewMockFacadeCaller(ctrl)
    50  	mockFacadeCaller.EXPECT().FacadeCall("Set", annotationsSetMatcher{c, args}, result).SetArg(2, results).DoAndReturn(
    51  		func(arg0 string, args params.AnnotationsSet, results *params.ErrorResults) []error {
    52  			for _, aParam := range args.Annotations {
    53  				// Since sometimes arrays returned on some
    54  				// architectures vary the order within params.AnnotationsSet,
    55  				// simply assert that each entity has its own annotations.
    56  				// Bug 1409141
    57  				c.Assert(aParam.Annotations, gc.DeepEquals, setParams[aParam.EntityTag])
    58  			}
    59  			return nil
    60  		})
    61  
    62  	annotationsClient := annotations.NewClientFromCaller(mockFacadeCaller)
    63  	callErrs, err := annotationsClient.Set(setParams)
    64  	c.Assert(err, jc.ErrorIsNil)
    65  	c.Assert(callErrs, gc.HasLen, 0)
    66  }
    67  
    68  func (s *annotationsMockSuite) TestGetEntitiesAnnotations(c *gc.C) {
    69  	ctrl := gomock.NewController(c)
    70  	defer ctrl.Finish()
    71  
    72  	args := params.Entities{
    73  		Entities: []params.Entity{{"charm"}},
    74  	}
    75  	facadeAnnts := map[string]string{
    76  		"annotations": "test",
    77  	}
    78  	entitiesAnnts := params.AnnotationsGetResult{
    79  		EntityTag:   "charm",
    80  		Annotations: facadeAnnts,
    81  	}
    82  	result := new(params.AnnotationsGetResults)
    83  	results := params.AnnotationsGetResults{
    84  		Results: []params.AnnotationsGetResult{entitiesAnnts},
    85  	}
    86  
    87  	mockFacadeCaller := basemocks.NewMockFacadeCaller(ctrl)
    88  	mockFacadeCaller.EXPECT().FacadeCall("Get", args, result).SetArg(2, results).Return(nil)
    89  
    90  	annotationsClient := annotations.NewClientFromCaller(mockFacadeCaller)
    91  	found, err := annotationsClient.Get([]string{"charm"})
    92  	c.Assert(err, jc.ErrorIsNil)
    93  	c.Assert(found, gc.HasLen, 1)
    94  }
    95  
    96  type annotationsSetMatcher struct {
    97  	m            *gc.C
    98  	expectedArgs params.AnnotationsSet
    99  }
   100  
   101  func (c annotationsSetMatcher) Matches(x interface{}) bool {
   102  	obtainedArgs, ok := x.(params.AnnotationsSet)
   103  	if !ok {
   104  		return false
   105  	}
   106  	c.m.Assert(obtainedArgs.Annotations, gc.HasLen, len(c.expectedArgs.Annotations))
   107  
   108  	for _, obt := range obtainedArgs.Annotations {
   109  		var found bool
   110  		for _, exp := range c.expectedArgs.Annotations {
   111  			if obt.EntityTag == exp.EntityTag {
   112  				c.m.Assert(obt, jc.DeepEquals, exp)
   113  				found = true
   114  				break
   115  			}
   116  		}
   117  		c.m.Assert(found, jc.IsTrue, gc.Commentf("unexpected annotation entity tag %s"))
   118  	}
   119  	return true
   120  }
   121  
   122  func (c annotationsSetMatcher) String() string {
   123  	return pretty.Sprintf("Match the contents of %v", c.expectedArgs)
   124  }