|
| 1 | +package resources |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 8 | + ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types" |
| 9 | + "github.com/gotidy/ptr" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func Test_EC2Snapshot_String(t *testing.T) { |
| 14 | + a := assert.New(t) |
| 15 | + |
| 16 | + ec2Snapshot := EC2Snapshot{ |
| 17 | + SnapshotID: ptr.String("snap-1234567890abcdef0"), |
| 18 | + } |
| 19 | + |
| 20 | + a.Equal("snap-1234567890abcdef0", ec2Snapshot.String()) |
| 21 | +} |
| 22 | + |
| 23 | +func Test_EC2Snapshot_Properties(t *testing.T) { |
| 24 | + a := assert.New(t) |
| 25 | + |
| 26 | + startTime := time.Now() |
| 27 | + restoreExpiryTime := time.Now().Add(24 * time.Hour) |
| 28 | + state := ec2types.SnapshotStateCompleted |
| 29 | + storageTier := ec2types.StorageTierStandard |
| 30 | + |
| 31 | + ec2Snapshot := EC2Snapshot{ |
| 32 | + SnapshotID: ptr.String("snap-1234567890abcdef0"), |
| 33 | + Description: ptr.String("My snapshot"), |
| 34 | + VolumeID: ptr.String("vol-1234567890abcdef0"), |
| 35 | + VolumeSize: ptr.Int32(100), |
| 36 | + State: &state, |
| 37 | + StateMessage: ptr.String("100% complete"), |
| 38 | + StartTime: &startTime, |
| 39 | + Progress: ptr.String("100%"), |
| 40 | + OwnerID: ptr.String("123456789012"), |
| 41 | + OwnerAlias: ptr.String("amazon"), |
| 42 | + Encrypted: ptr.Bool(true), |
| 43 | + KmsKeyID: ptr.String("arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012"), |
| 44 | + DataEncryptionKeyID: ptr.String("12345678-1234-1234-1234-123456789012"), |
| 45 | + StorageTier: &storageTier, |
| 46 | + RestoreExpiryTime: &restoreExpiryTime, |
| 47 | + Tags: &[]ec2types.Tag{ |
| 48 | + { |
| 49 | + Key: aws.String("Environment"), |
| 50 | + Value: aws.String("production"), |
| 51 | + }, |
| 52 | + { |
| 53 | + Key: aws.String("Backup"), |
| 54 | + Value: aws.String("daily"), |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + properties := ec2Snapshot.Properties() |
| 60 | + |
| 61 | + a.Equal("snap-1234567890abcdef0", properties.Get("SnapshotID")) |
| 62 | + a.Equal("My snapshot", properties.Get("Description")) |
| 63 | + a.Equal("vol-1234567890abcdef0", properties.Get("VolumeID")) |
| 64 | + a.Equal("100", properties.Get("VolumeSize")) |
| 65 | + a.Equal("completed", properties.Get("State")) |
| 66 | + a.Equal("100% complete", properties.Get("StateMessage")) |
| 67 | + a.Equal("100%", properties.Get("Progress")) |
| 68 | + a.Equal("123456789012", properties.Get("OwnerID")) |
| 69 | + a.Equal("amazon", properties.Get("OwnerAlias")) |
| 70 | + a.Equal("true", properties.Get("Encrypted")) |
| 71 | + a.Equal("arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012", properties.Get("KmsKeyID")) |
| 72 | + a.Equal("12345678-1234-1234-1234-123456789012", properties.Get("DataEncryptionKeyID")) |
| 73 | + a.Equal("standard", properties.Get("StorageTier")) |
| 74 | + a.Equal("production", properties.Get("tag:Environment")) |
| 75 | + a.Equal("daily", properties.Get("tag:Backup")) |
| 76 | +} |
| 77 | + |
| 78 | +func Test_EC2Snapshot_Properties_EmptyTags(t *testing.T) { |
| 79 | + a := assert.New(t) |
| 80 | + |
| 81 | + startTime := time.Now() |
| 82 | + state := ec2types.SnapshotStatePending |
| 83 | + |
| 84 | + ec2Snapshot := EC2Snapshot{ |
| 85 | + SnapshotID: ptr.String("snap-1234567890abcdef0"), |
| 86 | + Description: ptr.String("Automated backup"), |
| 87 | + VolumeID: ptr.String("vol-1234567890abcdef0"), |
| 88 | + VolumeSize: ptr.Int32(50), |
| 89 | + State: &state, |
| 90 | + StartTime: &startTime, |
| 91 | + Progress: ptr.String("50%"), |
| 92 | + OwnerID: ptr.String("123456789012"), |
| 93 | + Encrypted: ptr.Bool(false), |
| 94 | + Tags: &[]ec2types.Tag{}, |
| 95 | + } |
| 96 | + |
| 97 | + properties := ec2Snapshot.Properties() |
| 98 | + |
| 99 | + a.Equal("snap-1234567890abcdef0", properties.Get("SnapshotID")) |
| 100 | + a.Equal("Automated backup", properties.Get("Description")) |
| 101 | + a.Equal("vol-1234567890abcdef0", properties.Get("VolumeID")) |
| 102 | + a.Equal("50", properties.Get("VolumeSize")) |
| 103 | + a.Equal("pending", properties.Get("State")) |
| 104 | + a.Equal("50%", properties.Get("Progress")) |
| 105 | + a.Equal("123456789012", properties.Get("OwnerID")) |
| 106 | + a.Equal("false", properties.Get("Encrypted")) |
| 107 | +} |
| 108 | + |
| 109 | +func Test_EC2Snapshot_Properties_SpecialCharactersInTags(t *testing.T) { |
| 110 | + a := assert.New(t) |
| 111 | + |
| 112 | + startTime := time.Now() |
| 113 | + state := ec2types.SnapshotStateCompleted |
| 114 | + storageTier := ec2types.StorageTierArchive |
| 115 | + |
| 116 | + ec2Snapshot := EC2Snapshot{ |
| 117 | + SnapshotID: ptr.String("snap-1234567890abcdef0"), |
| 118 | + Description: ptr.String("Weekly backup"), |
| 119 | + VolumeID: ptr.String("vol-1234567890abcdef0"), |
| 120 | + VolumeSize: ptr.Int32(200), |
| 121 | + State: &state, |
| 122 | + StartTime: &startTime, |
| 123 | + Progress: ptr.String("100%"), |
| 124 | + OwnerID: ptr.String("123456789012"), |
| 125 | + Encrypted: ptr.Bool(true), |
| 126 | + StorageTier: &storageTier, |
| 127 | + Tags: &[]ec2types.Tag{ |
| 128 | + { |
| 129 | + Key: aws.String("Environment:Stage"), |
| 130 | + Value: aws.String("prod/staging"), |
| 131 | + }, |
| 132 | + { |
| 133 | + Key: aws.String("Backup-Schedule"), |
| 134 | + Value: aws.String("weekly/monthly"), |
| 135 | + }, |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + properties := ec2Snapshot.Properties() |
| 140 | + |
| 141 | + a.Equal("snap-1234567890abcdef0", properties.Get("SnapshotID")) |
| 142 | + a.Equal("Weekly backup", properties.Get("Description")) |
| 143 | + a.Equal("vol-1234567890abcdef0", properties.Get("VolumeID")) |
| 144 | + a.Equal("200", properties.Get("VolumeSize")) |
| 145 | + a.Equal("completed", properties.Get("State")) |
| 146 | + a.Equal("100%", properties.Get("Progress")) |
| 147 | + a.Equal("123456789012", properties.Get("OwnerID")) |
| 148 | + a.Equal("true", properties.Get("Encrypted")) |
| 149 | + a.Equal("archive", properties.Get("StorageTier")) |
| 150 | + a.Equal("prod/staging", properties.Get("tag:Environment:Stage")) |
| 151 | + a.Equal("weekly/monthly", properties.Get("tag:Backup-Schedule")) |
| 152 | +} |
0 commit comments