1 Star 0 Fork 28

阿翔与山海经/syscontainer-tools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0004-add-dt-test.patch 4.67 KB
一键复制 编辑 原始数据 按行查看 历史
vegbir 提交于 2022-10-17 17:11 . add dt-test
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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/a-xiang-and-shanhaijing/syscontainer-tools.git
git@gitee.com:a-xiang-and-shanhaijing/syscontainer-tools.git
a-xiang-and-shanhaijing
syscontainer-tools
syscontainer-tools
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385