test, document, cleanup puppet guest codde

This commit is contained in:
Matthew Hooker 2017-10-03 11:39:33 -07:00
parent 7523cc76de
commit bafcf7dfb1
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
3 changed files with 151 additions and 16 deletions

View File

@ -79,12 +79,14 @@ var guestOSTypeConfigs = map[string]guestOSTypeConfig{
provisioner.UnixOSType: {
stagingDir: "/tmp/packer-puppet-masterless",
executeCommand: "cd {{.WorkingDir}} && " +
"{{.FacterVars}} {{if .Sudo}} sudo -E {{end}}" +
"{{if ne .PuppetBinDir \"\"}}{{.PuppetBinDir}}/{{end}}puppet apply --verbose --modulepath='{{.ModulePath}}' " +
"{{if ne .HieraConfigPath \"\"}}--hiera_config='{{.HieraConfigPath}}' {{end}}" +
"{{if ne .ManifestDir \"\"}}--manifestdir='{{.ManifestDir}}' {{end}}" +
`{{if ne .FacterVars ""}}{{.FacterVars}} {{end}}` +
"{{if .Sudo}}sudo -E {{end}}" +
`{{if ne .PuppetBinDir ""}}{{.PuppetBinDir}}/{{end}}` +
`puppet apply --verbose --modulepath='{{.ModulePath}}' ` +
`{{if ne .HieraConfigPath ""}}--hiera_config='{{.HieraConfigPath}}' {{end}}` +
`{{if ne .ManifestDir ""}}--manifestdir='{{.ManifestDir}}' {{end}}` +
"--detailed-exitcodes " +
"{{if ne .ExtraArguments \"\"}}{{.ExtraArguments}} {{end}}" +
`{{if ne .ExtraArguments ""}}{{.ExtraArguments}} {{end}}` +
"{{.ManifestFile}}",
facterVarsFmt: "FACTER_%s='%s'",
facterVarsJoiner: " ",
@ -94,13 +96,14 @@ var guestOSTypeConfigs = map[string]guestOSTypeConfig{
stagingDir: "C:/Windows/Temp/packer-puppet-masterless",
executeCommand: "cd {{.WorkingDir}} && " +
"{{.FacterVars}} && " +
"{{if ne .PuppetBinDir \"\"}}{{.PuppetBinDir}}/{{end}}puppet apply --verbose --modulepath='{{.ModulePath}}' " +
"{{if ne .HieraConfigPath \"\"}}--hiera_config='{{.HieraConfigPath}}' {{end}}" +
"{{if ne .ManifestDir \"\"}}--manifestdir='{{.ManifestDir}}' {{end}}" +
`{{if ne .PuppetBinDir ""}}{{.PuppetBinDir}}/{{end}}` +
`puppet apply --verbose --modulepath='{{.ModulePath}}' ` +
`{{if ne .HieraConfigPath ""}}--hiera_config='{{.HieraConfigPath}}' {{end}}` +
`{{if ne .ManifestDir ""}}--manifestdir='{{.ManifestDir}}' {{end}}` +
"--detailed-exitcodes " +
"{{if ne .ExtraArguments \"\"}}{{.ExtraArguments}} {{end}}" +
`{{if ne .ExtraArguments ""}}{{.ExtraArguments}} {{end}}` +
"{{.ManifestFile}}",
facterVarsFmt: "SET \"FACTER_%s=%s\"",
facterVarsFmt: `SET "FACTER_%s=%s"`,
facterVarsJoiner: " & ",
modulePathJoiner: ";",
},

View File

@ -1,12 +1,16 @@
package puppetmasterless
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"testing"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"github.com/stretchr/testify/assert"
)
func testConfig() map[string]interface{} {
@ -28,6 +32,131 @@ func TestProvisioner_Impl(t *testing.T) {
}
}
func TestGuestOSConfig_empty_unix(t *testing.T) {
config := testConfig()
p := new(Provisioner)
err := p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
// Execute Puppet
p.config.ctx.Data = &ExecuteTemplate{
ManifestFile: "/r/m/f",
PuppetBinDir: p.config.PuppetBinDir,
Sudo: !p.config.PreventSudo,
WorkingDir: p.config.WorkingDir,
}
log.Println(p.config.ExecuteCommand)
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
if err != nil {
t.Fatalf("err: %s", err)
}
expected := "cd /tmp/packer-puppet-masterless && " +
"sudo -E puppet apply --verbose --modulepath='' --detailed-exitcodes /r/m/f"
assert.Equal(t, expected, command)
}
func TestGuestOSConfig_full_unix(t *testing.T) {
config := testConfig()
p := new(Provisioner)
err := p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
facterVars := []string{
fmt.Sprintf(p.guestOSTypeConfig.facterVarsFmt, "lhs", "rhs"),
fmt.Sprintf(p.guestOSTypeConfig.facterVarsFmt, "foo", "bar"),
}
modulePaths := []string{"/m/p", "/a/b"}
// Execute Puppet
p.config.ctx.Data = &ExecuteTemplate{
FacterVars: strings.Join(facterVars, p.guestOSTypeConfig.facterVarsJoiner),
HieraConfigPath: "/h/c/p",
ManifestDir: "/r/m/d",
ManifestFile: "/r/m/f",
ModulePath: strings.Join(modulePaths, p.guestOSTypeConfig.modulePathJoiner),
PuppetBinDir: p.config.PuppetBinDir,
Sudo: !p.config.PreventSudo,
WorkingDir: p.config.WorkingDir,
ExtraArguments: strings.Join(p.config.ExtraArguments, " "),
}
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
if err != nil {
t.Fatalf("err: %s", err)
}
expected := "cd /tmp/packer-puppet-masterless && FACTER_lhs='rhs' FACTER_foo='bar' " +
"sudo -E puppet apply " +
"--verbose --modulepath='/m/p:/a/b' --hiera_config='/h/c/p' " +
"--manifestdir='/r/m/d' --detailed-exitcodes /r/m/f"
assert.Equal(t, expected, command)
}
func TestGuestOSConfig_empty_windows(t *testing.T) {
config := testConfig()
config["guest_os_type"] = "windows"
p := new(Provisioner)
err := p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
// Execute Puppet
p.config.ctx.Data = &ExecuteTemplate{
ManifestFile: "/r/m/f",
PuppetBinDir: p.config.PuppetBinDir,
Sudo: !p.config.PreventSudo,
WorkingDir: p.config.WorkingDir,
}
log.Println(p.config.ExecuteCommand)
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
if err != nil {
t.Fatalf("err: %s", err)
}
expected := "cd C:/Windows/Temp/packer-puppet-masterless && && puppet apply --verbose --modulepath='' --detailed-exitcodes /r/m/f"
assert.Equal(t, expected, command)
}
func TestGuestOSConfig_full_windows(t *testing.T) {
config := testConfig()
config["guest_os_type"] = "windows"
p := new(Provisioner)
err := p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
facterVars := []string{
fmt.Sprintf(p.guestOSTypeConfig.facterVarsFmt, "lhs", "rhs"),
fmt.Sprintf(p.guestOSTypeConfig.facterVarsFmt, "foo", "bar"),
}
modulePaths := []string{"/m/p", "/a/b"}
// Execute Puppet
p.config.ctx.Data = &ExecuteTemplate{
FacterVars: strings.Join(facterVars, p.guestOSTypeConfig.facterVarsJoiner),
HieraConfigPath: "/h/c/p",
ManifestDir: "/r/m/d",
ManifestFile: "/r/m/f",
ModulePath: strings.Join(modulePaths, p.guestOSTypeConfig.modulePathJoiner),
PuppetBinDir: p.config.PuppetBinDir,
Sudo: !p.config.PreventSudo,
WorkingDir: p.config.WorkingDir,
ExtraArguments: strings.Join(p.config.ExtraArguments, " "),
}
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
if err != nil {
t.Fatalf("err: %s", err)
}
expected := "cd C:/Windows/Temp/packer-puppet-masterless && " +
"SET \"FACTER_lhs=rhs\" & SET \"FACTER_foo=bar\" && " +
"puppet apply --verbose --modulepath='/m/p;/a/b' --hiera_config='/h/c/p' " +
"--manifestdir='/r/m/d' --detailed-exitcodes /r/m/f"
assert.Equal(t, expected, command)
}
func TestProvisionerPrepare_puppetBinDir(t *testing.T) {
config := testConfig()

View File

@ -124,12 +124,14 @@ readability) to execute Puppet:
```
cd {{.WorkingDir}} &&
{{.FacterVars}} {{if .Sudo}} sudo -E {{end}}
{{if ne .FacterVars ""}}{{.FacterVars}} {{end}}
{{if .Sudo}}sudo -E {{end}}
{{if ne .PuppetBinDir ""}}{{.PuppetBinDir}}/{{end}}
puppet apply --verbose --modulepath='{{.ModulePath}}'
{{if ne .HieraConfigPath ""}}--hiera_config='{{.HieraConfigPath}}' {{end}}
{{if ne .HieraConfigPath ""}}--hiera_config='{{.HieraConfigPath}}' {{end}}
{{if ne .ManifestDir ""}}--manifestdir='{{.ManifestDir}}' {{end}}
--detailed-exitcodes
{{if ne .ExtraArguments ""}}{{.ExtraArguments}} {{end}}
{{if ne .ExtraArguments ""}}{{.ExtraArguments}} {{end}}
{{.ManifestFile}}
```
@ -137,12 +139,13 @@ The following command is used if guest OS type is windows:
```
cd {{.WorkingDir}} &&
{{.FacterVars}} &&
{{.FacterVars}} &&
{{if ne .PuppetBinDir ""}}{{.PuppetBinDir}}/{{end}}
puppet apply --verbose --modulepath='{{.ModulePath}}'
{{if ne .HieraConfigPath ""}}--hiera_config='{{.HieraConfigPath}}' {{end}}
{{if ne .HieraConfigPath ""}}--hiera_config='{{.HieraConfigPath}}' {{end}}
{{if ne .ManifestDir ""}}--manifestdir='{{.ManifestDir}}' {{end}}
--detailed-exitcodes
{{if ne .ExtraArguments ""}}{{.ExtraArguments}} {{end}}
{{if ne .ExtraArguments ""}}{{.ExtraArguments}} {{end}}
{{.ManifestFile}}
```