|
14 | 14 | package aws
|
15 | 15 |
|
16 | 16 | import (
|
| 17 | + "context" |
17 | 18 | "testing"
|
18 | 19 |
|
19 | 20 | awsapi "github.com/aws/aws-sdk-go/aws"
|
20 | 21 | "github.com/aws/aws-sdk-go/service/ec2"
|
| 22 | + "github.com/aws/aws-sdk-go/service/ec2/ec2iface" |
21 | 23 | "github.com/stretchr/testify/require"
|
22 | 24 | )
|
23 | 25 |
|
@@ -76,3 +78,127 @@ func TestHandleDescribeVolumesResponse(t *testing.T) {
|
76 | 78 | require.Equal(t, int64(4), createdVolumeSize)
|
77 | 79 | require.Equal(t, 1, len(unfinishedVolumes))
|
78 | 80 | }
|
| 81 | + |
| 82 | +type mockEC2 struct { |
| 83 | + ec2iface.EC2API |
| 84 | + output ec2.DescribeSnapshotsOutput |
| 85 | +} |
| 86 | + |
| 87 | +func (m mockEC2) DescribeSnapshots(*ec2.DescribeSnapshotsInput) (*ec2.DescribeSnapshotsOutput, error) { |
| 88 | + return &m.output, nil |
| 89 | +} |
| 90 | + |
| 91 | +func NewMockEc2Session(mock mockEC2) *EC2Session { |
| 92 | + return &EC2Session{ |
| 93 | + ec2: mock, |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestWaitSnapshotsCreated(t *testing.T) { |
| 98 | + snapIdMap := map[string]string{ |
| 99 | + "vol-1": "snap-1", |
| 100 | + "vol-2": "snap-2", |
| 101 | + } |
| 102 | + |
| 103 | + cases := []struct { |
| 104 | + desc string |
| 105 | + snapshotsOutput ec2.DescribeSnapshotsOutput |
| 106 | + expectedSize int64 |
| 107 | + expectErr bool |
| 108 | + expectTimeout bool |
| 109 | + }{ |
| 110 | + { |
| 111 | + desc: "snapshots are all completed", |
| 112 | + snapshotsOutput: ec2.DescribeSnapshotsOutput{ |
| 113 | + Snapshots: []*ec2.Snapshot{ |
| 114 | + { |
| 115 | + SnapshotId: awsapi.String("snap-1"), |
| 116 | + VolumeSize: awsapi.Int64(1), |
| 117 | + State: awsapi.String(ec2.SnapshotStateCompleted), |
| 118 | + }, |
| 119 | + { |
| 120 | + SnapshotId: awsapi.String("snap-2"), |
| 121 | + VolumeSize: awsapi.Int64(2), |
| 122 | + State: awsapi.String(ec2.SnapshotStateCompleted), |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + expectedSize: 3, |
| 127 | + expectErr: false, |
| 128 | + }, |
| 129 | + { |
| 130 | + desc: "snapshot failed", |
| 131 | + snapshotsOutput: ec2.DescribeSnapshotsOutput{ |
| 132 | + Snapshots: []*ec2.Snapshot{ |
| 133 | + { |
| 134 | + SnapshotId: awsapi.String("snap-1"), |
| 135 | + VolumeSize: awsapi.Int64(1), |
| 136 | + State: awsapi.String(ec2.SnapshotStateCompleted), |
| 137 | + }, |
| 138 | + { |
| 139 | + SnapshotId: awsapi.String("snap-2"), |
| 140 | + State: awsapi.String(ec2.SnapshotStateError), |
| 141 | + StateMessage: awsapi.String("snapshot failed"), |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + expectedSize: 0, |
| 146 | + expectErr: true, |
| 147 | + }, |
| 148 | + { |
| 149 | + desc: "snapshots pending", |
| 150 | + snapshotsOutput: ec2.DescribeSnapshotsOutput{ |
| 151 | + Snapshots: []*ec2.Snapshot{ |
| 152 | + { |
| 153 | + SnapshotId: awsapi.String("snap-1"), |
| 154 | + VolumeSize: awsapi.Int64(1), |
| 155 | + State: awsapi.String(ec2.SnapshotStateCompleted), |
| 156 | + }, |
| 157 | + { |
| 158 | + SnapshotId: awsapi.String("snap-2"), |
| 159 | + State: awsapi.String(ec2.SnapshotStatePending), |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + expectTimeout: true, |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + for _, c := range cases { |
| 168 | + e := NewMockEc2Session(mockEC2{ |
| 169 | + output: c.snapshotsOutput, |
| 170 | + }) |
| 171 | + |
| 172 | + if c.expectTimeout { |
| 173 | + func() { |
| 174 | + // We wait 5s before checking snapshots |
| 175 | + ctx, cancel := context.WithTimeout(context.Background(), 6) |
| 176 | + defer cancel() |
| 177 | + |
| 178 | + done := make(chan struct{}) |
| 179 | + go func() { |
| 180 | + _, _ = e.WaitSnapshotsCreated(snapIdMap, nil) |
| 181 | + done <- struct{}{} |
| 182 | + }() |
| 183 | + |
| 184 | + select { |
| 185 | + case <-done: |
| 186 | + t.Fatal("WaitSnapshotsCreated should not return before timeout") |
| 187 | + case <-ctx.Done(): |
| 188 | + require.True(t, true) |
| 189 | + } |
| 190 | + }() |
| 191 | + |
| 192 | + continue |
| 193 | + } |
| 194 | + |
| 195 | + size, err := e.WaitSnapshotsCreated(snapIdMap, nil) |
| 196 | + if c.expectErr { |
| 197 | + require.Error(t, err) |
| 198 | + } else { |
| 199 | + require.NoError(t, err) |
| 200 | + } |
| 201 | + |
| 202 | + require.Equal(t, c.expectedSize, size) |
| 203 | + } |
| 204 | +} |
0 commit comments