From 7a8327e4391a02aadfbd29cfaa61720feadec8af Mon Sep 17 00:00:00 2001
From: yangjiaqi <yangjiaqi16@huawei.com>
Date: Mon, 17 Oct 2022 17:07:00 +0800
Subject: [PATCH] add dt-test

---
 Makefile                 |  2 +-
 libnetwork/interfaces.go |  3 +-
 utils/utils.go           |  2 +-
 utils/utils_test.go      | 74 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 78 insertions(+), 3 deletions(-)
 create mode 100644 utils/utils_test.go

diff --git a/Makefile b/Makefile
index 5881aec..a76cab2 100644
--- a/Makefile
+++ b/Makefile
@@ -48,7 +48,7 @@ syscontainer-hooks: $(SOURCES) | $(DEPS_LINK)
 	@echo "Done!"
 
 localtest:
-	go test -tags ${TAGS} -ldflags ${GO_LDFLAGS} -v ./...
+	${ENV} go test -mod=vendor -tags ${TAGS} -ldflags ${GO_LDFLAGS} -v ./...
 
 clean:
 	rm -rf build
diff --git a/libnetwork/interfaces.go b/libnetwork/interfaces.go
index c3c71c1..6e1349a 100644
--- a/libnetwork/interfaces.go
+++ b/libnetwork/interfaces.go
@@ -229,7 +229,8 @@ func UpdateNic(ctr *container.Container, config *types.InterfaceConf, updateConf
 	tmpConfig.HostNicName = newConfig.HostNicName
 
 	if hConfig.IsSameInterface(tmpConfig) {
-		logrus.Infof("Network interface in container %s: Identical setting, nothing to change", config.CtrNicName, ctr.Name())
+		logrus.Infof("Network interface in container (%s, %s): Identical setting, nothing to change",
+			config.CtrNicName, ctr.Name())
 		return nil
 	}
 	if err := hConfig.UpdateNetworkInterface(newConfig, false); err != nil {
diff --git a/utils/utils.go b/utils/utils.go
index c774da0..4f5581b 100644
--- a/utils/utils.go
+++ b/utils/utils.go
@@ -98,7 +98,7 @@ func ParseSyslogService(service string) (*SyslogService, error) {
 		serviceAddr = ""
 	} else if strings.HasPrefix(service, syslogUDPPrefix) {
 		serviceType = "udp"
-		serviceAddr := service[len(syslogUDPPrefix):]
+		serviceAddr = service[len(syslogUDPPrefix):]
 		if serviceAddr == "" {
 			serviceAddr = syslogDefaultUDPService
 		}
diff --git a/utils/utils_test.go b/utils/utils_test.go
new file mode 100644
index 0000000..4cd168b
--- /dev/null
+++ b/utils/utils_test.go
@@ -0,0 +1,74 @@
+// Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
+// rubik licensed under the Mulan PSL v2.
+// You can use this software according to the terms and conditions of the Mulan PSL v2.
+// You may obtain a copy of Mulan PSL v2 at:
+//     http://license.coscl.org.cn/MulanPSL2
+// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+// PURPOSE.
+// See the Mulan PSL v2 for more details.
+// Author: Jiaqi Yang
+// Date: 2022-10-15
+// Description: This file is used for utils.go
+
+package utils
+
+import (
+	"testing"
+)
+
+// TestParseSyslogService tests ParseSyslogService
+func TestParseSyslogService(t *testing.T) {
+	const ip, invalidPrefix = "10.20.30.40", "invalid"
+	tests := []struct {
+		name, service, serviceType, addr string
+		wantErr                          bool
+	}{
+		{name: "TC1-udp", service: syslogUDPPrefix + ip, wantErr: false, serviceType: "udp", addr: ip},
+		{name: "TC1.1-udp(default)", service: syslogUDPPrefix, wantErr: false, serviceType: "udp",
+			addr: syslogDefaultUDPService},
+		{name: "TC2-tcp", service: syslogTCPPrefix + ip, wantErr: false, serviceType: "tcp", addr: ip},
+		{name: "TC2.1-tcp(default)", service: syslogTCPPrefix, wantErr: false, serviceType: "tcp",
+			addr: syslogDefaultTCPService},
+		{name: "TC3-default", serviceType: "default", addr: "", wantErr: false},
+		{name: "TC4-unix", service: syslogUnixSock + ip, wantErr: false, addr: ip},
+		{name: "TC5-invalid", service: invalidPrefix + ip, wantErr: true},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			out, err := ParseSyslogService(tt.service)
+			if (err != nil) != tt.wantErr {
+				t.Errorf("ParseSyslogService() = %v, want %v", err, tt.wantErr)
+			}
+			if err == nil && (out.Addr != tt.addr || out.Type != tt.serviceType) {
+				t.Errorf("get false results: expect(addr: %v, type: %v), get(addr: %v, type: %v)",
+					tt.addr, tt.serviceType, out.Addr, out.Type)
+			}
+		})
+	}
+}
+
+// TestHookSyslog tests HookSyslog
+func TestHookSyslog(t *testing.T) {
+	const ip, invalidPrefix, syslogTag = "10.20.30.40", "invalid", "tools "
+	tests := []struct {
+		name, service string
+		wantErr       bool
+	}{
+		{
+			name:    "TC1-fail to parse",
+			service: invalidPrefix,
+			wantErr: true,
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			err := HookSyslog(tt.service, syslogTag)
+			if (err != nil) != tt.wantErr {
+				t.Errorf("HookSyslog() = %v, want %v", err, tt.wantErr)
+			}
+		})
+	}
+}
-- 
2.30.0

