-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add E2E tests for sidecars (#577)
* chore: add E2E tests for sidecars * fix: rename sidecar file * chore: applied feedback
- Loading branch information
1 parent
f86e86c
commit 9bb917a
Showing
7 changed files
with
224 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package sidecars | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
) | ||
|
||
func (s *Suite) TestExecuteCommandInSidecar() { | ||
const ( | ||
namePrefix = "execute-command-in-sidecar" | ||
cmdMsg = "Hello World!" | ||
command = "echo " + cmdMsg | ||
) | ||
|
||
ctx := context.Background() | ||
|
||
sidecar := &testSidecar{ | ||
StartCommand: []string{"sh", "-c", "sleep infinity"}, | ||
} | ||
s.startNewInstanceWithSidecar(ctx, namePrefix, sidecar) | ||
|
||
// Create a file in the sidecar instance | ||
out, err := sidecar.Instance().Execution().ExecuteCommand(ctx, command) | ||
s.Require().NoError(err, "executing command output: %v", out) | ||
|
||
outTrimmed := strings.TrimSpace(out) | ||
s.Assert().Equal(cmdMsg, outTrimmed) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package sidecars | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
func (s *Suite) TestDownloadFileFromRunningSidecar() { | ||
const ( | ||
namePrefix = "download-file-running-sidecar" | ||
fileContent = "Hello World!" | ||
filePath = "/hello.txt" | ||
) | ||
ctx := context.Background() | ||
|
||
sidecar := &testSidecar{ | ||
StartCommand: []string{"sh", "-c", "sleep infinity"}, | ||
} | ||
s.startNewInstanceWithSidecar(ctx, namePrefix, sidecar) | ||
|
||
// Create a file in the sidecar instance | ||
out, err := sidecar.Instance().Execution().ExecuteCommand(ctx, "echo", "-n", fileContent, ">", filePath) | ||
s.Require().NoError(err, "executing command output: %v", out) | ||
|
||
gotContent, err := sidecar.Instance().Storage().GetFileBytes(ctx, filePath) | ||
s.Require().NoError(err, "Failed to read file %s from sidecar: %v", filePath, err) | ||
|
||
s.Assert().Equal(fileContent, string(gotContent)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package sidecars | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func (s *Suite) TestLogsWithSidecar() { | ||
const ( | ||
namePrefix = "logs-sidecar" | ||
expectedLogMsg = "Hello World" | ||
timeout = 10 * time.Second | ||
interval = 1 * time.Second | ||
) | ||
ctx := context.Background() | ||
|
||
sidecar := &testSidecar{ | ||
StartCommand: []string{ | ||
"sh", "-c", | ||
fmt.Sprintf("while true; do echo '%s'; sleep 1; done", expectedLogMsg), | ||
}, | ||
} | ||
s.startNewInstanceWithSidecar(ctx, namePrefix, sidecar) | ||
|
||
// Wait for a short duration to allow log generation | ||
s.Require().Eventually(func() bool { | ||
logStream, err := sidecar.Instance().Monitoring().Logs(ctx) | ||
if err != nil { | ||
return false | ||
} | ||
defer logStream.Close() | ||
|
||
logs, err := io.ReadAll(logStream) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return strings.Contains(string(logs), expectedLogMsg) | ||
}, timeout, interval, "failed to get expected log message") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package sidecars | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/celestiaorg/knuu/pkg/instance" | ||
"github.com/celestiaorg/knuu/pkg/system" | ||
) | ||
|
||
type testSidecar struct { | ||
instance *instance.Instance | ||
StartCommand []string | ||
} | ||
|
||
var _ instance.SidecarManager = (*testSidecar)(nil) | ||
|
||
func (s *testSidecar) Initialize(ctx context.Context, namePrefix string, sysDeps *system.SystemDependencies) error { | ||
var err error | ||
s.instance, err = instance.New(namePrefix+"-sidecar-logs", sysDeps) | ||
if err != nil { | ||
return err | ||
} | ||
s.instance.Sidecars().SetIsSidecar(true) | ||
|
||
if err := s.instance.Build().SetImage(ctx, alpineImage); err != nil { | ||
return err | ||
} | ||
|
||
err = s.instance.Build().SetStartCommand(s.StartCommand...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := s.instance.Build().Commit(ctx); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (s *testSidecar) PreStart(ctx context.Context) error { | ||
if s.instance == nil { | ||
return errors.New("instance not initialized") | ||
} | ||
if len(s.StartCommand) == 0 { | ||
return errors.New("start command not configured") | ||
} | ||
return nil | ||
} | ||
|
||
func (s *testSidecar) Instance() *instance.Instance { | ||
return s.instance | ||
} | ||
|
||
func (s *testSidecar) Clone(namePrefix string) (instance.SidecarManager, error) { | ||
clone, err := s.instance.CloneWithName(namePrefix + "-" + s.instance.Name()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &testSidecar{ | ||
instance: clone, | ||
StartCommand: append([]string{}, s.StartCommand...), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package sidecars | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/celestiaorg/knuu/e2e" | ||
"github.com/celestiaorg/knuu/pkg/instance" | ||
"github.com/celestiaorg/knuu/pkg/knuu" | ||
) | ||
|
||
const ( | ||
testTimeout = time.Minute * 5 // the same time that is used in the ci/cd pipeline | ||
alpineImage = "alpine:3.20.3" | ||
) | ||
|
||
type Suite struct { | ||
e2e.Suite | ||
} | ||
|
||
func TestRunSuite(t *testing.T) { | ||
suite.Run(t, new(Suite)) | ||
} | ||
|
||
func (s *Suite) SetupSuite() { | ||
var ( | ||
ctx = context.Background() | ||
logger = logrus.New() | ||
err error | ||
) | ||
|
||
s.Knuu, err = knuu.New(ctx, knuu.Options{ | ||
Timeout: testTimeout, | ||
Logger: logger, | ||
}) | ||
s.Require().NoError(err) | ||
|
||
s.T().Logf("Scope: %s", s.Knuu.Scope) | ||
s.Knuu.HandleStopSignal(ctx) | ||
|
||
s.Executor.Kn = s.Knuu | ||
} | ||
|
||
func (s *Suite) startNewInstanceWithSidecar(ctx context.Context, namePrefix string, sidecar *testSidecar) *instance.Instance { | ||
target, err := s.Knuu.NewInstance(namePrefix + "-target") | ||
s.Require().NoError(err) | ||
|
||
s.Require().NoError(target.Build().SetImage(ctx, alpineImage)) | ||
s.Require().NoError(target.Build().SetStartCommand("sh", "-c", "sleep infinity")) | ||
s.Require().NoError(target.Build().Commit(ctx)) | ||
s.Require().NoError(target.Sidecars().Add(ctx, sidecar)) | ||
s.Require().NoError(target.Execution().Start(ctx)) | ||
|
||
return target | ||
} |