Skip to content

Commit

Permalink
Force MySQL Go test to fail on GitHub workflow if the MySQL test data…
Browse files Browse the repository at this point in the history
…base is not available
  • Loading branch information
roger2hk committed Aug 1, 2024
1 parent 01d2050 commit a616826
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ jobs:
sudo /etc/init.d/mysql start
mysql -e "CREATE DATABASE IF NOT EXISTS $DB_DATABASE;" -u$DB_USER -p$DB_PASSWORD
- name: Test with Go
run: go test -v -race ./storage/mysql/...
run: go test -v -race ./storage/mysql/... -is_mysql_test_optional=false
19 changes: 14 additions & 5 deletions storage/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"k8s.io/klog/v2"
)

var mysqlURI = flag.String("mysql_uri", "root:root@tcp(localhost:3306)/test_tessera", "Connection string for a MySQL database")
var (
mysqlURI = flag.String("mysql_uri", "root:root@tcp(localhost:3306)/test_tessera", "Connection string for a MySQL database")
isMySQLTestOptional = flag.Bool("is_mysql_test_optional", true, "Boolean value to control whether the MySQL test is optional")
)

func TestMain(m *testing.M) {
klog.InitFlags(nil)
Expand All @@ -19,12 +22,18 @@ func TestMain(m *testing.M) {

db, err := sql.Open("mysql", *mysqlURI)
if err != nil {
klog.Errorf("MySQL not available, skipping all MySQL storage tests")
return
if *isMySQLTestOptional {
klog.Warning("MySQL not available, skipping all MySQL storage tests")
return
}
klog.Fatalf("Failed to open MySQL test db: %v", err)
}
if err := db.PingContext(ctx); err != nil {
klog.Errorf("MySQL not available, skipping all MySQL storage tests")
return
if *isMySQLTestOptional {
klog.Warning("MySQL not available, skipping all MySQL storage tests")
return
}
klog.Fatalf("Failed to ping MySQL test db: %v", err)
}
klog.Info("Successfully connected to MySQL test database")

Expand Down

0 comments on commit a616826

Please sign in to comment.