github.com/anthropics/anthropic-sdk-go@v1.70.1/bedrock/bedrockmantle_live_test.go (about)

     1  package bedrock_test
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/anthropics/anthropic-sdk-go"
     9  	"github.com/anthropics/anthropic-sdk-go/bedrock"
    10  )
    11  
    12  // Live integration tests for Bedrock Mantle. Skipped unless ANTHROPIC_LIVE=1.
    13  //
    14  // Required env vars vary by auth mode:
    15  //
    16  //	API key mode:  AWS_BEARER_TOKEN_BEDROCK (or ANTHROPIC_AWS_API_KEY),
    17  //	               AWS_REGION
    18  //
    19  //	SigV4 mode:    AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION
    20  //
    21  // Run: ANTHROPIC_LIVE=1 go test ./bedrock/... -run TestLiveMantle -v
    22  
    23  func skipUnlessLive(t *testing.T) {
    24  	t.Helper()
    25  	if os.Getenv("ANTHROPIC_LIVE") != "1" {
    26  		t.Skip("set ANTHROPIC_LIVE=1 to run live integration tests")
    27  	}
    28  }
    29  
    30  func requireEnv(t *testing.T, names ...string) {
    31  	t.Helper()
    32  	for _, name := range names {
    33  		if os.Getenv(name) == "" {
    34  			t.Fatalf("required env var %s is not set", name)
    35  		}
    36  	}
    37  }
    38  
    39  func liveModel() string {
    40  	if m := os.Getenv("ANTHROPIC_LIVE_MODEL"); m != "" {
    41  		return m
    42  	}
    43  	return "claude-sonnet-4-6"
    44  }
    45  
    46  func sendMantleMessage(t *testing.T, client *bedrock.MantleClient) {
    47  	t.Helper()
    48  
    49  	message, err := client.Messages.New(context.Background(), anthropic.MessageNewParams{
    50  		Model:     liveModel(),
    51  		MaxTokens: 32,
    52  		Messages: []anthropic.MessageParam{
    53  			anthropic.NewUserMessage(anthropic.NewTextBlock("Say exactly: hello")),
    54  		},
    55  	})
    56  	if err != nil {
    57  		t.Fatalf("Messages.New failed: %v", err)
    58  	}
    59  	if len(message.Content) == 0 {
    60  		t.Fatal("expected non-empty content in response")
    61  	}
    62  	t.Logf("response: %s", message.Content[0].Text)
    63  }
    64  
    65  func TestLiveMantleAPIKey(t *testing.T) {
    66  	skipUnlessLive(t)
    67  	requireEnv(t, "AWS_REGION")
    68  
    69  	// Need at least one of these for the API key
    70  	apiKey := os.Getenv("AWS_BEARER_TOKEN_BEDROCK")
    71  	if apiKey == "" {
    72  		apiKey = os.Getenv("ANTHROPIC_AWS_API_KEY")
    73  	}
    74  	if apiKey == "" {
    75  		t.Fatal("required env var AWS_BEARER_TOKEN_BEDROCK or ANTHROPIC_AWS_API_KEY is not set")
    76  	}
    77  
    78  	client, err := bedrock.NewMantleClient(context.Background(), bedrock.MantleClientConfig{
    79  		APIKey: apiKey,
    80  	})
    81  	if err != nil {
    82  		t.Fatalf("NewMantleClient failed: %v", err)
    83  	}
    84  
    85  	sendMantleMessage(t, client)
    86  }
    87  
    88  func TestLiveMantleSigV4ExplicitCreds(t *testing.T) {
    89  	skipUnlessLive(t)
    90  	requireEnv(t, "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_REGION")
    91  
    92  	client, err := bedrock.NewMantleClient(context.Background(), bedrock.MantleClientConfig{
    93  		AWSAccessKey:       os.Getenv("AWS_ACCESS_KEY_ID"),
    94  		AWSSecretAccessKey: os.Getenv("AWS_SECRET_ACCESS_KEY"),
    95  		AWSSessionToken:    os.Getenv("AWS_SESSION_TOKEN"),
    96  	})
    97  	if err != nil {
    98  		t.Fatalf("NewMantleClient failed: %v", err)
    99  	}
   100  
   101  	sendMantleMessage(t, client)
   102  }
   103  
   104  func TestLiveMantleSigV4DefaultChain(t *testing.T) {
   105  	skipUnlessLive(t)
   106  	requireEnv(t, "AWS_REGION")
   107  
   108  	// Clear all API key env vars so the default AWS credential chain is used
   109  	t.Setenv("AWS_BEARER_TOKEN_BEDROCK", "")
   110  	t.Setenv("ANTHROPIC_AWS_API_KEY", "")
   111  	t.Setenv("ANTHROPIC_API_KEY", "")
   112  
   113  	client, err := bedrock.NewMantleClient(context.Background(), bedrock.MantleClientConfig{})
   114  	if err != nil {
   115  		t.Fatalf("NewMantleClient failed (default AWS credential chain): %v", err)
   116  	}
   117  
   118  	sendMantleMessage(t, client)
   119  }
   120  
   121  func TestLiveMantleSigV4ProfileFromCredentialsFile(t *testing.T) {
   122  	skipUnlessLive(t)
   123  	requireEnv(t, "AWS_REGION", "AWS_PROFILE")
   124  
   125  	// Clear explicit creds and API keys so the SDK must resolve from ~/.aws/credentials
   126  	t.Setenv("AWS_BEARER_TOKEN_BEDROCK", "")
   127  	t.Setenv("ANTHROPIC_AWS_API_KEY", "")
   128  	t.Setenv("ANTHROPIC_API_KEY", "")
   129  	t.Setenv("AWS_ACCESS_KEY_ID", "")
   130  	t.Setenv("AWS_SECRET_ACCESS_KEY", "")
   131  	t.Setenv("AWS_SESSION_TOKEN", "")
   132  
   133  	client, err := bedrock.NewMantleClient(context.Background(), bedrock.MantleClientConfig{
   134  		AWSProfile: os.Getenv("AWS_PROFILE"),
   135  	})
   136  	if err != nil {
   137  		t.Fatalf("NewMantleClient failed (profile from credentials file): %v", err)
   138  	}
   139  
   140  	sendMantleMessage(t, client)
   141  }