From b0c129e4616c7c2d9fdfef3b0082d1fcfe227b30 Mon Sep 17 00:00:00 2001 From: sophia Date: Wed, 21 Jul 2021 10:49:40 -0500 Subject: [PATCH] Fix runner accept tests --- internal/config/testing.go | 6 +- internal/runner/accept_test.go | 359 ++++++++++++++++++--------------- internal/runner/testing.go | 29 +++ 3 files changed, 226 insertions(+), 168 deletions(-) diff --git a/internal/config/testing.go b/internal/config/testing.go index 226d3de92..b92a6ad6c 100644 --- a/internal/config/testing.go +++ b/internal/config/testing.go @@ -39,8 +39,4 @@ func TestConfigFile(t testing.T, src string) { require.NoError(t, ioutil.WriteFile(Filename, []byte(src), 0644)) } -const testSourceVal = ` -Vagrant.configure("2") do |config| - config.vm.box = "hashicorp/bionic64" -end -` +const testSourceVal = `` diff --git a/internal/runner/accept_test.go b/internal/runner/accept_test.go index 63a488a6a..7a54fa2de 100644 --- a/internal/runner/accept_test.go +++ b/internal/runner/accept_test.go @@ -1,204 +1,237 @@ package runner -// import ( -// "context" -// "os" -// "os/exec" -// "path/filepath" -// "testing" -// "time" +import ( + "context" + "os" + "os/exec" + "path/filepath" + "testing" + "time" -// "github.com/stretchr/testify/require" + "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" -// ) + "github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk" + "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" +) -// var testHasGit bool +var testHasGit bool -// func init() { -// if _, err := exec.LookPath("git"); err == nil { -// testHasGit = true -// } -// } +func init() { + if _, err := exec.LookPath("git"); err == nil { + testHasGit = true + } +} -// func TestRunnerAccept(t *testing.T) { -// require := require.New(t) -// ctx := context.Background() +func TestRunnerAccept(t *testing.T) { + require := require.New(t) + ctx := context.Background() -// // Setup our runner -// client := singleprocess.TestServer(t) -// runner := TestRunner(t, WithClient(client)) -// require.NoError(runner.Start()) + // Setup our runner + client := singleprocess.TestServer(t) + runner := TestRunner(t, WithClient(client)) + require.NoError(runner.Start()) -// // Initialize our basis -// core.TestBasis(t) + // Initialize our basis + testBasis := TestBasis(t, core.WithClient(client)) -// // Queue a job -// queueResp, err := client.QueueJob(ctx, &vagrant_server.QueueJobRequest{ -// Job: serverptypes.TestJobNew(t, nil), -// }) -// require.NoError(err) -// jobId := queueResp.JobId + // Queue a job + queueResp, err := client.QueueJob(ctx, &vagrant_server.QueueJobRequest{ + Job: serverptypes.TestJobNew(t, &vagrant_server.Job{ + Target: &vagrant_plugin_sdk.Ref_Target{ + ResourceId: "TESTMACH", + Project: &vagrant_plugin_sdk.Ref_Project{ + ResourceId: "TESTPROJ", + Basis: testBasis, + }, + }, + }), + }) + require.NoError(err) + jobId := queueResp.JobId -// // Accept should complete -// require.NoError(runner.Accept(ctx)) + // Accept should complete + require.NoError(runner.Accept(ctx)) -// // Verify that the job is completed -// job, err := client.GetJob(ctx, &vagrant_server.GetJobRequest{JobId: jobId}) -// require.NoError(err) -// require.Equal(vagrant_server.Job_SUCCESS, job.State) -// } + // Verify that the job is completed + job, err := client.GetJob(ctx, &vagrant_server.GetJobRequest{JobId: jobId}) + require.NoError(err) + require.Equal(vagrant_server.Job_SUCCESS, job.State) +} -// func TestRunnerAccept_cancelContext(t *testing.T) { -// require := require.New(t) -// ctx, cancel := context.WithCancel(context.Background()) +func TestRunnerAccept_cancelContext(t *testing.T) { + require := require.New(t) + ctx, cancel := context.WithCancel(context.Background()) -// // Setup our runner -// client := singleprocess.TestServer(t) -// runner := TestRunner(t, WithClient(client)) -// require.NoError(runner.Start()) + // Setup our runner + client := singleprocess.TestServer(t) + runner := TestRunner(t, WithClient(client)) + require.NoError(runner.Start()) -// // Initialize our basis -// core.TestBasis(t) + // Initialize our basis + testBasis := TestBasis(t, core.WithClient(client)) -// // Set a blocker -// noopCh := make(chan struct{}) -// runner.noopCh = noopCh + // Set a blocker + noopCh := make(chan struct{}) + runner.noopCh = noopCh -// // Queue a job -// queueResp, err := client.QueueJob(ctx, &vagrant_server.QueueJobRequest{ -// Job: serverptypes.TestJobNew(t, nil), -// }) -// require.NoError(err) -// jobId := queueResp.JobId + // Queue a job + queueResp, err := client.QueueJob(ctx, &vagrant_server.QueueJobRequest{ + Job: serverptypes.TestJobNew(t, &vagrant_server.Job{ + Target: &vagrant_plugin_sdk.Ref_Target{ + ResourceId: "TESTMACH", + Project: &vagrant_plugin_sdk.Ref_Project{ + ResourceId: "TESTPROJ", + Basis: testBasis, + }, + }, + }), + }) + require.NoError(err) + jobId := queueResp.JobId -// // Cancel the context eventually. This isn't CI-sensitive cause -// // we'll block no matter what. -// time.AfterFunc(500*time.Millisecond, cancel) + // Cancel the context eventually. This isn't CI-sensitive cause + // we'll block no matter what. + time.AfterFunc(500*time.Millisecond, cancel) -// // Accept should complete with an error -// require.NoError(runner.Accept(ctx)) + // Accept should complete with an error + require.NoError(runner.Accept(ctx)) -// // Verify that the job is completed -// require.Eventually(func() bool { -// job, err := client.GetJob(context.Background(), &vagrant_server.GetJobRequest{JobId: jobId}) -// require.NoError(err) -// return job.State == vagrant_server.Job_ERROR -// }, 3*time.Second, 25*time.Millisecond) -// } + // Verify that the job is completed + require.Eventually(func() bool { + job, err := client.GetJob(context.Background(), &vagrant_server.GetJobRequest{JobId: jobId}) + require.NoError(err) + return job.State == vagrant_server.Job_ERROR + }, 3*time.Second, 25*time.Millisecond) +} -// func TestRunnerAccept_cancelJob(t *testing.T) { -// require := require.New(t) -// ctx := context.Background() +func TestRunnerAccept_cancelJob(t *testing.T) { + require := require.New(t) + ctx := context.Background() -// // Setup our runner -// client := singleprocess.TestServer(t) -// runner := TestRunner(t, WithClient(client)) -// require.NoError(runner.Start()) + // Setup our runner + client := singleprocess.TestServer(t) + runner := TestRunner(t, WithClient(client)) + require.NoError(runner.Start()) -// // Initialize our basis -// core.TestBasis(t) + // Initialize our basis + testBasis := TestBasis(t, core.WithClient(client)) -// // Set a blocker -// noopCh := make(chan struct{}) -// runner.noopCh = noopCh + // Set a blocker + noopCh := make(chan struct{}) + runner.noopCh = noopCh -// // Queue a job -// queueResp, err := client.QueueJob(ctx, &vagrant_server.QueueJobRequest{ -// Job: serverptypes.TestJobNew(t, nil), -// }) -// require.NoError(err) -// jobId := queueResp.JobId + // Queue a job + queueResp, err := client.QueueJob(ctx, &vagrant_server.QueueJobRequest{ + Job: serverptypes.TestJobNew(t, &vagrant_server.Job{ + Target: &vagrant_plugin_sdk.Ref_Target{ + ResourceId: "TESTMACH", + Project: &vagrant_plugin_sdk.Ref_Project{ + ResourceId: "TESTPROJ", + Basis: testBasis, + }, + }, + }), + }) + require.NoError(err) + jobId := queueResp.JobId -// // Cancel the context eventually. This isn't CI-sensitive cause -// // we'll block no matter what. -// time.AfterFunc(500*time.Millisecond, func() { -// _, err := client.CancelJob(ctx, &vagrant_server.CancelJobRequest{ -// JobId: jobId, -// }) -// require.NoError(err) -// }) + // Cancel the context eventually. This isn't CI-sensitive cause + // we'll block no matter what. + time.AfterFunc(500*time.Millisecond, func() { + _, err := client.CancelJob(ctx, &vagrant_server.CancelJobRequest{ + JobId: jobId, + }) + require.NoError(err) + }) -// // Accept should complete with an error -// require.NoError(runner.Accept(ctx)) + // Accept should complete with an error + require.NoError(runner.Accept(ctx)) -// // Verify that the job is completed -// require.Eventually(func() bool { -// job, err := client.GetJob(context.Background(), &vagrant_server.GetJobRequest{JobId: jobId}) -// require.NoError(err) -// return job.State == vagrant_server.Job_ERROR -// }, 3*time.Second, 25*time.Millisecond) -// } + // Verify that the job is completed + require.Eventually(func() bool { + job, err := client.GetJob(context.Background(), &vagrant_server.GetJobRequest{JobId: jobId}) + require.NoError(err) + return job.State == vagrant_server.Job_ERROR + }, 3*time.Second, 25*time.Millisecond) +} -// func TestRunnerAccept_gitData(t *testing.T) { -// if !testHasGit { -// t.Skip("git not installed") -// return -// } +func TestRunnerAccept_gitData(t *testing.T) { + if !testHasGit { + t.Skip("git not installed") + return + } -// require := require.New(t) -// ctx := context.Background() + require := require.New(t) + ctx := context.Background() -// // Get a repo path -// path := testGitFixture(t, "git-noop") + // Get a repo path + path := testGitFixture(t, "git-noop") -// // Setup our runner -// client := singleprocess.TestServer(t) -// runner := TestRunner(t, WithClient(client)) -// require.NoError(runner.Start()) + // Setup our runner + client := singleprocess.TestServer(t) + runner := TestRunner(t, WithClient(client)) + require.NoError(runner.Start()) -// // Initialize our basis -// core.TestBasis(t) + // Initialize our basis + testBasis := TestBasis(t, core.WithClient(client)) -// // Queue a job -// queueResp, err := client.QueueJob(ctx, &vagrant_server.QueueJobRequest{ -// Job: serverptypes.TestJobNew(t, &vagrant_server.Job{ -// DataSource: &vagrant_server.Job_DataSource{ -// Source: &vagrant_server.Job_DataSource_Git{ -// Git: &vagrant_server.Job_Git{ -// Url: path, -// }, -// }, -// }, -// }), -// }) -// require.NoError(err) -// jobId := queueResp.JobId + // Queue a job + queueResp, err := client.QueueJob(ctx, &vagrant_server.QueueJobRequest{ + Job: serverptypes.TestJobNew(t, &vagrant_server.Job{ + DataSource: &vagrant_server.Job_DataSource{ + Source: &vagrant_server.Job_DataSource_Git{ + Git: &vagrant_server.Job_Git{ + Url: path, + }, + }, + }, + Target: &vagrant_plugin_sdk.Ref_Target{ + ResourceId: "TESTMACH", + Project: &vagrant_plugin_sdk.Ref_Project{ + ResourceId: "TESTPROJ", + Basis: testBasis, + }, + }, + }), + }) -// // Accept should complete -// require.NoError(runner.Accept(ctx)) + require.NoError(err) + jobId := queueResp.JobId -// // Verify that the job is completed -// job, err := client.GetJob(ctx, &vagrant_server.GetJobRequest{JobId: jobId}) -// require.NoError(err) -// require.Equal(vagrant_server.Job_SUCCESS, job.State) -// } + // Accept should complete + require.NoError(runner.Accept(ctx)) -// // testGitFixture MUST be called before TestRunner since TestRunner -// // changes our working directory. -// func testGitFixture(t *testing.T, n string) string { -// t.Helper() + // Verify that the job is completed + job, err := client.GetJob(ctx, &vagrant_server.GetJobRequest{JobId: jobId}) + require.NoError(err) + require.Equal(vagrant_server.Job_SUCCESS, job.State) +} -// // We need to get our working directory since the TestRunner call -// // changes it. -// wd, err := os.Getwd() -// require.NoError(t, err) -// wd, err = filepath.Abs(wd) -// require.NoError(t, err) -// path := filepath.Join(wd, "testdata", n) +// testGitFixture MUST be called before TestRunner since TestRunner +// changes our working directory. +func testGitFixture(t *testing.T, n string) string { + t.Helper() -// // Look for a DOTgit -// original := filepath.Join(path, "DOTgit") -// _, err = os.Stat(original) -// require.NoError(t, err) + // We need to get our working directory since the TestRunner call + // changes it. + wd, err := os.Getwd() + require.NoError(t, err) + wd, err = filepath.Abs(wd) + require.NoError(t, err) + path := filepath.Join(wd, "testdata", n) -// // Rename it -// newPath := filepath.Join(path, ".git") -// require.NoError(t, os.Rename(original, newPath)) -// t.Cleanup(func() { os.Rename(newPath, original) }) + // Look for a DOTgit + original := filepath.Join(path, "DOTgit") + _, err = os.Stat(original) + require.NoError(t, err) -// return path -// } + // Rename it + newPath := filepath.Join(path, ".git") + require.NoError(t, os.Rename(original, newPath)) + t.Cleanup(func() { os.Rename(newPath, original) }) + + return path +} diff --git a/internal/runner/testing.go b/internal/runner/testing.go index b44ab1928..e0b6b3d6b 100644 --- a/internal/runner/testing.go +++ b/internal/runner/testing.go @@ -1,6 +1,7 @@ package runner import ( + "context" "io/ioutil" "os" "os/exec" @@ -12,7 +13,10 @@ import ( "github.com/hashicorp/go-hclog" "github.com/hashicorp/go-plugin" + "github.com/hashicorp/vagrant-plugin-sdk/datadir" + "github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk" configpkg "github.com/hashicorp/vagrant/internal/config" + "github.com/hashicorp/vagrant/internal/core" "github.com/hashicorp/vagrant/internal/server/singleprocess" "github.com/hashicorp/vagrant/internal/serverclient" ) @@ -90,3 +94,28 @@ func testTempDir(t testing.T) string { t.Cleanup(func() { os.RemoveAll(dir) }) return dir } + +func TestBasis(t testing.T, opts ...core.BasisOption) (b *vagrant_plugin_sdk.Ref_Basis) { + td, err := ioutil.TempDir("", "core") + require.NoError(t, err) + t.Cleanup(func() { os.RemoveAll(td) }) + + projDir, err := datadir.NewBasis(td) + require.NoError(t, err) + + defaultOpts := []core.BasisOption{ + core.WithBasisDataDir(projDir), + core.WithBasisRef(&vagrant_plugin_sdk.Ref_Basis{Name: "TESTBAS"}), + } + + // Create the default factory for all component types + for typ := range core.TestingTypeMap { + f, _ := core.TestFactorySingle(t, typ, "TESTBAS") + defaultOpts = append(defaultOpts, core.WithFactory(typ, f)) + } + + basis, err := core.NewBasis(context.Background(), append(opts, defaultOpts...)...) + require.NoError(t, err) + b = basis.Ref().(*vagrant_plugin_sdk.Ref_Basis) + return +}