diff --git a/builder/azure/chroot/builder_test.go b/builder/azure/chroot/builder_test.go index 40156bf1f..dd40f7737 100644 --- a/builder/azure/chroot/builder_test.go +++ b/builder/azure/chroot/builder_test.go @@ -1,58 +1,53 @@ package chroot import ( - "reflect" - "regexp" "testing" - "github.com/hashicorp/packer/packer" + "github.com/Azure/azure-sdk-for-go/profiles/latest/compute/mgmt/compute" ) -func TestBuilder_Prepare_DiskAsInput(t *testing.T) { - b := Builder{} - _, err := b.Prepare(map[string]interface{}{ - "source": "/subscriptions/28279221-ccbe-40f0-b70b-4d78ab822e09/resourceGroups/testrg/providers/Microsoft.Compute/disks/diskname", - }) - - if err != nil { - // make sure there is no error about the source field - errs, ok := err.(*packer.MultiError) - if !ok { - t.Error("Expected the returned error to be of type packer.MultiError") - } - for _, err := range errs.Errors { - if matched, _ := regexp.MatchString(`(^|\W)source\W`, err.Error()); matched { - t.Errorf("Did not expect an error about the 'source' field, but found %q", err) - } - } - } -} - func TestBuilder_Prepare(t *testing.T) { type config map[string]interface{} - + type regexMatchers map[string]string // map of regex : error message + tests := []struct { name string config config - want []string validate func(Config) wantErr bool }{ { - name: "HappyPath", + name: "HappyPathFromPlatformImage", config: config{ "client_id": "123", "client_secret": "456", "subscription_id": "789", - "resource_group": "rgname", "image_resource_id": "/subscriptions/789/resourceGroups/otherrgname/providers/Microsoft.Compute/images/MyDebianOSImage-{{timestamp}}", "source": "credativ:Debian:9:latest", }, - wantErr: false, - validate: func(c Config){ - if(c.OSDiskSizeGB!=0){ - t.Fatalf("Expected OSDiskSizeGB to be 0, was %+v", c.OSDiskSizeGB) + validate: func(c Config) { + if c.OSDiskSizeGB != 0 { + t.Errorf("Expected OSDiskSizeGB to be 0, was %+v", c.OSDiskSizeGB) } + if c.MountPartition != "1" { + t.Errorf("Expected MountPartition to be %s, but found %s", "1", c.MountPartition) + } + if c.OSDiskStorageAccountType != string(compute.PremiumLRS) { + t.Errorf("Expected OSDiskStorageAccountType to be %s, but found %s", string(compute.PremiumLRS), c.OSDiskStorageAccountType) + } + if c.OSDiskCacheType != string(compute.CachingTypesReadOnly) { + t.Errorf("Expected OSDiskCacheType to be %s, but found %s", string(compute.CachingTypesReadOnly), c.OSDiskCacheType) + } + if c.ImageHyperVGeneration != string(compute.V1) { + t.Errorf("Expected ImageHyperVGeneration to be %s, but found %s", string(compute.V1), c.ImageHyperVGeneration) + } + }, + }, + { + name: "HappyPathFromPlatformImage", + config: config{ + "image_resource_id": "/subscriptions/789/resourceGroups/otherrgname/providers/Microsoft.Compute/images/MyDebianOSImage-{{timestamp}}", + "source": "/subscriptions/789/resourceGroups/testrg/providers/Microsoft.Compute/disks/diskname", }, }, } @@ -60,13 +55,15 @@ func TestBuilder_Prepare(t *testing.T) { t.Run(tt.name, func(t *testing.T) { b := &Builder{} - got, err := b.Prepare(tt.config) + _, err := b.Prepare(tt.config) + if (err != nil) != tt.wantErr { t.Errorf("Builder.Prepare() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("Builder.Prepare() = %v, want %v", got, tt.want) + + if tt.validate != nil { + tt.validate(b.config) } }) } diff --git a/examples/azure/debian-chroot.json b/examples/azure/debian-chroot.json index 4b29632b1..00909a55f 100644 --- a/examples/azure/debian-chroot.json +++ b/examples/azure/debian-chroot.json @@ -2,7 +2,8 @@ "variables": { "client_id": "{{env `ARM_CLIENT_ID`}}", "client_secret": "{{env `ARM_CLIENT_SECRET`}}", - "subscription_id": "{{env `ARM_SUBSCRIPTION_ID`}}" + "subscription_id": "{{env `ARM_SUBSCRIPTION_ID`}}", + "resource_group": "{{env `ARM_IMAGE_RESOURCEGROUP_ID`}}" }, "builders": [{ "type": "azure-chroot", @@ -23,4 +24,4 @@ "inline_shebang": "/bin/sh -x", "type": "shell" }] -} +} \ No newline at end of file diff --git a/website/source/docs/builders/azure-chroot.html.md.erb b/website/source/docs/builders/azure-chroot.html.md.erb index 71b488deb..7244671a7 100644 --- a/website/source/docs/builders/azure-chroot.html.md.erb +++ b/website/source/docs/builders/azure-chroot.html.md.erb @@ -104,18 +104,19 @@ mounts `/prod` and `/dev`: ## Example Here is an example that creates a Debian image with updated packages. Specify all environment variables (`ARM_CLIENT_ID`, `ARM_CLIENT_SECRET`, -`ARM_SUBSCRIPTION_ID`) to use a service principal, specify only `ARM_SUBSCRIPTION_ID` -to use interactive login or leave them empty to use the system-assigned identity -of the VM you run Packer on. +`ARM_SUBSCRIPTION_ID`) to use a service principal. The identity you choose should have permission to create disks and images and also to update your VM. +Set the `ARM_IMAGE_RESOURCEGROUP_ID` variable to an existing resource group in the +subscription where the resulting image will be created. ``` json { "variables": { "client_id": "{{env `ARM_CLIENT_ID`}}", "client_secret": "{{env `ARM_CLIENT_SECRET`}}", - "subscription_id": "{{env `ARM_SUBSCRIPTION_ID`}}" + "subscription_id": "{{env `ARM_SUBSCRIPTION_ID`}}", + "resource_group": "{{env `ARM_IMAGE_RESOURCEGROUP_ID`}}" }, "builders": [{ "type": "azure-chroot",