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

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package api_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	"github.com/juju/utils/set"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/api"
    12  	"github.com/juju/juju/apiserver/common"
    13  	coretesting "github.com/juju/juju/testing"
    14  )
    15  
    16  type facadeVersionSuite struct {
    17  	coretesting.BaseSuite
    18  }
    19  
    20  var _ = gc.Suite(&facadeVersionSuite{})
    21  
    22  func (s *facadeVersionSuite) TestFacadeVersionsMatchServerVersions(c *gc.C) {
    23  	// The client side code doesn't want to directly import the server side
    24  	// code just to list out what versions are available. However, we do
    25  	// want to make sure that the two sides are kept in sync.
    26  	clientFacadeNames := set.NewStrings()
    27  	for name := range *api.FacadeVersions {
    28  		clientFacadeNames.Add(name)
    29  	}
    30  	allServerFacades := common.Facades.List()
    31  	serverFacadeNames := set.NewStrings()
    32  	serverFacadeBestVersions := make(map[string]int, len(allServerFacades))
    33  	for _, facade := range allServerFacades {
    34  		serverFacadeNames.Add(facade.Name)
    35  		serverFacadeBestVersions[facade.Name] = facade.Versions[len(facade.Versions)-1]
    36  	}
    37  	// First check that both sides know about all the same versions
    38  	c.Check(serverFacadeNames.Difference(clientFacadeNames).SortedValues(), gc.HasLen, 0)
    39  	c.Check(clientFacadeNames.Difference(serverFacadeNames).SortedValues(), gc.HasLen, 0)
    40  	// Next check that the best versions match
    41  	c.Check(*api.FacadeVersions, jc.DeepEquals, serverFacadeBestVersions)
    42  }
    43  
    44  func checkBestVersion(c *gc.C, desiredVersion int, versions []int, expectedVersion int) {
    45  	resultVersion := api.BestVersion(desiredVersion, versions)
    46  	c.Check(resultVersion, gc.Equals, expectedVersion)
    47  }
    48  
    49  func (*facadeVersionSuite) TestBestVersionDesiredAvailable(c *gc.C) {
    50  	checkBestVersion(c, 0, []int{0, 1, 2}, 0)
    51  	checkBestVersion(c, 1, []int{0, 1, 2}, 1)
    52  	checkBestVersion(c, 2, []int{0, 1, 2}, 2)
    53  }
    54  
    55  func (*facadeVersionSuite) TestBestVersionDesiredNewer(c *gc.C) {
    56  	checkBestVersion(c, 3, []int{0}, 0)
    57  	checkBestVersion(c, 3, []int{0, 1, 2}, 2)
    58  }
    59  
    60  func (*facadeVersionSuite) TestBestVersionDesiredGap(c *gc.C) {
    61  	checkBestVersion(c, 1, []int{0, 2}, 0)
    62  }
    63  
    64  func (*facadeVersionSuite) TestBestVersionNoVersions(c *gc.C) {
    65  	checkBestVersion(c, 0, []int{}, 0)
    66  	checkBestVersion(c, 1, []int{}, 0)
    67  	checkBestVersion(c, 0, []int(nil), 0)
    68  	checkBestVersion(c, 1, []int(nil), 0)
    69  }
    70  
    71  func (*facadeVersionSuite) TestBestVersionNotSorted(c *gc.C) {
    72  	checkBestVersion(c, 0, []int{0, 3, 1, 2}, 0)
    73  	checkBestVersion(c, 3, []int{0, 3, 1, 2}, 3)
    74  	checkBestVersion(c, 1, []int{0, 3, 1, 2}, 1)
    75  	checkBestVersion(c, 2, []int{0, 3, 1, 2}, 2)
    76  }
    77  
    78  func (s *facadeVersionSuite) TestBestFacadeVersionExactMatch(c *gc.C) {
    79  	s.PatchValue(api.FacadeVersions, map[string]int{"Client": 1})
    80  	st := api.NewTestingState(api.TestingStateParams{
    81  		FacadeVersions: map[string][]int{
    82  			"Client": {0, 1},
    83  		}})
    84  	c.Check(st.BestFacadeVersion("Client"), gc.Equals, 1)
    85  }
    86  
    87  func (s *facadeVersionSuite) TestBestFacadeVersionNewerServer(c *gc.C) {
    88  	s.PatchValue(api.FacadeVersions, map[string]int{"Client": 1})
    89  	st := api.NewTestingState(api.TestingStateParams{
    90  		FacadeVersions: map[string][]int{
    91  			"Client": {0, 1, 2},
    92  		}})
    93  	c.Check(st.BestFacadeVersion("Client"), gc.Equals, 1)
    94  }
    95  
    96  func (s *facadeVersionSuite) TestBestFacadeVersionNewerClient(c *gc.C) {
    97  	s.PatchValue(api.FacadeVersions, map[string]int{"Client": 2})
    98  	st := api.NewTestingState(api.TestingStateParams{
    99  		FacadeVersions: map[string][]int{
   100  			"Client": {0, 1},
   101  		}})
   102  	c.Check(st.BestFacadeVersion("Client"), gc.Equals, 1)
   103  }
   104  
   105  func (s *facadeVersionSuite) TestBestFacadeVersionServerUnknown(c *gc.C) {
   106  	s.PatchValue(api.FacadeVersions, map[string]int{"TestingAPI": 2})
   107  	st := api.NewTestingState(api.TestingStateParams{
   108  		FacadeVersions: map[string][]int{
   109  			"Client": {0, 1},
   110  		}})
   111  	c.Check(st.BestFacadeVersion("TestingAPI"), gc.Equals, 0)
   112  }