You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by rs...@apache.org on 2023/03/14 15:44:15 UTC
[trafficcontrol] branch master updated: Crconfig test coverage (#7401)
This is an automated email from the ASF dual-hosted git repository.
rshah pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git
The following commit(s) were added to refs/heads/master by this push:
new af86550f70 Crconfig test coverage (#7401)
af86550f70 is described below
commit af86550f700aef7f20a7e8b154eacaea8da30d7c
Author: Kurtis Michie <49...@users.noreply.github.com>
AuthorDate: Tue Mar 14 09:44:05 2023 -0600
Crconfig test coverage (#7401)
* Test for createMaxmindDefaultOverrideObj
* Test success for TestGetSnapshotMonitoring
* Created test case for TestGetSnapshot in snapshot_test.go
* Created test case for TestGetSnapshotMonitoring in snapshot_test.go
* Changed %s to %v in snapshot_test.go and reorganized import in config_test.go
---
.../traffic_ops_golang/crconfig/config_test.go | 33 ++++
.../traffic_ops_golang/crconfig/snapshot_test.go | 182 ++++++++++++++++-----
2 files changed, 177 insertions(+), 38 deletions(-)
diff --git a/traffic_ops/traffic_ops_golang/crconfig/config_test.go b/traffic_ops/traffic_ops_golang/crconfig/config_test.go
index 9414baf970..0880676793 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/config_test.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/config_test.go
@@ -21,12 +21,14 @@ package crconfig
import (
"context"
+ "errors"
"reflect"
"strings"
"testing"
"time"
"github.com/apache/trafficcontrol/lib/go-tc"
+ "github.com/apache/trafficcontrol/lib/go-util"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/test"
"gopkg.in/DATA-DOG/go-sqlmock.v1"
@@ -146,3 +148,34 @@ func TestMakeCRConfigConfig(t *testing.T) {
t.Errorf("makeCRConfigConfig expected: %+v, actual: %+v", expected, actual)
}
}
+
+func TestCreateMaxmindDefaultOverrideObj(t *testing.T) {
+ errs := ""
+ separator := ", "
+ testCases := []string{
+ "US;12.345,-12.345",
+ "US",
+ "US;12.345",
+ "US;abc,-12.345",
+ "US;1,abc",
+ }
+
+ for _, v := range testCases {
+ _, err := createMaxmindDefaultOverrideObj(v)
+ if err != nil {
+ errs += util.JoinErrsStr([]error{err}) + separator
+ }
+ }
+ errs = errs[:len(errs)-len(separator)]
+
+ expectedErrs := util.JoinErrsStr([]error{
+ errors.New(`malformed maxmind.default.override parameter: 'US'`),
+ errors.New(`malformed maxmind.default.override parameter coordinates 'US;12.345'`),
+ errors.New(`malformed maxmind.default.override parameter coordinates, latitude not a number: 'US;abc,-12.345'`),
+ errors.New(`malformed maxmind.default.override parameter coordinates, longitude not an number: 'US;1,abc'`),
+ })
+
+ if !reflect.DeepEqual(expectedErrs, errs) {
+ t.Errorf("expected %s, got %s", expectedErrs, errs)
+ }
+}
diff --git a/traffic_ops/traffic_ops_golang/crconfig/snapshot_test.go b/traffic_ops/traffic_ops_golang/crconfig/snapshot_test.go
index 4a59a3bfa1..9c67806e4b 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/snapshot_test.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/snapshot_test.go
@@ -25,6 +25,7 @@ import (
"database/sql/driver"
"encoding/json"
"reflect"
+ "strings"
"testing"
"time"
@@ -37,56 +38,161 @@ func ExpectedGetSnapshot(crc *tc.CRConfig) ([]byte, error) {
return json.Marshal(crc)
}
-func ExpectedGetMontioringSnapshot(crc *tc.CRConfig, tx *sql.Tx) ([]byte, error) {
+func ExpectedGetMonitoringSnapshot(crc *tc.CRConfig, tx *sql.Tx) ([]byte, error) {
tm, _ := monitoring.GetMonitoringJSON(tx, *crc.Stats.CDNName)
return json.Marshal(tm)
}
-func MockGetSnapshot(mock sqlmock.Sqlmock, expected []byte, cdn string) {
- rows := sqlmock.NewRows([]string{"snapshot"})
- rows = rows.AddRow(expected)
- rows = rows.AddRow(expected)
- mock.ExpectQuery("SELECT").WithArgs(cdn).WillReturnRows(rows)
+func MockGetSnapshotTestCases(mock sqlmock.Sqlmock, expected []byte, cdn string) {
+ if expected != nil {
+ rows := sqlmock.NewRows([]string{"snapshot"})
+ rows = rows.AddRow(expected)
+ mock.ExpectQuery("SELECT").WithArgs(cdn).WillReturnRows(rows)
+ } else if expected == nil {
+ rows := sqlmock.NewRows([]string{"snapshot"})
+ mock.ExpectQuery("SELECT").WithArgs(cdn).WillReturnRows(rows)
+ }
}
func TestGetSnapshot(t *testing.T) {
- db, mock, err := sqlmock.New()
- if err != nil {
- t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
- }
- defer db.Close()
+ testCases := []string{"success", "emptyRows", "badCdnName"}
- cdn := "mycdn"
+ for _, v := range testCases {
+ db, mock, err := sqlmock.New()
+ if err != nil {
+ t.Fatalf("an error '%v' was not expected when opening a stub database connection", err)
+ }
+ defer db.Close()
- crc := &tc.CRConfig{}
- crc.Stats.CDNName = &cdn
+ cdn := "mycdn"
- 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()
+ crc := &tc.CRConfig{}
+ crc.Stats.CDNName = &cdn
- dbCtx, cancelTx := context.WithTimeout(context.TODO(), 10*time.Second)
- defer cancelTx()
- tx, err := db.BeginTx(dbCtx, nil)
- if err != nil {
- t.Fatalf("creating transaction: %v", err)
- }
- defer tx.Commit()
+ mock.ExpectBegin()
- actual, exists, err := GetSnapshot(tx, cdn)
- if err != nil {
- t.Fatalf("GetSnapshot err expected: nil, actual: %v", err)
- }
- if !exists {
- t.Fatalf("GetSnapshot exists expected: true, actual: false")
+ expected, err := ExpectedGetSnapshot(crc)
+
+ if err != nil {
+ t.Fatalf("GetSnapshot creating expected err expected: nil, actual: %v", err)
+ }
+ if v == "success" {
+ MockGetSnapshotTestCases(mock, expected, cdn)
+ } else if v == "emptyRows" {
+ MockGetSnapshotTestCases(mock, nil, cdn)
+ } else if v == "badCdnName" {
+ MockGetSnapshotTestCases(mock, expected, "bad")
+ } else {
+ t.Fatalf("GetSnapshot testCase %v not found", v)
+ }
+ mock.ExpectCommit()
+
+ dbCtx, cancelTx := context.WithTimeout(context.TODO(), 10*time.Second)
+ defer cancelTx()
+ tx, err := db.BeginTx(dbCtx, nil)
+
+ actual, exists, err := GetSnapshot(tx, cdn)
+
+ if v == "success" {
+ if err != nil {
+ t.Fatalf("GetSnapshot err expected: nil, actual: %v", err)
+ }
+ if !exists {
+ t.Fatalf("GetSnapshot exists expected: true, actual: false")
+ }
+ if !reflect.DeepEqual(string(expected), actual) {
+ t.Errorf("GetSnapshot expected: %+v, actual: %+v", string(expected), actual)
+ }
+ } else if v == "emptyRows" {
+ if err != nil {
+ t.Fatalf("GetSnapshot err expected: nil, actual: %v", err)
+ }
+ if !reflect.DeepEqual("", actual) {
+ t.Errorf("GetSnapshot expected an empty string, actual: %+v", actual)
+ }
+ } else if v == "badCdnName" {
+ if err == nil && strings.Contains("does not match actual [string - mycdn]", err.Error()) {
+ t.Errorf("Expected a mismatched error when supplying a bad CDN name in GetSnapshot")
+ }
+ if !reflect.DeepEqual("", actual) {
+ t.Errorf("GetSnapshot expected an empty string, actual: %+v", actual)
+ }
+ } else {
+ t.Fatalf("Test case %v not correctly accounted for", v)
+ }
+
+ defer tx.Commit()
}
+}
+
+func TestGetSnapshotMonitoring(t *testing.T) {
+ testCases := []string{"success", "emptyRows", "badCdnName"}
+
+ for _, v := range testCases {
+ db, mock, err := sqlmock.New()
+ if err != nil {
+ t.Fatalf("an error '%v' was not expected when opening a stub database connection", err)
+ }
+ defer db.Close()
+
+ cdn := "mycdn"
+
+ crc := &tc.CRConfig{}
+ crc.Stats.CDNName = &cdn
+
+ mock.ExpectBegin()
+
+ expected, err := ExpectedGetSnapshot(crc)
+
+ if err != nil {
+ t.Fatalf("GetSnapshotMonitoring creating expected err expected: nil, actual: %v", err)
+ }
+ if v == "success" {
+ MockGetSnapshotTestCases(mock, expected, cdn)
+ } else if v == "emptyRows" {
+ MockGetSnapshotTestCases(mock, nil, cdn)
+ } else if v == "badCdnName" {
+ MockGetSnapshotTestCases(mock, expected, "bad")
+ } else {
+ t.Fatalf("GetSnapshotMonitoring testCase %v not found", v)
+ }
+ mock.ExpectCommit()
+
+ dbCtx, cancelTx := context.WithTimeout(context.TODO(), 10*time.Second)
+ defer cancelTx()
+ tx, err := db.BeginTx(dbCtx, nil)
+
+ actual, exists, err := GetSnapshotMonitoring(tx, cdn)
+
+ if v == "success" {
+ if err != nil {
+ t.Fatalf("GetSnapshotMonitoring err expected: nil, actual: %v", err)
+ }
+ if !exists {
+ t.Fatalf("GetSnapshotMonitoring exists expected: true, actual: false")
+ }
+ if !reflect.DeepEqual(string(expected), actual) {
+ t.Errorf("GetSnapshotMonitoring expected: %+v, actual: %+v", string(expected), actual)
+ }
+ } else if v == "emptyRows" {
+ if err != nil {
+ t.Fatalf("GetSnapshotMonitoring err expected: nil, actual: %v", err)
+ }
+ if !reflect.DeepEqual("", actual) {
+ t.Errorf("GetSnapshotMonitoring expected an empty string, actual: %+v", actual)
+ }
+ } else if v == "badCdnName" {
+ if err == nil && strings.Contains("does not match actual [string - mycdn]", err.Error()) {
+ t.Errorf("Expected a mismatched error when supplying a bad CDN name in GetSnapshotMonitoring")
+ }
+ if !reflect.DeepEqual("", actual) {
+ t.Errorf("GetSnapshotMonitoring expected an empty string, actual: %+v", actual)
+ }
+ } else {
+ t.Fatalf("Test case %v not correctly accounted for", v)
+ }
- if !reflect.DeepEqual(string(expected), actual) {
- t.Errorf("GetSnapshot expected: %+v, actual: %+v", string(expected), actual)
+ defer tx.Commit()
}
}
@@ -112,7 +218,7 @@ func MockSnapshot(mock sqlmock.Sqlmock, expected []byte, expectedtm []byte, cdn
func TestSnapshot(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
- t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
+ t.Fatalf("an error '%v' was not expected when opening a stub database connection", err)
}
defer db.Close()
@@ -134,7 +240,7 @@ func TestSnapshot(t *testing.T) {
t.Fatalf("GetSnapshot creating expected err expected: nil, actual: %v", err)
}
- expectedtm, err := ExpectedGetMontioringSnapshot(crc, tx)
+ expectedtm, err := ExpectedGetMonitoringSnapshot(crc, tx)
if err != nil {
t.Fatalf("GetSnapshotMonitor creating expected err expected: nil, actual: %v", err)
}