github.phpd.cn/hashicorp/packer@v1.3.2/builder/amazon/chroot/step_create_volume_test.go (about)

     1  package chroot
     2  
     3  import (
     4  	"github.com/aws/aws-sdk-go/aws"
     5  	"github.com/aws/aws-sdk-go/service/ec2"
     6  	"github.com/stretchr/testify/assert"
     7  	"testing"
     8  )
     9  
    10  func buildTestRootDevice() *ec2.BlockDeviceMapping {
    11  	return &ec2.BlockDeviceMapping{
    12  		Ebs: &ec2.EbsBlockDevice{
    13  			VolumeSize: aws.Int64(10),
    14  			SnapshotId: aws.String("snap-1234"),
    15  			VolumeType: aws.String("gp2"),
    16  		},
    17  	}
    18  }
    19  
    20  func TestCreateVolume_Default(t *testing.T) {
    21  	stepCreateVolume := new(StepCreateVolume)
    22  	_, err := stepCreateVolume.buildCreateVolumeInput("test-az", buildTestRootDevice())
    23  	assert.NoError(t, err)
    24  }
    25  
    26  func TestCreateVolume_Shrink(t *testing.T) {
    27  	stepCreateVolume := StepCreateVolume{RootVolumeSize: 1}
    28  	testRootDevice := buildTestRootDevice()
    29  	ret, err := stepCreateVolume.buildCreateVolumeInput("test-az", testRootDevice)
    30  	assert.NoError(t, err)
    31  	// Ensure that the new value is equal to the size of the old root device
    32  	assert.Equal(t, *ret.Size, *testRootDevice.Ebs.VolumeSize)
    33  }
    34  
    35  func TestCreateVolume_Expand(t *testing.T) {
    36  	stepCreateVolume := StepCreateVolume{RootVolumeSize: 25}
    37  	testRootDevice := buildTestRootDevice()
    38  	ret, err := stepCreateVolume.buildCreateVolumeInput("test-az", testRootDevice)
    39  	assert.NoError(t, err)
    40  	// Ensure that the new value is equal to the size of the value passed in
    41  	assert.Equal(t, *ret.Size, stepCreateVolume.RootVolumeSize)
    42  }
    43  
    44  func TestCreateVolume_io1_to_io1(t *testing.T) {
    45  	stepCreateVolume := StepCreateVolume{RootVolumeType: "io1"}
    46  	testRootDevice := buildTestRootDevice()
    47  	testRootDevice.Ebs.VolumeType = aws.String("io1")
    48  	testRootDevice.Ebs.Iops = aws.Int64(1000)
    49  	ret, err := stepCreateVolume.buildCreateVolumeInput("test-az", testRootDevice)
    50  	assert.NoError(t, err)
    51  	assert.Equal(t, *ret.VolumeType, stepCreateVolume.RootVolumeType)
    52  	assert.Equal(t, *ret.Iops, *testRootDevice.Ebs.Iops)
    53  }
    54  
    55  func TestCreateVolume_io1_to_gp2(t *testing.T) {
    56  	stepCreateVolume := StepCreateVolume{RootVolumeType: "gp2"}
    57  	testRootDevice := buildTestRootDevice()
    58  	testRootDevice.Ebs.VolumeType = aws.String("io1")
    59  	testRootDevice.Ebs.Iops = aws.Int64(1000)
    60  
    61  	ret, err := stepCreateVolume.buildCreateVolumeInput("test-az", testRootDevice)
    62  	assert.NoError(t, err)
    63  	assert.Equal(t, *ret.VolumeType, stepCreateVolume.RootVolumeType)
    64  	assert.Nil(t, ret.Iops)
    65  }
    66  
    67  func TestCreateVolume_gp2_to_io1(t *testing.T) {
    68  	stepCreateVolume := StepCreateVolume{RootVolumeType: "io1"}
    69  	testRootDevice := buildTestRootDevice()
    70  
    71  	_, err := stepCreateVolume.buildCreateVolumeInput("test-az", testRootDevice)
    72  	assert.Error(t, err)
    73  }