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,50 @@ 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 " 
63+             '        """Upload config file to S3 bucket."""' ,
64+             "    def upload_config(self, config, config_name, format=S3FileFormat.YAML):\n " 
65+             '        """Upload config file to S3 bucket."""\n ' 
66+             '        if config_name == "cluster-config.yaml":\n ' 
67+             "            config = re.sub(r'cr-[0-9a-f]{17}', 'cr-11111111111111111', config)" ,
68+         )
69+ 
70+         # Write the modified content back 
71+         with  open (s3_bucket_file , "w" ) as  f :
72+             f .write (modified_content )
73+ 
74+         # Install the CLI 
75+         logging .info ("Installing CLI from local repository" )
76+         subprocess .run (["pip" , "install" , "./cli" ], cwd = repo_root , check = True )
77+ 
78+         # Create the cluster 
79+         cluster  =  clusters_factory (cluster_config )
80+     finally :
81+         # Revert the patch by restoring the original file 
82+         logging .info ("Reverting patch from the repository" )
83+         with  open (s3_bucket_file , "w" ) as  f :
84+             f .write (original_content )
85+ 
86+         # Reinstall the CLI 
87+         logging .info ("Reinstalling CLI from local repository" )
88+         subprocess .run (["pip" , "install" , "./cli" ], cwd = repo_root , check = True )
4489
4590    _assert_instance_in_capacity_reservation (cluster , region , "open-odcr-id-cr" , odcr_resources ["integTestsOpenOdcr" ])
4691    _assert_instance_in_capacity_reservation (cluster , region , "open-odcr-arn-cr" , odcr_resources ["integTestsOpenOdcr" ])
@@ -64,6 +109,19 @@ def test_on_demand_capacity_reservation(
64109    )
65110    _assert_instance_in_capacity_reservation (cluster , region , "pg-odcr-id-cr" , odcr_resources ["integTestsPgOdcr" ])
66111    _assert_instance_in_capacity_reservation (cluster , region , "pg-odcr-arn-cr" , odcr_resources ["integTestsPgOdcr" ])
112+     cluster .stop ()
113+     wait_for_computefleet_changed (cluster , "STOPPED" )
114+     updated_config_file  =  pcluster_config_reader (
115+         config_file = "pcluster.config.update.yaml" ,
116+         placement_group = placement_group_stack .cfn_resources ["PlacementGroup" ],
117+         open_capacity_reservation_id = odcr_resources ["integTestsOpenOdcr" ],
118+         open_capacity_reservation_arn = resource_group_arn ,
119+         target_capacity_reservation_id = odcr_resources ["integTestsTargetOdcr" ],
120+         target_capacity_reservation_arn = resource_group_arn ,
121+         pg_capacity_reservation_id = odcr_resources ["integTestsPgOdcr" ],
122+         pg_capacity_reservation_arn = resource_group_arn ,
123+     )
124+     cluster .update (str (updated_config_file ))
67125
68126
69127def  _assert_instance_in_capacity_reservation (cluster , region , compute_resource_name , expected_reservation ):
0 commit comments