55from datetime import datetime
66from dateutil .relativedelta import relativedelta
77import uuid
8+ import json
9+ import tofu_test_data
810
911coral_uri = os .environ .get ("TF_VAR_coral_uri" )
1012headers = {"Authorization" : "Bearer " + os .environ .get ("TF_VAR_auth_token" )}
1113
1214
13- def get_lease_request_json ():
14- start_time = datetime .now ().strftime ("%Y-%m-%d-%H:%M:%S" )
15- end_time = (datetime .now () + relativedelta (months = 1 )).strftime ("%Y-%m-%d-%H:%M:%S" )
16- return {
17- "context" : {
18- "user_id" : "caa8b54a-eb5e-4134-8ae2-a3946a428ec7" ,
19- "project_id" : "c2eced313b324cdb8e670e6e30bf387d" ,
20- "auth_url" : "http://api.example.com:5000/v3" ,
21- "region_name" : "RegionOne" ,
22- },
23- "lease" : {
24- "id" : str (uuid .uuid4 ()),
25- "name" : "my_new_lease" ,
26- "start_date" : start_time ,
27- "end_date" : end_time ,
28- "reservations" : [
29- {
30- "amount" : 2 ,
31- "flavor_id" : "e26a4241-b83d-4516-8e0e-8ce2665d1966" ,
32- "resource_type" : "flavor:instance" ,
33- "affinity" : None ,
34- "allocations" : [],
35- }
36- ],
37- "resource_requests" : {"DISK_GB" : 35 , "MEMORY_MB" : 1000 , "VCPU" : 4 },
38- },
39- }
15+ initial_time = datetime .now ()
16+ def time_with_month_offset (offset ):
17+ return (datetime .now () + relativedelta (months = offset )).strftime ("%Y-%m-%d-%H:%M:%S" )
18+
19+ q1_standard_resources = {
20+ "VCPU" : "40000" ,
21+ "MEMORY_MB" : "4423680" ,
22+ "DISK_GB" : "108000"
23+ }
24+
25+ q1_extra_resources = {
26+ "VCPU" : "41000" ,
27+ "MEMORY_MB" : "4424680" ,
28+ "DISK_GB" : "109000"
29+ }
30+
31+ q1_insufficient_resources = {
32+ "VCPU" : "10" ,
33+ "MEMORY_MB" : "10" ,
34+ "DISK_GB" : "10"
35+ }
36+
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 )
41+
42+ standard_test_data = tofu_test_data .get_standard_test_vars (
43+ q1_st ,
44+ q1_end ,
45+ q2_st ,
46+ q2_end ,
47+ q1_standard_resources
48+ )
4049
50+ updated_test_data = tofu_test_data .get_standard_test_vars (
51+ q1_st ,
52+ q1_end ,
53+ q2_st ,
54+ q2_end ,
55+ q1_extra_resources
56+ )
4157
42- lease_request_json = get_lease_request_json ()
58+ empty_test_data = tofu_test_data .get_empty_test_data_copy (standard_test_data )
59+ try_active_delete_test_data = tofu_test_data .get_no_q1_copy (standard_test_data )
4360
61+ lease_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" )
64+ )
4465
4566@pytest .fixture (scope = "session" )
4667def terraform_rest_setup ():
@@ -50,11 +71,11 @@ def terraform_rest_setup():
5071
5172 tf = Tofu (cwd = working_dir )
5273 tf .init ()
53- tf .apply (extra_args = [ "--var-file=" + var_file ] )
74+ tf .apply (variables = standard_test_data )
5475
5576 yield tf
5677
57- destroy = tf .apply (extra_args = [ "--var-file=" + delete_file ] )
78+ destroy = tf .apply (variables = empty_test_data )
5879 assert len (destroy .errors ) == 0
5980
6081
@@ -70,38 +91,39 @@ def add_consumer_request(terraform_rest_setup):
7091 },
7192 json = lease_request_json ,
7293 )
73- yield dict (status = consumer .status_code , tf_workspace = terraform_rest_setup )
94+ return dict (status = consumer .status_code , tf_workspace = terraform_rest_setup )
7495
96+ @pytest .fixture (scope = "session" )
97+ def update_allocation_resources (add_consumer_request ):
98+ workspace = add_consumer_request ["tf_workspace" ]
99+ workspace .apply (variables = updated_test_data )
100+ return dict (tf_workspace = add_consumer_request ["tf_workspace" ])
75101
76102@pytest .fixture (scope = "session" )
77- def try_delete_active_allocation (add_consumer_request ):
78- delete_file = os .path .join (
79- os .path .dirname (__file__ ), ".." , "tests" , "tofu_configs" , "delete-active.tfvars"
80- )
81- try_delete = add_consumer_request ["tf_workspace" ].apply (
82- extra_args = ["--var-file=" + delete_file ]
103+ def try_delete_active_allocation (update_allocation_resources ):
104+ print ("Testing deleting active allocation, will see 403 errors" )
105+ try_delete = update_allocation_resources ["tf_workspace" ].apply (
106+ variables = try_active_delete_test_data
83107 )
108+ print ("End of active allocation delete test" )
84109 return dict (
85110 error_count = len (try_delete .errors ),
86- tf_workspace = add_consumer_request ["tf_workspace" ],
111+ tf_workspace = update_allocation_resources ["tf_workspace" ],
87112 )
88113
89114
90115@pytest .fixture (scope = "session" )
91116def try_destroy_with_active_consumers (try_delete_active_allocation ):
92- all_file = os .path .join (
93- os .path .dirname (__file__ ), ".." , "tests" , "tofu_configs" , "initial.tfvars"
94- )
95- delete_file = os .path .join (
96- os .path .dirname (__file__ ), ".." , "tests" , "tofu_configs" , "empty.tfvars"
97- )
117+
118+ print ("Testing destroy with active consumer, will see 403 errors" )
98119 try_delete = try_delete_active_allocation ["tf_workspace" ].apply (
99- extra_args = [ "--var-file=" + delete_file ]
120+ variables = empty_test_data
100121 )
122+ print ("End of destroy with active consumers test" )
101123 yield len (try_delete .errors )
102124 # Undo any destroys
103125 reapply = try_delete_active_allocation ["tf_workspace" ].apply (
104- extra_args = [ "--var-file=" + all_file ]
126+ variables = standard_test_data
105127 )
106128 assert len (reapply .errors ) == 0
107129
@@ -219,8 +241,17 @@ def test_only_allocation_resources_returned(terraform_rest_setup):
219241 (
220242 "add_consumer_request" ,
221243 {
222- # q1-0 = original - resource hours requested for a month by lease
223- "Q1-0" : {"VCPU" : 37120 , "MEMORY_MB" : 3703680 , "DISK_GB" : 82800 },
244+ # q1-0 = original - 31 days * 24 hours * lease resources
245+ "Q1-0" : {"VCPU" : 37024 , "MEMORY_MB" : 3679680 , "DISK_GB" : 81960 },
246+ "Q1-1" : {"VCPU" : 20000 , "MEMORY_MB" : 2000000 , "DISK_GB" : 200000 },
247+ "Q2-0" : {"VCPU" : 80000 , "MEMORY_MB" : 8000000 , "DISK_GB" : 300000 },
248+ },
249+ ),
250+ (
251+ "update_allocation_resources" ,
252+ {
253+ # q1-0 after consumption + new resources
254+ "Q1-0" : {"VCPU" : 38024 , "MEMORY_MB" : 3680680 , "DISK_GB" : 82960 },
224255 "Q1-1" : {"VCPU" : 20000 , "MEMORY_MB" : 2000000 , "DISK_GB" : 200000 },
225256 "Q2-0" : {"VCPU" : 80000 , "MEMORY_MB" : 8000000 , "DISK_GB" : 300000 },
226257 },
@@ -229,7 +260,7 @@ def test_only_allocation_resources_returned(terraform_rest_setup):
229260 "delete_consumer" ,
230261 {
231262 # historical consumer consumption data should be preserved
232- "Q1-0" : {"VCPU" : 37120 , "MEMORY_MB" : 3703680 , "DISK_GB" : 82800 },
263+ "Q1-0" : {"VCPU" : 38024 , "MEMORY_MB" : 3680680 , "DISK_GB" : 82960 },
233264 "Q1-1" : {"VCPU" : 20000 , "MEMORY_MB" : 2000000 , "DISK_GB" : 200000 },
234265 "Q2-0" : {"VCPU" : 80000 , "MEMORY_MB" : 8000000 , "DISK_GB" : 300000 },
235266 },
0 commit comments