10
10
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied.
11
11
# See the License for the specific language governing permissions and limitations under the License.
12
12
import logging
13
+ import os
14
+ import subprocess
13
15
14
16
import boto3
15
17
import pytest
@@ -40,7 +42,46 @@ def test_on_demand_capacity_reservation(
40
42
pg_capacity_reservation_id = odcr_resources ["integTestsPgOdcr" ],
41
43
pg_capacity_reservation_arn = resource_group_arn ,
42
44
)
43
- cluster = clusters_factory (cluster_config )
45
+
46
+ # Apply patch to the repo
47
+ logging .info ("Applying patch to the repository" )
48
+ repo_root = os .path .abspath (os .path .join (os .path .dirname (__file__ ), "../../../.." ))
49
+ s3_bucket_file = os .path .join (repo_root , "cli/src/pcluster/models/s3_bucket.py" )
50
+
51
+ # Backup the original file
52
+ with open (s3_bucket_file , "r" ) as f :
53
+ original_content = f .read ()
54
+
55
+ try :
56
+ # Apply the patch - inject the bug that replaces capacity reservation IDs
57
+ with open (s3_bucket_file , "r" ) as f :
58
+ content = f .read ()
59
+
60
+ # Add the bug injection line after the upload_config method definition
61
+ modified_content = content .replace (
62
+ " def upload_config(self, config, config_name, format=S3FileFormat.YAML):\n \" \" \" Upload config file to S3 bucket.\" \" \" " ,
63
+ " def upload_config(self, config, config_name, format=S3FileFormat.YAML):\n \" \" \" Upload config file to S3 bucket.\" \" \" \n if config_name == \" cluster-config.yaml\" :\n config = re.sub(r'cr-[0-9a-f]{17}', 'cr-11111111111111111', config)"
64
+ )
65
+
66
+ # Write the modified content back
67
+ with open (s3_bucket_file , "w" ) as f :
68
+ f .write (modified_content )
69
+
70
+ # Install the CLI
71
+ logging .info ("Installing CLI from local repository" )
72
+ subprocess .run (["pip" , "install" , "./cli" ], cwd = repo_root , check = True )
73
+
74
+ # Create the cluster
75
+ cluster = clusters_factory (cluster_config )
76
+ finally :
77
+ # Revert the patch by restoring the original file
78
+ logging .info ("Reverting patch from the repository" )
79
+ with open (s3_bucket_file , "w" ) as f :
80
+ f .write (original_content )
81
+
82
+ # Reinstall the CLI
83
+ logging .info ("Reinstalling CLI from local repository" )
84
+ subprocess .run (["pip" , "install" , "./cli" ], cwd = repo_root , check = True )
44
85
45
86
_assert_instance_in_capacity_reservation (cluster , region , "open-odcr-id-cr" , odcr_resources ["integTestsOpenOdcr" ])
46
87
_assert_instance_in_capacity_reservation (cluster , region , "open-odcr-arn-cr" , odcr_resources ["integTestsOpenOdcr" ])
@@ -64,6 +105,18 @@ def test_on_demand_capacity_reservation(
64
105
)
65
106
_assert_instance_in_capacity_reservation (cluster , region , "pg-odcr-id-cr" , odcr_resources ["integTestsPgOdcr" ])
66
107
_assert_instance_in_capacity_reservation (cluster , region , "pg-odcr-arn-cr" , odcr_resources ["integTestsPgOdcr" ])
108
+ cluster .stop ()
109
+ updated_config_file = pcluster_config_reader (
110
+ config_file = "pcluster.config.update.yaml" ,
111
+ placement_group = placement_group_stack .cfn_resources ["PlacementGroup" ],
112
+ open_capacity_reservation_id = odcr_resources ["integTestsOpenOdcr" ],
113
+ open_capacity_reservation_arn = resource_group_arn ,
114
+ target_capacity_reservation_id = odcr_resources ["integTestsTargetOdcr" ],
115
+ target_capacity_reservation_arn = resource_group_arn ,
116
+ pg_capacity_reservation_id = odcr_resources ["integTestsPgOdcr" ],
117
+ pg_capacity_reservation_arn = resource_group_arn ,
118
+ )
119
+ cluster .update (str (updated_config_file ))
67
120
68
121
69
122
def _assert_instance_in_capacity_reservation (cluster , region , compute_resource_name , expected_reservation ):
0 commit comments