-
Notifications
You must be signed in to change notification settings - Fork 0
/
environinstance_test.go
190 lines (153 loc) · 5.21 KB
/
environinstance_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package cloudsigma
import (
"time"
"github.com/altoros/gosigma/mock"
"github.com/juju/errors"
"github.com/juju/loggo"
jc "github.com/juju/testing/checkers"
"github.com/juju/version"
gc "gopkg.in/check.v1"
"github.com/juju/juju/cloudconfig/instancecfg"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/environs/imagemetadata"
"github.com/juju/juju/instance"
"github.com/juju/juju/testing"
"github.com/juju/juju/tools"
)
type environInstanceSuite struct {
testing.BaseSuite
cloud environs.CloudSpec
baseConfig *config.Config
callCtx context.ProviderCallContext
}
var _ = gc.Suite(&environInstanceSuite{})
func (s *environInstanceSuite) SetUpSuite(c *gc.C) {
s.BaseSuite.SetUpSuite(c)
mock.Start()
s.cloud = fakeCloudSpec()
s.cloud.Endpoint = mock.Endpoint("")
attrs := testing.Attrs{
"name": "testname",
"uuid": "f54aac3a-9dcd-4a0c-86b5-24091478478c",
}
s.baseConfig = newConfig(c, validAttrs().Merge(attrs))
}
func (s *environInstanceSuite) TearDownSuite(c *gc.C) {
mock.Stop()
s.BaseSuite.TearDownSuite(c)
}
func (s *environInstanceSuite) SetUpTest(c *gc.C) {
s.BaseSuite.SetUpTest(c)
ll := logger.LogLevel()
logger.SetLogLevel(loggo.TRACE)
s.AddCleanup(func(*gc.C) { logger.SetLogLevel(ll) })
mock.Reset()
s.callCtx = context.NewCloudCallContext()
}
func (s *environInstanceSuite) TearDownTest(c *gc.C) {
mock.Reset()
s.BaseSuite.TearDownTest(c)
}
func (s *environInstanceSuite) createEnviron(c *gc.C, cfg *config.Config) environs.Environ {
s.PatchValue(&findInstanceImage, func([]*imagemetadata.ImageMetadata) (*imagemetadata.ImageMetadata, error) {
img := &imagemetadata.ImageMetadata{
Id: validImageId,
}
return img, nil
})
if cfg == nil {
cfg = s.baseConfig
}
environ, err := environs.New(environs.OpenParams{
Cloud: s.cloud,
Config: cfg,
})
c.Assert(err, gc.IsNil)
c.Assert(environ, gc.NotNil)
return environ
}
func (s *environInstanceSuite) TestInstances(c *gc.C) {
env := s.createEnviron(c, nil)
instances, err := env.AllInstances(s.callCtx)
c.Assert(instances, gc.NotNil)
c.Assert(err, gc.IsNil)
c.Check(instances, gc.HasLen, 0)
uuid0 := addTestClientServer(c, jujuMetaInstanceServer, "f54aac3a-9dcd-4a0c-86b5-24091478478c")
uuid1 := addTestClientServer(c, jujuMetaInstanceController, "f54aac3a-9dcd-4a0c-86b5-24091478478c")
addTestClientServer(c, jujuMetaInstanceServer, "other-model")
addTestClientServer(c, jujuMetaInstanceController, "other-model")
instances, err = env.AllInstances(s.callCtx)
c.Assert(instances, gc.NotNil)
c.Assert(err, gc.IsNil)
c.Check(instances, gc.HasLen, 2)
ids := []instance.Id{instance.Id(uuid0), instance.Id(uuid1)}
instances, err = env.Instances(s.callCtx, ids)
c.Assert(instances, gc.NotNil)
c.Assert(err, gc.IsNil)
c.Check(instances, gc.HasLen, 2)
ids = append(ids, instance.Id("fake-instance"))
instances, err = env.Instances(s.callCtx, ids)
c.Assert(instances, gc.NotNil)
c.Assert(errors.Cause(err), gc.Equals, environs.ErrPartialInstances)
c.Check(instances, gc.HasLen, 3)
c.Check(instances[0], gc.NotNil)
c.Check(instances[1], gc.NotNil)
c.Check(instances[2], gc.IsNil)
err = env.StopInstances(s.callCtx, ids...)
c.Assert(err, gc.ErrorMatches, "404 Not Found.*")
instances, err = env.Instances(s.callCtx, ids)
c.Assert(instances, gc.NotNil)
c.Assert(errors.Cause(err), gc.Equals, environs.ErrNoInstances)
c.Check(instances, gc.HasLen, 3)
c.Check(instances[0], gc.IsNil)
c.Check(instances[1], gc.IsNil)
c.Check(instances[2], gc.IsNil)
}
func (s *environInstanceSuite) TestInstancesFail(c *gc.C) {
newClientFunc := newClient
s.PatchValue(&newClient, func(spec environs.CloudSpec, uuid string) (*environClient, error) {
spec.Endpoint = "https://0.1.2.3:2000/api/2.0/"
cli, err := newClientFunc(spec, uuid)
if cli != nil {
cli.conn.ConnectTimeout(10 * time.Millisecond)
}
return cli, err
})
environ := s.createEnviron(c, nil)
instances, err := environ.AllInstances(s.callCtx)
c.Assert(instances, gc.IsNil)
c.Assert(err, gc.NotNil)
instances, err = environ.Instances(s.callCtx, []instance.Id{instance.Id("123"), instance.Id("321")})
c.Assert(instances, gc.IsNil)
c.Assert(err, gc.NotNil)
}
func (s *environInstanceSuite) TestStartInstanceError(c *gc.C) {
environ := s.createEnviron(c, nil)
res, err := environ.StartInstance(s.callCtx, environs.StartInstanceParams{})
c.Check(res, gc.IsNil)
c.Check(err, gc.ErrorMatches, "instance configuration is nil")
toolsVal := &tools.Tools{
Version: version.Binary{
Series: "trusty",
},
URL: "https://0.1.2.3:2000/x.y.z.tgz",
}
res, err = environ.StartInstance(s.callCtx, environs.StartInstanceParams{
InstanceConfig: &instancecfg.InstanceConfig{},
})
c.Check(res, gc.IsNil)
c.Check(err, gc.ErrorMatches, "agent binaries not found")
icfg := &instancecfg.InstanceConfig{}
err = icfg.SetTools(tools.List{toolsVal})
c.Assert(err, jc.ErrorIsNil)
res, err = environ.StartInstance(s.callCtx, environs.StartInstanceParams{
Tools: tools.List{toolsVal},
InstanceConfig: icfg,
})
c.Check(res, gc.IsNil)
c.Check(err, gc.ErrorMatches, "cannot make user data: series \"\" not valid")
}