Add basic test file for provisioner-chef-solo.

This commit is contained in:
James Van Dyke 2013-07-11 10:47:51 -04:00
parent 24c4b5334b
commit a84f26bfdf
2 changed files with 121 additions and 52 deletions

View File

@ -5,6 +5,7 @@ package chefSolo
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"github.com/mitchellh/iochan"
"github.com/mitchellh/mapstructure"
@ -13,16 +14,15 @@ import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"path/filepath"
"encoding/json"
)
const RemoteStagingPath = "/tmp/provision/chef-solo"
const RemoteFileCachePath = "/tmp/provision/chef-solo"
const RemoteCookbookPath = "/tmp/provision/chef-solo/cookbooks"
const DefaultCookbookPath = "cookbooks"
const DefaultCookbooksPath = "cookbooks"
var Ui packer.Ui
@ -50,8 +50,8 @@ type Provisioner struct {
type ExecuteRecipeTemplate struct {
SoloRbPath string
JsonPath string
Sudo bool
JsonPath string
Sudo bool
}
type ExecuteInstallChefTemplate struct {
@ -67,7 +67,8 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
}
if p.config.CookbooksPaths == nil {
p.config.CookbooksPaths = make([]string, 0)
p.config.CookbooksPaths = []string{DefaultCookbooksPath}
}
if p.config.Recipes == nil {
@ -154,7 +155,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
}
func UploadLocalDirectory(localDir string, comm packer.Communicator) (err error) {
visitPath := func (path string, f os.FileInfo, err error) (err2 error) {
visitPath := func(path string, f os.FileInfo, err error) (err2 error) {
var remotePath = RemoteCookbookPath + "/" + path
if f.IsDir() {
// Make remote directory
@ -197,7 +198,7 @@ func CreateRemoteDirectory(path string, comm packer.Communicator) (err error) {
// Start the command
if err := comm.Start(&cmd); err != nil {
return fmt.Errorf("Unable to create remote directory %s: %d", path, err)
return fmt.Errorf("Unable to create remote directory %s: %d", path, err)
}
// Wait for it to complete
@ -219,7 +220,7 @@ func CreateSoloRb(cookbooksPaths []string, comm packer.Communicator) (str string
writer := bufio.NewWriter(tf)
// Messy, messy...
cbPathsCat := "\"" + RemoteCookbookPath + "/" + strings.Join(cookbooksPaths, "\",\"" + RemoteCookbookPath + "/") + "\""
cbPathsCat := "\"" + RemoteCookbookPath + "/" + strings.Join(cookbooksPaths, "\",\""+RemoteCookbookPath+"/") + "\""
contents := "file_cache_path \"" + RemoteFileCachePath + "\"\ncookbook_path [" + cbPathsCat + "]\n"
if _, err := writer.WriteString(contents); err != nil {
@ -252,7 +253,7 @@ func CreateAttributesJson(jsonAttrs map[string]interface{}, recipes []string, co
var formattedRecipes []string
for _, value := range recipes {
formattedRecipes = append(formattedRecipes, "recipe[" + value + "]")
formattedRecipes = append(formattedRecipes, "recipe["+value+"]")
}
// Add Recipes to JSON
@ -307,7 +308,7 @@ func InstallChefSolo(preventSudo bool, comm packer.Communicator) (err error) {
err = executeCommand(command.String(), comm)
if err != nil {
return fmt.Errorf("Unable to install Chef Solo: %d", err)
return fmt.Errorf("Unable to install Chef Solo: %d", err)
}
return nil

View File

@ -0,0 +1,68 @@
package chefSolo
import (
"github.com/mitchellh/packer/packer"
"io/ioutil"
"os"
"testing"
)
func testConfig() map[string]interface{} {
return map[string]interface{}{
// "inline": []interface{}{"foo", "bar"},
}
}
func TestProvisioner_Impl(t *testing.T) {
var raw interface{}
raw = &Provisioner{}
if _, ok := raw.(packer.Provisioner); !ok {
t.Fatalf("must be a Provisioner")
}
}
// Cookbook paths
//////////////////
func TestProvisionerPrepare_DefaultCookbookPathIsUsed(t *testing.T) {
var p Provisioner
config := testConfig()
err := p.Prepare(config)
if err == nil {
t.Errorf("expected error to be thrown for unavailable cookbook path")
}
if len(p.config.CookbooksPaths) != 1 || p.config.CookbooksPaths[0] != DefaultCookbooksPath {
t.Errorf("unexpected default cookbook path: %s", p.config.CookbooksPaths)
}
}
func TestProvisionerPrepare_GivenCookbookPathsAreAddedToConfig(t *testing.T) {
var p Provisioner
path1, err := ioutil.TempDir("", "cookbooks_one")
if err != nil {
t.Errorf("err: %s", err)
}
path2, err := ioutil.TempDir("", "cookbooks_two")
if err != nil {
t.Errorf("err: %s", err)
}
defer os.Remove(path1)
defer os.Remove(path2)
config := testConfig()
config["cookbooks_paths"] = []string{path1, path2}
err = p.Prepare(config)
if err != nil {
t.Errorf("err: %s", err)
}
if len(p.config.CookbooksPaths) != 2 || p.config.CookbooksPaths[0] != path1 || p.config.CookbooksPaths[1] != path2 {
t.Errorf("unexpected default cookbook path: %s", p.config.CookbooksPaths)
}
}