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