From c4e016a4d6c66d84c2867b94d1bb4f1891ea9aa6 Mon Sep 17 00:00:00 2001 From: sophia Date: Wed, 21 Jul 2021 09:53:24 -0500 Subject: [PATCH] Fix runner test --- internal/config/testing.go | 4 +- internal/runner/runner_test.go | 234 +++++++++++++++++---------------- 2 files changed, 121 insertions(+), 117 deletions(-) diff --git a/internal/config/testing.go b/internal/config/testing.go index e334aea7a..226d3de92 100644 --- a/internal/config/testing.go +++ b/internal/config/testing.go @@ -40,5 +40,7 @@ func TestConfigFile(t testing.T, src string) { } const testSourceVal = ` -project = "test" +Vagrant.configure("2") do |config| + config.vm.box = "hashicorp/bionic64" +end ` diff --git a/internal/runner/runner_test.go b/internal/runner/runner_test.go index dc63eeeb1..77311d571 100644 --- a/internal/runner/runner_test.go +++ b/internal/runner/runner_test.go @@ -1,145 +1,147 @@ package runner -// import ( -// "context" -// "os" -// "testing" -// "time" +import ( + "context" + "os" + "testing" + "time" -// "github.com/stretchr/testify/require" -// "google.golang.org/grpc/codes" -// "google.golang.org/grpc/status" + "github.com/stretchr/testify/require" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" -// "github.com/hashicorp/vagrant/internal/server/proto/vagrant_server" -// "github.com/hashicorp/vagrant/internal/server/singleprocess" -// ) + "github.com/hashicorp/vagrant/internal/server/proto/vagrant_server" + "github.com/hashicorp/vagrant/internal/server/singleprocess" +) -// func TestRunnerStart(t *testing.T) { -// require := require.New(t) -// ctx := context.Background() -// client := singleprocess.TestServer(t) +func TestRunnerStart(t *testing.T) { + require := require.New(t) + ctx := context.Background() + client := singleprocess.TestServer(t) + rubyRunTime, err := TestRunnerVagrantRubyRuntime() -// // Initialize our runner -// runner, err := New( -// WithClient(client), -// ) -// require.NoError(err) -// defer runner.Close() + // Initialize our runner + runner, err := New( + WithClient(client), + WithVagrantRubyRuntime(rubyRunTime), + ) + require.NoError(err) + defer runner.Close() -// // The runner should not be registered -// _, err = client.GetRunner(ctx, &vagrant_server.GetRunnerRequest{RunnerId: runner.Id()}) -// require.Error(err) -// require.Equal(codes.NotFound, status.Code(err)) + // The runner should not be registered + _, err = client.GetRunner(ctx, &vagrant_server.GetRunnerRequest{RunnerId: runner.Id()}) + require.Error(err) + require.Equal(codes.NotFound, status.Code(err)) -// // Start it -// require.NoError(runner.Start()) + // Start it + require.NoError(runner.Start()) -// // The runner should be registered -// resp, err := client.GetRunner(ctx, &vagrant_server.GetRunnerRequest{RunnerId: runner.Id()}) -// require.NoError(err) -// require.Equal(runner.Id(), resp.Id) + // The runner should be registered + resp, err := client.GetRunner(ctx, &vagrant_server.GetRunnerRequest{RunnerId: runner.Id()}) + require.NoError(err) + require.Equal(runner.Id(), resp.Id) -// // Close -// require.NoError(runner.Close()) -// time.Sleep(100 * time.Millisecond) + // Close + require.NoError(runner.Close()) + time.Sleep(100 * time.Millisecond) -// // The runner should not be registered -// _, err = client.GetRunner(ctx, &vagrant_server.GetRunnerRequest{RunnerId: runner.Id()}) -// require.Error(err) -// require.Equal(codes.NotFound, status.Code(err)) -// } + // The runner should not be registered + _, err = client.GetRunner(ctx, &vagrant_server.GetRunnerRequest{RunnerId: runner.Id()}) + require.Error(err) + require.Equal(codes.NotFound, status.Code(err)) +} -// func TestRunnerStart_config(t *testing.T) { -// t.Run("set and unset", func(t *testing.T) { -// require := require.New(t) -// ctx := context.Background() -// client := singleprocess.TestServer(t) +func TestRunnerStart_config(t *testing.T) { + t.Run("set and unset", func(t *testing.T) { + require := require.New(t) + ctx := context.Background() + client := singleprocess.TestServer(t) -// cfgVar := &vagrant_server.ConfigVar{ -// Scope: &vagrant_server.ConfigVar_Runner{ -// Runner: &vagrant_server.Ref_Runner{ -// Target: &vagrant_server.Ref_Runner_Any{ -// Any: &vagrant_server.Ref_RunnerAny{}, -// }, -// }, -// }, + cfgVar := &vagrant_server.ConfigVar{ + Scope: &vagrant_server.ConfigVar_Runner{ + Runner: &vagrant_server.Ref_Runner{ + Target: &vagrant_server.Ref_Runner_Any{ + Any: &vagrant_server.Ref_RunnerAny{}, + }, + }, + }, -// Name: "I_AM_A_TEST_VALUE", -// Value: "1234567890", -// } + Name: "I_AM_A_TEST_VALUE", + Value: "1234567890", + } -// // Initialize our runner -// runner := TestRunner(t, WithClient(client)) -// defer runner.Close() -// require.NoError(runner.Start()) + // Initialize our runner + runner := TestRunner(t, WithClient(client)) + defer runner.Close() + require.NoError(runner.Start()) -// // Verify it is not set -// require.Empty(os.Getenv(cfgVar.Name)) + // Verify it is not set + require.Empty(os.Getenv(cfgVar.Name)) -// // Set some config -// _, err := client.SetConfig(ctx, &vagrant_server.ConfigSetRequest{Variables: []*vagrant_server.ConfigVar{cfgVar}}) -// require.NoError(err) + // Set some config + _, err := client.SetConfig(ctx, &vagrant_server.ConfigSetRequest{Variables: []*vagrant_server.ConfigVar{cfgVar}}) + require.NoError(err) -// // Should be set -// require.Eventually(func() bool { -// return os.Getenv(cfgVar.Name) == cfgVar.Value -// }, 1000*time.Millisecond, 50*time.Millisecond) + // Should be set + require.Eventually(func() bool { + return os.Getenv(cfgVar.Name) == cfgVar.Value + }, 1000*time.Millisecond, 50*time.Millisecond) -// // Unset -// cfgVar.Value = "" -// _, err = client.SetConfig(ctx, &vagrant_server.ConfigSetRequest{Variables: []*vagrant_server.ConfigVar{cfgVar}}) -// require.NoError(err) + // Unset + cfgVar.Value = "" + _, err = client.SetConfig(ctx, &vagrant_server.ConfigSetRequest{Variables: []*vagrant_server.ConfigVar{cfgVar}}) + require.NoError(err) -// // Should be unset -// require.Eventually(func() bool { -// return os.Getenv(cfgVar.Name) == "" -// }, 1000*time.Millisecond, 50*time.Millisecond) -// }) + // Should be unset + require.Eventually(func() bool { + return os.Getenv(cfgVar.Name) == "" + }, 1000*time.Millisecond, 50*time.Millisecond) + }) -// t.Run("unset with original env", func(t *testing.T) { -// require := require.New(t) -// ctx := context.Background() -// client := singleprocess.TestServer(t) + t.Run("unset with original env", func(t *testing.T) { + require := require.New(t) + ctx := context.Background() + client := singleprocess.TestServer(t) -// cfgVar := &vagrant_server.ConfigVar{ -// Scope: &vagrant_server.ConfigVar_Runner{ -// Runner: &vagrant_server.Ref_Runner{ -// Target: &vagrant_server.Ref_Runner_Any{ -// Any: &vagrant_server.Ref_RunnerAny{}, -// }, -// }, -// }, + cfgVar := &vagrant_server.ConfigVar{ + Scope: &vagrant_server.ConfigVar_Runner{ + Runner: &vagrant_server.Ref_Runner{ + Target: &vagrant_server.Ref_Runner_Any{ + Any: &vagrant_server.Ref_RunnerAny{}, + }, + }, + }, -// Name: "I_AM_A_TEST_VALUE", -// Value: "1234567890", -// } + Name: "I_AM_A_TEST_VALUE", + Value: "1234567890", + } -// // Set a value -// require.NoError(os.Setenv(cfgVar.Name, "ORIGINAL")) -// defer os.Unsetenv(cfgVar.Name) + // Set a value + require.NoError(os.Setenv(cfgVar.Name, "ORIGINAL")) + defer os.Unsetenv(cfgVar.Name) -// // Initialize our runner -// runner := TestRunner(t, WithClient(client)) -// defer runner.Close() -// require.NoError(runner.Start()) + // Initialize our runner + runner := TestRunner(t, WithClient(client)) + defer runner.Close() + require.NoError(runner.Start()) -// // Set some config -// _, err := client.SetConfig(ctx, &vagrant_server.ConfigSetRequest{Variables: []*vagrant_server.ConfigVar{cfgVar}}) -// require.NoError(err) + // Set some config + _, err := client.SetConfig(ctx, &vagrant_server.ConfigSetRequest{Variables: []*vagrant_server.ConfigVar{cfgVar}}) + require.NoError(err) -// // Should be set -// require.Eventually(func() bool { -// return os.Getenv(cfgVar.Name) == cfgVar.Value -// }, 1000*time.Millisecond, 50*time.Millisecond) + // Should be set + require.Eventually(func() bool { + return os.Getenv(cfgVar.Name) == cfgVar.Value + }, 1000*time.Millisecond, 50*time.Millisecond) -// // Unset -// cfgVar.Value = "" -// _, err = client.SetConfig(ctx, &vagrant_server.ConfigSetRequest{Variables: []*vagrant_server.ConfigVar{cfgVar}}) -// require.NoError(err) + // Unset + cfgVar.Value = "" + _, err = client.SetConfig(ctx, &vagrant_server.ConfigSetRequest{Variables: []*vagrant_server.ConfigVar{cfgVar}}) + require.NoError(err) -// // Should be unset back to original value -// require.Eventually(func() bool { -// return os.Getenv(cfgVar.Name) == "ORIGINAL" -// }, 1000*time.Millisecond, 50*time.Millisecond) -// }) -// } + // Should be unset back to original value + require.Eventually(func() bool { + return os.Getenv(cfgVar.Name) == "ORIGINAL" + }, 1000*time.Millisecond, 50*time.Millisecond) + }) +}