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

111 lines
2.8 KiB
Go
Raw Normal View History

2020-04-08 17:05:26 -04:00
package chroot
import (
"context"
"io/ioutil"
"net/http"
"reflect"
"regexp"
"testing"
"github.com/hashicorp/packer/builder/azure/common/client"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"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"
)
func TestStepCreateSharedImageVersion_Run(t *testing.T) {
type fields struct {
Destination SharedImageGalleryDestination
OSDiskCacheType string
Location string
}
tests := []struct {
name string
fields fields
want multistep.StepAction
expectedPutBody string
}{
{
name: "happy path",
fields: fields{
Destination: SharedImageGalleryDestination{
ResourceGroup: "ResourceGroup",
GalleryName: "GalleryName",
ImageName: "ImageName",
ImageVersion: "0.1.2",
TargetRegions: []TargetRegion{
TargetRegion{
Name: "region1",
ReplicaCount: 5,
StorageAccountType: "Standard_ZRS",
},
},
ExcludeFromLatest: true,
},
Location: "region2",
},
expectedPutBody: `{
"location": "region2",
"properties": {
"publishingProfile": {
"targetRegions": [
{
"name": "region1",
"regionalReplicaCount": 5,
"storageAccountType": "Standard_ZRS"
}
],
"excludeFromLatest": true
},
"storageProfile": {
"osDiskImage": {
"source": {
"id": "osdisksnapshotresourceid"
}
}
}
}
}`,
},
}
for _, tt := range tests {
expectedPutBody := regexp.MustCompile(`[\s\n]`).ReplaceAllString(tt.expectedPutBody, "")
m := compute.NewGalleryImageVersionsClient("subscriptionId")
m.Sender = autorest.SenderFunc(func(r *http.Request) (*http.Response, error) {
if r.Method != "PUT" {
t.Fatal("Expected only a PUT call")
}
b, _ := ioutil.ReadAll(r.Body)
if string(b) != expectedPutBody {
t.Errorf("expected body to be %v, but got %v", expectedPutBody, string(b))
}
return &http.Response{
Request: r,
StatusCode: 200,
}, nil
})
state := new(multistep.BasicStateBag)
state.Put("azureclient", &client.AzureClientSetMock{
GalleryImageVersionsClientMock: m,
})
state.Put("ui", packer.TestUi(t))
state.Put(stateBagKey_OSDiskSnapshotResourceID, "osdisksnapshotresourceid")
t.Run(tt.name, func(t *testing.T) {
s := &StepCreateSharedImageVersion{
Destination: tt.fields.Destination,
OSDiskCacheType: tt.fields.OSDiskCacheType,
Location: tt.fields.Location,
}
if got := s.Run(context.TODO(), state); !reflect.DeepEqual(got, tt.want) {
t.Errorf("StepCreateSharedImageVersion.Run() = %v, want %v", got, tt.want)
}
})
}
}