|
| 1 | +import { |
| 2 | + buildStartReplicationJobRequest, |
| 3 | + formatReplicationJob, |
| 4 | + formatReplicationJobs, |
| 5 | + formatSupportedResources, |
| 6 | +} from "../../tools/localstack-aws-replicator"; |
| 7 | + |
| 8 | +describe("localstack-aws-replicator", () => { |
| 9 | + const originalEnv = process.env; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + process.env = { |
| 13 | + ...originalEnv, |
| 14 | + AWS_ACCESS_KEY_ID: "AKIA...", |
| 15 | + AWS_SECRET_ACCESS_KEY: "secret", |
| 16 | + AWS_DEFAULT_REGION: "us-east-1", |
| 17 | + }; |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + process.env = originalEnv; |
| 22 | + }); |
| 23 | + |
| 24 | + describe("buildStartReplicationJobRequest", () => { |
| 25 | + it("builds a single-resource request from type and identifier using env credentials", () => { |
| 26 | + const request = buildStartReplicationJobRequest({ |
| 27 | + action: "start", |
| 28 | + replication_type: "SINGLE_RESOURCE", |
| 29 | + resource_type: "AWS::EC2::VPC", |
| 30 | + resource_identifier: "vpc-123", |
| 31 | + target_account_id: "111111111111", |
| 32 | + target_region_name: "eu-central-1", |
| 33 | + } as any); |
| 34 | + |
| 35 | + expect(request).toEqual({ |
| 36 | + replication_type: "SINGLE_RESOURCE", |
| 37 | + replication_job_config: { |
| 38 | + resource_type: "AWS::EC2::VPC", |
| 39 | + resource_identifier: "vpc-123", |
| 40 | + }, |
| 41 | + source_aws_config: { |
| 42 | + aws_access_key_id: "AKIA...", |
| 43 | + aws_secret_access_key: "secret", |
| 44 | + region_name: "us-east-1", |
| 45 | + }, |
| 46 | + target_aws_config: { |
| 47 | + aws_access_key_id: "111111111111", |
| 48 | + aws_secret_access_key: "test", |
| 49 | + region_name: "eu-central-1", |
| 50 | + }, |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + it("builds a resource ARN request without requiring resource type using env credentials", () => { |
| 55 | + process.env.AWS_SESSION_TOKEN = "token"; |
| 56 | + process.env.AWS_ENDPOINT_URL = "https://example.com"; |
| 57 | + |
| 58 | + const request = buildStartReplicationJobRequest({ |
| 59 | + action: "start", |
| 60 | + replication_type: "SINGLE_RESOURCE", |
| 61 | + resource_arn: "arn:aws:secretsmanager:us-east-1:123456789012:secret:my-secret", |
| 62 | + } as any); |
| 63 | + |
| 64 | + expect(request).toEqual({ |
| 65 | + replication_type: "SINGLE_RESOURCE", |
| 66 | + replication_job_config: { |
| 67 | + resource_arn: "arn:aws:secretsmanager:us-east-1:123456789012:secret:my-secret", |
| 68 | + }, |
| 69 | + source_aws_config: { |
| 70 | + aws_access_key_id: "AKIA...", |
| 71 | + aws_secret_access_key: "secret", |
| 72 | + aws_session_token: "token", |
| 73 | + region_name: "us-east-1", |
| 74 | + endpoint_url: "https://example.com", |
| 75 | + }, |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it("does not mix prefixed source credentials with generic AWS session values", () => { |
| 80 | + process.env.AWS_REPLICATOR_SOURCE_AWS_ACCESS_KEY_ID = "replicator-key"; |
| 81 | + process.env.AWS_REPLICATOR_SOURCE_AWS_SECRET_ACCESS_KEY = "replicator-secret"; |
| 82 | + process.env.AWS_REPLICATOR_SOURCE_REGION_NAME = "eu-west-1"; |
| 83 | + process.env.AWS_SESSION_TOKEN = "generic-token"; |
| 84 | + |
| 85 | + const request = buildStartReplicationJobRequest({ |
| 86 | + action: "start", |
| 87 | + replication_type: "SINGLE_RESOURCE", |
| 88 | + resource_type: "AWS::SSM::Parameter", |
| 89 | + resource_identifier: "my-param", |
| 90 | + } as any); |
| 91 | + |
| 92 | + expect(request.source_aws_config).toEqual({ |
| 93 | + aws_access_key_id: "replicator-key", |
| 94 | + aws_secret_access_key: "replicator-secret", |
| 95 | + region_name: "eu-west-1", |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it("does not allow a target endpoint URL override", () => { |
| 100 | + process.env.AWS_REPLICATOR_TARGET_ENDPOINT_URL = "https://not-localstack.example.com"; |
| 101 | + |
| 102 | + const request = buildStartReplicationJobRequest({ |
| 103 | + action: "start", |
| 104 | + replication_type: "SINGLE_RESOURCE", |
| 105 | + resource_type: "AWS::EC2::VPC", |
| 106 | + resource_identifier: "vpc-123", |
| 107 | + target_region_name: "eu-central-1", |
| 108 | + } as any); |
| 109 | + |
| 110 | + expect(request.target_aws_config).toEqual({ |
| 111 | + aws_access_key_id: "test", |
| 112 | + aws_secret_access_key: "test", |
| 113 | + region_name: "eu-central-1", |
| 114 | + }); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe("formatReplicationJob", () => { |
| 119 | + it("includes batch result details", () => { |
| 120 | + const formatted = formatReplicationJob("AWS Replicator Job Status", { |
| 121 | + job_id: "job-123", |
| 122 | + state: "SUCCEEDED", |
| 123 | + type: "BATCH", |
| 124 | + replication_config: { |
| 125 | + resource_type: "AWS::SSM::Parameter", |
| 126 | + identifier: "/dev/", |
| 127 | + }, |
| 128 | + result: { |
| 129 | + resources_succeeded: 2, |
| 130 | + resources_failed: 0, |
| 131 | + }, |
| 132 | + }); |
| 133 | + |
| 134 | + expect(formatted).toContain("AWS Replicator Job Status"); |
| 135 | + expect(formatted).toContain("`job-123`"); |
| 136 | + expect(formatted).toContain("resources_succeeded"); |
| 137 | + expect(formatted).toContain("AWS::SSM::Parameter"); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + describe("formatReplicationJobs", () => { |
| 142 | + it("summarizes listed jobs and includes the raw response", () => { |
| 143 | + const formatted = formatReplicationJobs([ |
| 144 | + { |
| 145 | + job_id: "job-123", |
| 146 | + state: "SUCCEEDED", |
| 147 | + type: "SINGLE_RESOURCE", |
| 148 | + replication_config: { |
| 149 | + resource_type: "AWS::EC2::VPC", |
| 150 | + resource_identifier: "vpc-123", |
| 151 | + }, |
| 152 | + }, |
| 153 | + ]); |
| 154 | + |
| 155 | + expect(formatted).toContain("AWS Replicator Jobs"); |
| 156 | + expect(formatted).toContain("job-123"); |
| 157 | + expect(formatted).toContain("SUCCEEDED"); |
| 158 | + expect(formatted).toContain("Raw Response"); |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + describe("formatSupportedResources", () => { |
| 163 | + it("summarizes supported resource types and identifiers", () => { |
| 164 | + const formatted = formatSupportedResources([ |
| 165 | + { |
| 166 | + resource_type: "AWS::SSM::Parameter", |
| 167 | + service: "ssm", |
| 168 | + identifier: "Name", |
| 169 | + }, |
| 170 | + ]); |
| 171 | + |
| 172 | + expect(formatted).toContain("AWS Replicator Supported Resources"); |
| 173 | + expect(formatted).toContain("AWS::SSM::Parameter"); |
| 174 | + expect(formatted).toContain("identifier: `Name`"); |
| 175 | + expect(formatted).toContain("Raw Response"); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments