1010# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. 
1111# See the License for the specific language governing permissions and limitations under the License. 
1212import  logging 
13+ import  os 
14+ import  subprocess 
1315
1416import  boto3 
1517import  pytest 
1618from  assertpy  import  assert_that 
17- from  utils  import  describe_cluster_instances , retrieve_cfn_resources 
19+ from  utils  import  describe_cluster_instances , retrieve_cfn_resources ,  wait_for_computefleet_changed 
1820
1921
2022@pytest .mark .usefixtures ("os" , "region" ) 
@@ -40,7 +42,46 @@ def test_on_demand_capacity_reservation(
4042        pg_capacity_reservation_id = odcr_resources ["integTestsPgOdcr" ],
4143        pg_capacity_reservation_arn = resource_group_arn ,
4244    )
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 )
4485
4586    _assert_instance_in_capacity_reservation (cluster , region , "open-odcr-id-cr" , odcr_resources ["integTestsOpenOdcr" ])
4687    _assert_instance_in_capacity_reservation (cluster , region , "open-odcr-arn-cr" , odcr_resources ["integTestsOpenOdcr" ])
@@ -64,6 +105,19 @@ def test_on_demand_capacity_reservation(
64105    )
65106    _assert_instance_in_capacity_reservation (cluster , region , "pg-odcr-id-cr" , odcr_resources ["integTestsPgOdcr" ])
66107    _assert_instance_in_capacity_reservation (cluster , region , "pg-odcr-arn-cr" , odcr_resources ["integTestsPgOdcr" ])
108+     cluster .stop ()
109+     wait_for_computefleet_changed (cluster , "STOPPED" )
110+     updated_config_file  =  pcluster_config_reader (
111+         config_file = "pcluster.config.update.yaml" ,
112+         placement_group = placement_group_stack .cfn_resources ["PlacementGroup" ],
113+         open_capacity_reservation_id = odcr_resources ["integTestsOpenOdcr" ],
114+         open_capacity_reservation_arn = resource_group_arn ,
115+         target_capacity_reservation_id = odcr_resources ["integTestsTargetOdcr" ],
116+         target_capacity_reservation_arn = resource_group_arn ,
117+         pg_capacity_reservation_id = odcr_resources ["integTestsPgOdcr" ],
118+         pg_capacity_reservation_arn = resource_group_arn ,
119+     )
120+     cluster .update (str (updated_config_file ))
67121
68122
69123def  _assert_instance_in_capacity_reservation (cluster , region , compute_resource_name , expected_reservation ):
0 commit comments