mirror of https://github.com/hashicorp/packer
parent
cb729e5a38
commit
11ef06b94d
@ -0,0 +1,65 @@
|
||||
package chroot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/Azure/go-autorest/autorest/azure"
|
||||
"github.com/Azure/go-autorest/autorest/to"
|
||||
|
||||
"github.com/hashicorp/packer/builder/azure/common/client"
|
||||
"github.com/hashicorp/packer/helper/multistep"
|
||||
"github.com/hashicorp/packer/packer"
|
||||
)
|
||||
|
||||
type StepVerifySourceDisk struct {
|
||||
SubscriptionID string
|
||||
SourceDiskResourceID string
|
||||
Location string
|
||||
}
|
||||
|
||||
func (s StepVerifySourceDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
||||
azcli := state.Get("azureclient").(client.AzureClientSet)
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
|
||||
ui.Say("Checking source disk location")
|
||||
resource, err := azure.ParseResourceID(s.SourceDiskResourceID)
|
||||
if err != nil {
|
||||
ui.Error(fmt.Sprintf("Could not parse resource id %q: %s", s.SourceDiskResourceID, err))
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
if !strings.EqualFold(resource.SubscriptionID, s.SubscriptionID) {
|
||||
ui.Error(fmt.Sprintf("Source disk resource %q is in a different subscription than this VM (%q). "+
|
||||
"Packer does not know how to handle that.",
|
||||
s.SourceDiskResourceID, s.SubscriptionID))
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
if !(strings.EqualFold(resource.Provider, "Microsoft.Compute") && strings.EqualFold(resource.ResourceType, "disks")) {
|
||||
ui.Error(fmt.Sprintf("Resource ID %q is not a managed disk resource", s.SourceDiskResourceID))
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
disk, err := azcli.DisksClient().Get(ctx,
|
||||
resource.ResourceGroup, resource.ResourceName)
|
||||
if err != nil {
|
||||
ui.Error(fmt.Sprintf("Unable to retrieve disk (%q): %s", s.SourceDiskResourceID, err))
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
location := to.String(disk.Location)
|
||||
if !strings.EqualFold(location, s.Location) {
|
||||
ui.Error(fmt.Sprintf("Source disk resource %q is in a different location (%q) than this VM (%q). "+
|
||||
"Packer does not know how to handle that.",
|
||||
s.SourceDiskResourceID,
|
||||
location,
|
||||
s.Location))
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
return multistep.ActionContinue
|
||||
}
|
||||
|
||||
func (s StepVerifySourceDisk) Cleanup(state multistep.StateBag) {}
|
||||
@ -0,0 +1,146 @@
|
||||
package chroot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/profiles/latest/compute/mgmt/compute"
|
||||
"github.com/Azure/go-autorest/autorest"
|
||||
"github.com/hashicorp/packer/builder/azure/common/client"
|
||||
"github.com/hashicorp/packer/helper/multistep"
|
||||
"github.com/hashicorp/packer/packer"
|
||||
)
|
||||
|
||||
func Test_StepVerifySourceDisk_Run(t *testing.T) {
|
||||
type fields struct {
|
||||
SubscriptionID string
|
||||
SourceDiskResourceID string
|
||||
Location string
|
||||
|
||||
GetDiskResponseCode int
|
||||
GetDiskResponseBody string
|
||||
}
|
||||
type args struct {
|
||||
state multistep.StateBag
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want multistep.StepAction
|
||||
errormatch string
|
||||
}{
|
||||
{
|
||||
name: "HappyPath",
|
||||
fields: fields{
|
||||
SubscriptionID: "subid1",
|
||||
SourceDiskResourceID: "/subscriptions/subid1/resourcegroups/rg1/providers/Microsoft.Compute/disks/disk1",
|
||||
Location: "westus2",
|
||||
|
||||
GetDiskResponseCode: 200,
|
||||
GetDiskResponseBody: `{"location":"westus2"}`,
|
||||
},
|
||||
want: multistep.ActionContinue,
|
||||
},
|
||||
{
|
||||
name: "DiskNotFound",
|
||||
fields: fields{
|
||||
SubscriptionID: "subid1",
|
||||
SourceDiskResourceID: "/subscriptions/subid1/resourcegroups/rg1/providers/Microsoft.Compute/disks/disk1",
|
||||
Location: "westus2",
|
||||
|
||||
GetDiskResponseCode: 404,
|
||||
GetDiskResponseBody: `{}`,
|
||||
},
|
||||
want: multistep.ActionHalt,
|
||||
errormatch: "Unable to retrieve",
|
||||
},
|
||||
{
|
||||
name: "NotADisk",
|
||||
fields: fields{
|
||||
SubscriptionID: "subid1",
|
||||
SourceDiskResourceID: "/subscriptions/subid1/resourcegroups/rg1/providers/Microsoft.Compute/images/image1",
|
||||
Location: "westus2",
|
||||
|
||||
GetDiskResponseCode: 404,
|
||||
},
|
||||
want: multistep.ActionHalt,
|
||||
errormatch: "not a managed disk",
|
||||
},
|
||||
{
|
||||
name: "OtherSubscription",
|
||||
fields: fields{
|
||||
SubscriptionID: "subid1",
|
||||
SourceDiskResourceID: "/subscriptions/subid2/resourcegroups/rg1/providers/Microsoft.Compute/disks/disk1",
|
||||
Location: "westus2",
|
||||
|
||||
GetDiskResponseCode: 200,
|
||||
GetDiskResponseBody: `{"location":"westus2"}`,
|
||||
},
|
||||
want: multistep.ActionHalt,
|
||||
errormatch: "different subscription",
|
||||
},
|
||||
{
|
||||
name: "OtherLocation",
|
||||
fields: fields{
|
||||
SubscriptionID: "subid1",
|
||||
SourceDiskResourceID: "/subscriptions/subid1/resourcegroups/rg1/providers/Microsoft.Compute/disks/disk1",
|
||||
Location: "eastus",
|
||||
|
||||
GetDiskResponseCode: 200,
|
||||
GetDiskResponseBody: `{"location":"westus2"}`,
|
||||
},
|
||||
want: multistep.ActionHalt,
|
||||
errormatch: "different location",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := StepVerifySourceDisk{
|
||||
SubscriptionID: tt.fields.SubscriptionID,
|
||||
SourceDiskResourceID: tt.fields.SourceDiskResourceID,
|
||||
Location: tt.fields.Location,
|
||||
}
|
||||
|
||||
m := compute.NewDisksClient("subscriptionId")
|
||||
m.Sender = autorest.SenderFunc(func(r *http.Request) (*http.Response, error) {
|
||||
return &http.Response{
|
||||
Request: r,
|
||||
Body: ioutil.NopCloser(strings.NewReader(tt.fields.GetDiskResponseBody)),
|
||||
StatusCode: tt.fields.GetDiskResponseCode,
|
||||
}, nil
|
||||
})
|
||||
errorBuffer := &strings.Builder{}
|
||||
ui := &packer.BasicUi{
|
||||
Reader: strings.NewReader(""),
|
||||
Writer: ioutil.Discard,
|
||||
ErrorWriter: errorBuffer,
|
||||
}
|
||||
|
||||
state := new(multistep.BasicStateBag)
|
||||
state.Put("azureclient", &client.AzureClientSetMock{
|
||||
DisksClientMock: m,
|
||||
})
|
||||
state.Put("ui", ui)
|
||||
|
||||
if got := s.Run(context.TODO(), state); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("StepVerifySourceDisk.Run() = %v, want %v", got, tt.want)
|
||||
}
|
||||
if tt.errormatch != "" {
|
||||
if !regexp.MustCompile(tt.errormatch).MatchString(errorBuffer.String()) {
|
||||
t.Errorf("Expected the error output (%q) to match %q", errorBuffer.String(), tt.errormatch)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type uiThatRemebersErrors struct {
|
||||
packer.Ui
|
||||
LastError string
|
||||
}
|
||||
Loading…
Reference in new issue