You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2018/08/14 19:02:09 UTC

[GitHub] DylanVolz closed pull request #2679: Fix Traffic Ops unit tests

DylanVolz closed pull request #2679: Fix Traffic Ops unit tests
URL: https://github.com/apache/trafficcontrol/pull/2679
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/traffic_ops/traffic_ops_golang/crconfig/config_test.go b/traffic_ops/traffic_ops_golang/crconfig/config_test.go
index 844f5d628..0878265be 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/config_test.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/config_test.go
@@ -20,9 +20,11 @@ package crconfig
  */
 
 import (
+	"context"
 	"reflect"
 	"strings"
 	"testing"
+	"time"
 
 	"gopkg.in/DATA-DOG/go-sqlmock.v1"
 )
@@ -54,11 +56,19 @@ func TestGetConfigParams(t *testing.T) {
 
 	cdn := "mycdn"
 	domain := "mycdn.invalid"
-
+	mock.ExpectBegin()
 	expected := ExpectedGetConfigParams(domain)
 	MockGetConfigParams(mock, expected, cdn)
+	mock.ExpectCommit()
+
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
 
-	actual, err := getConfigParams(cdn, db)
+	actual, err := getConfigParams(cdn, tx)
 	if err != nil {
 		t.Fatalf("getConfigParams err expected: nil, actual: %v", err)
 	}
@@ -107,12 +117,21 @@ func TestMakeCRConfigConfig(t *testing.T) {
 	domain := "mycdn.invalid"
 	dnssecEnabled := true
 
+	mock.ExpectBegin()
 	expectedGetConfigParams := ExpectedGetConfigParams(domain)
 	MockGetConfigParams(mock, expectedGetConfigParams, cdn)
 
 	expected := ExpectedMakeCRConfigConfig(expectedGetConfigParams, dnssecEnabled)
+	mock.ExpectCommit()
+
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
 
-	actual, err := makeCRConfigConfig(cdn, db, dnssecEnabled, domain)
+	actual, err := makeCRConfigConfig(cdn, tx, dnssecEnabled, domain)
 
 	if err != nil {
 		t.Fatalf("makeCRConfigConfig err expected: nil, actual: %v", err)
diff --git a/traffic_ops/traffic_ops_golang/crconfig/deliveryservice_test.go b/traffic_ops/traffic_ops_golang/crconfig/deliveryservice_test.go
index 25c233f86..e7ad51f79 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/deliveryservice_test.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/deliveryservice_test.go
@@ -20,12 +20,14 @@ package crconfig
  */
 
 import (
+	"context"
 	"encoding/json"
 	"math/rand"
 	"reflect"
 	"strconv"
 	"strings"
 	"testing"
+	"time"
 
 	"github.com/apache/trafficcontrol/lib/go-tc"
 
@@ -173,22 +175,29 @@ func TestMakeDSes(t *testing.T) {
 	domain := "mycdn.invalid"
 
 	expected := ExpectedMakeDSes()
-	MockMakeDSes(mock, expected, cdn)
-
 	expectedParams := ExpectedGetServerProfileParams(expected)
-	MockGetServerProfileParams(mock, expectedParams, cdn)
-
 	expectedDSParams, err := getDSParams(expectedParams)
 	if err != nil {
 		t.Fatalf("getDSParams error expected: nil, actual: %v", err)
 	}
 	expectedMatchsets, expectedDomains := ExpectedGetDSRegexesDomains(expectedDSParams)
-	MockGetDSRegexesDomains(mock, expectedMatchsets, expectedDomains, cdn)
-
 	expectedStaticDNSEntries := ExpectedGetStaticDNSEntries(expected)
+
+	mock.ExpectBegin()
+	MockGetServerProfileParams(mock, expectedParams, cdn)
+	MockGetDSRegexesDomains(mock, expectedMatchsets, expectedDomains, cdn)
 	MockGetStaticDNSEntries(mock, expectedStaticDNSEntries, cdn)
+	MockMakeDSes(mock, expected, cdn)
+	mock.ExpectCommit()
+
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
 
-	actual, err := makeDSes(cdn, domain, db)
+	actual, err := makeDSes(cdn, domain, tx)
 	if err != nil {
 		t.Fatalf("makeDSes expected: nil error, actual: %v", err)
 	}
@@ -243,9 +252,19 @@ func TestGetServerProfileParams(t *testing.T) {
 
 	expectedMakeDSes := ExpectedMakeDSes()
 	expected := ExpectedGetServerProfileParams(expectedMakeDSes)
+
+	mock.ExpectBegin()
 	MockGetServerProfileParams(mock, expected, cdn)
+	mock.ExpectCommit()
+
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
 
-	actual, err := getServerProfileParams(cdn, db)
+	actual, err := getServerProfileParams(cdn, tx)
 	if err != nil {
 		t.Fatalf("getServerProfileParams expected: nil error, actual: %v", err)
 	}
@@ -321,9 +340,19 @@ func TestGetDSRegexesDomains(t *testing.T) {
 		t.Fatalf("getDSParams error expected: nil, actual: %v", err)
 	}
 	expectedMatchsets, expectedDomains := ExpectedGetDSRegexesDomains(expectedDSParams)
+
+	mock.ExpectBegin()
 	MockGetDSRegexesDomains(mock, expectedMatchsets, expectedDomains, cdn)
+	mock.ExpectCommit()
+
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
 
-	actualMatchsets, actualDomains, err := getDSRegexesDomains(cdn, domain, db)
+	actualMatchsets, actualDomains, err := getDSRegexesDomains(cdn, domain, tx)
 	if err != nil {
 		t.Fatalf("getDSRegexesDomains expected: nil error, actual: %v", err)
 	}
@@ -374,9 +403,19 @@ func TestGetStaticDNSEntries(t *testing.T) {
 
 	expectedMakeDSes := ExpectedMakeDSes()
 	expected := ExpectedGetStaticDNSEntries(expectedMakeDSes)
+
+	mock.ExpectBegin()
 	MockGetStaticDNSEntries(mock, expected, cdn)
+	mock.ExpectCommit()
+
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
 
-	actual, err := getStaticDNSEntries(cdn, db)
+	actual, err := getStaticDNSEntries(cdn, tx)
 	if err != nil {
 		t.Fatalf("getStaticDNSEntries expected: nil error, actual: %v", err)
 	}
diff --git a/traffic_ops/traffic_ops_golang/crconfig/edgelocations_test.go b/traffic_ops/traffic_ops_golang/crconfig/edgelocations_test.go
index 3465fb710..f7b14d3c4 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/edgelocations_test.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/edgelocations_test.go
@@ -20,8 +20,10 @@ package crconfig
  */
 
 import (
+	"context"
 	"reflect"
 	"testing"
+	"time"
 
 	"github.com/apache/trafficcontrol/lib/go-tc"
 
@@ -56,6 +58,10 @@ func ExpectedMakeLocations() (map[string]tc.CRConfigLatitudeLongitude, map[strin
 }
 
 func MockMakeLocations(mock sqlmock.Sqlmock, expectedEdgeLocs map[string]tc.CRConfigLatitudeLongitude, expectedRouterLocs map[string]tc.CRConfigLatitudeLongitude, cdn string) {
+
+	fallbackRows := sqlmock.NewRows([]string{"primary_cg", "name"})
+	mock.ExpectQuery("SELECT").WillReturnRows(fallbackRows)
+
 	rows := sqlmock.NewRows([]string{"name", "id", "type", "latitude", "longitude", "fallback_to_closest", "localization_methods"})
 	for s, l := range expectedEdgeLocs {
 		rows = rows.AddRow(s, 1, EdgeTypePrefix, l.Lat, l.Lon, false, []byte("{CZ}"))
@@ -63,12 +69,8 @@ func MockMakeLocations(mock sqlmock.Sqlmock, expectedEdgeLocs map[string]tc.CRCo
 	for s, l := range expectedRouterLocs {
 		rows = rows.AddRow(s, 1, RouterTypeName, l.Lat, l.Lon, false, nil)
 	}
-	mock.ExpectQuery("select").WithArgs(cdn).WillReturnRows(rows)
 
-	fallbackRows := sqlmock.NewRows([]string{"name"})
-	for range expectedEdgeLocs {
-		mock.ExpectQuery("select").WithArgs(1).WillReturnRows(fallbackRows)
-	}
+	mock.ExpectQuery("SELECT").WithArgs(cdn).WillReturnRows(rows)
 }
 
 func TestMakeLocations(t *testing.T) {
@@ -80,10 +82,19 @@ func TestMakeLocations(t *testing.T) {
 
 	cdn := "mycdn"
 
+	mock.ExpectBegin()
 	expectedEdgeLocs, expectedRouterLocs := ExpectedMakeLocations()
 	MockMakeLocations(mock, expectedEdgeLocs, expectedRouterLocs, cdn)
+	mock.ExpectCommit()
+
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
 
-	actualEdgeLocs, actualRouterLocs, err := makeLocations(cdn, db)
+	actualEdgeLocs, actualRouterLocs, err := makeLocations(cdn, tx)
 	if err != nil {
 		t.Fatalf("makeLocations expected: nil error, actual: %v", err)
 	}
diff --git a/traffic_ops/traffic_ops_golang/crconfig/servers_test.go b/traffic_ops/traffic_ops_golang/crconfig/servers_test.go
index fcaeb543d..603025a32 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/servers_test.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/servers_test.go
@@ -20,9 +20,11 @@ package crconfig
  */
 
 import (
+	"context"
 	"math/rand"
 	"reflect"
 	"testing"
+	"time"
 
 	"github.com/apache/trafficcontrol/lib/go-tc"
 
@@ -111,10 +113,19 @@ func TestGetServerParams(t *testing.T) {
 
 	cdn := "mycdn"
 
+	mock.ExpectBegin()
 	expected := ExpectedGetServerParams()
 	MockGetServerParams(mock, expected, cdn)
+	mock.ExpectCommit()
 
-	actual, err := getServerParams(cdn, db)
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
+
+	actual, err := getServerParams(cdn, tx)
 	if err != nil {
 		t.Fatalf("getServerParams expected: nil error, actual: %v", err)
 	}
@@ -166,13 +177,22 @@ func TestGetAllServers(t *testing.T) {
 
 	cdn := "mycdn"
 
+	mock.ExpectBegin()
 	getServerParamsExpected := ExpectedGetServerParams()
 	MockGetServerParams(mock, getServerParamsExpected, cdn)
 
 	expected := ExpectedGetAllServers(getServerParamsExpected)
 	MockGetAllServers(mock, expected, cdn)
+	mock.ExpectCommit()
 
-	actual, err := getAllServers(cdn, db)
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
+
+	actual, err := getAllServers(cdn, tx)
 
 	if err != nil {
 		t.Fatalf("getAllServers expected: nil error, actual: %v", err)
@@ -220,10 +240,19 @@ func TestGetServerDSNames(t *testing.T) {
 
 	cdn := "mycdn"
 
+	mock.ExpectBegin()
 	expected := ExpectedGetServerDSNames()
 	MockGetServerDSNames(mock, expected, cdn)
+	mock.ExpectCommit()
+
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
 
-	actual, err := getServerDSNames(cdn, db)
+	actual, err := getServerDSNames(cdn, tx)
 
 	if err != nil {
 		t.Fatalf("getServerDSNames expected: nil error, actual: %v", err)
@@ -276,13 +305,22 @@ func TestGetServerDSes(t *testing.T) {
 	cdn := "mycdn"
 	domain := "mydomain"
 
+	mock.ExpectBegin()
 	expectedGetServerDSNames := ExpectedGetServerDSNames()
 	MockGetServerDSNames(mock, expectedGetServerDSNames, cdn)
 
 	expected := ExpectedGetServerDSes(expectedGetServerDSNames)
 	MockGetServerDSes(mock, expected, cdn)
+	mock.ExpectCommit()
 
-	actual, err := getServerDSes(cdn, db, domain)
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
+
+	actual, err := getServerDSes(cdn, tx, domain)
 
 	if err != nil {
 		t.Fatalf("getServerDSes expected: nil error, actual: %v", err)
@@ -312,10 +350,19 @@ func TestGetCDNInfo(t *testing.T) {
 
 	cdn := "mycdn"
 
+	mock.ExpectBegin()
 	expectedDomain, expectedDNSSECEnabled := ExpectedGetCDNInfo()
 	MockGetCDNInfo(mock, expectedDomain, expectedDNSSECEnabled, cdn)
+	mock.ExpectCommit()
 
-	actualDomain, actualDNSSECEnabled, err := getCDNInfo(cdn, db)
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
+
+	actualDomain, actualDNSSECEnabled, err := getCDNInfo(cdn, tx)
 	if err != nil {
 		t.Fatalf("getCDNInfo expected: nil error, actual: %v", err)
 	}
@@ -347,10 +394,19 @@ func TestGetCDNNameFromID(t *testing.T) {
 
 	cdnID := 42
 
+	mock.ExpectBegin()
 	expected := ExpectedGetCDNNameFromID()
 	MockGetCDNNameFromID(mock, expected, cdnID)
+	mock.ExpectCommit()
+
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
 
-	actual, exists, err := getCDNNameFromID(cdnID, db)
+	actual, exists, err := getCDNNameFromID(cdnID, tx)
 	if err != nil {
 		t.Fatalf("getCDNNameFromID expected: nil error, actual: %v", err)
 	}
diff --git a/traffic_ops/traffic_ops_golang/crconfig/snapshot_test.go b/traffic_ops/traffic_ops_golang/crconfig/snapshot_test.go
index 82b568e62..09fc1dbce 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/snapshot_test.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/snapshot_test.go
@@ -20,6 +20,7 @@ package crconfig
  */
 
 import (
+	"context"
 	"database/sql/driver"
 	"encoding/json"
 	"reflect"
@@ -52,13 +53,23 @@ func TestGetSnapshot(t *testing.T) {
 
 	crc := &tc.CRConfig{}
 	crc.Stats.CDNName = &cdn
+
+	mock.ExpectBegin()
 	expected, err := ExpectedGetSnapshot(crc)
 	if err != nil {
 		t.Fatalf("GetSnapshot creating expected err expected: nil, actual: %v", err)
 	}
 	MockGetSnapshot(mock, expected, cdn)
+	mock.ExpectCommit()
+
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
 
-	actual, exists, err := GetSnapshot(db, cdn)
+	actual, exists, err := GetSnapshot(tx, cdn)
 	if err != nil {
 		t.Fatalf("GetSnapshot err expected: nil, actual: %v", err)
 	}
@@ -102,13 +113,22 @@ func TestSnapshot(t *testing.T) {
 	crc := &tc.CRConfig{}
 	crc.Stats.CDNName = &cdn
 
+	mock.ExpectBegin()
 	expected, err := ExpectedGetSnapshot(crc)
 	if err != nil {
 		t.Fatalf("GetSnapshot creating expected err expected: nil, actual: %v", err)
 	}
 	MockSnapshot(mock, expected, cdn)
+	mock.ExpectCommit()
+
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
 
-	if err := Snapshot(db, crc); err != nil {
+	if err := Snapshot(tx, crc); err != nil {
 		t.Fatalf("GetSnapshot err expected: nil, actual: %v", err)
 	}
 }
diff --git a/traffic_ops/traffic_ops_golang/crconfig/stats_test.go b/traffic_ops/traffic_ops_golang/crconfig/stats_test.go
index 430ba2cb2..8a31a7b42 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/stats_test.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/stats_test.go
@@ -40,7 +40,7 @@ func ExpectedMakeStats() tc.CRConfigStats {
 func TestMakeStats(t *testing.T) {
 	expected := ExpectedMakeStats()
 	start := time.Now()
-	actual := makeStats(*expected.CDNName, *expected.TMUser, *expected.TMHost, *expected.TMPath, *expected.TMVersion, nil)
+	actual := makeStats(*expected.CDNName, *expected.TMUser, *expected.TMHost, *expected.TMPath, *expected.TMVersion)
 	end := time.Now()
 	expected.DateUnixSeconds = actual.DateUnixSeconds
 	if !reflect.DeepEqual(expected, actual) {
diff --git a/traffic_ops/traffic_ops_golang/hwinfo/hwinfo_test.go b/traffic_ops/traffic_ops_golang/hwinfo/hwinfo_test.go
index 46608ae42..d93c1471d 100644
--- a/traffic_ops/traffic_ops_golang/hwinfo/hwinfo_test.go
+++ b/traffic_ops/traffic_ops_golang/hwinfo/hwinfo_test.go
@@ -20,6 +20,7 @@ package hwinfo
  */
 
 import (
+	"context"
 	"testing"
 	"time"
 
@@ -62,6 +63,7 @@ func TestGetHWInfo(t *testing.T) {
 	db := sqlx.NewDb(mockDB, "sqlmock")
 	defer db.Close()
 
+	mock.ExpectBegin()
 	testHWInfo := getTestHWInfo()
 	cols := test.ColsFromStructByTag("db", tc.HWInfo{})
 	rows := sqlmock.NewRows(cols)
@@ -78,10 +80,18 @@ func TestGetHWInfo(t *testing.T) {
 	}
 	mock.ExpectQuery("SELECT").WillReturnRows(rows)
 	v := map[string]string{"ServerId": "1"}
+	mock.ExpectCommit()
 
-	hwinfos, errs, errType := getHWInfo(v, db)
-	if len(errs) > 0 {
-		t.Errorf("getHWInfo expected: no errors, actual: %v with error type: %s", errs, errType.String())
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTxx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
+
+	hwinfos, err := getHWInfo(tx, v)
+	if err != nil {
+		t.Errorf("getHWInfo expected: error nil, actual: %v ", err)
 	}
 
 	if len(hwinfos) != 2 {
diff --git a/traffic_ops/traffic_ops_golang/monitoring_test.go b/traffic_ops/traffic_ops_golang/monitoring/monitoring_test.go
similarity index 86%
rename from traffic_ops/traffic_ops_golang/monitoring_test.go
rename to traffic_ops/traffic_ops_golang/monitoring/monitoring_test.go
index f7dbc364f..fc3f03045 100644
--- a/traffic_ops/traffic_ops_golang/monitoring_test.go
+++ b/traffic_ops/traffic_ops_golang/monitoring/monitoring_test.go
@@ -1,4 +1,4 @@
-package main
+package monitoring
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -20,9 +20,11 @@ package main
  */
 
 import (
+	"context"
 	"reflect"
 	"sort"
 	"testing"
+	"time"
 
 	"github.com/jmoiron/sqlx"
 	"github.com/lib/pq"
@@ -75,6 +77,7 @@ func TestGetMonitoringServers(t *testing.T) {
 		Profile: "routerProfile",
 	}
 
+	mock.ExpectBegin()
 	rows := sqlmock.NewRows([]string{"hostName", "fqdn", "status", "cachegroup", "port", "ip", "ip6", "profile", "interfaceName", "type", "hashId"})
 	rows = rows.AddRow(monitor.HostName, monitor.FQDN, monitor.Status, monitor.Cachegroup, monitor.Port, monitor.IP, monitor.IP6, monitor.Profile, "noInterface", MonitorType, "noHash")
 	rows = rows.AddRow(cache.HostName, cache.FQDN, cache.Status, cache.Cachegroup, cache.Port, cache.IP, cache.IP6, cache.Profile, cache.InterfaceName, cache.Type, cache.HashID)
@@ -82,7 +85,13 @@ func TestGetMonitoringServers(t *testing.T) {
 
 	mock.ExpectQuery("SELECT").WithArgs(cdn).WillReturnRows(rows)
 
-	monitors, caches, routers, err := getMonitoringServers(db, cdn)
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTxx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+
+	monitors, caches, routers, err := getMonitoringServers(tx.Tx, cdn)
 	if err != nil {
 		t.Errorf("getMonitoringServers expected: nil error, actual: %v", err)
 	}
@@ -136,12 +145,19 @@ func TestGetCachegroups(t *testing.T) {
 		},
 	}
 
+	mock.ExpectBegin()
 	rows := sqlmock.NewRows([]string{"name", "latitude", "longitude"})
 	rows = rows.AddRow(cachegroup.Name, cachegroup.Coordinates.Latitude, cachegroup.Coordinates.Longitude)
 
 	mock.ExpectQuery("SELECT").WithArgs(cdn).WillReturnRows(rows)
 
-	sqlCachegroups, err := getCachegroups(db, cdn)
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTxx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+
+	sqlCachegroups, err := getCachegroups(tx.Tx, cdn)
 	if err != nil {
 		t.Errorf("getCachegroups expected: nil error, actual: %v", err)
 	}
@@ -294,6 +310,7 @@ func TestGetProfiles(t *testing.T) {
 		},
 	}
 
+	mock.ExpectBegin()
 	rows := sqlmock.NewRows([]string{"profile", "name", "value"})
 	for _, profile := range profiles {
 		for paramName, paramVal := range profile.Parameters {
@@ -308,7 +325,13 @@ func TestGetProfiles(t *testing.T) {
 
 	mock.ExpectQuery("SELECT").WithArgs(pq.Array(profileNames), CacheMonitorConfigFile).WillReturnRows(rows)
 
-	sqlProfiles, err := getProfiles(db, caches, routers)
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTxx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+
+	sqlProfiles, err := getProfiles(tx.Tx, caches, routers)
 	if err != nil {
 		t.Errorf("getProfiles expected: nil error, actual: %v", err)
 	}
@@ -365,6 +388,7 @@ func TestGetDeliveryServices(t *testing.T) {
 	deliveryservices := []DeliveryService{deliveryservice}
 	routers := []Router{router}
 
+	mock.ExpectBegin()
 	rows := sqlmock.NewRows([]string{"xml_id", "global_max_tps", "global_max_mbps"})
 	for _, deliveryservice := range deliveryservices {
 		rows = rows.AddRow(deliveryservice.XMLID, deliveryservice.TotalTPSThreshold, deliveryservice.TotalKBPSThreshold/KilobitsPerMegabit)
@@ -374,7 +398,13 @@ func TestGetDeliveryServices(t *testing.T) {
 
 	mock.ExpectQuery("SELECT").WithArgs(pq.Array(profileNames)).WillReturnRows(rows)
 
-	sqlDeliveryservices, err := getDeliveryServices(db, routers)
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTxx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+
+	sqlDeliveryservices, err := getDeliveryServices(tx.Tx, routers)
 	if err != nil {
 		t.Errorf("getProfiles expected: nil error, actual: %v", err)
 	}
@@ -410,6 +440,7 @@ func TestGetConfig(t *testing.T) {
 		"name1": "val1",
 	}
 
+	mock.ExpectBegin()
 	rows := sqlmock.NewRows([]string{"name", "value"})
 	for name, val := range config {
 		rows = rows.AddRow(name, val)
@@ -417,7 +448,13 @@ func TestGetConfig(t *testing.T) {
 
 	mock.ExpectQuery("SELECT").WillReturnRows(rows)
 
-	sqlConfig, err := getConfig(db)
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTxx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+
+	sqlConfig, err := getConfig(tx.Tx)
 	if err != nil {
 		t.Errorf("getProfiles expected: nil error, actual: %v", err)
 	}
@@ -444,6 +481,7 @@ func TestGetMonitoringJSON(t *testing.T) {
 
 	cdn := "mycdn"
 
+	mock.ExpectBegin()
 	{
 		//
 		// getMonitoringServers
@@ -615,39 +653,45 @@ func TestGetMonitoringJSON(t *testing.T) {
 		resp.Response.Config = config
 	}
 
-	sqlResp, err, errType := getMonitoringJSON(cdn, db)
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTxx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+
+	sqlResp, err := getMonitoringJSON(tx.Tx, cdn)
 	if err != nil {
-		t.Errorf("getMonitoringJSON expected: nil error, actual: %v with error type: %s", err, errType.String())
+		t.Errorf("getMonitoringJSON expected: nil error, actual: %v", err)
 	}
 
 	resp.Response.TrafficServers = sortCaches(resp.Response.TrafficServers)
-	sqlResp.Response.TrafficServers = sortCaches(sqlResp.Response.TrafficServers)
+	sqlResp.TrafficServers = sortCaches(sqlResp.TrafficServers)
 	resp.Response.TrafficMonitors = sortMonitors(resp.Response.TrafficMonitors)
-	sqlResp.Response.TrafficMonitors = sortMonitors(sqlResp.Response.TrafficMonitors)
+	sqlResp.TrafficMonitors = sortMonitors(sqlResp.TrafficMonitors)
 	resp.Response.Cachegroups = sortCachegroups(resp.Response.Cachegroups)
-	sqlResp.Response.Cachegroups = sortCachegroups(sqlResp.Response.Cachegroups)
+	sqlResp.Cachegroups = sortCachegroups(sqlResp.Cachegroups)
 	resp.Response.Profiles = sortProfiles(resp.Response.Profiles)
-	sqlResp.Response.Profiles = sortProfiles(sqlResp.Response.Profiles)
+	sqlResp.Profiles = sortProfiles(sqlResp.Profiles)
 	resp.Response.DeliveryServices = sortDeliveryServices(resp.Response.DeliveryServices)
-	sqlResp.Response.DeliveryServices = sortDeliveryServices(sqlResp.Response.DeliveryServices)
+	sqlResp.DeliveryServices = sortDeliveryServices(sqlResp.DeliveryServices)
 
-	if !reflect.DeepEqual(sqlResp.Response.TrafficServers, resp.Response.TrafficServers) {
-		t.Errorf("getMonitoringJSON expected TrafficServers: %+v actual: %+v", resp.Response.TrafficServers, sqlResp.Response.TrafficServers)
+	if !reflect.DeepEqual(sqlResp.TrafficServers, resp.Response.TrafficServers) {
+		t.Errorf("getMonitoringJSON expected TrafficServers: %+v actual: %+v", resp.Response.TrafficServers, sqlResp.TrafficServers)
 	}
-	if !reflect.DeepEqual(sqlResp.Response.TrafficMonitors, resp.Response.TrafficMonitors) {
-		t.Errorf("getMonitoringJSON expected TrafficMonitors: %+v actual: %+v", resp.Response.TrafficMonitors, sqlResp.Response.TrafficMonitors)
+	if !reflect.DeepEqual(sqlResp.TrafficMonitors, resp.Response.TrafficMonitors) {
+		t.Errorf("getMonitoringJSON expected TrafficMonitors: %+v actual: %+v", resp.Response.TrafficMonitors, sqlResp.TrafficMonitors)
 	}
-	if !reflect.DeepEqual(sqlResp.Response.Cachegroups, resp.Response.Cachegroups) {
-		t.Errorf("getMonitoringJSON expected Cachegroups: %+v actual: %+v", resp.Response.Cachegroups, sqlResp.Response.Cachegroups)
+	if !reflect.DeepEqual(sqlResp.Cachegroups, resp.Response.Cachegroups) {
+		t.Errorf("getMonitoringJSON expected Cachegroups: %+v actual: %+v", resp.Response.Cachegroups, sqlResp.Cachegroups)
 	}
-	if !reflect.DeepEqual(sqlResp.Response.Profiles, resp.Response.Profiles) {
-		t.Errorf("getMonitoringJSON expected Profiles: %+v actual: %+v", resp.Response.Profiles, sqlResp.Response.Profiles)
+	if !reflect.DeepEqual(sqlResp.Profiles, resp.Response.Profiles) {
+		t.Errorf("getMonitoringJSON expected Profiles: %+v actual: %+v", resp.Response.Profiles, sqlResp.Profiles)
 	}
-	if !reflect.DeepEqual(sqlResp.Response.DeliveryServices, resp.Response.DeliveryServices) {
-		t.Errorf("getMonitoringJSON expected DeliveryServices: %+v actual: %+v", resp.Response.DeliveryServices, sqlResp.Response.DeliveryServices)
+	if !reflect.DeepEqual(sqlResp.DeliveryServices, resp.Response.DeliveryServices) {
+		t.Errorf("getMonitoringJSON expected DeliveryServices: %+v actual: %+v", resp.Response.DeliveryServices, sqlResp.DeliveryServices)
 	}
-	if !reflect.DeepEqual(sqlResp.Response.Config, resp.Response.Config) {
-		t.Errorf("getMonitoringJSON expected Config: %+v actual: %+v", resp.Response.Config, sqlResp.Response.Config)
+	if !reflect.DeepEqual(sqlResp.Config, resp.Response.Config) {
+		t.Errorf("getMonitoringJSON expected Config: %+v actual: %+v", resp.Response.Config, sqlResp.Config)
 	}
 
 }
diff --git a/traffic_ops/traffic_ops_golang/riaksvc/riak_services_test.go b/traffic_ops/traffic_ops_golang/riaksvc/riak_services_test.go
index 746e2b132..eeaab1d39 100644
--- a/traffic_ops/traffic_ops_golang/riaksvc/riak_services_test.go
+++ b/traffic_ops/traffic_ops_golang/riaksvc/riak_services_test.go
@@ -20,9 +20,11 @@ package riaksvc
  */
 
 import (
+	"context"
 	"crypto/tls"
 	"errors"
 	"testing"
+	"time"
 
 	"github.com/basho/riak-go-client"
 	"github.com/jmoiron/sqlx"
@@ -147,11 +149,22 @@ func TestGetRiakCluster(t *testing.T) {
 	db := sqlx.NewDb(mockDB, "sqlmock")
 	defer db.Close()
 
-	rows1 := sqlmock.NewRows([]string{"s.host_name", "s.domain_name"})
-	rows1.AddRow("www", "devnull.com")
+	mock.ExpectBegin()
+	rows1 := sqlmock.NewRows([]string{"fqdn"})
+	rows1.AddRow("www.devnull.com")
+
+	mock.ExpectQuery("SELECT").WillReturnError(errors.New("foo"))
+
 	mock.ExpectQuery("SELECT").WillReturnRows(rows1)
 
-	if _, err := GetRiakCluster(db.DB, nil); err == nil {
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTxx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
+
+	if _, err := GetRiakClusterTx(tx.Tx, nil); err == nil {
 		t.Errorf("expected an error due to nil RiakAuthoptions in the config but, go no error.")
 	}
 
@@ -161,14 +174,14 @@ func TestGetRiakCluster(t *testing.T) {
 		TlsConfig: &tls.Config{},
 	}
 
-	if _, err := GetRiakCluster(db.DB, &authOptions); err != nil {
+	if _, err := GetRiakClusterTx(tx.Tx, &authOptions); err != nil {
 		t.Errorf("expected no errors, actual: %s.", err)
 	}
 
 	rows2 := sqlmock.NewRows([]string{"s.host_name", "s.domain_name"})
 	mock.ExpectQuery("SELECT").WillReturnRows(rows2)
 
-	if _, err := GetRiakCluster(db.DB, &authOptions); err == nil {
+	if _, err := GetRiakClusterTx(tx.Tx, &authOptions); err == nil {
 		t.Errorf("expected an error due to no available riak servers.")
 	}
 }
diff --git a/traffic_ops/traffic_ops_golang/server/servers_assignment_test.go b/traffic_ops/traffic_ops_golang/server/servers_assignment_test.go
index 672ea7119..f2eb6d82f 100644
--- a/traffic_ops/traffic_ops_golang/server/servers_assignment_test.go
+++ b/traffic_ops/traffic_ops_golang/server/servers_assignment_test.go
@@ -20,8 +20,10 @@ package server
  */
 
 import (
+	"context"
 	"reflect"
 	"testing"
+	"time"
 
 	"github.com/jmoiron/sqlx"
 
@@ -85,7 +87,14 @@ func TestAssignDsesToServer(t *testing.T) {
 	mock.ExpectExec("DELETE").WithArgs(pq.Array(delete)).WillReturnResult(sqlmock.NewResult(1, 3))
 	mock.ExpectCommit()
 
-	result, err := assignDeliveryServicesToServer(100, newDses, true, db)
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTxx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
+
+	result, err := assignDeliveryServicesToServer(100, newDses, true, tx.Tx)
 	if err != nil {
 		t.Errorf("error assigning deliveryservice: %v", err)
 	}
diff --git a/traffic_ops/traffic_ops_golang/server/servers_update_status_test.go b/traffic_ops/traffic_ops_golang/server/servers_update_status_test.go
index cb0509917..d9a11627e 100644
--- a/traffic_ops/traffic_ops_golang/server/servers_update_status_test.go
+++ b/traffic_ops/traffic_ops_golang/server/servers_update_status_test.go
@@ -23,9 +23,9 @@ import (
 	"context"
 	"reflect"
 	"testing"
+	"time"
 
 	"github.com/apache/trafficcontrol/lib/go-tc"
-	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/config"
 	"github.com/jmoiron/sqlx"
 	sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
@@ -41,12 +41,21 @@ func TestGetServerUpdateStatus(t *testing.T) {
 	db := sqlx.NewDb(mockDB, "sqlmock")
 	defer db.Close()
 
+	mock.ExpectBegin()
 	serverStatusRow := sqlmock.NewRows([]string{"id", "host_name", "type", "server_reval_pending", "use_reval_pending", "upd_pending", "status", "parent_upd_pending", "parent_reval_pending"})
 	serverStatusRow.AddRow(1, "host_name_1", "EDGE", true, true, true, "ONLINE", true, false)
 
 	mock.ExpectQuery("SELECT").WillReturnRows(serverStatusRow)
+	mock.ExpectCommit()
 
-	result, err := getServerUpdateStatus("host_name_1", db, context.WithValue(context.Background(), api.ConfigContextKey, &config.Config{ConfigTrafficOpsGolang: config.ConfigTrafficOpsGolang{DBQueryTimeoutSeconds: 20}}))
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTxx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+	defer tx.Commit()
+
+	result, err := getServerUpdateStatus(tx.Tx, &config.Config{ConfigTrafficOpsGolang: config.ConfigTrafficOpsGolang{DBQueryTimeoutSeconds: 20}}, "host_name_1")
 	if err != nil {
 		t.Errorf("getServerUpdateStatus: %v", err)
 	}
diff --git a/traffic_ops/traffic_ops_golang/systeminfo/system_info_test.go b/traffic_ops/traffic_ops_golang/systeminfo/system_info_test.go
index ec36710ef..de4606935 100644
--- a/traffic_ops/traffic_ops_golang/systeminfo/system_info_test.go
+++ b/traffic_ops/traffic_ops_golang/systeminfo/system_info_test.go
@@ -20,17 +20,17 @@ package systeminfo
  */
 
 import (
+	"context"
+	"encoding/json"
 	"testing"
 	"time"
 
-	sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
-
-	"encoding/json"
-
-	tc "github.com/apache/trafficcontrol/lib/go-tc"
+	"github.com/apache/trafficcontrol/lib/go-tc"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/test"
 	"github.com/jmoiron/sqlx"
+
+	sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
 )
 
 func TestGetSystemInfo(t *testing.T) {
@@ -92,13 +92,21 @@ func TestGetSystemInfo(t *testing.T) {
 		)
 	}
 
-	mock.ExpectQuery("SELECT.*WHERE p.config_file='global'").WillReturnRows(rows)
-	sysinfo, err := getSystemInfo(db, auth.PrivLevelReadOnly, 20*time.Second)
+	mock.ExpectBegin()
+	mock.ExpectQuery("SELECT.*WHERE p.config_file = 'global'").WillReturnRows(rows)
+
+	dbCtx, _ := context.WithTimeout(context.TODO(), time.Duration(10)*time.Second)
+	tx, err := db.BeginTxx(dbCtx, nil)
+	if err != nil {
+		t.Fatalf("creating transaction: %v", err)
+	}
+
+	sysinfo, err := getSystemInfo(tx, auth.PrivLevelReadOnly, 20*time.Second)
 	if err != nil {
-		t.Errorf("getSystemInfo expected: nil error, actual: %v", err)
+		t.Fatalf("getSystemInfo expected: nil error, actual: %v", err)
 	}
 
-	if len(sysinfo) != 2 {
-		t.Errorf("getSystemInfo expected: len(sysinfo) == 2, actual: %v", len(sysinfo))
+	if len(sysinfo.ParametersNullable) != 2 {
+		t.Fatalf("getSystemInfo expected: len(sysinfo) == 2, actual: %v", len(sysinfo.ParametersNullable))
 	}
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services