1+ import json
12import logging
23import textwrap
34from typing import List
45
6+ import boto3
57from click .testing import Result
68
79from tests_integ .cli .runtime .base_test import BaseCLIRuntimeTest , CommandInvocation
@@ -17,6 +19,12 @@ class TestSimpleAgent(BaseCLIRuntimeTest):
1719 """
1820
1921 def setup (self ):
22+ # Extract role name from ARN if provided
23+ if TEST_ROLE :
24+ self .role_name = TEST_ROLE .split ("/" )[- 1 ]
25+ else :
26+ self .role_name = None
27+
2028 self .agent_file = "agent.py"
2129 self .requirements_file = "requirements.txt"
2230
@@ -43,6 +51,64 @@ async def agent_invocation(payload):
4351 """ ).strip ()
4452 file .write (content )
4553
54+ def _setup_role_trust_policy (self ):
55+ """
56+ Ensure the IAM role has the required trust relationship with Bedrock.
57+ """
58+ try :
59+ iam_client = boto3 .client ("iam" )
60+
61+ # Get current trust policy
62+ response = iam_client .get_role (RoleName = self .role_name )
63+ current_policy = response ["Role" ]["AssumeRolePolicyDocument" ]
64+
65+ # Check if bedrock is already a trusted service
66+ bedrock_trusted = False
67+ for statement in current_policy .get ("Statement" , []):
68+ principal = statement .get ("Principal" , {})
69+ service = principal .get ("Service" , [])
70+ if isinstance (service , str ):
71+ service = [service ]
72+ if "bedrock.amazonaws.com" in service :
73+ bedrock_trusted = True
74+ break
75+
76+ # Add bedrock trust if needed
77+ if not bedrock_trusted :
78+ logger .info ("Adding bedrock.amazonaws.com to trust policy for role %s" , self .role_name )
79+
80+ # Copy the existing policy and add bedrock
81+ if len (current_policy .get ("Statement" , [])) > 0 :
82+ # Add to existing policy
83+ new_statement = {
84+ "Effect" : "Allow" ,
85+ "Principal" : {"Service" : "bedrock.amazonaws.com" },
86+ "Action" : "sts:AssumeRole" ,
87+ }
88+ current_policy ["Statement" ].append (new_statement )
89+ else :
90+ # Create new policy
91+ current_policy = {
92+ "Version" : "2012-10-17" ,
93+ "Statement" : [
94+ {
95+ "Effect" : "Allow" ,
96+ "Principal" : {"Service" : "bedrock.amazonaws.com" },
97+ "Action" : "sts:AssumeRole" ,
98+ }
99+ ],
100+ }
101+
102+ # Update the role
103+ iam_client .update_assume_role_policy (RoleName = self .role_name , PolicyDocument = json .dumps (current_policy ))
104+ logger .info ("Updated trust policy for role %s" , self .role_name )
105+ else :
106+ logger .info ("Role %s already trusts bedrock.amazonaws.com" , self .role_name )
107+
108+ except Exception as e :
109+ logger .error ("Error updating role trust policy: %s" , str (e ))
110+ raise
111+
46112 def get_command_invocations (self ) -> List [CommandInvocation ]:
47113 configure_invocation = CommandInvocation (
48114 command = [
@@ -85,7 +151,13 @@ def validate_configure(self, result: Result):
85151
86152 assert "Configuration Success" in output
87153 assert "Agent Name: agent" in output
88- assert TEST_ROLE in output
154+
155+ # Handle both explicit role and auto-create
156+ if TEST_ROLE :
157+ assert TEST_ROLE in output
158+ else :
159+ assert "Auto-create" in output or "Execution Role:" in output
160+
89161 assert "Authorization: IAM" in output
90162 assert ".bedrock_agentcore.yaml" in output
91163
0 commit comments