66from rest_framework .authtoken .models import Token
77
88from api .serializers import MEMLIMIT_MATCH
9+ from api .serializers import CPUSHARE_MATCH
910from api .tests import adapter , DeisTransactionTestCase
1011
1112
@@ -26,12 +27,31 @@ def tearDown(self):
2627
2728 def test_memlimit_regex (self , mock_requests ):
2829 """Tests the regex for unit format used by "deis limits:set --memory=<limit>"."""
30+ self .assertTrue (MEMLIMIT_MATCH .match ("0/100MB" ))
31+ self .assertTrue (MEMLIMIT_MATCH .match ("200GB/100MB" ))
2932 self .assertTrue (MEMLIMIT_MATCH .match ("20MB" ))
30- self .assertFalse (MEMLIMIT_MATCH .match ("20MK" ))
3133 self .assertTrue (MEMLIMIT_MATCH .match ("20gb" ))
34+ self .assertTrue (MEMLIMIT_MATCH .match ("0m" ))
35+ self .assertFalse (MEMLIMIT_MATCH .match ("20MK" ))
36+ self .assertFalse (MEMLIMIT_MATCH .match ("10" ))
3237 self .assertFalse (MEMLIMIT_MATCH .match ("20gK" ))
38+ self .assertFalse (MEMLIMIT_MATCH .match ("mb" ))
3339
34- def test_limit_memory (self , mock_requests ):
40+ def test_cpushare_regex (self , mock_requests ):
41+ """Tests the regex for unit format used by "deis limits:set --cpu=<limit>"."""
42+ self .assertTrue (CPUSHARE_MATCH .match ("0/2" ))
43+ self .assertTrue (CPUSHARE_MATCH .match ("500m/600m" ))
44+ self .assertTrue (CPUSHARE_MATCH .match ("0.5" ))
45+ self .assertTrue (CPUSHARE_MATCH .match (".123" ))
46+ self .assertTrue (CPUSHARE_MATCH .match ("1.123" ))
47+ self .assertTrue (CPUSHARE_MATCH .match ("200m" ))
48+ self .assertTrue (CPUSHARE_MATCH .match ("0" ))
49+ self .assertFalse (CPUSHARE_MATCH .match ("20MK" ))
50+ self .assertFalse (CPUSHARE_MATCH .match ("20gK" ))
51+ self .assertFalse (CPUSHARE_MATCH .match ("m" ))
52+ self .assertFalse (CPUSHARE_MATCH .match ("." ))
53+
54+ def test_request_limit_memory (self , mock_requests ):
3555 """
3656 Test that limit is auto-created for a new app and that
3757 limits can be updated using a PATCH
@@ -101,6 +121,38 @@ def test_limit_memory(self, mock_requests):
101121 self .assertIn ('web' , memory )
102122 self .assertEqual (memory ['web' ], '1G' )
103123
124+ # add with requests/limits
125+ body = {'memory' : json .dumps ({'db' : '1G/2G' })}
126+ response = self .client .post (url , body )
127+ self .assertEqual (response .status_code , 201 , response .data )
128+
129+ # read the limit again
130+ response = self .client .get (url )
131+ self .assertEqual (response .status_code , 200 , response .data )
132+ memory = response .data ['memory' ]
133+ self .assertIn ('worker' , memory )
134+ self .assertEqual (memory ['worker' ], '512M' )
135+ self .assertIn ('web' , memory )
136+ self .assertEqual (memory ['web' ], '1G' )
137+ self .assertIn ('db' , memory )
138+ self .assertEqual (memory ['db' ], '1G/2G' )
139+
140+ # replace one with requests/limits
141+ body = {'memory' : json .dumps ({'web' : '3G/4G' })}
142+ response = self .client .post (url , body )
143+ self .assertEqual (response .status_code , 201 , response .data )
144+
145+ # read the limit again
146+ response = self .client .get (url )
147+ self .assertEqual (response .status_code , 200 , response .data )
148+ memory = response .data ['memory' ]
149+ self .assertIn ('worker' , memory )
150+ self .assertEqual (memory ['worker' ], '512M' )
151+ self .assertIn ('web' , memory )
152+ self .assertEqual (memory ['web' ], '3G/4G' )
153+ self .assertIn ('db' , memory )
154+ self .assertEqual (memory ['db' ], '1G/2G' )
155+
104156 # unset a value
105157 body = {'memory' : json .dumps ({'worker' : None })}
106158 response = self .client .post (url , body )
@@ -129,9 +181,9 @@ def test_limit_memory(self, mock_requests):
129181 self .assertEqual (response .status_code , 405 , response .data )
130182 return limit4
131183
132- def test_limit_cpu (self , mock_requests ):
184+ def test_request_limit_cpu (self , mock_requests ):
133185 """
134- Test that CPU limits can be set
186+ Test that CPU requests/ limits can be set
135187 """
136188 app_id = self .create_app ()
137189 url = '/v2/apps/{app_id}/config' .format (** locals ())
@@ -150,7 +202,7 @@ def test_limit_cpu(self, mock_requests):
150202 self .assertEqual (response .status_code , 201 , response .data )
151203 limit1 = response .data
152204
153- # check memory limits
205+ # check cpu limits
154206 response = self .client .get (url )
155207 self .assertEqual (response .status_code , 200 , response .data )
156208 self .assertIn ('cpu' , response .data )
@@ -181,6 +233,38 @@ def test_limit_cpu(self, mock_requests):
181233 self .assertIn ('web' , cpu )
182234 self .assertEqual (cpu ['web' ], '1024' )
183235
236+ # add with requests/limits
237+ body = {'cpu' : json .dumps ({'db' : '1/2' })}
238+ response = self .client .post (url , body )
239+ self .assertEqual (response .status_code , 201 , response .data )
240+
241+ # read the limit again
242+ response = self .client .get (url )
243+ self .assertEqual (response .status_code , 200 , response .data )
244+ cpu = response .data ['cpu' ]
245+ self .assertIn ('worker' , cpu )
246+ self .assertEqual (cpu ['worker' ], '512m' )
247+ self .assertIn ('web' , cpu )
248+ self .assertEqual (cpu ['web' ], '1024' )
249+ self .assertIn ('db' , cpu )
250+ self .assertEqual (cpu ['db' ], '1/2' )
251+
252+ # replace one with requests/limits
253+ body = {'cpu' : json .dumps ({'web' : '300m/4000m' })}
254+ response = self .client .post (url , body )
255+ self .assertEqual (response .status_code , 201 , response .data )
256+
257+ # read the limit again
258+ response = self .client .get (url )
259+ self .assertEqual (response .status_code , 200 , response .data )
260+ cpu = response .data ['cpu' ]
261+ self .assertIn ('worker' , cpu )
262+ self .assertEqual (cpu ['worker' ], '512m' )
263+ self .assertIn ('web' , cpu )
264+ self .assertEqual (cpu ['web' ], '300m/4000m' )
265+ self .assertIn ('db' , cpu )
266+ self .assertEqual (cpu ['db' ], '1/2' )
267+
184268 # unset a value
185269 body = {'cpu' : json .dumps ({'worker' : None })}
186270 response = self .client .post (url , body )
0 commit comments