package integration
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/bazelbuild/sandboxfs/integration/utils"
)
func TestProfiling_OptionalSupport(t *testing.T) {
tempDir, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err)
}
defer os.RemoveAll(tempDir)
profile := filepath.Join(tempDir, "cpu.prof")
arg := "--cpu_profile=" + profile
if _, ok := utils.GetConfig().Features["profiling"]; ok {
state := utils.MountSetup(t, arg, "--mapping=ro:/:%ROOT%")
state.TearDown(t)
stat, err := os.Lstat(profile)
if err != nil {
t.Fatalf("Cannot find expected profile %s", profile)
}
if stat.Size() == 0 {
t.Errorf("Expected profile %s is empty", profile)
}
} else {
_, stderr, err := utils.RunAndWait(1, arg, filepath.Join(tempDir, "root"))
if err != nil {
t.Fatal(err)
}
wantStderr := "Failed to start CPU profile.*feature not enabled"
if !utils.MatchesRegexp(wantStderr, stderr) {
t.Errorf("Got %s; want stderr to match %s", stderr, wantStderr)
}
}
}