Tests: Use require instead of assert if test depends on it

This commit is contained in:
Noah Hilverling 2019-09-30 10:44:46 +02:00
parent 363ae23656
commit 5d9e459bcd
3 changed files with 14 additions and 11 deletions

View file

@ -7,6 +7,7 @@ import (
"github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"os"
"sync"
"sync/atomic"
@ -161,7 +162,7 @@ func TestDBWrapper_SqlBegin(t *testing.T) {
func TestDBWrapper_SqlTransaction(t *testing.T) {
dbw, err := NewDBWrapper(os.Getenv("ICINGADB_TEST_MYSQL"))
assert.NoError(t, err, "Is the MySQL server running?")
require.NoError(t, err, "Is the MySQL server running?")
err = dbw.SqlTransaction(false, true, false, func(tx DbTransaction) error {
return nil
@ -295,13 +296,13 @@ func TestGetConnectionCheckInterval(t *testing.T) {
func TestDBWrapper_SqlFetchAll(t *testing.T) {
dbw, err := NewDBWrapper(os.Getenv("ICINGADB_TEST_MYSQL"))
assert.NoError(t, err, "Is the MySQL server running?")
require.NoError(t, err, "Is the MySQL server running?")
_, err = dbw.Db.Exec("CREATE TABLE testing0815 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(255) NOT NULL)")
assert.NoError(t, err)
require.NoError(t, err)
_, err = dbw.Db.Exec("INSERT INTO testing0815 (name) VALUES ('horst'), ('test')")
assert.NoError(t, err)
require.NoError(t, err)
var res [][]interface{}
done := make(chan bool)

View file

@ -8,6 +8,7 @@ import (
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"sync"
"testing"
@ -36,7 +37,7 @@ func createTestingHA(t *testing.T) *HA {
ha.uid = uuid.MustParse("551bc748-94b2-4d27-b6a4-15c52aecfe85")
_, err = ha.super.Dbw.SqlExec(mysqlTestObserver, "TRUNCATE TABLE icingadb_instance")
assert.NoError(t, err, "This test needs a working MySQL connection!")
require.NoError(t, err, "This test needs a working MySQL connection!")
ha.logger = log.WithFields(log.Fields{
"context": "HA-Testing",
@ -52,14 +53,14 @@ func TestHA_InsertInstance(t *testing.T) {
ha := createTestingHA(t)
err := ha.insertInstance()
assert.NoError(t, err, "insertInstance should not return an error")
require.NoError(t, err, "insertInstance should not return an error")
rows, err := ha.super.Dbw.SqlFetchAll(mysqlObservers.selectIdHeartbeatFromIcingadbInstanceByEnvironmentId,
"SELECT id, heartbeat from icingadb_instance where environment_id = ? LIMIT 1",
ha.super.EnvId,
)
assert.NoError(t, err, "There was an unexpected SQL error")
require.NoError(t, err, "There was an unexpected SQL error")
assert.Equal(t, 1, len(rows), "There should be a row inserted")
var theirUUID uuid.UUID
@ -75,13 +76,13 @@ func TestHA_checkResponsibility(t *testing.T) {
assert.Equal(t, true, ha.isActive, "HA should be responsible, if no other instance is active")
_, err := ha.super.Dbw.SqlExec(mysqlTestObserver, "TRUNCATE TABLE icingadb_instance")
assert.NoError(t, err, "This test needs a working MySQL connection!")
require.NoError(t, err, "This test needs a working MySQL connection!")
_, err = ha.super.Dbw.SqlExec(mysqlObservers.insertIntoIcingadbInstance,
"INSERT INTO icingadb_instance(id, environment_id, heartbeat, responsible) VALUES (?, ?, ?, 'y')",
ha.uid[:], ha.super.EnvId, 0)
assert.NoError(t, err, "This test needs a working MySQL connection!")
require.NoError(t, err, "This test needs a working MySQL connection!")
ha.isActive = false
ha.checkResponsibility()
@ -89,7 +90,7 @@ func TestHA_checkResponsibility(t *testing.T) {
assert.Equal(t, true, ha.isActive, "HA should be responsible, if another instance was inactive for a long time")
_, err = ha.super.Dbw.SqlExec(mysqlTestObserver, "TRUNCATE TABLE icingadb_instance")
assert.NoError(t, err, "This test needs a working MySQL connection!")
require.NoError(t, err, "This test needs a working MySQL connection!")
_, err = ha.super.Dbw.SqlExec(mysqlObservers.insertIntoIcingadbInstance,
"INSERT INTO icingadb_instance(id, environment_id, heartbeat, responsible) VALUES (?, ?, ?, 'y')",

View file

@ -4,6 +4,7 @@ import (
"encoding/json"
"git.icinga.com/icingadb/icingadb-main/connection"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"testing"
"time"
@ -26,7 +27,7 @@ func TestIcingaHeartbeatListener(t *testing.T) {
go func() {
chErr := make(chan error)
IcingaHeartbeatListener(rdb, chEnv, chErr)
assert.NoError(t, <-chErr, "redis connection error")
require.NoError(t, <-chErr, "redis connection error")
}()
time.Sleep(time.Second * 2)