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

158 lines
4.0 KiB
Go
Raw Normal View History

2019-09-26 18:17:07 -04:00
package chroot
import (
"context"
"io/ioutil"
"net/http"
"reflect"
"regexp"
"strings"
"testing"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute"
2019-09-26 18:17:07 -04:00
"github.com/Azure/go-autorest/autorest"
2020-12-17 16:29:25 -05:00
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
2019-09-26 18:17:07 -04:00
"github.com/hashicorp/packer/builder/azure/common/client"
)
func Test_StepVerifySourceDisk_Run(t *testing.T) {
type fields struct {
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{
SourceDiskResourceID: "/subscriptions/subid1/resourcegroups/rg1/providers/Microsoft.Compute/disks/disk1",
Location: "westus2",
GetDiskResponseCode: 200,
GetDiskResponseBody: `{"location":"westus2"}`,
},
want: multistep.ActionContinue,
},
2019-10-04 12:59:41 -04:00
{
name: "NotAResourceID",
fields: fields{
SourceDiskResourceID: "/other",
Location: "westus2",
GetDiskResponseCode: 200,
GetDiskResponseBody: `{"location":"westus2"}`,
},
2019-10-04 15:00:22 -04:00
want: multistep.ActionHalt,
2019-10-04 12:59:41 -04:00
errormatch: "Could not parse resource id",
},
2019-09-26 18:17:07 -04:00
{
name: "DiskNotFound",
fields: fields{
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{
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{
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{
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{
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
})
2019-10-04 12:59:41 -04:00
2020-03-25 14:08:09 -04:00
ui, getErr := testUI()
2019-09-26 18:17:07 -04:00
state := new(multistep.BasicStateBag)
state.Put("azureclient", &client.AzureClientSetMock{
2020-03-25 16:11:51 -04:00
DisksClientMock: m,
SubscriptionIDMock: "subid1",
2019-09-26 18:17:07 -04:00
})
state.Put("ui", ui)
2019-10-04 15:00:22 -04:00
got := s.Run(context.TODO(), state)
2019-10-04 12:59:41 -04:00
if !reflect.DeepEqual(got, tt.want) {
2019-09-26 18:17:07 -04:00
t.Errorf("StepVerifySourceDisk.Run() = %v, want %v", got, tt.want)
}
2019-10-04 12:59:41 -04:00
2019-09-26 18:17:07 -04:00
if tt.errormatch != "" {
2020-03-25 14:08:09 -04:00
errs := getErr()
if !regexp.MustCompile(tt.errormatch).MatchString(errs) {
t.Errorf("Expected the error output (%q) to match %q", errs, tt.errormatch)
2019-09-26 18:17:07 -04:00
}
}
2019-10-04 15:00:22 -04:00
2019-10-04 12:59:41 -04:00
if got == multistep.ActionHalt {
if _, ok := state.GetOk("error"); !ok {
t.Fatal("Expected 'error' to be set in statebag after failure")
}
}
2019-09-26 18:17:07 -04:00
})
}
}
type uiThatRemebersErrors struct {
packersdk.Ui
2019-09-26 18:17:07 -04:00
LastError string
}