|
| 1 | +package s3 |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/infracost/go-proto/pkg/tree/resource" |
| 7 | + "github.com/infracost/go-proto/pkg/tree/value" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +// CloudFormation/CDK buckets frequently have no explicit BucketName (the physical |
| 12 | +// name is generated at deploy time). Their inline configurations carry an empty |
| 13 | +// bucket name but share the owning bucket's logical ID, so PostProcess must link |
| 14 | +// them to the bucket via that ID fallback rather than the (empty) name. |
| 15 | +func TestPostProcess_LinksInlineConfigsForNamelessBucketByID(t *testing.T) { |
| 16 | + const bucketID = "DevArtifactBucket2651DA98" |
| 17 | + |
| 18 | + s := &S3{ |
| 19 | + Buckets: []Bucket{ |
| 20 | + { |
| 21 | + Resource: resource.Resource{ID: bucketID}, |
| 22 | + Name: value.New("", 0, "", nil), // no explicit BucketName |
| 23 | + }, |
| 24 | + }, |
| 25 | + LifecycleConfigurations: []LifecycleConfiguration{ |
| 26 | + { |
| 27 | + Resource: resource.Resource{ID: bucketID}, // inline config shares the bucket's logical ID |
| 28 | + BucketName: value.New("", 0, "", nil), |
| 29 | + }, |
| 30 | + }, |
| 31 | + IntelligentTieringConfigurations: []IntelligentTieringConfiguration{ |
| 32 | + { |
| 33 | + Resource: resource.Resource{ID: bucketID}, |
| 34 | + BucketName: value.New("", 0, "", nil), |
| 35 | + }, |
| 36 | + }, |
| 37 | + BucketVersioningConfigurations: []BucketVersioningConfiguration{ |
| 38 | + { |
| 39 | + Resource: resource.Resource{ID: bucketID}, |
| 40 | + BucketName: value.New("", 0, "", nil), |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + s.PostProcess() |
| 46 | + |
| 47 | + assert.Len(t, s.Buckets[0].Relationships.LifecycleConfigurations, 1, |
| 48 | + "inline lifecycle configuration should link to a nameless bucket via shared ID") |
| 49 | + assert.Len(t, s.Buckets[0].Relationships.IntelligentTieringConfigurations, 1) |
| 50 | + assert.Len(t, s.Buckets[0].Relationships.BucketVersioningConfigurations, 1) |
| 51 | +} |
| 52 | + |
| 53 | +// A standalone sub-resource with its own distinct ID and a non-matching bucket |
| 54 | +// name must NOT be linked to an unrelated bucket by the ID fallback. |
| 55 | +func TestPostProcess_DoesNotFalseMatchStandaloneConfig(t *testing.T) { |
| 56 | + s := &S3{ |
| 57 | + Buckets: []Bucket{ |
| 58 | + { |
| 59 | + Resource: resource.Resource{ID: "bucket-1"}, |
| 60 | + Name: value.New("bucket-1", 0, "", nil), |
| 61 | + }, |
| 62 | + }, |
| 63 | + LifecycleConfigurations: []LifecycleConfiguration{ |
| 64 | + { |
| 65 | + Resource: resource.Resource{ID: "standalone-lc"}, |
| 66 | + BucketName: value.New("some-other-bucket", 0, "", nil), |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + s.PostProcess() |
| 72 | + |
| 73 | + assert.Empty(t, s.Buckets[0].Relationships.LifecycleConfigurations, |
| 74 | + "a standalone config pointing at another bucket must not link via the ID fallback") |
| 75 | +} |
0 commit comments