Skip to content

Commit

Permalink
[TO] Added unit tests for server folder (#7406)
Browse files Browse the repository at this point in the history
* Added unit tests for server folder.

* Fix go vet issue.

* updated test case(insertProfile).

* updated test case(updateServer).

* Arranged import and checking the correct error.
  • Loading branch information
rimashah25 authored Mar 23, 2023
1 parent 048711c commit f7765ff
Show file tree
Hide file tree
Showing 4 changed files with 441 additions and 3 deletions.
20 changes: 20 additions & 0 deletions traffic_ops/traffic_ops_golang/server/put_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ import (
"gopkg.in/DATA-DOG/go-sqlmock.v1"
)

func TestInvalidStatusForDeliveryServicesAlertText(t *testing.T) {
type testStruct struct {
dsId []int
expected string
}

var testData = []testStruct{
{[]int{0}, " #0 with no 'ONLINE' or 'REPORTED' EDGE servers"},
{[]int{0, 1}, "s #0 and #1 with no 'ONLINE' or 'REPORTED' EDGE servers"},
{[]int{0, 1, 2}, "s #0, #1, and #2 with no 'ONLINE' or 'REPORTED' EDGE servers"},
}

for i, _ := range testData {
desc := InvalidStatusForDeliveryServicesAlertText("", "EDGE", testData[i].dsId)
if testData[i].expected != desc {
t.Errorf("strings don't't match, got:%s; expected:%s", desc, testData[i].expected)
}
}
}

func TestCheckExistingStatusInfo(t *testing.T) {
mockDB, mock, err := sqlmock.New()
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func ValidateDSCapabilities(dsIDs []int, serverName string, tx *sql.Tx) (error,
}
}

return nil, nil, 0
return nil, nil, http.StatusOK
}

func assignDeliveryServicesToServer(server int, dses []int, replace bool, tx *sql.Tx) ([]int, error) {
Expand Down
33 changes: 33 additions & 0 deletions traffic_ops/traffic_ops_golang/server/servers_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,36 @@ func TestCheckTenancyAndCDN(t *testing.T) {
t.Errorf("tenancy and cdn check for a given user failed with status code:%d", code)
}
}

func TestValidateDSCapabilities(t *testing.T) {
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%v' was not expected when opening a stub database connection", err)
}
defer mockDB.Close()

db := sqlx.NewDb(mockDB, "sqlmock")
defer db.Close()

mock.ExpectBegin()
rows := sqlmock.NewRows([]string{"server_capability"})
rows.AddRow([]byte("{eas}"))
mock.ExpectQuery("SELECT").WithArgs("eas").WillReturnRows(rows)

dsIDs := []int64{1}
rows1 := sqlmock.NewRows([]string{"id", "required_capabilities"})
rows1.AddRow(1, []byte("{eas}"))
mock.ExpectQuery("SELECT ").WithArgs(pq.Array(dsIDs)).WillReturnRows(rows1)
mock.ExpectCommit()

usrErr, sysErr, code := ValidateDSCapabilities([]int{1}, "eas", db.MustBegin().Tx)
if usrErr != nil {
t.Errorf("unable to validate DS capability, most likely cache doesn't have the DS capability, error: %v", usrErr)
}
if sysErr != nil {
t.Errorf("unable to validate DS Capability, system error: %v", sysErr)
}
if code != http.StatusOK {
t.Errorf("DS validation failed with status code:%d", code)
}
}
Loading

0 comments on commit f7765ff

Please sign in to comment.