Skip to content

Commit 3de5c42

Browse files
committed
Make test more configurable, add (failing) test for updating allocations
1 parent 0e90bb9 commit 3de5c42

File tree

6 files changed

+178
-150
lines changed

6 files changed

+178
-150
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ tzdata==2024.1
1212
psycopg2-binary==2.9.9
1313
whitenoise==6.9.0
1414
tofupy==1.1.1
15+
requests==2.32.5

tofu/tests/tofu_configs/delete-active.tfvars

Lines changed: 0 additions & 33 deletions
This file was deleted.

tofu/tests/tofu_configs/empty.tfvars

Lines changed: 0 additions & 6 deletions
This file was deleted.

tofu/tests/tofu_configs/initial.tfvars

Lines changed: 0 additions & 62 deletions
This file was deleted.

tofu/tests/tofu_test_data.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from datetime import datetime
2+
from dateutil.relativedelta import relativedelta
3+
import uuid
4+
import json
5+
import copy
6+
7+
def get_standard_test_vars(q1_start,q1_end,q2_start,q2_end,q1_0_resources):
8+
return {
9+
"resource_provider_name": "Test Provider",
10+
"resource_provider_email": "[email protected]",
11+
"resource_provider_info_url": "https://www.google.com",
12+
"accounts": json.dumps([
13+
{
14+
"name": "TestAccount1",
15+
"email": "[email protected]",
16+
"openstack_project_id": "c2eced313b324cdb8e670e6e30bf387d"
17+
},
18+
{
19+
"name": "TestAccount2",
20+
"email": "[email protected]",
21+
"openstack_project_id": "2fbf511968aa443e883a82283b0f0160"
22+
}
23+
]),
24+
"allocations": json.dumps({
25+
"Q1": {
26+
"start_date": str(q1_start),
27+
"end_date": str(q1_end),
28+
"projects": [
29+
{
30+
"account_email": "[email protected]",
31+
"resources": q1_0_resources
32+
},
33+
{
34+
"account_email": "[email protected]",
35+
"resources": {
36+
"VCPU": "20000",
37+
"MEMORY_MB": "2000000",
38+
"DISK_GB": "200000"
39+
}
40+
}
41+
]
42+
},
43+
"Q2": {
44+
"start_date": str(q2_start),
45+
"end_date": str(q2_end),
46+
"projects": [
47+
{
48+
"account_email": "[email protected]",
49+
"resources": {
50+
"VCPU": "80000",
51+
"MEMORY_MB": "8000000",
52+
"DISK_GB": "300000"
53+
}
54+
}
55+
]
56+
}
57+
})
58+
}
59+
60+
def get_empty_test_data_copy(data):
61+
tmp = copy.deepcopy(data)
62+
tmp["allocations"] = "{}"
63+
tmp["accounts"] = "[]"
64+
return tmp
65+
66+
def get_no_q1_copy(data):
67+
tmp = copy.deepcopy(data)
68+
tmp["allocations"] = json.dumps({"Q2": json.loads(tmp["allocations"])["Q2"]})
69+
return tmp
70+
71+
def get_lease_request_json(start,end):
72+
start_time = start
73+
end_time = end
74+
return {
75+
"context": {
76+
"user_id": "caa8b54a-eb5e-4134-8ae2-a3946a428ec7",
77+
"project_id": "c2eced313b324cdb8e670e6e30bf387d",
78+
"auth_url": "http://api.example.com:5000/v3",
79+
"region_name": "RegionOne",
80+
},
81+
"lease": {
82+
"id": str(uuid.uuid4()),
83+
"name": "my_new_lease",
84+
"start_date": start_time,
85+
"end_date": end_time,
86+
"reservations": [
87+
{
88+
"amount": 2,
89+
"flavor_id": "e26a4241-b83d-4516-8e0e-8ce2665d1966",
90+
"resource_type": "flavor:instance",
91+
"affinity": None,
92+
"allocations": [],
93+
}
94+
],
95+
"resource_requests": {"DISK_GB": 35, "MEMORY_MB": 1000, "VCPU": 4},
96+
},
97+
}

tofu/tests/tofu_tests.py

Lines changed: 80 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,63 @@
55
from datetime import datetime
66
from dateutil.relativedelta import relativedelta
77
import uuid
8+
import json
9+
import tofu_test_data
810

911
coral_uri = os.environ.get("TF_VAR_coral_uri")
1012
headers = {"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")
4667
def 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")
91116
def 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

Comments
 (0)