-
Notifications
You must be signed in to change notification settings - Fork 527
/
Copy pathcustom_virtual_machine_spec_test.py
181 lines (151 loc) · 5.63 KB
/
custom_virtual_machine_spec_test.py
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
# Copyright 2017 PerfKitBenchmarker Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for perfkitbenchmarker.custom_virtual_machine_spec."""
import unittest
from perfkitbenchmarker import custom_virtual_machine_spec
from perfkitbenchmarker import errors
_COMPONENT = 'test_component'
_FLAGS = None
_STRING_TYPE_NAME = (str,)[0].__name__
class MemoryDecoderTestCase(unittest.TestCase):
def setUp(self):
super().setUp()
self.decoder = custom_virtual_machine_spec.MemoryDecoder(option='memory')
def testValidStrings(self):
self.assertEqual(self.decoder.Decode('1280MiB', _COMPONENT, _FLAGS), 1280)
self.assertEqual(self.decoder.Decode('7.5GiB', _COMPONENT, _FLAGS), 7680)
def testImproperPattern(self):
with self.assertRaises(errors.Config.InvalidValue) as cm:
self.decoder.Decode('1280', _COMPONENT, _FLAGS)
self.assertEqual(
str(cm.exception),
(
'Invalid test_component.memory value: "1280". Examples of valid '
'values: "1280MiB", "7.5GiB".'
),
)
def testInvalidFloat(self):
with self.assertRaises(errors.Config.InvalidValue) as cm:
self.decoder.Decode('1280.9.8MiB', _COMPONENT, _FLAGS)
self.assertEqual(
str(cm.exception),
(
'Invalid test_component.memory value: "1280.9.8MiB". "1280.9.8" is'
' not a valid float.'
),
)
def testNonIntegerMiB(self):
with self.assertRaises(errors.Config.InvalidValue) as cm:
self.decoder.Decode('7.6GiB', _COMPONENT, _FLAGS)
self.assertEqual(
str(cm.exception),
(
'Invalid test_component.memory value: "7.6GiB". The specified size '
'must be an integer number of MiB.'
),
)
class CustomMachineTypeSpecTestCase(unittest.TestCase):
def testValid(self):
result = custom_virtual_machine_spec.CustomMachineTypeSpec(
_COMPONENT, cpus=1, memory='7.5GiB'
)
self.assertEqual(result.cpus, 1)
self.assertEqual(result.memory, 7680)
def testMissingCpus(self):
with self.assertRaises(errors.Config.MissingOption) as cm:
custom_virtual_machine_spec.CustomMachineTypeSpec(
_COMPONENT, memory='7.5GiB'
)
self.assertEqual(
str(cm.exception),
'Required options were missing from test_component: cpus.',
)
def testMissingMemory(self):
with self.assertRaises(errors.Config.MissingOption) as cm:
custom_virtual_machine_spec.CustomMachineTypeSpec(_COMPONENT, cpus=1)
self.assertEqual(
str(cm.exception),
'Required options were missing from test_component: memory.',
)
def testExtraOptions(self):
with self.assertRaises(errors.Config.UnrecognizedOption) as cm:
custom_virtual_machine_spec.CustomMachineTypeSpec(
_COMPONENT, cpus=1, memory='7.5GiB', extra1='one', extra2=2
)
self.assertEqual(
str(cm.exception),
'Unrecognized options were found in test_component: extra1, extra2.'
)
def testInvalidCpus(self):
with self.assertRaises(errors.Config.InvalidValue) as cm:
custom_virtual_machine_spec.CustomMachineTypeSpec(
_COMPONENT, cpus=0, memory='7.5GiB'
)
self.assertEqual(
str(cm.exception),
'Invalid test_component.cpus value: "0". Value must be at least 1.',
)
def testInvalidMemory(self):
with self.assertRaises(errors.Config.InvalidValue) as cm:
custom_virtual_machine_spec.CustomMachineTypeSpec(
_COMPONENT, cpus=1, memory=None
)
self.assertEqual(
str(cm.exception),
(
'Invalid test_component.memory value: "None" (of type "NoneType"). '
'Value must be one of the following types: %s.' % _STRING_TYPE_NAME
),
)
class MachineTypeDecoderTestCase(unittest.TestCase):
def setUp(self):
super().setUp()
self.decoder = custom_virtual_machine_spec.MachineTypeDecoder(
option='machine_type'
)
def testDecodeString(self):
result = self.decoder.Decode('n1-standard-8', _COMPONENT, {})
self.assertEqual(result, 'n1-standard-8')
def testDecodeCustomVm(self):
result = self.decoder.Decode(
{'cpus': 1, 'memory': '7.5GiB'}, _COMPONENT, {}
)
self.assertIsInstance(
result, custom_virtual_machine_spec.CustomMachineTypeSpec
)
self.assertEqual(result.cpus, 1)
self.assertEqual(result.memory, 7680)
def testDecodeInvalidType(self):
with self.assertRaises(errors.Config.InvalidValue) as cm:
self.decoder.Decode(None, _COMPONENT, {})
self.assertEqual(
str(cm.exception),
(
'Invalid test_component.machine_type value: "None" (of type '
'"NoneType"). Value must be one of the following types: %s, '
'dict.' % _STRING_TYPE_NAME
),
)
def testDecodeInvalidValue(self):
with self.assertRaises(errors.Config.InvalidValue) as cm:
self.decoder.Decode({'cpus': 0, 'memory': '7.5GiB'}, _COMPONENT, {})
self.assertEqual(
str(cm.exception),
(
'Invalid test_component.machine_type.cpus value: "0". Value must be'
' at least 1.'
),
)
if __name__ == '__main__':
unittest.main()