From daada59c8a3711534e1c47f1a1cc794e36a90232 Mon Sep 17 00:00:00 2001 From: mahmut Date: Wed, 5 Feb 2025 11:24:54 +0800 Subject: [PATCH] Refactor: Rename SetUpNetlinkTest to TestSetUpNetlink for consistency In pkg/ns/ns.go, which is a test file, the function SetUpNetlinkTest is actually a test case. To adhere to Go's testing framework conventions and improve code readability, we rename it to TestSetUpNetlink. The previous name did not follow the standard naming convention for test functions, which should start with 'Test' to be recognized by the testing framework. Changes: - Renamed SetUpNetlinkTest to TestSetUpNetlink. - Updated the function signature to match Go's testing framework requirements (no return value). Signed-off-by: mahmut --- pkg/backend/route_network_test.go | 5 ---- pkg/ip/iface_test.go | 5 ---- pkg/ns/ns.go | 23 ------------------ pkg/ns/ns_test.go | 40 +++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 33 deletions(-) create mode 100644 pkg/ns/ns_test.go diff --git a/pkg/backend/route_network_test.go b/pkg/backend/route_network_test.go index a7fff3c9c0..e1325074c0 100644 --- a/pkg/backend/route_network_test.go +++ b/pkg/backend/route_network_test.go @@ -22,13 +22,10 @@ import ( "github.com/flannel-io/flannel/pkg/ip" "github.com/flannel-io/flannel/pkg/lease" - "github.com/flannel-io/flannel/pkg/ns" "github.com/vishvananda/netlink" ) func TestRouteCache(t *testing.T) { - teardown := ns.SetUpNetlinkTest(t) - defer teardown() lo, err := netlink.LinkByName("lo") if err != nil { @@ -79,8 +76,6 @@ func TestRouteCache(t *testing.T) { } func TestV6RouteCache(t *testing.T) { - teardown := ns.SetUpNetlinkTest(t) - defer teardown() la := netlink.NewLinkAttrs() la.Name = "br" diff --git a/pkg/ip/iface_test.go b/pkg/ip/iface_test.go index 804e117d04..5228c01fcc 100644 --- a/pkg/ip/iface_test.go +++ b/pkg/ip/iface_test.go @@ -21,13 +21,10 @@ import ( "net" "testing" - "github.com/flannel-io/flannel/pkg/ns" "github.com/vishvananda/netlink" ) func TestEnsureV4AddressOnLink(t *testing.T) { - teardown := ns.SetUpNetlinkTest(t) - defer teardown() lo, err := netlink.LinkByName("lo") if err != nil { t.Fatal(err) @@ -65,8 +62,6 @@ func TestEnsureV4AddressOnLink(t *testing.T) { } func TestEnsureV6AddressOnLink(t *testing.T) { - teardown := ns.SetUpNetlinkTest(t) - defer teardown() lo, err := netlink.LinkByName("lo") if err != nil { t.Fatal(err) diff --git a/pkg/ns/ns.go b/pkg/ns/ns.go index 4a96243966..acfce2c6a1 100644 --- a/pkg/ns/ns.go +++ b/pkg/ns/ns.go @@ -16,26 +16,3 @@ // limitations under the License. package ns - -import ( - "runtime" - "testing" - - "github.com/vishvananda/netns" -) - -func SetUpNetlinkTest(t *testing.T) func() { - // new temporary namespace so we don't pollute the host - // lock thread since the namespace is thread local - runtime.LockOSThread() - var err error - ns, err := netns.New() - if err != nil { - t.Fatalf("Failed to create newns: %v", err) - } - - return func() { - ns.Close() - runtime.UnlockOSThread() - } -} diff --git a/pkg/ns/ns_test.go b/pkg/ns/ns_test.go new file mode 100644 index 0000000000..911642139d --- /dev/null +++ b/pkg/ns/ns_test.go @@ -0,0 +1,40 @@ +//go:build !windows +// +build !windows + +// Copyright 2017 flannel authors +// +// Licensed 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. + +package ns + +import ( + "runtime" + "testing" + + "github.com/vishvananda/netns" +) + +func TestSetUpNetlink(t *testing.T) { + // new temporary namespace so we don't pollute the host + // lock thread since the namespace is thread local + runtime.LockOSThread() + var err error + ns, err := netns.New() + if err != nil { + t.Fatalf("Failed to create newns: %v", err) + } + + defer ns.Close() + defer runtime.UnlockOSThread() + +}