Skip to content

Commit

Permalink
extract code to run binary
Browse files Browse the repository at this point in the history
Signed-off-by: Yuri Shkuro <[email protected]>
  • Loading branch information
yurishkuro committed Aug 31, 2024
1 parent 33abea7 commit 0e711e7
Showing 1 changed file with 53 additions and 41 deletions.
94 changes: 53 additions & 41 deletions cmd/jaeger/internal/integration/e2e_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,78 +48,90 @@ type E2EStorageIntegration struct {
HealthCheckEndpoint string
}

// e2eInitialize starts the Jaeger-v2 collector with the provided config file,
// it also initialize the SpanWriter and SpanReader below.
// This function should be called before any of the tests start.
func (s *E2EStorageIntegration) e2eInitialize(t *testing.T, storage string) {
logger := zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller()))
if s.BinaryName == "" {
s.BinaryName = "jaeger-v2"
}
configFile := s.ConfigFile
if !s.SkipStorageCleaner {
configFile = createStorageCleanerConfig(t, s.ConfigFile, storage)
}
configFile, err := filepath.Abs(configFile)
require.NoError(t, err, "Failed to get absolute path of the config file")
require.FileExists(t, configFile, "Config file does not exist at the resolved path")

t.Logf("Starting %s in the background with config file %s", s.BinaryName, configFile)
// Binary is a wrapper around exec.Cmd to help running binaries in tests.
type Binary struct {
Name string
exec.Cmd
}

func (b *Binary) Start(t *testing.T) {
outFile, err := os.OpenFile(
filepath.Join(t.TempDir(), "jaeger_output_logs.txt"),
filepath.Join(t.TempDir(), "output_logs.txt"),
os.O_CREATE|os.O_WRONLY,
os.ModePerm,
)
require.NoError(t, err)
t.Logf("Writing the %s output logs into %s", s.BinaryName, outFile.Name())
t.Logf("Writing the %s output logs into %s", b.Name, outFile.Name())

errFile, err := os.OpenFile(
filepath.Join(t.TempDir(), "jaeger_error_logs.txt"),
filepath.Join(t.TempDir(), "error_logs.txt"),
os.O_CREATE|os.O_WRONLY,
os.ModePerm,
)
require.NoError(t, err)
t.Logf("Writing the %s error logs into %s", s.BinaryName, errFile.Name())

cmd := exec.Cmd{
Path: "./cmd/jaeger/jaeger",
Args: []string{"jaeger", "--config", configFile},
// Change the working directory to the root of this project
// since the binary config file jaeger_query's ui_config points to
// "./cmd/jaeger/config-ui.json"
Dir: "../../../..",
Stdout: outFile,
Stderr: errFile,
}
t.Logf("Running command: %v", cmd.Args)
require.NoError(t, cmd.Start())
t.Logf("Writing the %s error logs into %s", b.Name, errFile.Name())

b.Stdout = outFile
b.Stderr = errFile

t.Logf("Running command: %v", b.Args)
require.NoError(t, b.Cmd.Start())
t.Cleanup(func() {
if err := cmd.Process.Kill(); err != nil {
t.Errorf("Failed to kill %s process: %v", s.BinaryName, err)
if err := b.Process.Kill(); err != nil {
t.Errorf("Failed to kill %s process: %v", b.Name, err)
}
if t.Failed() {
// A Github Actions special annotation to create a foldable section
// in the Github runner output.
// A special annotation to create a foldable section in the GitHub Actions runner output.
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines
fmt.Printf("::group::🚧 🚧 🚧 %s binary logs\n", s.BinaryName)
fmt.Printf("::group::🚧 🚧 🚧 %s binary logs\n", b.Name)
outLogs, err := os.ReadFile(outFile.Name())
if err != nil {
t.Errorf("Failed to read output logs: %v", err)
} else {
fmt.Printf("🚧 🚧 🚧 %s output logs:\n%s", s.BinaryName, outLogs)
fmt.Printf("🚧 🚧 🚧 %s output logs:\n%s", b.Name, outLogs)
}

errLogs, err := os.ReadFile(errFile.Name())
if err != nil {
t.Errorf("Failed to read error logs: %v", err)
} else {
fmt.Printf("🚧 🚧 🚧 %s error logs:\n%s", s.BinaryName, errLogs)
fmt.Printf("🚧 🚧 🚧 %s error logs:\n%s", b.Name, errLogs)
}
// End of Github Actions foldable section annotation.
fmt.Println("::endgroup::")
}
})
}

// e2eInitialize starts the Jaeger-v2 collector with the provided config file,
// it also initialize the SpanWriter and SpanReader below.
// This function should be called before any of the tests start.
func (s *E2EStorageIntegration) e2eInitialize(t *testing.T, storage string) {
logger := zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller()))
if s.BinaryName == "" {
s.BinaryName = "jaeger-v2"
}
configFile := s.ConfigFile
if !s.SkipStorageCleaner {
configFile = createStorageCleanerConfig(t, s.ConfigFile, storage)
}
configFile, err := filepath.Abs(configFile)
require.NoError(t, err, "Failed to get absolute path of the config file")
require.FileExists(t, configFile, "Config file does not exist at the resolved path")

t.Logf("Starting %s in the background with config file %s", s.BinaryName, configFile)
cmd := Binary{
Name: s.BinaryName,
Cmd: exec.Cmd{
Path: "./cmd/jaeger/jaeger",
Args: []string{"jaeger", "--config", configFile},
// Change the working directory to the root of this project
// since the binary config file jaeger_query's ui_config points to
// "./cmd/jaeger/config-ui.json"
Dir: "../../../..",
},
}
cmd.Start(t)

// Wait for the binary to start and become ready to serve requests.
require.Eventually(t, func() bool { return s.doHealthCheck(t) },
Expand Down

0 comments on commit 0e711e7

Please sign in to comment.