-
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.
- Loading branch information
1 parent
f86e86c
commit 3b02a98
Showing
6 changed files
with
223 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package sidecars | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
) | ||
|
||
func (s *Suite) TestExecuteCommandInSidecar() { | ||
const ( | ||
namePrefix = "execute-command-in-sidecar" | ||
cmdMsg = "Hello World!" | ||
command = "echo " + cmdMsg | ||
) | ||
|
||
target, err := s.Knuu.NewInstance(namePrefix + "-target") | ||
s.Require().NoError(err) | ||
|
||
ctx := context.Background() | ||
s.Require().NoError(target.Build().SetImage(ctx, alpineImage)) | ||
s.Require().NoError(target.Build().SetArgs("tail", "-f", "/dev/null")) // Keep the container running | ||
s.Require().NoError(target.Build().Commit(ctx)) | ||
|
||
sidecar := &testSidecar{ | ||
StartCommand: []string{"sh", "-c", "sleep infinity"}, | ||
} | ||
|
||
s.Require().NoError(target.Sidecars().Add(ctx, sidecar)) | ||
s.Require().NoError(target.Execution().Start(ctx)) | ||
|
||
// 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,37 @@ | ||
package sidecars | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
func (s *Suite) TestDownloadFileFromRunningSidecar() { | ||
const ( | ||
namePrefix = "download-file-running-sidecar" | ||
fileContent = "Hello World!" | ||
filePath = "/hello.txt" | ||
) | ||
|
||
target, err := s.Knuu.NewInstance(namePrefix + "-target") | ||
s.Require().NoError(err) | ||
|
||
ctx := context.Background() | ||
s.Require().NoError(target.Build().SetImage(ctx, alpineImage)) | ||
s.Require().NoError(target.Build().SetArgs("tail", "-f", "/dev/null")) // Keep the container running | ||
s.Require().NoError(target.Build().Commit(ctx)) | ||
|
||
sidecar := &testSidecar{ | ||
StartCommand: []string{"sh", "-c", "sleep infinity"}, | ||
} | ||
|
||
s.Require().NoError(target.Sidecars().Add(ctx, sidecar)) | ||
s.Require().NoError(target.Execution().Start(ctx)) | ||
|
||
// 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, "Error getting file bytes") | ||
|
||
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,45 @@ | ||
package sidecars | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"time" | ||
) | ||
|
||
const expectedLogMsg = "Hello World" | ||
|
||
func (s *Suite) TestLogsWithSidecar() { | ||
const namePrefix = "logs-sidecar" | ||
ctx := context.Background() | ||
|
||
// Create a new instance | ||
target, err := s.Knuu.NewInstance(namePrefix + "-target") | ||
s.Require().NoError(err) | ||
|
||
sidecar := &testSidecar{ | ||
StartCommand: []string{ | ||
"sh", "-c", | ||
fmt.Sprintf("while true; do echo '%s'; sleep 1; done", expectedLogMsg), | ||
}, | ||
} | ||
|
||
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)) | ||
|
||
// Wait for a short duration to allow log generation | ||
time.Sleep(5 * time.Second) | ||
|
||
logStream, err := sidecar.Instance().Monitoring().Logs(ctx) | ||
s.Require().NoError(err) | ||
defer logStream.Close() | ||
|
||
logs, err := io.ReadAll(logStream) | ||
s.Require().NoError(err) | ||
|
||
logOutput := string(logs) | ||
s.Contains(logOutput, expectedLogMsg) | ||
} |
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,60 @@ | ||
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") | ||
} | ||
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, | ||
}, 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,45 @@ | ||
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/knuu" | ||
) | ||
|
||
const ( | ||
testTimeout = time.Minute * 5 // the same time that is used in the ci/cd pipeline | ||
alpineImage = "alpine:latest" | ||
) | ||
|
||
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 | ||
} |