Skip to content

Commit

Permalink
Merge pull request #88 from rhmdnd/add-subject-integration-tests
Browse files Browse the repository at this point in the history
Implement database tests for subject table
  • Loading branch information
openshift-ci[bot] authored Jul 14, 2022
2 parents 512f1d1 + 85da02e commit 31b86c1
Show file tree
Hide file tree
Showing 25 changed files with 1,283 additions and 12 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/aws/aws-sdk-go v1.44.22
github.com/golang-migrate/migrate/v4 v4.15.2
github.com/google/uuid v1.3.0
github.com/lib/pq v1.10.6
github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.7.4
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
Expand Down
2 changes: 1 addition & 1 deletion migrations/000001_create_subject_table.up.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE IF NOT EXISTS subjects (
id serial PRIMARY KEY,
id uuid PRIMARY KEY,
name VARCHAR(255),
type VARCHAR(50)
);
7 changes: 7 additions & 0 deletions tests/integration/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package integration_test // nolint:typecheck

type Subject struct {
ID string
Name string
Type string
}
132 changes: 121 additions & 11 deletions tests/integration/integration_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package integration_test
package integration_test // nolint:typecheck

import (
"database/sql"
"errors"
"fmt"
"net"
"strings"
"testing"
"time"

migrate "github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/lib/pq"
_ "github.com/golang-migrate/migrate/v4/source/file" // Necessary to invoke migrations locally in the repository
"github.com/google/uuid"
_ "github.com/lib/pq" // Necessary to use the PostgreSQL database driver and connecting to a PostgreSQL database
"github.com/stretchr/testify/assert"
gorm_postgres "gorm.io/driver/postgres"
"gorm.io/gorm"
)

const clusterName = "cluster.example.com"

func getDatabaseConnection(t *testing.T) *sql.DB {
t.Helper()
// Generlize this so that it can be used to connect to any Postgres
Expand All @@ -39,7 +45,14 @@ func getDatabaseConnection(t *testing.T) *sql.DB {
var netError *net.OpError
if errors.As(err, &netError) {
t.Logf("Retrying database connection due to error: %s", err)
time.Sleep(3 * time.Second)
// Linting says we shouldn't use the following:
// time.Sleep(3 * time.Second)
// but we can't use
// duration := 3
// time.Sleep(duration * time.Second)
// which causes a type mismatch.
duration, _ := time.ParseDuration("0m3s")
time.Sleep(duration)
continue
} else {
msg := fmt.Sprintf("Unable to establish connection to test database: %s", err)
Expand All @@ -66,18 +79,115 @@ func getMigrationHelper(t *testing.T) *migrate.Migrate {
return m
}

func getGormHelper() *gorm.DB {
connStr := "host=localhost user=dbadmin dbname=compliance password=secret port=5432 sslmode=disable"
gormDB, _ := gorm.Open(gorm_postgres.Open(connStr), &gorm.Config{})
return gormDB
}

func getUUIDString() string {
value, _ := uuid.NewRandom()
return value.String()
}

// Each test assumes the database is unmanaged. The test is responsible for
// setting up the state it requires for its test logic. This keeps the
// getMigrationHelper() method clean of any assumptions about what the tests
// expect from it.
func TestSubject(t *testing.T) { // nolint:paralleltest // database tests should not run in parallel
getMigrationHelper(t)
// m.Up()
// Test logic
// m.Down()
// expect from it. Each test must be run serially they have the power to change
// database schema and affect other tests.
func TestInsertSubjectSucceeds(t *testing.T) { // nolint:paralleltest // database tests should run serially
m := getMigrationHelper(t)
if err := m.Up(); err != nil {
t.Fatalf("Unable to upgrade database: %s", err)
}

id := getUUIDString()
subjectTypeStr := getUUIDString()

s := Subject{ID: id, Name: clusterName, Type: subjectTypeStr}
gormDB := getGormHelper()
gormDB.Create(&s)

subject := Subject{}
gormDB.First(&subject, "id = ?", id)

expectedSubject := Subject{ID: id, Name: clusterName, Type: subjectTypeStr}
assert.Equal(t, expectedSubject.ID, subject.ID, "expected %s got %s", expectedSubject.ID, subject.ID)
assert.Equal(t, expectedSubject.Name, subject.Name, "expected %s got %s", expectedSubject.Name, subject.Name)
assert.Equal(t, expectedSubject.Type, subject.Type, "expected %s got %s", expectedSubject.Type, subject.Type)

// Drop the database instead of downgrading since we don't need the
// data anyway
if err := m.Drop(); err != nil {
t.Fatalf("Unable to drop database: %s", err)
}
}

func TestInsertSubjectWithLongNameFails(t *testing.T) { // nolint:paralleltest // database tests should run serially
m := getMigrationHelper(t)
if err := m.Up(); err != nil {
t.Fatalf("Unable to upgrade database: %s", err)
}

id := getUUIDString()
maxNameLength := 256
name := strings.Repeat("a", maxNameLength)
subjectTypeStr := getUUIDString()

s := Subject{ID: id, Name: name, Type: subjectTypeStr}
gormDB := getGormHelper()
err := gormDB.Create(&s).Error
assert.NotEmpty(t, err, "Shouldn't be able to insert name values longer than 255 characters")
// Drop the database instead of downgrading since we don't need the
// data anyway
if err := m.Drop(); err != nil {
t.Fatalf("Unable to drop database: %s", err)
}
}

func TestInsertSubjectWithNonUUIDFails(t *testing.T) { // nolint:paralleltest // database tests should run serially
m := getMigrationHelper(t)
if err := m.Up(); err != nil {
t.Fatalf("Unable to upgrade database: %s", err)
}

id := "1"
subjectTypeStr := getUUIDString()

s := Subject{ID: id, Name: clusterName, Type: subjectTypeStr}
gormDB := getGormHelper()
err := gormDB.Create(&s).Error
fmt.Print(err)
assert.NotEmpty(t, err, "Expect an error when creating IDs of the wrong type.")
// Drop the database instead of downgrading since we don't need the
// data anyway
if err := m.Drop(); err != nil {
t.Fatalf("Unable to drop database: %s", err)
}
}

func TestInsertSubjectWithLongTypeFails(t *testing.T) { // nolint:paralleltest // database tests should run serially
m := getMigrationHelper(t)
if err := m.Up(); err != nil {
t.Fatalf("Unable to upgrade database: %s", err)
}

id := getUUIDString()
maxTypeLength := 51
subjectTypeStr := strings.Repeat("a", maxTypeLength)

s := Subject{ID: id, Name: clusterName, Type: subjectTypeStr}
gormDB := getGormHelper()
err := gormDB.Create(&s).Error
assert.NotEmpty(t, err, "Shouldn't be able to insert type values longer than 50 characters")
// Drop the database instead of downgrading since we don't need the
// data anyway
if err := m.Drop(); err != nil {
t.Fatalf("Unable to drop database: %s", err)
}
}

func TestMigration(t *testing.T) { // nolint:paralleltest // database tests should not run in parallel
func TestMigration(t *testing.T) { // nolint:paralleltest // database tests should run serially
m := getMigrationHelper(t)

version, dirty, err := m.Version()
Expand Down
9 changes: 9 additions & 0 deletions vendor/github.com/google/uuid/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/google/uuid/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/google/uuid/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions vendor/github.com/google/uuid/dce.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/google/uuid/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 31b86c1

Please sign in to comment.