You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by zr...@apache.org on 2023/04/25 13:43:27 UTC

[trafficcontrol] branch master updated: Adding unit tests for cdn folder (#7460)

This is an automated email from the ASF dual-hosted git repository.

zrhoffman 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 ba24d5bfe1 Adding unit tests for cdn folder (#7460)
ba24d5bfe1 is described below

commit ba24d5bfe10e6954addbd71a8de958c4f573d247
Author: Srijeet Chatterjee <30...@users.noreply.github.com>
AuthorDate: Tue Apr 25 09:43:20 2023 -0400

    Adding unit tests for cdn folder (#7460)
    
    * Adding unit tests for cdn folder
    
    * ad license
    
    * fix import and license ordering
---
 traffic_ops/traffic_ops_golang/cdn/domains_test.go | 70 ++++++++++++++++++++
 .../traffic_ops_golang/cdn/namedelete_test.go      | 75 ++++++++++++++++++++++
 traffic_ops/traffic_ops_golang/cdn/queue_test.go   | 48 ++++++++++++++
 3 files changed, 193 insertions(+)

diff --git a/traffic_ops/traffic_ops_golang/cdn/domains_test.go b/traffic_ops/traffic_ops_golang/cdn/domains_test.go
new file mode 100644
index 0000000000..11cea159f3
--- /dev/null
+++ b/traffic_ops/traffic_ops_golang/cdn/domains_test.go
@@ -0,0 +1,70 @@
+package cdn
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+	"net/http"
+	"testing"
+
+	"github.com/jmoiron/sqlx"
+
+	"gopkg.in/DATA-DOG/go-sqlmock.v1"
+)
+
+func TestGetDomainsList(t *testing.T) {
+	mockDB, mock, err := sqlmock.New()
+	if err != nil {
+		t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
+	}
+	defer mockDB.Close()
+
+	db := sqlx.NewDb(mockDB, "sqlmock")
+	defer db.Close()
+
+	cols := []string{"id", "name", "description", "domain_name"}
+	rows := sqlmock.NewRows(cols)
+	rows.AddRow(1, "profile1", "profiledesc1", "profiledomain1")
+	rows.AddRow(2, "profile2", "profiledesc2", "profiledomain2")
+
+	mock.ExpectBegin()
+	mock.ExpectQuery("SELECT p.id").WillReturnRows(rows)
+	mock.ExpectCommit()
+
+	domainList, err, sc, _ := getDomainsList(false, nil, db.MustBegin())
+	if err != nil {
+		t.Fatalf("expected no error while getting domains list, but got: %v", err)
+	}
+	if sc != http.StatusOK {
+		t.Errorf("expected a 200 status, but got %d", sc)
+	}
+	if len(domainList) != 2 {
+		t.Fatalf("expected domains to have a length of 2, but got %d", len(domainList))
+	}
+	if domainList[0].ProfileID != 1 || domainList[0].ProfileName != "profile1" ||
+		domainList[0].ProfileDescription != "profiledesc1" || domainList[0].DomainName != "profiledomain1" {
+		t.Errorf("expected: profile ID: 1, profile name: profile1, profile desc: profiledesc1, profile domain: profiledomain1; got: %d, %s, %s, %s",
+			domainList[0].ProfileID, domainList[0].ProfileName, domainList[0].ProfileDescription, domainList[0].DomainName)
+	}
+	if domainList[1].ProfileID != 2 || domainList[1].ProfileName != "profile2" ||
+		domainList[1].ProfileDescription != "profiledesc2" || domainList[1].DomainName != "profiledomain2" {
+		t.Errorf("expected: profile ID: 2, profile name: profile2, profile desc: profiledesc2, profile domain: profiledomain2; got: %d, %s, %s, %s",
+			domainList[1].ProfileID, domainList[1].ProfileName, domainList[1].ProfileDescription, domainList[1].DomainName)
+	}
+}
diff --git a/traffic_ops/traffic_ops_golang/cdn/namedelete_test.go b/traffic_ops/traffic_ops_golang/cdn/namedelete_test.go
new file mode 100644
index 0000000000..986e4f74a3
--- /dev/null
+++ b/traffic_ops/traffic_ops_golang/cdn/namedelete_test.go
@@ -0,0 +1,75 @@
+package cdn
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+	"testing"
+
+	"github.com/jmoiron/sqlx"
+
+	"gopkg.in/DATA-DOG/go-sqlmock.v1"
+)
+
+func TestDeleteCDNByName(t *testing.T) {
+	mockDB, mock, err := sqlmock.New()
+	if err != nil {
+		t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
+	}
+	defer mockDB.Close()
+
+	db := sqlx.NewDb(mockDB, "sqlmock")
+	defer db.Close()
+
+	mock.ExpectBegin()
+	mock.ExpectExec("DELETE FROM cdn").WithArgs("cdn1").WillReturnResult(sqlmock.NewResult(1, 1))
+	mock.ExpectCommit()
+
+	err = deleteCDNByName(db.MustBegin().Tx, "cdn1")
+	if err != nil {
+		t.Fatalf("no error expected while deleting CDN by name, but got: %v", err)
+	}
+}
+
+func TestCDNUsed(t *testing.T) {
+	mockDB, mock, err := sqlmock.New()
+	if err != nil {
+		t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
+	}
+	defer mockDB.Close()
+
+	db := sqlx.NewDb(mockDB, "sqlmock")
+	defer db.Close()
+
+	cols := []string{"?column?"}
+	rows := sqlmock.NewRows(cols)
+	rows.AddRow(5)
+
+	mock.ExpectBegin()
+	mock.ExpectQuery("WITH cdn_id as").WithArgs("cdn1").WillReturnRows(rows)
+	mock.ExpectCommit()
+
+	unused, err := cdnUnused(db.MustBegin().Tx, "cdn1")
+	if err != nil {
+		t.Fatalf("no error expected in call to cdnUnused, but got: %v", err)
+	}
+	if unused {
+		t.Errorf("expected CDN to be used, but is unused")
+	}
+}
diff --git a/traffic_ops/traffic_ops_golang/cdn/queue_test.go b/traffic_ops/traffic_ops_golang/cdn/queue_test.go
new file mode 100644
index 0000000000..a363975865
--- /dev/null
+++ b/traffic_ops/traffic_ops_golang/cdn/queue_test.go
@@ -0,0 +1,48 @@
+package cdn
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+	"testing"
+
+	"github.com/jmoiron/sqlx"
+
+	"gopkg.in/DATA-DOG/go-sqlmock.v1"
+)
+
+func TestQueueUpdates(t *testing.T) {
+	mockDB, mock, err := sqlmock.New()
+	if err != nil {
+		t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
+	}
+	defer mockDB.Close()
+
+	db := sqlx.NewDb(mockDB, "sqlmock")
+	defer db.Close()
+
+	mock.ExpectBegin()
+	mock.ExpectExec("UPDATE public.server").WillReturnResult(sqlmock.NewResult(1, 1))
+	mock.ExpectCommit()
+
+	_, err = queueUpdates(db.MustBegin(), "UPDATE public.server SET config_update_time = now()", nil)
+	if err != nil {
+		t.Fatalf("expected no error while queueing updates, but got: %v", err)
+	}
+}