Debian Codegraph @

pkg/utils/sysctl/sysctl_linux_test.go

raw symbols SCIP
1 // Copyright 2017-2020 CNI authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package sysctl_test
16
17 import (
18 "fmt"
19 "math/rand"
20 "runtime"
21 "strings"
22
23 . "github.com/onsi/ginkgo/v2"
24 . "github.com/onsi/gomega"
25 "github.com/vishvananda/netlink"
26
27 "github.com/containernetworking/plugins/pkg/ns"
28 "github.com/containernetworking/plugins/pkg/testutils"
29 "github.com/containernetworking/plugins/pkg/utils/sysctl"
30 )
31
32 const (
33 sysctlDotKeyTemplate = "net.ipv4.conf.%s.proxy_arp"
34 sysctlSlashKeyTemplate = "net/ipv4/conf/%s/proxy_arp"
35 )
36
37 var _ = Describe("Sysctl tests", func() {
38 var testIfaceName string
39 var cleanup func()
40
41 beforeEach := func() {
42 // Save a reference to the original namespace,
43 // Add a new NS
44 currNs, err := ns.GetCurrentNS()
45 Expect(err).NotTo(HaveOccurred())
46
47 testNs, err := testutils.NewNS()
48 Expect(err).NotTo(HaveOccurred())
49
50 testIfaceName = fmt.Sprintf("cnitest.%d", rand.Intn(100000))
51 testLinkAttrs := netlink.NewLinkAttrs()
52 testLinkAttrs.Name = testIfaceName
53 testLinkAttrs.Namespace = netlink.NsFd(int(testNs.Fd()))
54 testIface := &netlink.Dummy{
55 LinkAttrs: testLinkAttrs,
56 }
57
58 err = netlink.LinkAdd(testIface)
59 Expect(err).NotTo(HaveOccurred())
60
61 runtime.LockOSThread()
62 err = testNs.Set()
63 Expect(err).NotTo(HaveOccurred())
64
65 cleanup = func() {
66 netlink.LinkDel(testIface)
67 currNs.Set()
68 }
69 }
70
71 AfterEach(func() {
72 cleanup()
73 })
74
75 Describe("Sysctl", func() {
76 It("reads keys with dot separators", func() {
77 beforeEach()
78 sysctlIfaceName := strings.ReplaceAll(testIfaceName, ".", "/")
79 sysctlKey := fmt.Sprintf(sysctlDotKeyTemplate, sysctlIfaceName)
80
81 _, err := sysctl.Sysctl(sysctlKey)
82 Expect(err).NotTo(HaveOccurred())
83 })
84 })
85
86 Describe("Sysctl", func() {
87 It("reads keys with slash separators", func() {
88 beforeEach()
89 sysctlKey := fmt.Sprintf(sysctlSlashKeyTemplate, testIfaceName)
90
91 _, err := sysctl.Sysctl(sysctlKey)
92 Expect(err).NotTo(HaveOccurred())
93 })
94 })
95
96 Describe("Sysctl", func() {
97 It("writes keys with dot separators", func() {
98 beforeEach()
99 sysctlIfaceName := strings.ReplaceAll(testIfaceName, ".", "/")
100 sysctlKey := fmt.Sprintf(sysctlDotKeyTemplate, sysctlIfaceName)
101
102 _, err := sysctl.Sysctl(sysctlKey, "1")
103 Expect(err).NotTo(HaveOccurred())
104 })
105 })
106
107 Describe("Sysctl", func() {
108 It("writes keys with slash separators", func() {
109 beforeEach()
110 sysctlKey := fmt.Sprintf(sysctlSlashKeyTemplate, testIfaceName)
111
112 _, err := sysctl.Sysctl(sysctlKey, "1")
113 Expect(err).NotTo(HaveOccurred())
114 })
115 })
116 })