2019-02-07 15:39:56 -05:00
|
|
|
package vagrant
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStepCreateVagrantfile_Impl(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = new(StepCreateVagrantfile)
|
|
|
|
if _, ok := raw.(multistep.Step); !ok {
|
|
|
|
t.Fatalf("initialize should be a step")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateFile(t *testing.T) {
|
|
|
|
testy := StepCreateVagrantfile{
|
|
|
|
OutputDir: "./",
|
2019-08-07 11:51:14 -04:00
|
|
|
SourceBox: "apples",
|
2019-07-09 18:42:48 -04:00
|
|
|
BoxName: "bananas",
|
2019-02-07 15:39:56 -05:00
|
|
|
}
|
|
|
|
templatePath, err := testy.createVagrantfile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
defer os.Remove(templatePath)
|
|
|
|
contents, err := ioutil.ReadFile(templatePath)
|
2019-09-17 08:39:23 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
2019-02-07 15:39:56 -05:00
|
|
|
actual := string(contents)
|
|
|
|
expected := `Vagrant.configure("2") do |config|
|
2019-08-07 11:51:14 -04:00
|
|
|
config.vm.define "source", autostart: false do |source|
|
|
|
|
source.vm.box = "apples"
|
2019-10-30 16:48:12 -04:00
|
|
|
config.ssh.insert_key = false
|
2019-08-07 11:51:14 -04:00
|
|
|
end
|
|
|
|
config.vm.define "output" do |output|
|
|
|
|
output.vm.box = "bananas"
|
|
|
|
output.vm.box_url = "file://package.box"
|
2019-10-30 16:48:12 -04:00
|
|
|
config.ssh.insert_key = false
|
2019-08-07 11:51:14 -04:00
|
|
|
end
|
2019-02-07 15:39:56 -05:00
|
|
|
config.vm.synced_folder ".", "/vagrant", disabled: true
|
|
|
|
end`
|
|
|
|
if ok := strings.Compare(actual, expected); ok != 0 {
|
|
|
|
t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateFile_customSync(t *testing.T) {
|
|
|
|
testy := StepCreateVagrantfile{
|
|
|
|
OutputDir: "./",
|
|
|
|
SyncedFolder: "myfolder/foldertimes",
|
|
|
|
}
|
|
|
|
templatePath, err := testy.createVagrantfile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
defer os.Remove(templatePath)
|
|
|
|
contents, err := ioutil.ReadFile(templatePath)
|
2019-09-17 08:39:23 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
2019-02-07 15:39:56 -05:00
|
|
|
actual := string(contents)
|
|
|
|
expected := `Vagrant.configure("2") do |config|
|
2019-08-07 11:51:14 -04:00
|
|
|
config.vm.define "source", autostart: false do |source|
|
|
|
|
source.vm.box = ""
|
2019-10-30 16:48:12 -04:00
|
|
|
config.ssh.insert_key = false
|
2019-08-07 11:51:14 -04:00
|
|
|
end
|
|
|
|
config.vm.define "output" do |output|
|
|
|
|
output.vm.box = ""
|
|
|
|
output.vm.box_url = "file://package.box"
|
2019-10-30 16:48:12 -04:00
|
|
|
config.ssh.insert_key = false
|
2019-08-07 11:51:14 -04:00
|
|
|
end
|
2019-02-07 15:39:56 -05:00
|
|
|
config.vm.synced_folder "myfolder/foldertimes", "/vagrant"
|
|
|
|
end`
|
|
|
|
if ok := strings.Compare(actual, expected); ok != 0 {
|
|
|
|
t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 16:48:12 -04:00
|
|
|
|
|
|
|
func TestCreateFile_InsertKeyTrue(t *testing.T) {
|
|
|
|
testy := StepCreateVagrantfile{
|
|
|
|
OutputDir: "./",
|
|
|
|
InsertKey: true,
|
|
|
|
}
|
|
|
|
templatePath, err := testy.createVagrantfile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
defer os.Remove(templatePath)
|
|
|
|
contents, err := ioutil.ReadFile(templatePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
actual := string(contents)
|
|
|
|
expected := `Vagrant.configure("2") do |config|
|
|
|
|
config.vm.define "source", autostart: false do |source|
|
|
|
|
source.vm.box = ""
|
|
|
|
config.ssh.insert_key = true
|
|
|
|
end
|
|
|
|
config.vm.define "output" do |output|
|
|
|
|
output.vm.box = ""
|
|
|
|
output.vm.box_url = "file://package.box"
|
|
|
|
config.ssh.insert_key = true
|
|
|
|
end
|
|
|
|
config.vm.synced_folder ".", "/vagrant", disabled: true
|
|
|
|
end`
|
|
|
|
if ok := strings.Compare(actual, expected); ok != 0 {
|
|
|
|
t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|