77import uuid
88import json
99import tofu_test_data
10+ import time
1011
1112coral_uri = os .environ .get ("TF_VAR_coral_uri" )
1213headers = {"Authorization" : "Bearer " + os .environ .get ("TF_VAR_auth_token" )}
1314
1415
1516initial_time = datetime .now ()
16- def time_with_month_offset (offset ):
17- return (datetime .now () + relativedelta (months = offset )).strftime ("%Y-%m-%d-%H:%M:%S" )
17+ def time_with_days_offset (offset ):
18+ return (datetime .now () + relativedelta (days = offset )).strftime ("%Y-%m-%d-%H:%M:%S" )
1819
1920q1_standard_resources = {
2021 "VCPU" : "40000" ,
@@ -34,10 +35,10 @@ def time_with_month_offset(offset):
3435 "DISK_GB" : "10"
3536}
3637
37- q1_st = time_with_month_offset ( - 2 )
38- q1_end = time_with_month_offset ( 1 )
39- q2_st = time_with_month_offset ( 12 )
40- q2_end = time_with_month_offset ( 15 )
38+ q1_st = time_with_days_offset ( - 60 )
39+ q1_end = time_with_days_offset ( 30 )
40+ q2_st = time_with_days_offset ( 360 )
41+ q2_end = time_with_days_offset ( 450 )
4142
4243standard_test_data = tofu_test_data .get_standard_test_vars (
4344 q1_st ,
@@ -55,25 +56,32 @@ def time_with_month_offset(offset):
5556 q1_extra_resources
5657 )
5758
59+ insufficient_creds_data = tofu_test_data .get_standard_test_vars (
60+ q1_st ,
61+ q1_end ,
62+ q2_st ,
63+ q2_end ,
64+ q1_insufficient_resources
65+ )
66+
5867empty_test_data = tofu_test_data .get_empty_test_data_copy (standard_test_data )
5968try_active_delete_test_data = tofu_test_data .get_no_q1_copy (standard_test_data )
6069
6170lease_request_json = tofu_test_data .get_lease_request_json (
62- time_with_month_offset ( - 1 ),
63- ( initial_time + relativedelta ( days = 1 )). strftime ( "%Y-%m-%d-%H:%M:%S" )
71+ time_with_days_offset ( - 30 ),
72+ time_with_days_offset ( 1 )
6473 )
6574
6675@pytest .fixture (scope = "session" )
6776def terraform_rest_setup ():
6877 working_dir = os .path .join (os .path .dirname (__file__ ), ".." )
69- var_file = os .path .join (working_dir , "tests" , "tofu_configs" , "initial.tfvars" )
70- delete_file = os .path .join (working_dir , "tests" , "tofu_configs" , "empty.tfvars" )
7178
7279 tf = Tofu (cwd = working_dir )
7380 tf .init ()
7481 tf .apply (variables = standard_test_data )
7582
76- yield tf
83+ resources = get_current_allocation_resources ()
84+ yield dict (tf_workspace = tf , resources = resources )
7785
7886 destroy = tf .apply (variables = empty_test_data )
7987 assert len (destroy .errors ) == 0
@@ -91,24 +99,37 @@ def add_consumer_request(terraform_rest_setup):
9199 },
92100 json = lease_request_json ,
93101 )
94- return dict (status = consumer .status_code , tf_workspace = terraform_rest_setup )
102+ resources = get_current_allocation_resources ()
103+ return dict (status = consumer .status_code , tf_workspace = terraform_rest_setup ["tf_workspace" ],resources = resources )
95104
96105@pytest .fixture (scope = "session" )
97106def update_allocation_resources (add_consumer_request ):
98107 workspace = add_consumer_request ["tf_workspace" ]
99108 workspace .apply (variables = updated_test_data )
100- return dict (tf_workspace = add_consumer_request ["tf_workspace" ])
109+ resources = get_current_allocation_resources ()
110+ return dict (tf_workspace = workspace ,resources = resources )
111+
112+ @pytest .fixture (scope = "session" )
113+ def try_update_with_insufficient_creds (update_allocation_resources ):
114+ workspace = update_allocation_resources ["tf_workspace" ]
115+ print ("Starting update with insufficient creds, will see errors" )
116+ apply = workspace .apply (variables = insufficient_creds_data )
117+ print ("Insufficient credits test end" )
118+ resources = get_current_allocation_resources ()
119+ return dict (tf_workspace = update_allocation_resources ["tf_workspace" ],error_count = len (apply .errors ),resources = resources )
101120
102121@pytest .fixture (scope = "session" )
103- def try_delete_active_allocation (update_allocation_resources ):
122+ def try_delete_active_allocation (try_update_with_insufficient_creds ):
104123 print ("Testing deleting active allocation, will see 403 errors" )
105- try_delete = update_allocation_resources ["tf_workspace" ].apply (
124+ try_delete = try_update_with_insufficient_creds ["tf_workspace" ].apply (
106125 variables = try_active_delete_test_data
107126 )
108127 print ("End of active allocation delete test" )
128+ resources = get_current_allocation_resources ()
109129 return dict (
110130 error_count = len (try_delete .errors ),
111- tf_workspace = update_allocation_resources ["tf_workspace" ],
131+ tf_workspace = try_update_with_insufficient_creds ["tf_workspace" ],
132+ resources = resources
112133 )
113134
114135
@@ -138,7 +159,8 @@ def delete_consumer(try_destroy_with_active_consumers):
138159 },
139160 json = lease_request_json ,
140161 )
141- return
162+ resources = get_current_allocation_resources ()
163+ return dict (resources = resources )
142164
143165
144166def api_get_request (resource ):
@@ -227,6 +249,18 @@ def test_only_allocation_resources_returned(terraform_rest_setup):
227249 assert len (api_get_request ("allocation/" + str (allocation_id ) + "/resources" )) == 3
228250
229251
252+ def test_insufficient_creds_update (try_update_with_insufficient_creds ):
253+ assert try_update_with_insufficient_creds ["error_count" ] > 1
254+
255+ def get_current_allocation_resources ():
256+ allocations = api_get_request ("allocation" )
257+ return {
258+ a ["name" ]: to_resource_map (
259+ api_get_request ("allocation/" + str (a ["id" ]) + "/resources" )
260+ )
261+ for a in allocations
262+ }
263+
230264@pytest .mark .parametrize (
231265 "fixture_name, expected_resources" ,
232266 [
@@ -257,27 +291,30 @@ def test_only_allocation_resources_returned(terraform_rest_setup):
257291 },
258292 ),
259293 (
260- "delete_consumer " ,
294+ "try_update_with_insufficient_creds " ,
261295 {
262- # historical consumer consumption data should be preserved
296+ # shouldn't have modified allocated credits
263297 "Q1-0" : {"VCPU" : 38024 , "MEMORY_MB" : 3680680 , "DISK_GB" : 82960 },
264298 "Q1-1" : {"VCPU" : 20000 , "MEMORY_MB" : 2000000 , "DISK_GB" : 200000 },
265299 "Q2-0" : {"VCPU" : 80000 , "MEMORY_MB" : 8000000 , "DISK_GB" : 300000 },
266300 },
267301 ),
302+ (
303+ "delete_consumer" ,
304+ {
305+ # historical consumption data + unused extra day's worth of resources
306+ "Q1-0" : {"VCPU" : 38120 , "MEMORY_MB" : 3704680 , "DISK_GB" : 83800 },
307+ "Q1-1" : {"VCPU" : 20000 , "MEMORY_MB" : 2000000 , "DISK_GB" : 200000 },
308+ "Q2-0" : {"VCPU" : 80000 , "MEMORY_MB" : 8000000 , "DISK_GB" : 300000 },
309+ },
310+ ),
268311 ],
269312)
270313def test_resource_allocations_have_correct_resources (
271314 request , fixture_name , expected_resources
272315):
273- request .getfixturevalue (fixture_name ) # needed to dynamically set fixtures
274- allocations = api_get_request ("allocation" )
275- allocation_resources = {
276- a ["name" ]: to_resource_map (
277- api_get_request ("allocation/" + str (a ["id" ]) + "/resources" )
278- )
279- for a in allocations
280- }
316+ allocation_resources = request .getfixturevalue (fixture_name )["resources" ]
317+
281318 assert allocation_resources ["Q1-0" ] == expected_resources ["Q1-0" ]
282319 assert allocation_resources ["Q1-1" ] == expected_resources ["Q1-1" ]
283320 assert allocation_resources ["Q2-0" ] == expected_resources ["Q2-0" ]
0 commit comments