Skip to content

Commit

Permalink
Add MySQL Go test to GitHub workflows (#100)
Browse files Browse the repository at this point in the history
* Add MySQL Go test to GitHub workflows

* Update database user credential

* Add MySQL test to connect to the underlying test database

* Update MySQL URI to use localhost
  • Loading branch information
roger2hk authored Aug 1, 2024
1 parent 94afa30 commit 01d2050
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
36 changes: 29 additions & 7 deletions .github/workflows/go_test.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
on: [push, pull_request]
name: Test Go

on: [push, pull_request]

permissions:
contents: read

jobs:
test:
strategy:
matrix:
go-version: [1.22.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}

steps:
- uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2
with:
go-version: ${{ matrix.go-version }}
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v3.1.0
- run: go test -v -race -covermode=atomic -coverprofile=coverage.out ./...
- uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4.5.0

test-mysql:
env:
DB_DATABASE: test_tessera
DB_USER: root
DB_PASSWORD: root

runs-on: ubuntu-latest

steps:
- uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2
with:
go-version: ${{ matrix.go-version }}
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v3.1.0
- run: go test -v -race -covermode=atomic -coverprofile=coverage.out ./...
- uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4.5.0
- name: Checkout code
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v3.1.0
- name: Start MySQL
run: |
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/...
32 changes: 32 additions & 0 deletions storage/mysql/mysql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package mysql_test

import (
"context"
"database/sql"
"flag"
"os"
"testing"

"k8s.io/klog/v2"
)

var mysqlURI = flag.String("mysql_uri", "root:root@tcp(localhost:3306)/test_tessera", "Connection string for a MySQL database")

func TestMain(m *testing.M) {
klog.InitFlags(nil)
flag.Parse()
ctx := context.Background()

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

os.Exit(m.Run())
}

0 comments on commit 01d2050

Please sign in to comment.