Skip to content

Commit

Permalink
basic bats
Browse files Browse the repository at this point in the history
  • Loading branch information
max-hoffman committed Mar 4, 2025
1 parent b5aa110 commit f7ac3f9
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 25 deletions.
51 changes: 26 additions & 25 deletions go/cmd/dolt/commands/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"log"
"os"
"path/filepath"
"strings"
"time"
)

Expand Down Expand Up @@ -134,21 +133,23 @@ func (cmd DebugCmd) Exec(ctx context.Context, commandStr string, args []string,
return sqlHandleVErrAndExitCode(queryist, errhand.BuildDError("cannot run debug mode on a pre-existing server").Build(), usage)
}

// create a temp directory
tempDir, err := os.MkdirTemp("", "dolt-debug-*")
if err != nil {
return sqlHandleVErrAndExitCode(queryist, errhand.BuildDError("couldn't create tempdir %w", err).Build(), usage)
}

_, continueOnError := apr.GetValue(continueFlag)
outFile, ok := apr.GetValue(outputFlag)
outDir, outputDirSpecified := apr.GetValue(outputFlag)
if !outputDirSpecified {
outDir, err = os.MkdirTemp("", "dolt-debug-*")
if err != nil {
return sqlHandleVErrAndExitCode(queryist, errhand.BuildDError("couldn't create tempdir %w", err).Build(), usage)
}
} else {
err := os.Mkdir(outDir, os.ModePerm)
if err != nil {
return sqlHandleVErrAndExitCode(queryist, errhand.BuildDError("failed to create output directory %w", err).Build(), usage)
}
}

if outFile != "" {
if outputDirSpecified {
defer func() {
if !strings.HasPrefix(outFile, ".tar.gz") && !strings.HasPrefix(outFile, ".tgz") {
outFile += ".tar.gz"
}
file, err := os.Create(outFile)
file, err := os.Create(outDir + ".tar.gz")
if err != nil {
cli.Println("failed to create output file " + err.Error())
return
Expand All @@ -161,7 +162,7 @@ func (cmd DebugCmd) Exec(ctx context.Context, commandStr string, args []string,
tarWriter := tar.NewWriter(gzipWriter)
defer tarWriter.Close()

if err := filepath.WalkDir(tempDir, func(path string, d fs.DirEntry, err error) error {
if err := filepath.WalkDir(outDir, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
Expand All @@ -170,7 +171,7 @@ func (cmd DebugCmd) Exec(ctx context.Context, commandStr string, args []string,
cli.Println("failed to create output tar " + err.Error())
}

cli.Println("zipped results in: " + outFile)
cli.Println("zipped results in: " + outDir + ".tar.gz")
}()
}

Expand All @@ -196,9 +197,9 @@ func (cmd DebugCmd) Exec(ctx context.Context, commandStr string, args []string,
defer fileReadProg.close()
}

queryFile, err := os.CreateTemp(tempDir, "input.sql")
queryFile, err := os.Create(filepath.Join(outDir, "input.sql"))
if err != nil {
return sqlHandleVErrAndExitCode(queryist, errhand.BuildDError("couldn't create tempfile %w", err).Build(), usage)
return sqlHandleVErrAndExitCode(queryist, errhand.BuildDError("couldn't create file %s", err.Error()).Build(), usage)
}
defer queryFile.Close()

Expand All @@ -212,12 +213,12 @@ func (cmd DebugCmd) Exec(ctx context.Context, commandStr string, args []string,
return sqlHandleVErrAndExitCode(queryist, errhand.BuildDError("seek input sql %w", err).Build(), usage)
}

err = debugAnalyze(sqlCtx, tempDir, sqlEng, queryFile)
err = debugAnalyze(sqlCtx, outDir, sqlEng, queryFile)
if err != nil {
return sqlHandleVErrAndExitCode(queryist, errhand.VerboseErrorFromError(err), usage)
}

debugFile, err := os.CreateTemp(tempDir, "exec.txt")
debugFile, err := os.Create(filepath.Join(outDir, "exec.txt"))
if err != nil {
if err != nil {
return sqlHandleVErrAndExitCode(queryist, errhand.VerboseErrorFromError(err), usage)
Expand All @@ -226,7 +227,7 @@ func (cmd DebugCmd) Exec(ctx context.Context, commandStr string, args []string,
defer debugFile.Close()

func() {
defer profile.Start(profile.ProfilePath(tempDir), profile.CPUProfile).Stop()
defer profile.Start(profile.ProfilePath(outDir), profile.CPUProfile).Stop()
cli.Println("starting cpu profile...")

origStdout := cli.CliOut
Expand All @@ -251,7 +252,7 @@ func (cmd DebugCmd) Exec(ctx context.Context, commandStr string, args []string,
}()

func() {
defer profile.Start(profile.ProfilePath(tempDir), profile.MemProfile).Stop()
defer profile.Start(profile.ProfilePath(outDir), profile.MemProfile).Stop()
cli.Println("starting mem profile...")

origStdout := cli.CliOut
Expand All @@ -276,7 +277,7 @@ func (cmd DebugCmd) Exec(ctx context.Context, commandStr string, args []string,
}()

func() {
defer profile.Start(profile.ProfilePath(tempDir), profile.TraceProfile).Stop()
defer profile.Start(profile.ProfilePath(outDir), profile.TraceProfile).Stop()
cli.Println("starting trace profile...")

origStdout := cli.CliOut
Expand All @@ -300,7 +301,7 @@ func (cmd DebugCmd) Exec(ctx context.Context, commandStr string, args []string,
}
}()

defer cli.Printf("debug results in: %s\n", tempDir)
defer cli.Printf("debug results in: %s\n", outDir)

return 0
}
Expand Down Expand Up @@ -346,13 +347,13 @@ func debugAnalyze(ctx *sql.Context, tempDir string, sqlEng *engine.SqlEngine, sq
defer func() {
eng.Analyzer.Debug = false
}()
analysisFile, err := os.CreateTemp(tempDir, "analysis.txt")
analysisFile, err := os.Create(filepath.Join(tempDir, "analysis.txt"))
if err != nil {
return err
}
defer analysisFile.Close()

planFile, err := os.CreateTemp(tempDir, "plan.txt")
planFile, err := os.Create(filepath.Join(tempDir, "plan.txt"))
if err != nil {
return err
}
Expand Down
65 changes: 65 additions & 0 deletions integration-tests/bats/debug.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env bats
load $BATS_TEST_DIRNAME/helper/common.bash
load $BATS_TEST_DIRNAME/helper/query-server-common.bash

setup() {
skiponwindows "tests are flaky on Windows"
if [ "$SQL_ENGINE" = "remote-engine" ]; then
skip "This test tests remote connections directly, SQL_ENGINE is not needed."
fi

setup_common

TMPDIRS=$(pwd)/tmpdirs
mkdir -p $TMPDIRS/repo1

cd $TMPDIRS/repo1
dolt init

dolt sql <<SQL
create table ab (a int primary key, b varchar(100), key (b,a));
insert into ab
select * from (
with recursive cte(a,b) as (
select 0,'text_val'
union
select a+1, 'text_val' from cte where a < 1000
)
select * from cte
) dt;
SQL

}

teardown() {
teardown_common
stop_sql_server 1
rm -rf $TMPDIRS
cd $BATS_TMPDIR
}


@test "debug produces expected files" {
run dolt debug -t 1 -o out -q "select count(*) from ab where b = 'text_val'"
[ "$status" -eq 0 ]
[[ "${lines[0]}" =~ "cpu" ]] || false
[[ "${lines[1]}" =~ "mem" ]] || false
[[ "${lines[2]}" =~ "trace" ]] || false

ls
tar -xvzf out.tar.gz

run ls out
[ "$status" -eq 0 ]
[[ "${lines[0]}" =~ "analysis.txt" ]] || false
[[ "${lines[1]}" =~ "cpu.pprof" ]] || false
[[ "${lines[2]}" =~ "exec.txt" ]] || false
[[ "${lines[3]}" =~ "input.sql" ]] || false
[[ "${lines[4]}" =~ "mem.pprof" ]] || false
[[ "${lines[5]}" =~ "plan.txt" ]] || false
[[ "${lines[6]}" =~ "trace.out" ]] || false

run cat out/plan.txt
[ "$status" -eq 0 ]
[[ "$output" =~ "IndexedTableAccess" ]] || false
}

0 comments on commit f7ac3f9

Please sign in to comment.