diff --git a/internal/runner/accept_test.go b/internal/runner/accept_test.go index 6138f1a43..58e22d9b3 100644 --- a/internal/runner/accept_test.go +++ b/internal/runner/accept_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/require" + "github.com/hashicorp/vagrant/internal/core" "github.com/hashicorp/vagrant/internal/server/proto/vagrant_server" serverptypes "github.com/hashicorp/vagrant/internal/server/ptypes" "github.com/hashicorp/vagrant/internal/server/singleprocess" @@ -32,8 +33,8 @@ func TestRunnerAccept(t *testing.T) { runner := TestRunner(t, WithClient(client)) require.NoError(runner.Start()) - // Initialize our app - singleprocess.TestApp(t, client, serverptypes.TestJobNew(t, nil).Application) + // Initialize our basis + core.TestBasis(t) // Queue a job queueResp, err := client.QueueJob(ctx, &vagrant_server.QueueJobRequest{ @@ -60,8 +61,8 @@ func TestRunnerAccept_cancelContext(t *testing.T) { runner := TestRunner(t, WithClient(client)) require.NoError(runner.Start()) - // Initialize our app - singleprocess.TestApp(t, client, serverptypes.TestJobNew(t, nil).Application) + // Initialize our basis + core.TestBasis(t) // Set a blocker noopCh := make(chan struct{}) @@ -98,8 +99,8 @@ func TestRunnerAccept_cancelJob(t *testing.T) { runner := TestRunner(t, WithClient(client)) require.NoError(runner.Start()) - // Initialize our app - singleprocess.TestApp(t, client, serverptypes.TestJobNew(t, nil).Application) + // Initialize our basis + core.TestBasis(t) // Set a blocker noopCh := make(chan struct{}) @@ -149,8 +150,8 @@ func TestRunnerAccept_gitData(t *testing.T) { runner := TestRunner(t, WithClient(client)) require.NoError(runner.Start()) - // Initialize our app - singleprocess.TestApp(t, client, serverptypes.TestJobNew(t, nil).Application) + // Initialize our basis + core.TestBasis(t) // Queue a job queueResp, err := client.QueueJob(ctx, &vagrant_server.QueueJobRequest{ diff --git a/internal/runner/operation.go b/internal/runner/operation.go index c5b70b1a7..2d4b79b1c 100644 --- a/internal/runner/operation.go +++ b/internal/runner/operation.go @@ -99,7 +99,8 @@ func (r *Runner) executeJob( // Determine the evaluation context we'll be using log.Trace("reading configuration", "path", path) - cfg, err := configpkg.Load(path, filepath.Dir(path)) + pppp := filepath.Dir(path) + cfg, err := configpkg.Load(path, pppp) if err != nil { log.Warn("failed here trying to read configuration", "path", path) cfg = &configpkg.Config{} diff --git a/internal/runner/testdata/git-noop/DOTgit/COMMIT_EDITMSG b/internal/runner/testdata/git-noop/DOTgit/COMMIT_EDITMSG index 56f23a76c..d2473eea8 100644 --- a/internal/runner/testdata/git-noop/DOTgit/COMMIT_EDITMSG +++ b/internal/runner/testdata/git-noop/DOTgit/COMMIT_EDITMSG @@ -1,8 +1 @@ -Fixes -# Please enter the commit message for your changes. Lines starting -# with '#' will be ignored, and an empty message aborts the commit. -# -# On branch master -# Changes to be committed: -# modified: vagrant.hcl -# +Add Vagrantfile for testing diff --git a/internal/runner/testdata/git-noop/DOTgit/index b/internal/runner/testdata/git-noop/DOTgit/index index f8e85b688..2354e9290 100644 Binary files a/internal/runner/testdata/git-noop/DOTgit/index and b/internal/runner/testdata/git-noop/DOTgit/index differ diff --git a/internal/runner/testdata/git-noop/DOTgit/logs/HEAD b/internal/runner/testdata/git-noop/DOTgit/logs/HEAD index 9f7fbb439..5026d2438 100644 --- a/internal/runner/testdata/git-noop/DOTgit/logs/HEAD +++ b/internal/runner/testdata/git-noop/DOTgit/logs/HEAD @@ -1,2 +1,3 @@ 0000000000000000000000000000000000000000 1924b8606e1d56acb7e2059652ad059d4bac6f2e Mitchell Hashimoto 1597721732 -0700 commit (initial): Initial commit 1924b8606e1d56acb7e2059652ad059d4bac6f2e b6bf15100c570f2be6a231a095d395ed16dfed81 Mitchell Hashimoto 1597722255 -0700 commit: Fixes +b6bf15100c570f2be6a231a095d395ed16dfed81 cfa0599445d5025a7798f8949c83da3899e953df sophia 1626733782 -0500 commit: Add Vagrantfile for testing diff --git a/internal/runner/testdata/git-noop/DOTgit/logs/refs/heads/master b/internal/runner/testdata/git-noop/DOTgit/logs/refs/heads/master index 9f7fbb439..5026d2438 100644 --- a/internal/runner/testdata/git-noop/DOTgit/logs/refs/heads/master +++ b/internal/runner/testdata/git-noop/DOTgit/logs/refs/heads/master @@ -1,2 +1,3 @@ 0000000000000000000000000000000000000000 1924b8606e1d56acb7e2059652ad059d4bac6f2e Mitchell Hashimoto 1597721732 -0700 commit (initial): Initial commit 1924b8606e1d56acb7e2059652ad059d4bac6f2e b6bf15100c570f2be6a231a095d395ed16dfed81 Mitchell Hashimoto 1597722255 -0700 commit: Fixes +b6bf15100c570f2be6a231a095d395ed16dfed81 cfa0599445d5025a7798f8949c83da3899e953df sophia 1626733782 -0500 commit: Add Vagrantfile for testing diff --git a/internal/runner/testdata/git-noop/DOTgit/objects/b4/50d3da13b8f241226fca12ce3450b783d7038c b/internal/runner/testdata/git-noop/DOTgit/objects/b4/50d3da13b8f241226fca12ce3450b783d7038c new file mode 100644 index 000000000..f413133e0 Binary files /dev/null and b/internal/runner/testdata/git-noop/DOTgit/objects/b4/50d3da13b8f241226fca12ce3450b783d7038c differ diff --git a/internal/runner/testdata/git-noop/DOTgit/objects/cf/a0599445d5025a7798f8949c83da3899e953df b/internal/runner/testdata/git-noop/DOTgit/objects/cf/a0599445d5025a7798f8949c83da3899e953df new file mode 100644 index 000000000..3d6d18f1a Binary files /dev/null and b/internal/runner/testdata/git-noop/DOTgit/objects/cf/a0599445d5025a7798f8949c83da3899e953df differ diff --git a/internal/runner/testdata/git-noop/DOTgit/objects/ea/2549d66602a0a588e69f033c48944de5b7a45c b/internal/runner/testdata/git-noop/DOTgit/objects/ea/2549d66602a0a588e69f033c48944de5b7a45c new file mode 100644 index 000000000..d5cf29b37 Binary files /dev/null and b/internal/runner/testdata/git-noop/DOTgit/objects/ea/2549d66602a0a588e69f033c48944de5b7a45c differ diff --git a/internal/runner/testdata/git-noop/DOTgit/refs/heads/master b/internal/runner/testdata/git-noop/DOTgit/refs/heads/master index 26dc93b64..fc3ce06ca 100644 --- a/internal/runner/testdata/git-noop/DOTgit/refs/heads/master +++ b/internal/runner/testdata/git-noop/DOTgit/refs/heads/master @@ -1 +1 @@ -b6bf15100c570f2be6a231a095d395ed16dfed81 +cfa0599445d5025a7798f8949c83da3899e953df diff --git a/internal/runner/testdata/git-noop/Vagrantfile b/internal/runner/testdata/git-noop/Vagrantfile new file mode 100644 index 000000000..ea2549d66 --- /dev/null +++ b/internal/runner/testdata/git-noop/Vagrantfile @@ -0,0 +1,3 @@ +Vagrant.configure("2") do |config| + config.vm.box = "hashicorp/bionic64" +end diff --git a/internal/runner/testdata/git-noop/waypoint.hcl b/internal/runner/testdata/git-noop/vagrant.hcl similarity index 100% rename from internal/runner/testdata/git-noop/waypoint.hcl rename to internal/runner/testdata/git-noop/vagrant.hcl diff --git a/internal/runner/testing.go b/internal/runner/testing.go index c10ed760a..b44ab1928 100644 --- a/internal/runner/testing.go +++ b/internal/runner/testing.go @@ -1,54 +1,92 @@ package runner -// import ( -// "io/ioutil" -// "os" +import ( + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "runtime" -// "github.com/mitchellh/go-testing-interface" -// "github.com/stretchr/testify/require" + "github.com/mitchellh/go-testing-interface" + "github.com/stretchr/testify/require" -// configpkg "github.com/hashicorp/vagrant/internal/config" -// "github.com/hashicorp/vagrant/internal/server/singleprocess" -// ) + "github.com/hashicorp/go-hclog" + "github.com/hashicorp/go-plugin" + configpkg "github.com/hashicorp/vagrant/internal/config" + "github.com/hashicorp/vagrant/internal/server/singleprocess" + "github.com/hashicorp/vagrant/internal/serverclient" +) -// // TestRunner returns an initialized runner pointing to an in-memory test -// // server. This will close automatically on test completion. -// // -// // This will also change the working directory to a temporary directory -// // so that any side effect file creation doesn't impact the real working -// // directory. If you need to use your working directory, query it before -// // calling this. -// func TestRunner(t testing.T, opts ...Option) *Runner { -// require := require.New(t) -// client := singleprocess.TestServer(t) +// TestRunner returns an initialized runner pointing to an in-memory test +// server. This will close automatically on test completion. +// +// This will also change the working directory to a temporary directory +// so that any side effect file creation doesn't impact the real working +// directory. If you need to use your working directory, query it before +// calling this. +func TestRunner(t testing.T, opts ...Option) *Runner { + require := require.New(t) + client := singleprocess.TestServer(t) + rubyRunTime, err := TestRunnerVagrantRubyRuntime() -// // Initialize our runner -// runner, err := New(append([]Option{ -// WithClient(client), -// }, opts...)...) -// require.NoError(err) -// t.Cleanup(func() { runner.Close() }) + // Initialize our runner + runner, err := New(append([]Option{ + WithClient(client), + WithVagrantRubyRuntime(rubyRunTime), + }, opts...)...) + require.NoError(err) + t.Cleanup(func() { runner.Close() }) -// // Move into a temporary directory -// td := testTempDir(t) -// testChdir(t, td) + // Move into a temporary directory + td := testTempDir(t) + testChdir(t, td) -// // Create a valid vagrant configuration file -// configpkg.TestConfigFile(t, configpkg.TestSource(t)) + // Create a valid vagrant configuration file + configpkg.TestConfigFile(t, configpkg.TestSource(t)) -// return runner -// } + return runner +} -// func testChdir(t testing.T, dir string) { -// pwd, err := os.Getwd() -// require.NoError(t, err) -// require.NoError(t, os.Chdir(dir)) -// t.Cleanup(func() { require.NoError(t, os.Chdir(pwd)) }) -// } +func TestRunnerVagrantRubyRuntime() (rubyRuntime plugin.ClientProtocol, err error) { + // TODO: Update for actual release usage. This is dev only now. + // TODO: We should also locate a free port on startup and use that port + _, this_dir, _, _ := runtime.Caller(0) + cmd := exec.Command( + "bundle", "exec", "vagrant", "serve", + ) + cmd.Env = []string{ + "BUNDLE_GEMFILE=" + filepath.Join(this_dir, "../../..", "Gemfile"), + "VAGRANT_I_KNOW_WHAT_IM_DOING_PLEASE_BE_QUIET=true", + "VAGRANT_LOG=debug", + "VAGRANT_LOG_FILE=/tmp/vagrant.log", + } -// func testTempDir(t testing.T) string { -// dir, err := ioutil.TempDir("", "vagrant-test") -// require.NoError(t, err) -// t.Cleanup(func() { os.RemoveAll(dir) }) -// return dir -// } + config := serverclient.RubyVagrantPluginConfig(hclog.New(&hclog.LoggerOptions{Level: hclog.Trace})) + config.Cmd = cmd + c := plugin.NewClient(config) + if _, err = c.Start(); err != nil { + return + } + if rubyRuntime, err = c.Client(); err != nil { + return + } + + // Ensure the plugin is halted when the basis is cleaned up + // b.cleanup(func() { rubyRuntime.Close() }) + + return +} + +func testChdir(t testing.T, dir string) { + pwd, err := os.Getwd() + require.NoError(t, err) + require.NoError(t, os.Chdir(dir)) + t.Cleanup(func() { require.NoError(t, os.Chdir(pwd)) }) +} + +func testTempDir(t testing.T) string { + dir, err := ioutil.TempDir("", "vagrant-test") + require.NoError(t, err) + t.Cleanup(func() { os.RemoveAll(dir) }) + return dir +}