~ubuntu-branches/ubuntu/saucy/juju-core/saucy-proposed

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/environs/ec2/instancetype_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-07-11 17:18:27 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130711171827-vjqkg40r0dlf7ys2
Tags: 1.11.2-0ubuntu1
* New upstream release.
* Make juju-core the default juju (LP: #1190634):
  - d/control: Add virtual package juju -> juju-core.
  - d/juju-core.postinst.in: Bump priority of alternatives over that of
    python juju packages.
* Enable for all architectures (LP: #1172505):
  - d/control: Version BD on golang-go to >= 2:1.1.1 to ensure CGO
    support for non-x86 archs, make juju-core Arch: any.
  - d/README.source: Dropped - no longer required.
* d/watch: Updated for new upstream tarball naming.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package ec2
2
 
 
3
 
import (
4
 
        . "launchpad.net/gocheck"
5
 
        "launchpad.net/juju-core/constraints"
6
 
        "launchpad.net/juju-core/testing"
7
 
)
8
 
 
9
 
type instanceTypeSuite struct {
10
 
        testing.LoggingSuite
11
 
}
12
 
 
13
 
var _ = Suite(&instanceTypeSuite{})
14
 
 
15
 
func (s *instanceTypeSuite) SetUpSuite(c *C) {
16
 
        s.LoggingSuite.SetUpSuite(c)
17
 
        UseTestInstanceTypeData(instanceTypeData)
18
 
}
19
 
 
20
 
func (s *instanceTypeSuite) TearDownSuite(c *C) {
21
 
        UseTestInstanceTypeData(nil)
22
 
        s.LoggingSuite.TearDownTest(c)
23
 
}
24
 
 
25
 
var instanceTypeData = map[string]uint64{
26
 
        "m1.small":    60,
27
 
        "m1.medium":   120,
28
 
        "m1.large":    240,
29
 
        "m1.xlarge":   480,
30
 
        "t1.micro":    20,
31
 
        "c1.medium":   145,
32
 
        "c1.xlarge":   580,
33
 
        "cc1.4xlarge": 1300,
34
 
        "cc2.8xlarge": 2400,
35
 
}
36
 
 
37
 
var getInstanceTypesTest = []struct {
38
 
        info   string
39
 
        cons   string
40
 
        itypes []string
41
 
        arches []string
42
 
}{
43
 
        {
44
 
                info: "cpu-cores",
45
 
                cons: "cpu-cores=2",
46
 
                itypes: []string{
47
 
                        "c1.medium", "m1.large", "m1.xlarge", "c1.xlarge", "cc1.4xlarge",
48
 
                        "cc2.8xlarge",
49
 
                },
50
 
        }, {
51
 
                info:   "cpu-power",
52
 
                cons:   "cpu-power=2000",
53
 
                itypes: []string{"c1.xlarge", "cc1.4xlarge", "cc2.8xlarge"},
54
 
        }, {
55
 
                info: "mem",
56
 
                cons: "mem=4G",
57
 
                itypes: []string{
58
 
                        "m1.large", "m1.xlarge", "c1.xlarge", "cc1.4xlarge", "cc2.8xlarge",
59
 
                },
60
 
        }, {
61
 
                info:   "arches filtered by constraint",
62
 
                cons:   "arch=i386",
63
 
                itypes: []string{"m1.small", "m1.medium", "c1.medium"},
64
 
                arches: []string{"i386"},
65
 
        }, {
66
 
                info: "t1.micro filtered out when no cpu-power set",
67
 
                itypes: []string{
68
 
                        "m1.small", "m1.medium", "c1.medium", "m1.large", "m1.xlarge",
69
 
                        "c1.xlarge", "cc1.4xlarge", "cc2.8xlarge",
70
 
                },
71
 
        }, {
72
 
                info: "t1.micro included when small cpu-power set",
73
 
                cons: "cpu-power=1",
74
 
                itypes: []string{
75
 
                        "t1.micro", "m1.small", "m1.medium", "c1.medium", "m1.large",
76
 
                        "m1.xlarge", "c1.xlarge", "cc1.4xlarge", "cc2.8xlarge",
77
 
                },
78
 
        }, {
79
 
                info:   "t1.micro included when small cpu-power set 2",
80
 
                cons:   "cpu-power=1 arch=i386",
81
 
                itypes: []string{"t1.micro", "m1.small", "m1.medium", "c1.medium"},
82
 
                arches: []string{"i386"},
83
 
        },
84
 
}
85
 
 
86
 
func (s *instanceTypeSuite) TestGetInstanceTypes(c *C) {
87
 
        for i, t := range getInstanceTypesTest {
88
 
                c.Logf("test %d: %s", i, t.info)
89
 
                itypes, err := getInstanceTypes("test", constraints.MustParse(t.cons))
90
 
                c.Assert(err, IsNil)
91
 
                names := make([]string, len(itypes))
92
 
                for i, itype := range itypes {
93
 
                        if len(t.arches) > 0 {
94
 
                                c.Check(itype.arches, DeepEquals, filterArches(itype.arches, t.arches))
95
 
                        } else {
96
 
                                c.Check(len(itype.arches) > 0, Equals, true)
97
 
                        }
98
 
                        names[i] = itype.name
99
 
                }
100
 
                c.Check(names, DeepEquals, t.itypes)
101
 
        }
102
 
}
103
 
 
104
 
func (s *instanceTypeSuite) TestGetInstanceTypesErrors(c *C) {
105
 
        _, err := getInstanceTypes("unknown-region", constraints.Value{})
106
 
        c.Check(err, ErrorMatches, `no instance types found in unknown-region`)
107
 
 
108
 
        cons := constraints.MustParse("cpu-power=9001")
109
 
        _, err = getInstanceTypes("test", cons)
110
 
        c.Check(err, ErrorMatches, `no instance types in test matching constraints "cpu-power=9001"`)
111
 
 
112
 
        cons = constraints.MustParse("arch=i386 mem=8G")
113
 
        _, err = getInstanceTypes("test", cons)
114
 
        c.Check(err, ErrorMatches, `no instance types in test matching constraints "arch=i386 cpu-power=100 mem=8192M"`)
115
 
}
116
 
 
117
 
var instanceTypeMatchTests = []struct {
118
 
        cons   string
119
 
        itype  string
120
 
        arches []string
121
 
}{
122
 
        {"", "m1.small", both},
123
 
        {"", "m1.large", amd64},
124
 
        {"cpu-power=100", "m1.small", both},
125
 
        {"arch=amd64", "m1.small", amd64},
126
 
        {"cpu-cores=3", "m1.xlarge", amd64},
127
 
        {"cpu-power=", "t1.micro", both},
128
 
        {"cpu-power=500", "c1.medium", both},
129
 
        {"cpu-power=2000", "c1.xlarge", amd64},
130
 
        {"cpu-power=2001", "cc1.4xlarge", amd64},
131
 
        {"mem=2G", "m1.medium", both},
132
 
 
133
 
        {"arch=arm", "m1.small", nil},
134
 
        {"cpu-power=100", "t1.micro", nil},
135
 
        {"cpu-power=9001", "cc2.8xlarge", nil},
136
 
        {"mem=1G", "t1.micro", nil},
137
 
        {"arch=i386", "c1.xlarge", nil},
138
 
}
139
 
 
140
 
func (s *instanceTypeSuite) TestMatch(c *C) {
141
 
        for i, t := range instanceTypeMatchTests {
142
 
                c.Logf("test %d", i)
143
 
                cons := constraints.MustParse(t.cons)
144
 
                var itype instanceType
145
 
                for _, itype = range allInstanceTypes {
146
 
                        if itype.name == t.itype {
147
 
                                break
148
 
                        }
149
 
                }
150
 
                c.Assert(itype.name, Not(Equals), "")
151
 
                itype, match := itype.match(cons)
152
 
                if len(t.arches) > 0 {
153
 
                        c.Check(match, Equals, true)
154
 
                        expect := itype
155
 
                        expect.arches = t.arches
156
 
                        c.Check(itype, DeepEquals, expect)
157
 
                } else {
158
 
                        c.Check(match, Equals, false)
159
 
                        c.Check(itype, DeepEquals, instanceType{})
160
 
                }
161
 
        }
162
 
}