This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
forked from t0mk/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_model_test.go
147 lines (127 loc) · 4.47 KB
/
api_model_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package featuretests
import (
"bytes"
"time"
"github.com/juju/errors"
jc "github.com/juju/testing/checkers"
"github.com/juju/version"
gc "gopkg.in/check.v1"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/api"
"github.com/juju/juju/api/modelmanager"
"github.com/juju/juju/apiserver/params"
jujunames "github.com/juju/juju/juju/names"
"github.com/juju/juju/juju/testing"
"github.com/juju/juju/permission"
"github.com/juju/juju/state"
coretesting "github.com/juju/juju/testing"
"github.com/juju/juju/testing/factory"
)
type apiEnvironmentSuite struct {
testing.JujuConnSuite
client *api.Client
}
func (s *apiEnvironmentSuite) SetUpTest(c *gc.C) {
s.JujuConnSuite.SetUpTest(c)
s.client = s.APIState.Client()
c.Assert(s.client, gc.NotNil)
s.AddCleanup(func(*gc.C) {
s.client.ClientFacade.Close()
})
}
func (s *apiEnvironmentSuite) TestGrantModel(c *gc.C) {
username := "foo@ubuntuone"
model, err := s.State.Model()
c.Assert(err, jc.ErrorIsNil)
mm := modelmanager.NewClient(s.OpenControllerAPI(c))
defer mm.Close()
err = mm.GrantModel(username, "read", model.UUID())
c.Assert(err, jc.ErrorIsNil)
user := names.NewUserTag(username)
modelUser, err := s.State.UserAccess(user, model.ModelTag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(modelUser.UserName, gc.Equals, user.Id())
lastConn, err := s.State.LastModelConnection(modelUser.UserTag)
c.Assert(err, jc.Satisfies, state.IsNeverConnectedError)
c.Assert(lastConn.IsZero(), jc.IsTrue)
}
func (s *apiEnvironmentSuite) TestRevokeModel(c *gc.C) {
// First share an environment with a user.
user := s.Factory.MakeModelUser(c, &factory.ModelUserParams{User: "foo@ubuntuone"})
model, err := s.State.Model()
c.Assert(err, jc.ErrorIsNil)
mm := modelmanager.NewClient(s.OpenControllerAPI(c))
defer mm.Close()
modelUser, err := s.State.UserAccess(user.UserTag, s.State.ModelTag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(modelUser, gc.Not(gc.DeepEquals), permission.UserAccess{})
// Then test unsharing the environment.
err = mm.RevokeModel(user.UserName, "read", model.UUID())
c.Assert(err, jc.ErrorIsNil)
modelUser, err = s.State.UserAccess(user.UserTag, s.State.ModelTag())
c.Assert(errors.IsNotFound(err), jc.IsTrue)
c.Assert(modelUser, gc.DeepEquals, permission.UserAccess{})
}
func (s *apiEnvironmentSuite) TestEnvironmentUserInfo(c *gc.C) {
modelUser := s.Factory.MakeModelUser(c, &factory.ModelUserParams{User: "bobjohns@ubuntuone", DisplayName: "Bob Johns"})
mod, err := s.State.Model()
c.Assert(err, jc.ErrorIsNil)
owner, err := s.State.UserAccess(mod.Owner(), mod.ModelTag())
c.Assert(err, jc.ErrorIsNil)
obtained, err := s.client.ModelUserInfo()
c.Assert(err, jc.ErrorIsNil)
c.Assert(obtained, gc.DeepEquals, []params.ModelUserInfo{
{
UserName: owner.UserName,
DisplayName: owner.DisplayName,
Access: "admin",
LastConnection: lastConnPointer(c, s.State, owner),
}, {
UserName: "bobjohns@ubuntuone",
DisplayName: "Bob Johns",
Access: "admin",
LastConnection: lastConnPointer(c, s.State, modelUser),
},
})
}
func lastConnPointer(c *gc.C, st *state.State, modelUser permission.UserAccess) *time.Time {
lastConn, err := st.LastModelConnection(modelUser.UserTag)
if err != nil {
if state.IsNeverConnectedError(err) {
return nil
}
c.Fatal(err)
}
return &lastConn
}
func (s *apiEnvironmentSuite) TestUploadToolsOtherEnvironment(c *gc.C) {
// setup other environment
otherState := s.Factory.MakeModel(c, nil)
defer otherState.Close()
info := s.APIInfo(c)
info.ModelTag = otherState.ModelTag()
otherAPIState, err := api.Open(info, api.DefaultDialOpts())
c.Assert(err, jc.ErrorIsNil)
defer otherAPIState.Close()
otherClient := otherAPIState.Client()
defer otherClient.ClientFacade.Close()
newVersion := version.MustParseBinary("5.4.3-quantal-amd64")
vers := newVersion.String()
// build fake tools
tgz, checksum := coretesting.TarGz(
coretesting.NewTarFile(jujunames.Jujud, 0777, "jujud contents "+vers))
toolsList, err := otherClient.UploadTools(bytes.NewReader(tgz), newVersion)
c.Assert(err, jc.ErrorIsNil)
c.Assert(toolsList, gc.HasLen, 1)
c.Assert(toolsList[0].SHA256, gc.Equals, checksum)
toolStrg, err := otherState.ToolsStorage()
defer toolStrg.Close()
c.Assert(err, jc.ErrorIsNil)
meta, closer, err := toolStrg.Open(vers)
defer closer.Close()
c.Assert(err, jc.ErrorIsNil)
c.Assert(meta.SHA256, gc.Equals, checksum)
c.Assert(meta.Version, gc.Equals, vers)
}