mirror of https://github.com/hashicorp/packer
commit
17f7a973d6
@ -0,0 +1,32 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
builderT "github.com/hashicorp/packer/helper/builder/testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBuilderAcc_basic(t *testing.T) {
|
||||||
|
builderT.Test(t, builderT.TestCase{
|
||||||
|
Builder: &Builder{},
|
||||||
|
Template: testBuilderAccBasic,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const testBuilderAccBasic = `
|
||||||
|
{
|
||||||
|
"builders": [{
|
||||||
|
"type": "test",
|
||||||
|
|
||||||
|
"url": "https://vcenter.vsphere5.test/sdk",
|
||||||
|
"username": "root",
|
||||||
|
"password": "jetbrains",
|
||||||
|
|
||||||
|
"template": "basic",
|
||||||
|
"vm_name": "test1",
|
||||||
|
"host": "esxi-1.vsphere5.test",
|
||||||
|
|
||||||
|
"ssh_username": "jetbrains",
|
||||||
|
"ssh_password": "jetbrains"
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
`
|
||||||
@ -0,0 +1,14 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/hashicorp/packer/packer"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBuilder_ImplementsBuilder(t *testing.T) {
|
||||||
|
var raw interface{}
|
||||||
|
raw = &Builder{}
|
||||||
|
if _, ok := raw.(packer.Builder); !ok {
|
||||||
|
t.Fatalf("Builder should be a builder")
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,67 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMinimalConfig(t *testing.T) {
|
||||||
|
_, warns, errs := NewConfig(minimalConfig())
|
||||||
|
|
||||||
|
testConfigOk(t, warns, errs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInvalidCpu(t *testing.T) {
|
||||||
|
raw := minimalConfig()
|
||||||
|
raw["cpus"] = "string"
|
||||||
|
_, warns, errs := NewConfig(raw)
|
||||||
|
testConfigErr(t, warns, errs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInvalidRam(t *testing.T) {
|
||||||
|
raw := minimalConfig()
|
||||||
|
raw["RAM"] = "string"
|
||||||
|
_, warns, errs := NewConfig(raw)
|
||||||
|
testConfigErr(t, warns, errs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTimeout(t *testing.T) {
|
||||||
|
raw := minimalConfig()
|
||||||
|
raw["shutdown_timeout"] = "3m"
|
||||||
|
conf, warns, err := NewConfig(raw)
|
||||||
|
testConfigOk(t, warns, err)
|
||||||
|
if conf.ShutdownTimeout != 3 * time.Minute {
|
||||||
|
t.Fatalf("shutdown_timeout sourld equal 3 minutes")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func minimalConfig() map[string]interface{} {
|
||||||
|
return map[string]interface{}{
|
||||||
|
"url": "https://vcenter.domain.local/sdk",
|
||||||
|
"username": "root",
|
||||||
|
"password": "vmware",
|
||||||
|
"template": "ubuntu",
|
||||||
|
"vm_name": "vm1",
|
||||||
|
"host": "esxi1.domain.local",
|
||||||
|
"ssh_username": "root",
|
||||||
|
"ssh_password": "secret",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testConfigOk(t *testing.T, warns []string, err error) {
|
||||||
|
if len(warns) > 0 {
|
||||||
|
t.Fatalf("bad: %#v", warns)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("bad: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testConfigErr(t *testing.T, warns []string, err error) {
|
||||||
|
if len(warns) > 0 {
|
||||||
|
t.Fatalf("bad: %#v", warns)
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("should error")
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,73 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/mitchellh/multistep"
|
|
||||||
"github.com/hashicorp/packer/packer"
|
|
||||||
"github.com/vmware/govmomi/find"
|
|
||||||
"fmt"
|
|
||||||
"github.com/vmware/govmomi"
|
|
||||||
"context"
|
|
||||||
"net/url"
|
|
||||||
)
|
|
||||||
|
|
||||||
type StepSetup struct{
|
|
||||||
config *Config
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StepSetup) Run(state multistep.StateBag) multistep.StepAction {
|
|
||||||
ui := state.Get("ui").(packer.Ui)
|
|
||||||
ui.Say("setup...")
|
|
||||||
|
|
||||||
// Prepare entities: client (authentification), finder, folder, virtual machine
|
|
||||||
client, ctx, err := createClient(s.config.Url, s.config.Username, s.config.Password)
|
|
||||||
if err != nil {
|
|
||||||
state.Put("error", err)
|
|
||||||
return multistep.ActionHalt
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up finder
|
|
||||||
finder := find.NewFinder(client.Client, false)
|
|
||||||
dc, err := finder.DatacenterOrDefault(ctx, s.config.DCName)
|
|
||||||
if err != nil {
|
|
||||||
state.Put("error", err)
|
|
||||||
return multistep.ActionHalt
|
|
||||||
}
|
|
||||||
finder.SetDatacenter(dc)
|
|
||||||
|
|
||||||
// Get source VM
|
|
||||||
vmSrc, err := finder.VirtualMachine(ctx, s.config.Template)
|
|
||||||
if err != nil {
|
|
||||||
state.Put("error", err)
|
|
||||||
return multistep.ActionHalt
|
|
||||||
}
|
|
||||||
|
|
||||||
state.Put("client", client)
|
|
||||||
state.Put("ctx", ctx)
|
|
||||||
state.Put("finder", finder)
|
|
||||||
state.Put("dc", dc)
|
|
||||||
state.Put("vmSrc", vmSrc)
|
|
||||||
return multistep.ActionContinue
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StepSetup) Cleanup(state multistep.StateBag) {}
|
|
||||||
|
|
||||||
func createClient(URL, username, password string) (*govmomi.Client, context.Context, error) {
|
|
||||||
// create context
|
|
||||||
ctx := context.TODO() // an empty, default context (for those, who is unsure)
|
|
||||||
|
|
||||||
// create a client
|
|
||||||
// (connected to the specified URL,
|
|
||||||
// logged in with the username-password)
|
|
||||||
u, err := url.Parse(URL) // create a URL object from string
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
u.User = url.UserPassword(username, password) // set username and password for automatical authentification
|
|
||||||
fmt.Println(u.String())
|
|
||||||
client, err := govmomi.NewClient(ctx, u,true) // creating a client (logs in with given uname&pswd)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return client, ctx, nil
|
|
||||||
}
|
|
||||||
Loading…
Reference in new issue