16
16
17
17
import os
18
18
import time
19
-
20
- import random
19
+ import uuid
21
20
22
21
import pytest
23
22
from google .cloud import bigtable
24
23
from google .cloud .bigtable import enums
25
24
from mock import patch
26
25
27
26
from metricscaler import get_cpu_load
27
+ from metricscaler import get_storage_utilization
28
28
from metricscaler import main
29
29
from metricscaler import scale_bigtable
30
30
31
31
PROJECT = os .environ ['GCLOUD_PROJECT' ]
32
32
BIGTABLE_ZONE = os .environ ['BIGTABLE_ZONE' ]
33
33
SIZE_CHANGE_STEP = 3
34
34
INSTANCE_ID_FORMAT = 'metric-scale-test-{}'
35
- INSTANCE_ID_RANGE = 10000
36
- BIGTABLE_INSTANCE = INSTANCE_ID_FORMAT .format (
37
- random .randrange (INSTANCE_ID_RANGE ))
35
+ BIGTABLE_INSTANCE = INSTANCE_ID_FORMAT .format (str (uuid .uuid4 ())[:10 ])
36
+ BIGTABLE_DEV_INSTANCE = INSTANCE_ID_FORMAT .format (str (uuid .uuid4 ())[:10 ])
38
37
39
38
40
39
# System tests to verify API calls succeed
@@ -44,6 +43,10 @@ def test_get_cpu_load():
44
43
assert float (get_cpu_load ()) > 0.0
45
44
46
45
46
+ def test_get_storage_utilization ():
47
+ assert float (get_storage_utilization ()) > 0.0
48
+
49
+
47
50
@pytest .fixture ()
48
51
def instance ():
49
52
cluster_id = BIGTABLE_INSTANCE
@@ -68,6 +71,29 @@ def instance():
68
71
instance .delete ()
69
72
70
73
74
+ @pytest .fixture ()
75
+ def dev_instance ():
76
+ cluster_id = BIGTABLE_DEV_INSTANCE
77
+
78
+ client = bigtable .Client (project = PROJECT , admin = True )
79
+
80
+ storage_type = enums .StorageType .SSD
81
+ development = enums .Instance .Type .DEVELOPMENT
82
+ labels = {'dev-label' : 'dev-label' }
83
+ instance = client .instance (BIGTABLE_DEV_INSTANCE ,
84
+ instance_type = development ,
85
+ labels = labels )
86
+
87
+ if not instance .exists ():
88
+ cluster = instance .cluster (cluster_id , location_id = BIGTABLE_ZONE ,
89
+ default_storage_type = storage_type )
90
+ instance .create (clusters = [cluster ])
91
+
92
+ yield
93
+
94
+ instance .delete ()
95
+
96
+
71
97
def test_scale_bigtable (instance ):
72
98
bigtable_client = bigtable .Client (admin = True )
73
99
@@ -103,31 +129,70 @@ def test_scale_bigtable(instance):
103
129
raise
104
130
105
131
106
- # Unit test for logic
132
+ def test_handle_dev_instance (capsys , dev_instance ):
133
+ with pytest .raises (ValueError ):
134
+ scale_bigtable (BIGTABLE_DEV_INSTANCE , BIGTABLE_DEV_INSTANCE , True )
135
+
136
+
107
137
@patch ('time.sleep' )
138
+ @patch ('metricscaler.get_storage_utilization' )
108
139
@patch ('metricscaler.get_cpu_load' )
109
140
@patch ('metricscaler.scale_bigtable' )
110
- def test_main (scale_bigtable , get_cpu_load , sleep ):
141
+ def test_main (scale_bigtable , get_cpu_load , get_storage_utilization , sleep ):
111
142
SHORT_SLEEP = 5
112
143
LONG_SLEEP = 10
144
+
145
+ # Test okay CPU, okay storage utilization
113
146
get_cpu_load .return_value = 0.5
147
+ get_storage_utilization .return_value = 0.5
114
148
115
- main (BIGTABLE_INSTANCE , BIGTABLE_INSTANCE , 0.6 , 0.3 , SHORT_SLEEP ,
149
+ main (BIGTABLE_INSTANCE , BIGTABLE_INSTANCE , 0.6 , 0.3 , 0.6 , SHORT_SLEEP ,
116
150
LONG_SLEEP )
117
151
scale_bigtable .assert_not_called ()
118
152
scale_bigtable .reset_mock ()
119
153
154
+ # Test high CPU, okay storage utilization
120
155
get_cpu_load .return_value = 0.7
121
- main (BIGTABLE_INSTANCE , BIGTABLE_INSTANCE , 0.6 , 0.3 , SHORT_SLEEP ,
156
+ get_storage_utilization .return_value = 0.5
157
+ main (BIGTABLE_INSTANCE , BIGTABLE_INSTANCE , 0.6 , 0.3 , 0.6 , SHORT_SLEEP ,
122
158
LONG_SLEEP )
123
159
scale_bigtable .assert_called_once_with (BIGTABLE_INSTANCE ,
124
160
BIGTABLE_INSTANCE , True )
125
161
scale_bigtable .reset_mock ()
126
162
163
+ # Test low CPU, okay storage utilization
164
+ get_storage_utilization .return_value = 0.5
127
165
get_cpu_load .return_value = 0.2
128
- main (BIGTABLE_INSTANCE , BIGTABLE_INSTANCE , 0.6 , 0.3 , SHORT_SLEEP ,
166
+ main (BIGTABLE_INSTANCE , BIGTABLE_INSTANCE , 0.6 , 0.3 , 0.6 , SHORT_SLEEP ,
129
167
LONG_SLEEP )
130
168
scale_bigtable .assert_called_once_with (BIGTABLE_INSTANCE ,
131
169
BIGTABLE_INSTANCE , False )
170
+ scale_bigtable .reset_mock ()
171
+
172
+ # Test okay CPU, high storage utilization
173
+ get_cpu_load .return_value = 0.5
174
+ get_storage_utilization .return_value = 0.7
175
+
176
+ main (BIGTABLE_INSTANCE , BIGTABLE_INSTANCE , 0.6 , 0.3 , 0.6 , SHORT_SLEEP ,
177
+ LONG_SLEEP )
178
+ scale_bigtable .assert_called_once_with (BIGTABLE_INSTANCE ,
179
+ BIGTABLE_INSTANCE , True )
180
+ scale_bigtable .reset_mock ()
132
181
182
+ # Test high CPU, high storage utilization
183
+ get_cpu_load .return_value = 0.7
184
+ get_storage_utilization .return_value = 0.7
185
+ main (BIGTABLE_INSTANCE , BIGTABLE_INSTANCE , 0.6 , 0.3 , 0.6 , SHORT_SLEEP ,
186
+ LONG_SLEEP )
187
+ scale_bigtable .assert_called_once_with (BIGTABLE_INSTANCE ,
188
+ BIGTABLE_INSTANCE , True )
189
+ scale_bigtable .reset_mock ()
190
+
191
+ # Test low CPU, high storage utilization
192
+ get_cpu_load .return_value = 0.2
193
+ get_storage_utilization .return_value = 0.7
194
+ main (BIGTABLE_INSTANCE , BIGTABLE_INSTANCE , 0.6 , 0.3 , 0.6 , SHORT_SLEEP ,
195
+ LONG_SLEEP )
196
+ scale_bigtable .assert_called_once_with (BIGTABLE_INSTANCE ,
197
+ BIGTABLE_INSTANCE , True )
133
198
scale_bigtable .reset_mock ()
0 commit comments