Skip to content

Commit

Permalink
adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
facundoolano committed Jul 27, 2024
1 parent 5bfbc73 commit 10c8943
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
1 change: 0 additions & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
"strings"
"time"
)
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func main() {
ctx.FatalIfErrorf(err)
defer dbs.Close()

err = loadLogs(dbs, logPathPattern)
err = loadLogs(logPathPattern, dbs)
ctx.FatalIfErrorf(err)

columnNames, rowValues, err := dbs.QueryTop(spec)
Expand Down Expand Up @@ -186,7 +186,7 @@ func parseDuration(duration string) (time.Time, error) {
}

// Parse the most recent nginx access.logs and insert the ones not previously seen into the DB.
func loadLogs(dbs *dbSession, logPathPattern string) error {
func loadLogs(logPathPattern string, dbs *dbSession) error {
logFiles, err := filepath.Glob(logPathPattern)
if err != nil {
return err
Expand Down
17 changes: 7 additions & 10 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,28 +251,25 @@ func TestMultipleLogFiles(t *testing.T) {

func runCommand(t *testing.T, logs string, cliArgs []string) ([]string, [][]string) {
// write the logs to a temp file, and point the NGTOP_LOGS_PATH env to it
f, err := os.CreateTemp("", "access.log")
logFile, err := os.CreateTemp("", "access.log")
assertEqual(t, err, nil)
defer os.Remove(f.Name())
_, err = f.Write([]byte(logs))
defer os.Remove(logFile.Name())
_, err = logFile.Write([]byte(logs))
assertEqual(t, err, nil)
t.Setenv("NGTOP_LOGS_PATH", f.Name())

// create a one-off db file for the test
f, err = os.CreateTemp("", "ngtop.db")
dbFile, err := os.CreateTemp("", "ngtop.db")
assertEqual(t, err, nil)
defer os.Remove(f.Name())
os.Setenv("NGTOP_DB", f.Name())
defer os.Remove(dbFile.Name())

// some duplication from main here, maybe can refactored away
os.Args = append([]string{"ngtop"}, cliArgs...)
_, spec := querySpecFromCLI()

dbs, err := InitDB()
dbs, err := InitDB(dbFile.Name())
assertEqual(t, err, nil)
defer dbs.Close()

err = loadLogs(dbs)
err = loadLogs(logFile.Name(), dbs)
assertEqual(t, err, nil)
columnNames, rowValues, err := dbs.QueryTop(spec)
assertEqual(t, err, nil)
Expand Down

0 comments on commit 10c8943

Please sign in to comment.