Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust storage tests #25

Merged
merged 2 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 46 additions & 9 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,55 @@ jobs:
- name: Build
run: go build -v ./...

- name: Start test docker containers
if: matrix.platform == 'ubuntu-latest'
run: |
docker-compose -f storage/mysql/docker-compose-test.yml up &

- name: Set
if: matrix.platform == 'ubuntu-latest'
run: echo "NANODEP_MYSQL_STORAGE_TEST=1" >> $GITHUB_ENV

- name: Test
run: go test -v -race ./...

- name: Format
if: matrix.platform == 'ubuntu-latest'
run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi

mysql-integration-test:
name: Integration tests for the MySQL backend.
runs-on: 'ubuntu-latest'
needs: build-test
services:
mysql:
image: mysql:8.0
env:
MYSQL_RANDOM_ROOT_PASSWORD: yes
MYSQL_DATABASE: nanodep
MYSQL_USER: nanodep
MYSQL_PASSWORD: nanodep
ports:
- 3800:3306
options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3
defaults:
run:
shell: bash
env:
MYSQL_PWD: nanodep
PORT: 3800
steps:
- uses: actions/checkout@v3

- name: setup go
uses: actions/setup-go@v4
with:
go-version: '1.17.x'

- name: Verify MySQL connection
run: |
while ! mysqladmin ping --host=localhost --port=$PORT --protocol=TCP --silent; do
sleep 1
done

- name: Set up schema
run: |
mysql --version
mysql --user=nanodep --host=localhost --port=$PORT --protocol=TCP nanodep < ./storage/mysql/schema.sql

- name: Set
run: echo "NANODEP_MYSQL_STORAGE_TEST_DSN=nanodep:nanodep@tcp(localhost:$PORT)/nanodep" >> $GITHUB_ENV

- name: Test
run: go test -v ./storage/mysql
15 changes: 7 additions & 8 deletions storage/file/file_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package file

import (
"context"
"testing"

"github.com/micromdm/nanodep/storage"
"github.com/micromdm/nanodep/storage/test"
)

func TestFileStorage(t *testing.T) {
test.Run(t, func(t *testing.T) storage.AllStorage {
s, err := New(t.TempDir())
if err != nil {
t.Fatal(err)
}
return s
})
s, err := New(t.TempDir())
if err != nil {
t.Fatal(err)
}

test.TestWithStorages(t, context.Background(), s)
}
23 changes: 0 additions & 23 deletions storage/mysql/docker-compose-test.yml

This file was deleted.

71 changes: 4 additions & 67 deletions storage/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,23 @@ package mysql

import (
"context"
"database/sql"
"fmt"
"os"
"strings"
"testing"
"time"

_ "github.com/go-sql-driver/mysql"
"github.com/micromdm/nanodep/storage"
"github.com/micromdm/nanodep/storage/test"
)

func TestMySQLStorage(t *testing.T) {
testDSN := os.Getenv("NANODEP_MYSQL_STORAGE_TEST")
testDSN := os.Getenv("NANODEP_MYSQL_STORAGE_TEST_DSN")
if testDSN == "" {
t.Skip("NANODEP_MYSQL_STORAGE_TEST not set")
t.Skip("NANODEP_MYSQL_STORAGE_TEST_DSN not set")
}

test.Run(t, func(t *testing.T) storage.AllStorage {
dbName := initTestDB(t)
testDSN := fmt.Sprintf("nanodep:insecure@tcp(localhost:4242)/%s?charset=utf8mb4&loc=UTC", dbName)
s, err := New(WithDSN(testDSN))
if err != nil {
t.Fatal(err)
}
return s
})
}

// initTestDB clears any existing data from the database.
func initTestDB(t *testing.T) string {
rootDSN := "root:toor@tcp(localhost:4242)/?charset=utf8mb4&loc=UTC"
db, err := sql.Open("mysql", rootDSN)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
for {
err := db.PingContext(ctx)
if err == nil {
break
}
t.Logf("failed to connect: %s, retrying connection", err)
select {
case <-time.After(1 * time.Second):
// OK, continue.
case <-ctx.Done():
t.Fatal("timeout connecting to MySQL")
}
}
defer func() {
if err := db.Close(); err != nil {
t.Fatal(err)
}
}()
dbName := dbName(t)
_, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s;", dbName))
s, err := New(WithDSN(testDSN))
if err != nil {
t.Fatal(err)
}
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s;", dbName))
if err != nil {
t.Fatal(err)
}
_, err = db.Exec(fmt.Sprintf("USE %s;", dbName))
if err != nil {
t.Fatal(err)
}
_, err = db.Exec(Schema)
if err != nil {
t.Fatal(err)
}
_, err = db.Exec(fmt.Sprintf("GRANT ALL PRIVILEGES ON %s.* TO 'nanodep';", dbName))
if err != nil {
t.Fatal(err)
}
return dbName
}

func dbName(t *testing.T) string {
return strings.ReplaceAll(strings.ReplaceAll(t.Name(), "/", "_"), "-", "_")
test.TestWithStorages(t, context.Background(), s)
}
Loading