From 88f8d54689eea8fd83e15c22982652212397f9c1 Mon Sep 17 00:00:00 2001 From: sysrex <769991+sysrex@users.noreply.github.com> Date: Mon, 10 Jun 2024 18:02:47 +0100 Subject: [PATCH 1/9] refactor: change basic test to suite --- e2e/basic/basic_test.go | 76 +++++++++++++++++++---------------- e2e/basic/suite_setup_test.go | 41 +++++++++++++++++++ 2 files changed, 82 insertions(+), 35 deletions(-) create mode 100644 e2e/basic/suite_setup_test.go diff --git a/e2e/basic/basic_test.go b/e2e/basic/basic_test.go index 8c8ac26..6267114 100644 --- a/e2e/basic/basic_test.go +++ b/e2e/basic/basic_test.go @@ -1,53 +1,59 @@ package basic import ( - "testing" + "context" + "github.com/celestiaorg/knuu/pkg/instance" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" +) - "github.com/celestiaorg/knuu/pkg/knuu" +const ( + TestImage = "alpine:latest" ) -func TestBasic(t *testing.T) { - t.Parallel() +func (s *Suite) TestBasic() { + s.T().Parallel() // Setup - instance, err := knuu.NewInstance("alpine") - if err != nil { - t.Fatalf("Error creating instance '%v':", err) - } - err = instance.SetImage("docker.io/alpine:latest") - if err != nil { - t.Fatalf("Error setting image: %v", err) - } - err = instance.SetCommand("sleep", "infinity") - if err != nil { - t.Fatalf("Error setting command: %v", err) - } - err = instance.Commit() - if err != nil { - t.Fatalf("Error committing instance: %v", err) - } + ctx := context.Background() + + target, err := s.Knuu.NewInstance("alpine") + s.Require().NoError(err) - t.Cleanup(func() { - require.NoError(t, knuu.BatchDestroy(instance)) + s.Require().NoError(target.SetImage(ctx, TestImage)) + s.Require().NoError(target.SetCommand("sleep", "infinity")) + s.Require().NoError(target.Commit()) + + s.T().Cleanup(func() { + s.T().Log("Tearing down Basic Test...") + err := instance.BatchDestroy(ctx, target) + if err != nil { + s.T().Logf("error destroying instances: %v", err) + } }) - // Test logic + // Test Logic + s.Require().NoError(target.Start(ctx)) + s.Require().NoError(target.WaitInstanceIsRunning(ctx)) - err = instance.Start() - if err != nil { - t.Fatalf("Error starting instance: %v", err) - } - err = instance.WaitInstanceIsRunning() - if err != nil { - t.Fatalf("Error waiting for instance to be running: %v", err) + s.Require().NoError(err) + + //Perform the test + type testCase struct { + name string } - wget, err := instance.ExecuteCommand("echo", "Hello World!") - if err != nil { - t.Fatalf("Error executing command '%v':", err) + + tt := make([]testCase, 1) + + for _, tc := range tt { + tc := tc + s.Run(tc.name, func() { + s.T().Logf("Running test case: %s", tc.name) + output, err := target.ExecuteCommand(ctx, "echo", "Hello World") + s.Require().NoError(err) + + assert.Contains(s.T(), output, "Hello World") + }) } - assert.Contains(t, wget, "Hello World!") } diff --git a/e2e/basic/suite_setup_test.go b/e2e/basic/suite_setup_test.go new file mode 100644 index 0000000..ce4e8d4 --- /dev/null +++ b/e2e/basic/suite_setup_test.go @@ -0,0 +1,41 @@ +package basic + +import ( + "context" + "testing" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/suite" + + "github.com/celestiaorg/knuu/pkg/knuu" +) + +type Suite struct { + suite.Suite + Knuu *knuu.Knuu +} + +func (s *Suite) SetupSuite() { + var ( + err error + ctx = context.Background() + ) + s.Knuu, err = knuu.New(ctx, knuu.WithProxyEnabled()) + s.Require().NoError(err) + s.T().Logf("Scope: %s", s.Knuu.Scope()) + s.Knuu.HandleStopSignal(ctx) +} + +func (s *Suite) TearDownSuite() { + s.T().Cleanup(func() { + logrus.Info("Tearing down test suite...") + err := s.Knuu.CleanUp(context.Background()) + if err != nil { + s.T().Logf("Error cleaning up test suite: %v", err) + } + }) +} + +func TestRunSuite(t *testing.T) { + suite.Run(t, new(Suite)) +} From 04e5db372780867596970edbcb6f7e5111a4841d Mon Sep 17 00:00:00 2001 From: Alex Kiss <769991+sysrex@users.noreply.github.com> Date: Mon, 10 Jun 2024 19:11:20 +0100 Subject: [PATCH 2/9] refactor: change basic test to suite refactor: Change the Test suite for Basic for the time being without proxy and add more test cases --- e2e/basic/basic_test.go | 17 +++++++++-------- e2e/basic/suite_setup_test.go | 17 +++++++---------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/e2e/basic/basic_test.go b/e2e/basic/basic_test.go index 6267114..b459e7b 100644 --- a/e2e/basic/basic_test.go +++ b/e2e/basic/basic_test.go @@ -3,15 +3,16 @@ package basic import ( "context" - "github.com/celestiaorg/knuu/pkg/instance" "github.com/stretchr/testify/assert" + + "github.com/celestiaorg/knuu/pkg/instance" ) const ( TestImage = "alpine:latest" ) -func (s *Suite) TestBasic() { +func (s *TestSuite) TestBasic() { s.T().Parallel() // Setup @@ -36,23 +37,23 @@ func (s *Suite) TestBasic() { s.Require().NoError(target.Start(ctx)) s.Require().NoError(target.WaitInstanceIsRunning(ctx)) - s.Require().NoError(err) - - //Perform the test + // Perform the test type testCase struct { name string } - tt := make([]testCase, 1) + tt := []testCase{ + {"Hello World"}, + } for _, tc := range tt { tc := tc s.Run(tc.name, func() { s.T().Logf("Running test case: %s", tc.name) - output, err := target.ExecuteCommand(ctx, "echo", "Hello World") + output, err := target.ExecuteCommand(ctx, "echo", tc.name) s.Require().NoError(err) - assert.Contains(s.T(), output, "Hello World") + assert.Contains(s.T(), output, tc.name) }) } diff --git a/e2e/basic/suite_setup_test.go b/e2e/basic/suite_setup_test.go index ce4e8d4..db961fd 100644 --- a/e2e/basic/suite_setup_test.go +++ b/e2e/basic/suite_setup_test.go @@ -10,32 +10,29 @@ import ( "github.com/celestiaorg/knuu/pkg/knuu" ) -type Suite struct { +type TestSuite struct { suite.Suite Knuu *knuu.Knuu } -func (s *Suite) SetupSuite() { +func (s *TestSuite) SetupSuite() { var ( err error ctx = context.Background() ) - s.Knuu, err = knuu.New(ctx, knuu.WithProxyEnabled()) + s.Knuu, err = knuu.New(ctx) s.Require().NoError(err) - s.T().Logf("Scope: %s", s.Knuu.Scope()) + s.T().Logf("Error cleaning up test suite: %v", err) s.Knuu.HandleStopSignal(ctx) } -func (s *Suite) TearDownSuite() { +func (s *TestSuite) TearDownSuite() { s.T().Cleanup(func() { logrus.Info("Tearing down test suite...") - err := s.Knuu.CleanUp(context.Background()) - if err != nil { - s.T().Logf("Error cleaning up test suite: %v", err) - } + s.Require().NoError(s.Knuu.CleanUp(context.Background())) }) } func TestRunSuite(t *testing.T) { - suite.Run(t, new(Suite)) + suite.Run(t, new(TestSuite)) } From 7579d50713efa0d2176b1effcee6189d6fa3ba5e Mon Sep 17 00:00:00 2001 From: Alex Kiss <769991+sysrex@users.noreply.github.com> Date: Mon, 10 Jun 2024 22:18:29 +0100 Subject: [PATCH 3/9] refactor: TestMain --- e2e/basic/main_test.go | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/e2e/basic/main_test.go b/e2e/basic/main_test.go index e09a0f5..f2e0adc 100644 --- a/e2e/basic/main_test.go +++ b/e2e/basic/main_test.go @@ -2,19 +2,29 @@ package basic import ( "os" - "testing" - - "github.com/sirupsen/logrus" - - "github.com/celestiaorg/knuu/pkg/knuu" ) -func TestMain(m *testing.M) { - err := knuu.Initialize() - if err != nil { - logrus.Fatalf("error initializing knuu: %v", err) +func convertViaMap(b bool) int { + table := map[bool]int{ + true: 1, + false: 0, } - logrus.Infof("Scope: %s", knuu.Scope()) - exitVal := m.Run() - os.Exit(exitVal) + return table[b] +} + +func (s *TestSuite) TestMain() { + s.T().Parallel() + // Setup + + // Test Logic + s.T().Log("Running test case: TestMain") + + // Perform the test + exitVal := s.Run("TestMain", func() { + s.T().Logf("Scope: %s", s.Knuu.Scope()) + }) + + exitValue := convertViaMap(exitVal) + + os.Exit(exitValue) } From 645c2b405da6bc1ed4c255410c1269a9effbcfaa Mon Sep 17 00:00:00 2001 From: Alex Kiss <769991+sysrex@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:41:41 +0100 Subject: [PATCH 4/9] refactor: changes for the test refactor --- e2e/basic/basic_test.go | 29 ++++++++++++++--------------- e2e/basic/main_test.go | 10 +++++----- e2e/basic/probe_test.go | 17 +++++++++++++++++ e2e/basic/suite_setup_test.go | 6 ++++-- 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/e2e/basic/basic_test.go b/e2e/basic/basic_test.go index b459e7b..290c8b8 100644 --- a/e2e/basic/basic_test.go +++ b/e2e/basic/basic_test.go @@ -9,23 +9,23 @@ import ( ) const ( - TestImage = "alpine:latest" + testImage = "alpine:latest" ) -func (s *TestSuite) TestBasic() { - s.T().Parallel() +func (ts *TestSuite) TestBasic() { + ts.T().Parallel() // Setup ctx := context.Background() - target, err := s.Knuu.NewInstance("alpine") - s.Require().NoError(err) + target, err := ts.Knuu.NewInstance("alpine") + ts.Require().NoError(err) - s.Require().NoError(target.SetImage(ctx, TestImage)) - s.Require().NoError(target.SetCommand("sleep", "infinity")) - s.Require().NoError(target.Commit()) + ts.Require().NoError(target.SetImage(ctx, testImage)) + ts.Require().NoError(target.SetCommand("sleep", "infinity")) + ts.Require().NoError(target.Commit()) - s.T().Cleanup(func() { + ts.T().Cleanup(func() { s.T().Log("Tearing down Basic Test...") err := instance.BatchDestroy(ctx, target) if err != nil { @@ -34,8 +34,8 @@ func (s *TestSuite) TestBasic() { }) // Test Logic - s.Require().NoError(target.Start(ctx)) - s.Require().NoError(target.WaitInstanceIsRunning(ctx)) + ts.Require().NoError(target.Start(ctx)) + ts.Require().NoError(target.WaitInstanceIsRunning(ctx)) // Perform the test type testCase struct { @@ -48,12 +48,11 @@ func (s *TestSuite) TestBasic() { for _, tc := range tt { tc := tc - s.Run(tc.name, func() { - s.T().Logf("Running test case: %s", tc.name) + ts.Run(tc.name, func() { output, err := target.ExecuteCommand(ctx, "echo", tc.name) - s.Require().NoError(err) + ts.Require().NoError(err) - assert.Contains(s.T(), output, tc.name) + assert.Contains(ts.T(), output, tc.name) }) } diff --git a/e2e/basic/main_test.go b/e2e/basic/main_test.go index f2e0adc..acbf3b8 100644 --- a/e2e/basic/main_test.go +++ b/e2e/basic/main_test.go @@ -12,16 +12,16 @@ func convertViaMap(b bool) int { return table[b] } -func (s *TestSuite) TestMain() { - s.T().Parallel() +func (ts *TestSuite) TestMain() { + ts.T().Parallel() // Setup // Test Logic - s.T().Log("Running test case: TestMain") + ts.T().Log("Running test case: TestMain") // Perform the test - exitVal := s.Run("TestMain", func() { - s.T().Logf("Scope: %s", s.Knuu.Scope()) + exitVal := ts.Run("TestMain", func() { + s.T().Logf("Scope: %s", ts.Knuu.Scope()) }) exitValue := convertViaMap(exitVal) diff --git a/e2e/basic/probe_test.go b/e2e/basic/probe_test.go index 6f21415..912a061 100644 --- a/e2e/basic/probe_test.go +++ b/e2e/basic/probe_test.go @@ -86,3 +86,20 @@ func TestProbe(t *testing.T) { assert.Contains(t, wget, "Hello World!") } + +func (s *TestSuite) TestProbe { + s.T().Parallel() + // Setup + + executor, err := knuu.NewExecutor() + if err != nil { + s.T().Fatalf("Error creating executor: %v", err) + } + + web, err := knuu.NewInstance("web") + if err != nil { + s.T().Fatalf("Error creating instance '%v':", err) + } + err = web.SetImage("docker.io/nginx:latest") + if +} diff --git a/e2e/basic/suite_setup_test.go b/e2e/basic/suite_setup_test.go index db961fd..a9ea0e9 100644 --- a/e2e/basic/suite_setup_test.go +++ b/e2e/basic/suite_setup_test.go @@ -22,14 +22,16 @@ func (s *TestSuite) SetupSuite() { ) s.Knuu, err = knuu.New(ctx) s.Require().NoError(err) - s.T().Logf("Error cleaning up test suite: %v", err) s.Knuu.HandleStopSignal(ctx) } func (s *TestSuite) TearDownSuite() { s.T().Cleanup(func() { logrus.Info("Tearing down test suite...") - s.Require().NoError(s.Knuu.CleanUp(context.Background())) + err := s.Knuu.CleanUp(context.Background()) + if err != nil { + s.T().Logf("Error cleaning up test suite: %v", err) + } }) } From 8288a662611d3dd2be0593f3361800d026cb8ae7 Mon Sep 17 00:00:00 2001 From: Alex Kiss <769991+sysrex@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:37:51 +0100 Subject: [PATCH 5/9] fix: typo --- e2e/basic/probe_test.go | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/e2e/basic/probe_test.go b/e2e/basic/probe_test.go index 912a061..6f21415 100644 --- a/e2e/basic/probe_test.go +++ b/e2e/basic/probe_test.go @@ -86,20 +86,3 @@ func TestProbe(t *testing.T) { assert.Contains(t, wget, "Hello World!") } - -func (s *TestSuite) TestProbe { - s.T().Parallel() - // Setup - - executor, err := knuu.NewExecutor() - if err != nil { - s.T().Fatalf("Error creating executor: %v", err) - } - - web, err := knuu.NewInstance("web") - if err != nil { - s.T().Fatalf("Error creating instance '%v':", err) - } - err = web.SetImage("docker.io/nginx:latest") - if -} From 486d503710ebf6e14c938e9220bbd26899b29360 Mon Sep 17 00:00:00 2001 From: Alex Kiss <769991+sysrex@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:53:09 +0100 Subject: [PATCH 6/9] fix: typo --- e2e/basic/basic_test.go | 4 ++-- e2e/basic/main_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/e2e/basic/basic_test.go b/e2e/basic/basic_test.go index 290c8b8..ef58d69 100644 --- a/e2e/basic/basic_test.go +++ b/e2e/basic/basic_test.go @@ -26,10 +26,10 @@ func (ts *TestSuite) TestBasic() { ts.Require().NoError(target.Commit()) ts.T().Cleanup(func() { - s.T().Log("Tearing down Basic Test...") + ts.T().Log("Tearing down Basic Test...") err := instance.BatchDestroy(ctx, target) if err != nil { - s.T().Logf("error destroying instances: %v", err) + ts.T().Logf("error destroying instances: %v", err) } }) diff --git a/e2e/basic/main_test.go b/e2e/basic/main_test.go index acbf3b8..3d8ea32 100644 --- a/e2e/basic/main_test.go +++ b/e2e/basic/main_test.go @@ -21,7 +21,7 @@ func (ts *TestSuite) TestMain() { // Perform the test exitVal := ts.Run("TestMain", func() { - s.T().Logf("Scope: %s", ts.Knuu.Scope()) + ts.T().Logf("Scope: %s", ts.Knuu.Scope()) }) exitValue := convertViaMap(exitVal) From 1f30791a6348ba26e1c550e193f551b97f8b18c1 Mon Sep 17 00:00:00 2001 From: Mojtaba Date: Fri, 5 Jul 2024 10:12:11 +0200 Subject: [PATCH 7/9] fixed --- e2e/basic/suite_setup_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/basic/suite_setup_test.go b/e2e/basic/suite_setup_test.go index a9ea0e9..c1de649 100644 --- a/e2e/basic/suite_setup_test.go +++ b/e2e/basic/suite_setup_test.go @@ -20,7 +20,7 @@ func (s *TestSuite) SetupSuite() { err error ctx = context.Background() ) - s.Knuu, err = knuu.New(ctx) + s.Knuu, err = knuu.New(ctx, knuu.Options{}) s.Require().NoError(err) s.Knuu.HandleStopSignal(ctx) } From ff8201b511a003bc3c07d5a1fcb7a5c1796d9136 Mon Sep 17 00:00:00 2001 From: Mojtaba Date: Fri, 5 Jul 2024 10:49:11 +0200 Subject: [PATCH 8/9] fix: trim spaces --- e2e/basic/basic_test.go | 20 +++++++------------- e2e/basic/main_test.go | 33 +++++++++++---------------------- e2e/basic/suite_setup_test.go | 4 +++- 3 files changed, 21 insertions(+), 36 deletions(-) diff --git a/e2e/basic/basic_test.go b/e2e/basic/basic_test.go index ef58d69..b1578fd 100644 --- a/e2e/basic/basic_test.go +++ b/e2e/basic/basic_test.go @@ -2,10 +2,9 @@ package basic import ( "context" + "strings" "github.com/stretchr/testify/assert" - - "github.com/celestiaorg/knuu/pkg/instance" ) const ( @@ -26,24 +25,19 @@ func (ts *TestSuite) TestBasic() { ts.Require().NoError(target.Commit()) ts.T().Cleanup(func() { - ts.T().Log("Tearing down Basic Test...") - err := instance.BatchDestroy(ctx, target) - if err != nil { - ts.T().Logf("error destroying instances: %v", err) + if err := target.Destroy(ctx); err != nil { + ts.T().Logf("error destroying instance: %v", err) } }) // Test Logic ts.Require().NoError(target.Start(ctx)) - ts.Require().NoError(target.WaitInstanceIsRunning(ctx)) // Perform the test - type testCase struct { + tt := []struct { name string - } - - tt := []testCase{ - {"Hello World"}, + }{ + {name: "Hello World"}, } for _, tc := range tt { @@ -52,8 +46,8 @@ func (ts *TestSuite) TestBasic() { output, err := target.ExecuteCommand(ctx, "echo", tc.name) ts.Require().NoError(err) + output = strings.TrimSpace(output) assert.Contains(ts.T(), output, tc.name) }) } - } diff --git a/e2e/basic/main_test.go b/e2e/basic/main_test.go index 3d8ea32..5a3cb61 100644 --- a/e2e/basic/main_test.go +++ b/e2e/basic/main_test.go @@ -2,29 +2,18 @@ package basic import ( "os" + "testing" + + "github.com/celestiaorg/knuu/pkg/knuu" + "github.com/sirupsen/logrus" ) -func convertViaMap(b bool) int { - table := map[bool]int{ - true: 1, - false: 0, +func TestMain(m *testing.M) { + err := knuu.Initialize() + if err != nil { + logrus.Fatalf("error initializing knuu: %v", err) } - return table[b] -} - -func (ts *TestSuite) TestMain() { - ts.T().Parallel() - // Setup - - // Test Logic - ts.T().Log("Running test case: TestMain") - - // Perform the test - exitVal := ts.Run("TestMain", func() { - ts.T().Logf("Scope: %s", ts.Knuu.Scope()) - }) - - exitValue := convertViaMap(exitVal) - - os.Exit(exitValue) + logrus.Infof("Scope: %s", knuu.Scope()) + exitVal := m.Run() + os.Exit(exitVal) } diff --git a/e2e/basic/suite_setup_test.go b/e2e/basic/suite_setup_test.go index c1de649..f2a0ec0 100644 --- a/e2e/basic/suite_setup_test.go +++ b/e2e/basic/suite_setup_test.go @@ -20,7 +20,9 @@ func (s *TestSuite) SetupSuite() { err error ctx = context.Background() ) - s.Knuu, err = knuu.New(ctx, knuu.Options{}) + s.Knuu, err = knuu.New(ctx, knuu.Options{ + TestScope: "e2e-basic", + }) s.Require().NoError(err) s.Knuu.HandleStopSignal(ctx) } From 62be002fa3deb3e5de119aee72893ff364df3faf Mon Sep 17 00:00:00 2001 From: Mojtaba Date: Fri, 5 Jul 2024 11:01:13 +0200 Subject: [PATCH 9/9] fix: linter --- e2e/basic/main_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/e2e/basic/main_test.go b/e2e/basic/main_test.go index 5a3cb61..e09a0f5 100644 --- a/e2e/basic/main_test.go +++ b/e2e/basic/main_test.go @@ -4,8 +4,9 @@ import ( "os" "testing" - "github.com/celestiaorg/knuu/pkg/knuu" "github.com/sirupsen/logrus" + + "github.com/celestiaorg/knuu/pkg/knuu" ) func TestMain(m *testing.M) {