-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevices_test.go
88 lines (72 loc) · 2.64 KB
/
devices_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
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state_test
import (
gc "gopkg.in/check.v1"
jc "github.com/juju/testing/checkers"
"github.com/juju/juju/state"
)
type DevicesStateSuiteBase struct {
ConnSuite
series string
st *state.State
deviceBackend *state.DeviceBackend
}
func (s *DevicesStateSuiteBase) SetUpTest(c *gc.C) {
s.ConnSuite.SetUpTest(c)
if s.series == "kubernetes" {
s.st = s.Factory.MakeCAASModel(c, nil)
s.AddCleanup(func(_ *gc.C) { s.st.Close() })
} else {
s.st = s.State
s.series = "quantal"
}
var err error
s.deviceBackend, err = state.NewDeviceBackend(s.st)
c.Assert(err, jc.ErrorIsNil)
}
func (s *DevicesStateSuiteBase) AddTestingCharm(c *gc.C, name string) *state.Charm {
return state.AddTestingCharmForSeries(c, s.st, s.series, name)
}
func makeDeviceCons(t state.DeviceType, count int64) state.DeviceConstraints {
return state.DeviceConstraints{Type: t, Count: count}
}
type CAASDevicesStateSuite struct {
DevicesStateSuiteBase
}
func (s *CAASDevicesStateSuite) SetUpTest(c *gc.C) {
s.series = "kubernetes"
s.DevicesStateSuiteBase.SetUpTest(c)
}
var _ = gc.Suite(&CAASDevicesStateSuite{})
func (s *CAASDevicesStateSuite) TestAddApplicationDevicesConstraintsValidation(c *gc.C) {
ch := s.AddTestingCharm(c, "bitcoin-miner")
addApplication := func(devices map[string]state.DeviceConstraints) (*state.Application, error) {
return s.st.AddApplication(state.AddApplicationArgs{Name: "bitcoin-miner", Charm: ch, Devices: devices, Series: s.series})
}
assertErr := func(devices map[string]state.DeviceConstraints, expect string) {
_, err := addApplication(devices)
c.Assert(err, gc.ErrorMatches, expect)
}
deviceCons := map[string]state.DeviceConstraints{
"bitcoinminer-incorrect-name": makeDeviceCons("nvidia.com/gpu", 2),
}
assertErr(deviceCons, `cannot add application "bitcoin-miner": charm "bitcoin-miner" has no device called "bitcoinminer-incorrect-name"`)
deviceCons = map[string]state.DeviceConstraints{
"bitcoinminer": makeDeviceCons("nvidia.com/gpu", 0),
}
assertErr(deviceCons, `cannot add application "bitcoin-miner": charm "bitcoin-miner" device "bitcoinminer": minimum device size is 1, 0 specified`)
deviceCons["bitcoinminer"] = makeDeviceCons("nvidia.com/gpu", 2)
app, err := addApplication(deviceCons)
c.Assert(err, jc.ErrorIsNil)
var devs map[string]state.DeviceConstraints
devs, err = app.DeviceConstraints()
c.Assert(err, jc.ErrorIsNil)
c.Assert(devs, jc.DeepEquals, map[string]state.DeviceConstraints{
"bitcoinminer": {
Type: "nvidia.com/gpu",
Count: 2,
Attributes: map[string]string{},
},
})
}