Skip to content

Commit 9a30dfc

Browse files
Extract code to run test binary and log outputs (#5902)
Extract code for running a binary in tests to be able to reuse it in other places. --------- Signed-off-by: Mend Renovate <[email protected]> Signed-off-by: Yuri Shkuro <[email protected]> Co-authored-by: Yuri Shkuro <[email protected]> Co-authored-by: Yuri Shkuro <[email protected]>
1 parent 9a2921a commit 9a30dfc

File tree

1 file changed

+53
-41
lines changed

1 file changed

+53
-41
lines changed

cmd/jaeger/internal/integration/e2e_integration.go

+53-41
Original file line numberDiff line numberDiff line change
@@ -48,78 +48,90 @@ type E2EStorageIntegration struct {
4848
HealthCheckEndpoint string
4949
}
5050

51-
// e2eInitialize starts the Jaeger-v2 collector with the provided config file,
52-
// it also initialize the SpanWriter and SpanReader below.
53-
// This function should be called before any of the tests start.
54-
func (s *E2EStorageIntegration) e2eInitialize(t *testing.T, storage string) {
55-
logger := zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller()))
56-
if s.BinaryName == "" {
57-
s.BinaryName = "jaeger-v2"
58-
}
59-
configFile := s.ConfigFile
60-
if !s.SkipStorageCleaner {
61-
configFile = createStorageCleanerConfig(t, s.ConfigFile, storage)
62-
}
63-
configFile, err := filepath.Abs(configFile)
64-
require.NoError(t, err, "Failed to get absolute path of the config file")
65-
require.FileExists(t, configFile, "Config file does not exist at the resolved path")
66-
67-
t.Logf("Starting %s in the background with config file %s", s.BinaryName, configFile)
51+
// Binary is a wrapper around exec.Cmd to help running binaries in tests.
52+
type Binary struct {
53+
Name string
54+
exec.Cmd
55+
}
6856

57+
func (b *Binary) Start(t *testing.T) {
6958
outFile, err := os.OpenFile(
70-
filepath.Join(t.TempDir(), "jaeger_output_logs.txt"),
59+
filepath.Join(t.TempDir(), "output_logs.txt"),
7160
os.O_CREATE|os.O_WRONLY,
7261
os.ModePerm,
7362
)
7463
require.NoError(t, err)
75-
t.Logf("Writing the %s output logs into %s", s.BinaryName, outFile.Name())
64+
t.Logf("Writing the %s output logs into %s", b.Name, outFile.Name())
7665

7766
errFile, err := os.OpenFile(
78-
filepath.Join(t.TempDir(), "jaeger_error_logs.txt"),
67+
filepath.Join(t.TempDir(), "error_logs.txt"),
7968
os.O_CREATE|os.O_WRONLY,
8069
os.ModePerm,
8170
)
8271
require.NoError(t, err)
83-
t.Logf("Writing the %s error logs into %s", s.BinaryName, errFile.Name())
84-
85-
cmd := exec.Cmd{
86-
Path: "./cmd/jaeger/jaeger",
87-
Args: []string{"jaeger", "--config", configFile},
88-
// Change the working directory to the root of this project
89-
// since the binary config file jaeger_query's ui_config points to
90-
// "./cmd/jaeger/config-ui.json"
91-
Dir: "../../../..",
92-
Stdout: outFile,
93-
Stderr: errFile,
94-
}
95-
t.Logf("Running command: %v", cmd.Args)
96-
require.NoError(t, cmd.Start())
72+
t.Logf("Writing the %s error logs into %s", b.Name, errFile.Name())
73+
74+
b.Stdout = outFile
75+
b.Stderr = errFile
76+
77+
t.Logf("Running command: %v", b.Args)
78+
require.NoError(t, b.Cmd.Start())
9779
t.Cleanup(func() {
98-
if err := cmd.Process.Kill(); err != nil {
99-
t.Errorf("Failed to kill %s process: %v", s.BinaryName, err)
80+
if err := b.Process.Kill(); err != nil {
81+
t.Errorf("Failed to kill %s process: %v", b.Name, err)
10082
}
10183
if t.Failed() {
102-
// A Github Actions special annotation to create a foldable section
103-
// in the Github runner output.
84+
// A special annotation to create a foldable section in the GitHub Actions runner output.
10485
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines
105-
fmt.Printf("::group::🚧 🚧 🚧 %s binary logs\n", s.BinaryName)
86+
fmt.Printf("::group::🚧 🚧 🚧 %s binary logs\n", b.Name)
10687
outLogs, err := os.ReadFile(outFile.Name())
10788
if err != nil {
10889
t.Errorf("Failed to read output logs: %v", err)
10990
} else {
110-
fmt.Printf("🚧 🚧 🚧 %s output logs:\n%s", s.BinaryName, outLogs)
91+
fmt.Printf("🚧 🚧 🚧 %s output logs:\n%s", b.Name, outLogs)
11192
}
11293

11394
errLogs, err := os.ReadFile(errFile.Name())
11495
if err != nil {
11596
t.Errorf("Failed to read error logs: %v", err)
11697
} else {
117-
fmt.Printf("🚧 🚧 🚧 %s error logs:\n%s", s.BinaryName, errLogs)
98+
fmt.Printf("🚧 🚧 🚧 %s error logs:\n%s", b.Name, errLogs)
11899
}
119100
// End of Github Actions foldable section annotation.
120101
fmt.Println("::endgroup::")
121102
}
122103
})
104+
}
105+
106+
// e2eInitialize starts the Jaeger-v2 collector with the provided config file,
107+
// it also initialize the SpanWriter and SpanReader below.
108+
// This function should be called before any of the tests start.
109+
func (s *E2EStorageIntegration) e2eInitialize(t *testing.T, storage string) {
110+
logger := zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller()))
111+
if s.BinaryName == "" {
112+
s.BinaryName = "jaeger-v2"
113+
}
114+
configFile := s.ConfigFile
115+
if !s.SkipStorageCleaner {
116+
configFile = createStorageCleanerConfig(t, s.ConfigFile, storage)
117+
}
118+
configFile, err := filepath.Abs(configFile)
119+
require.NoError(t, err, "Failed to get absolute path of the config file")
120+
require.FileExists(t, configFile, "Config file does not exist at the resolved path")
121+
122+
t.Logf("Starting %s in the background with config file %s", s.BinaryName, configFile)
123+
cmd := Binary{
124+
Name: s.BinaryName,
125+
Cmd: exec.Cmd{
126+
Path: "./cmd/jaeger/jaeger",
127+
Args: []string{"jaeger", "--config", configFile},
128+
// Change the working directory to the root of this project
129+
// since the binary config file jaeger_query's ui_config points to
130+
// "./cmd/jaeger/config-ui.json"
131+
Dir: "../../../..",
132+
},
133+
}
134+
cmd.Start(t)
123135

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

0 commit comments

Comments
 (0)