Skip to content

Commit e2e27bc

Browse files
committed
tests: Print duration of setup steps
1 parent 406ce4d commit e2e27bc

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed

internal/testutils/cmd.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package testutils
2+
3+
import (
4+
"log"
5+
"os/exec"
6+
"strings"
7+
"time"
8+
)
9+
10+
// RunWithTiming runs the given command while logging its duration with the provided message.
11+
func RunWithTiming(msg string, cmd *exec.Cmd) error {
12+
log.Printf("%s: %s", msg, strings.Join(cmd.Args, " "))
13+
start := time.Now()
14+
err := cmd.Run()
15+
duration := time.Since(start)
16+
log.Printf("Finished in %.3fs", duration.Seconds())
17+
return err
18+
}
19+
20+
// CombinedOutputWithTiming runs the given command while logging its duration with the provided message
21+
// and returns its combined standard output and standard error.
22+
func CombinedOutputWithTiming(msg string, cmd *exec.Cmd) ([]byte, error) {
23+
log.Printf("%s: %s", msg, strings.Join(cmd.Args, " "))
24+
start := time.Now()
25+
out, err := cmd.CombinedOutput()
26+
duration := time.Since(start)
27+
log.Printf("Finished in %.3fs", duration.Seconds())
28+
return out, err
29+
}

internal/testutils/daemon.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,16 @@ paths:
169169
}
170170

171171
// Start the daemon
172+
start := time.Now()
172173
stopped = make(chan struct{})
173174
processPid := make(chan int)
174175
go func() {
175176
defer close(stopped)
176177
var b bytes.Buffer
177178
cmd.Stdout = &b
178179
cmd.Stderr = &b
180+
181+
t.Logf("Setup: Starting daemon: %s", cmd.String())
179182
err := cmd.Start()
180183
require.NoError(t, err, "Setup: daemon cannot start %v", cmd.Args)
181184
if opts.pidFile != "" {
@@ -226,6 +229,8 @@ paths:
226229
// Block until the daemon is started and ready to accept connections.
227230
err = grpcutils.WaitForConnection(ctx, conn, time.Second*30)
228231
require.NoError(t, err, "Setup: wait for daemon to be ready timed out")
232+
duration := time.Since(start)
233+
t.Logf("Setup: daemon is ready after %.3fs", duration.Seconds())
229234

230235
if opts.pidFile != "" {
231236
err := os.WriteFile(opts.pidFile, []byte(fmt.Sprint(<-processPid)), 0600)

internal/testutils/rust.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ func BuildRustNSSLib(t *testing.T, disableCoverage bool, features ...string) (li
101101
cmd.Env = append(cmd.Env, "RUSTFLAGS=-Zsanitizer=address")
102102
}
103103

104-
t.Log("Building NSS library...", cmd.Args)
105-
err = cmd.Run()
104+
err = RunWithTiming("Building NSS library", cmd)
106105
require.NoError(t, err, "Setup: could not build Rust NSS library")
107106

108107
// When building the crate with dh-cargo, this env is set to indicate which architecture the code

pam/integration-tests/helpers_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,9 @@ func buildPAMExecChild(t *testing.T) string {
221221
cmd.Env = append(os.Environ(), `CGO_CFLAGS=-O0 -g3`)
222222

223223
authdPam := filepath.Join(t.TempDir(), "authd-pam")
224-
t.Logf("Compiling Exec child at %s", authdPam)
225-
t.Log(strings.Join(cmd.Args, " "))
226224

227225
cmd.Args = append(cmd.Args, "-o", authdPam)
228-
out, err := cmd.CombinedOutput()
226+
out, err := testutils.CombinedOutputWithTiming("Building PAM exec child", cmd)
229227
require.NoError(t, err, "Setup: could not compile PAM exec child: %s", out)
230228

231229
return authdPam

pam/integration-tests/modulehelpers_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ func buildCModule(t *testing.T, sources []string, pkgConfigDeps []string, cFlags
3535

3636
require.NoError(t, os.MkdirAll(filepath.Dir(libPath), 0700),
3737
"Setup: Can't create loader build path")
38-
t.Logf("Compiling C Module library at %s", libPath)
3938
cmd.Args = append(cmd.Args, "-o", libPath)
4039
cmd.Args = append(cmd.Args, sources...)
4140
cmd.Args = append(cmd.Args,
@@ -119,8 +118,7 @@ func buildCModule(t *testing.T, sources []string, pkgConfigDeps []string, cFlags
119118
})
120119
}
121120

122-
t.Logf("Running compiler command: %s %s", cmd.Path, strings.Join(cmd.Args[1:], " "))
123-
out, err := cmd.CombinedOutput()
121+
out, err := testutils.CombinedOutputWithTiming("Building PAM module", cmd)
124122
require.NoError(t, err, "Setup: could not compile C module %s: %s", soname, out)
125123
if string(out) != "" {
126124
t.Log(string(out))

pam/integration-tests/ssh_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ func startSSHd(t *testing.T, hostKey, forcedCommand string, env []string, daemon
675675
}
676676

677677
t.Logf("Launching sshd: cmd: %s\nenv: %s", sshd.String(), sshd.Env)
678+
start := time.Now()
678679
err = sshd.Start()
679680
require.NoError(t, err, "Setup: Impossible to start sshd")
680681
sshdPid := sshd.Process.Pid
@@ -795,8 +796,9 @@ func startSSHd(t *testing.T, hostKey, forcedCommand string, env []string, daemon
795796
pidFileContent, err := os.ReadFile(sshdPidFile)
796797
require.NoError(t, err, "Setup: Reading SSHd pid file failed")
797798

798-
t.Logf("SSHd started with pid %d (%s) and listening on port %s",
799-
sshdPid, strings.TrimSpace(string(pidFileContent)), sshdPort)
799+
duration := time.Since(start)
800+
t.Logf("SSHd started in %.3fs - pid: %d (%s), listen port: %s",
801+
duration.Seconds(), sshdPid, strings.TrimSpace(string(pidFileContent)), sshdPort)
800802

801803
return sshdPort
802804
}

0 commit comments

Comments
 (0)