Update example and docs

pull/8185/head
Paul Meyer 7 years ago
parent b5c0742951
commit f72c4ec7a3

@ -1,58 +1,53 @@
package chroot package chroot
import ( import (
"reflect"
"regexp"
"testing" "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) { func TestBuilder_Prepare(t *testing.T) {
type config map[string]interface{} type config map[string]interface{}
type regexMatchers map[string]string // map of regex : error message
tests := []struct { tests := []struct {
name string name string
config config config config
want []string
validate func(Config) validate func(Config)
wantErr bool wantErr bool
}{ }{
{ {
name: "HappyPath", name: "HappyPathFromPlatformImage",
config: config{ config: config{
"client_id": "123", "client_id": "123",
"client_secret": "456", "client_secret": "456",
"subscription_id": "789", "subscription_id": "789",
"resource_group": "rgname",
"image_resource_id": "/subscriptions/789/resourceGroups/otherrgname/providers/Microsoft.Compute/images/MyDebianOSImage-{{timestamp}}", "image_resource_id": "/subscriptions/789/resourceGroups/otherrgname/providers/Microsoft.Compute/images/MyDebianOSImage-{{timestamp}}",
"source": "credativ:Debian:9:latest", "source": "credativ:Debian:9:latest",
}, },
wantErr: false, validate: func(c Config) {
validate: func(c Config){ if c.OSDiskSizeGB != 0 {
if(c.OSDiskSizeGB!=0){ t.Errorf("Expected OSDiskSizeGB to be 0, was %+v", c.OSDiskSizeGB)
t.Fatalf("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) { t.Run(tt.name, func(t *testing.T) {
b := &Builder{} b := &Builder{}
got, err := b.Prepare(tt.config) _, err := b.Prepare(tt.config)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("Builder.Prepare() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("Builder.Prepare() error = %v, wantErr %v", err, tt.wantErr)
return 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)
} }
}) })
} }

@ -2,7 +2,8 @@
"variables": { "variables": {
"client_id": "{{env `ARM_CLIENT_ID`}}", "client_id": "{{env `ARM_CLIENT_ID`}}",
"client_secret": "{{env `ARM_CLIENT_SECRET`}}", "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": [{ "builders": [{
"type": "azure-chroot", "type": "azure-chroot",
@ -23,4 +24,4 @@
"inline_shebang": "/bin/sh -x", "inline_shebang": "/bin/sh -x",
"type": "shell" "type": "shell"
}] }]
} }

@ -104,18 +104,19 @@ mounts `/prod` and `/dev`:
## Example ## Example
Here is an example that creates a Debian image with updated packages. Specify Here is an example that creates a Debian image with updated packages. Specify
all environment variables (`ARM_CLIENT_ID`, `ARM_CLIENT_SECRET`, all environment variables (`ARM_CLIENT_ID`, `ARM_CLIENT_SECRET`,
`ARM_SUBSCRIPTION_ID`) to use a service principal, specify only `ARM_SUBSCRIPTION_ID` `ARM_SUBSCRIPTION_ID`) to use a service principal.
to use interactive login or leave them empty to use the system-assigned identity
of the VM you run Packer on.
The identity you choose should have permission to create disks and images and also The identity you choose should have permission to create disks and images and also
to update your VM. 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 ``` json
{ {
"variables": { "variables": {
"client_id": "{{env `ARM_CLIENT_ID`}}", "client_id": "{{env `ARM_CLIENT_ID`}}",
"client_secret": "{{env `ARM_CLIENT_SECRET`}}", "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": [{ "builders": [{
"type": "azure-chroot", "type": "azure-chroot",

Loading…
Cancel
Save