packer-cn/builder/azure/chroot/step_create_new_diskset_tes...

142 lines
4.0 KiB
Go
Raw Normal View History

2019-09-10 08:48:55 -04:00
package chroot
import (
"context"
"io/ioutil"
"net/http"
"reflect"
2019-09-10 08:48:55 -04:00
"regexp"
"testing"
"github.com/hashicorp/packer/builder/azure/common/client"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
2020-04-08 17:05:26 -04:00
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute"
2020-04-08 17:05:26 -04:00
"github.com/Azure/go-autorest/autorest"
2019-09-10 08:48:55 -04:00
)
func TestStepCreateNewDisk_Run(t *testing.T) {
type fields struct {
2020-03-25 17:55:37 -04:00
ResourceID string
DiskSizeGB int32
DiskStorageAccountType string
HyperVGeneration string
Location string
PlatformImage *client.PlatformImage
SourceDiskResourceID string
expectedPutDiskBody string
2019-09-10 08:48:55 -04:00
}
tests := []struct {
name string
fields fields
want multistep.StepAction
}{
{
2020-04-08 17:05:26 -04:00
name: "from disk",
fields: fields{
2020-03-25 17:55:37 -04:00
ResourceID: "/subscriptions/SubscriptionID/resourcegroups/ResourceGroupName/providers/Microsoft.Compute/disks/TemporaryOSDiskName",
DiskSizeGB: 42,
DiskStorageAccountType: string(compute.PremiumLRS),
HyperVGeneration: string(compute.V1),
Location: "westus",
SourceDiskResourceID: "SourceDisk",
expectedPutDiskBody: `
{
"location": "westus",
"properties": {
"osType": "Linux",
"hyperVGeneration": "V1",
"creationData": {
"createOption": "Copy",
"sourceResourceId": "SourceDisk"
},
"diskSizeGB": 42
},
"sku": {
"name": "Premium_LRS"
}
}`,
},
want: multistep.ActionContinue,
},
{
2020-04-08 17:05:26 -04:00
name: "from image",
fields: fields{
2020-03-25 17:55:37 -04:00
ResourceID: "/subscriptions/SubscriptionID/resourcegroups/ResourceGroupName/providers/Microsoft.Compute/disks/TemporaryOSDiskName",
DiskStorageAccountType: string(compute.StandardLRS),
HyperVGeneration: string(compute.V1),
Location: "westus",
PlatformImage: &client.PlatformImage{
Publisher: "Microsoft",
Offer: "Windows",
Sku: "2016-DataCenter",
Version: "2016.1.4",
},
2019-09-10 08:48:55 -04:00
expectedPutDiskBody: `
{
"location": "westus",
"properties": {
"osType": "Linux",
"hyperVGeneration": "V1",
"creationData": {
"createOption":"FromImage",
"imageReference": {
"id":"/subscriptions/SubscriptionID/providers/Microsoft.Compute/locations/westus/publishers/Microsoft/artifacttypes/vmimage/offers/Windows/skus/2016-DataCenter/versions/2016.1.4"
}
}
},
"sku": {
"name": "Standard_LRS"
}
}`,
},
want: multistep.ActionContinue,
2019-09-10 08:48:55 -04:00
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := StepCreateNewDiskset{
OSDiskID: tt.fields.ResourceID,
OSDiskSizeGB: tt.fields.DiskSizeGB,
OSDiskStorageAccountType: tt.fields.DiskStorageAccountType,
HyperVGeneration: tt.fields.HyperVGeneration,
Location: tt.fields.Location,
SourcePlatformImage: tt.fields.PlatformImage,
SourceOSDiskResourceID: tt.fields.SourceDiskResourceID,
}
2019-09-10 08:48:55 -04:00
expectedPutDiskBody := regexp.MustCompile(`[\s\n]`).ReplaceAllString(tt.fields.expectedPutDiskBody, "")
2019-09-10 08:48:55 -04:00
m := compute.NewDisksClient("subscriptionId")
m.Sender = autorest.SenderFunc(func(r *http.Request) (*http.Response, error) {
if r.Method != "PUT" {
t.Fatal("Expected only a PUT disk call")
}
b, _ := ioutil.ReadAll(r.Body)
if string(b) != expectedPutDiskBody {
t.Fatalf("expected body to be %q, but got %q", expectedPutDiskBody, string(b))
}
return &http.Response{
Request: r,
StatusCode: 200,
}, nil
})
2019-09-10 08:48:55 -04:00
state := new(multistep.BasicStateBag)
state.Put("azureclient", &client.AzureClientSetMock{
SubscriptionIDMock: "SubscriptionID",
DisksClientMock: m,
})
state.Put("ui", packer.TestUi(t))
2019-09-10 08:48:55 -04:00
if got := s.Run(context.TODO(), state); !reflect.DeepEqual(got, tt.want) {
t.Errorf("StepCreateNewDisk.Run() = %v, want %v", got, tt.want)
}
})
2019-09-10 08:48:55 -04:00
}
}