Skip to content

Commit

Permalink
make testdb more comprehensive, add information to database, and modi…
Browse files Browse the repository at this point in the history
…fy visibility
  • Loading branch information
JudahNour committed Jun 21, 2023
1 parent 6253f6f commit ecd348d
Show file tree
Hide file tree
Showing 8 changed files with 116,905 additions and 39 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ test: build

.PHONY: testdb
testdb: export DB_BACKEND=sqlite
testdb: export DB_PATH=out/testdb/output2_sqlite_NoDBPATH.db
testdb: export DB_PATH=out/testdb/output_db.db
testdb: build
rm -f ./out/output.html
rm -f ./out/output2.html
rm -f ./out/testdb/output_sqlite_summary.db
rm -f ./out/testdb/output2_sqlite_summary.db
rm -f ./out/testdb/output_db.db
.${BINARY} -name "KVM Linux" -repo "github.com/kubernetes/minikube/" -pr "6096" -in "testdata/minikube-logs.json" -out_html "./out/output.html" -out_summary out/output_summary.json -db_path out/testdb/output_sqlite_summary.db -details "0c07e808219403a7241ee5a0fc6a85a897594339"
.${BINARY} -name "KVM Linux" -repo "github.com/kubernetes/minikube/" -pr "6096" -in "testdata/Docker_Linux.json" -out_html "./out/output2.html" -out_summary out/output2_summary.json -db_path out/testdb/output2_sqlite_summary.db -details "0c07e808219403a7241ee5a0fc6a85a897594339"
.${BINARY} -name "KVM Linux" -repo "github.com/kubernetes/minikube/" -pr "6096" -in "testdata/Docker_Linux.json" -out_html "./out/output2NoDBPath.html" -details "0c07e808219403a7241ee5a0fc6a85a897594339"
.${BINARY} -name "Docker MacOS" -repo "github.com/kubernetes/minikube/" -pr "16569" -in "testdata/testdb/Docker_macOS.json" -out_html "./out/docker_macOS_output.html" -details "0168d63fc8c165681b1cad1801eadd6bbe2c8a5c"
.${BINARY} -name "KVM Linux containerd" -repo "github.com/kubernetes/minikube/" -pr "16569" -in "testdata/testdb/KVM_Linux_containerd.json" -out_html "./out/kvm_linux_containerd_output.html" -details "0168d63fc8c165681b1cad1801eadd6bbe2c8a5c"
.${BINARY} -name "QEMU MacOS" -repo "github.com/kubernetes/minikube/" -pr "16569" -in "testdata/testdb/QEMU_macOS.json" -out_html "./out/qemu_macos_output.html" -details "0168d63fc8c165681b1cad1801eadd6bbe2c8a5c"


.PHONY: cross
Expand Down
12 changes: 6 additions & 6 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/medyagh/gopogh/pkg/models"
)

// Config is database configuration
type Config struct {
// config is database configuration
type config struct {
Type string
Path string
}
Expand All @@ -20,11 +20,11 @@ type datab interface {
Initialize() error
}

// New handles which database driver to use and initializes the db
func New(cfg Config) (datab, error) {
// newDB handles which database driver to use and initializes the db
func newDB(cfg config) (datab, error) {
switch cfg.Type {
case "sqlite":
return NewSQLite(cfg)
return newSQLite(cfg)
default:
return nil, fmt.Errorf("unknown backend: %q", cfg.Type)
}
Expand All @@ -47,7 +47,7 @@ func FromEnv(backend string, path string) (datab, error) {
return nil, fmt.Errorf("missing DB_PATH")
}

c, err := New(Config{
c, err := newDB(config{
Type: backend,
Path: path,
})
Expand Down
29 changes: 17 additions & 12 deletions pkg/db/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ var createEnvironmentTestsTableSQL = `
NumberOfFail INTEGER,
NumberOfPass INTEGER,
NumberOfSkip INTEGER,
PRIMARY KEY (CommitID)
TotalDuration REAL,
GopoghVersion TEXT,
PRIMARY KEY (CommitID, EnvName)
);
`
var createTestCasesTableSQL = `
Expand All @@ -29,17 +31,20 @@ var createTestCasesTableSQL = `
CommitId TEXT,
TestName TEXT,
Result TEXT,
PRIMARY KEY (CommitId, TestName)
Duration REAL,
EnvName TEXT,
TestOrder INTEGER,
PRIMARY KEY (CommitId, EnvName, TestName)
);
`

type SQLite struct {
type sqlite struct {
db *sqlx.DB
path string
}

// Set adds/updates rows to the database
func (m *SQLite) Set(commitRow models.DBEnvironmentTest, dbRows []models.DBTestCase) error {
func (m *sqlite) Set(commitRow models.DBEnvironmentTest, dbRows []models.DBTestCase) error {
tx, err := m.db.DB.Begin()
if err != nil {
return fmt.Errorf("failed to create SQL transaction: %v", err)
Expand All @@ -52,22 +57,22 @@ func (m *SQLite) Set(commitRow models.DBEnvironmentTest, dbRows []models.DBTestC
}
}()

sqlInsert := `INSERT OR REPLACE INTO db_test_cases (PR, CommitId, TestName, Result) VALUES (?, ?, ?, ?)`
sqlInsert := `INSERT OR REPLACE INTO db_test_cases (PR, CommitId, TestName, Result, Duration, EnvName, TestOrder) VALUES (?, ?, ?, ?, ?, ?, ?)`
stmt, err := tx.Prepare(sqlInsert)
if err != nil {
return fmt.Errorf("failed to prepare SQL insert statement: %v", err)
}
defer stmt.Close()

for _, r := range dbRows {
_, err := stmt.Exec(r.PR, r.CommitID, r.TestName, r.Result)
_, err := stmt.Exec(r.PR, r.CommitID, r.TestName, r.Result, r.Duration, r.EnvName, r.TestOrder)
if err != nil {
return fmt.Errorf("failed to execute SQL insert: %v", err)
}
}

sqlInsert = `INSERT OR REPLACE INTO db_environment_tests (CommitID, EnvName, GopoghTime, TestTime, NumberOfFail, NumberOfPass, NumberOfSkip) VALUES (?, ?, ?, ?, ?, ?, ?)`
_, err = tx.Exec(sqlInsert, commitRow.CommitID, commitRow.EnvName, commitRow.GopoghTime, commitRow.TestTime, commitRow.NumberOfFail, commitRow.NumberOfPass, commitRow.NumberOfSkip)
sqlInsert = `INSERT OR REPLACE INTO db_environment_tests (CommitID, EnvName, GopoghTime, TestTime, NumberOfFail, NumberOfPass, NumberOfSkip, TotalDuration, GopoghVersion) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
_, err = tx.Exec(sqlInsert, commitRow.CommitID, commitRow.EnvName, commitRow.GopoghTime, commitRow.TestTime, commitRow.NumberOfFail, commitRow.NumberOfPass, commitRow.NumberOfSkip, commitRow.TotalDuration, commitRow.GopoghVersion)
if err != nil {
return fmt.Errorf("failed to execute SQL insert: %v", err)
}
Expand All @@ -79,24 +84,24 @@ func (m *SQLite) Set(commitRow models.DBEnvironmentTest, dbRows []models.DBTestC
return rollbackError
}

// NewSQLite opens the database returning an SQLite database struct instance
func NewSQLite(cfg Config) (*SQLite, error) {
// newSQLite opens the database returning an SQLite database struct instance
func newSQLite(cfg config) (*sqlite, error) {
if err := os.MkdirAll(filepath.Dir(cfg.Path), 0755); err != nil {
return nil, fmt.Errorf("failed to create directory: %v", err)
}
database, err := sqlx.Connect("sqlite3", cfg.Path)
if err != nil {
return nil, fmt.Errorf("failed to open database connection: %v", err)
}
m := &SQLite{
m := &sqlite{
db: database,
path: cfg.Path,
}
return m, nil
}

// Initialize creates the tables within the SQLite database
func (m *SQLite) Initialize() error {
func (m *sqlite) Initialize() error {

if _, err := m.db.Exec(createEnvironmentTestsTableSQL); err != nil {
return fmt.Errorf("failed to initialize environment tests table: %w", err)
Expand Down
29 changes: 17 additions & 12 deletions pkg/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,24 @@ type TestGroup struct {

// DBTestCase represents a row in db table that holds each individual subtest
type DBTestCase struct {
PR string
CommitID string
TestName string
Result string
PR string
CommitID string
TestName string
Result string
Duration float64
EnvName string
TestOrder int
}

// DBEnvironmentTest represents a row in db table that has finished tests in each environments
// DBEnvironmentTest represents a row in db table that has finished tests in each environment
type DBEnvironmentTest struct {
CommitID string
EnvName string
GopoghTime string
TestTime string
NumberOfFail int
NumberOfPass int
NumberOfSkip int
CommitID string
EnvName string
GopoghTime string
TestTime string
NumberOfFail int
NumberOfPass int
NumberOfSkip int
TotalDuration float64
GopoghVersion string
}
18 changes: 10 additions & 8 deletions pkg/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,20 @@ func (c DisplayContent) SQL(dbPath string) error {
dbTestRows := make([]models.DBTestCase, 0, expectedRowNumber)
for resultType, testGroups := range c.Results {
for _, test := range testGroups {
r := models.DBTestCase{PR: c.Detail.PR, CommitID: c.Detail.Details, TestName: test.TestName, Result: resultType}
r := models.DBTestCase{PR: c.Detail.PR, CommitID: c.Detail.Details, TestName: test.TestName, Result: resultType, Duration: test.Duration, EnvName: c.Detail.Name, TestOrder: test.TestOrder}
dbTestRows = append(dbTestRows, r)
}
}
dbEnvironmentRow := models.DBEnvironmentTest{
CommitID: c.Detail.Details,
EnvName: c.Detail.Name,
GopoghTime: time.Now().String(),
TestTime: c.TestTime.String(),
NumberOfFail: len(c.Results[fail]),
NumberOfPass: len(c.Results[pass]),
NumberOfSkip: len(c.Results[skip]),
CommitID: c.Detail.Details,
EnvName: c.Detail.Name,
GopoghTime: time.Now().String(),
TestTime: c.TestTime.String(),
NumberOfFail: len(c.Results[fail]),
NumberOfPass: len(c.Results[pass]),
NumberOfSkip: len(c.Results[skip]),
TotalDuration: c.TotalDuration,
GopoghVersion: c.BuildVersion,
}

return database.Set(dbEnvironmentRow, dbTestRows)
Expand Down
Loading

0 comments on commit ecd348d

Please sign in to comment.