Skip to content

Commit

Permalink
Add unit tests for ipset DestroySet/DestroyAllSets functions
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis committed Apr 18, 2024
1 parent 68b6534 commit 020d4ab
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 8 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/projectcalico/api v0.0.0-20230602153125-fb7148692637
github.com/prometheus-community/pro-bing v0.4.0
github.com/prometheus/client_golang v1.19.0
github.com/submariner-io/admiral v0.18.0-m2
github.com/submariner-io/admiral v0.18.0-m2.0.20240417062804-5cfcef6bc3ba
github.com/submariner-io/shipyard v0.18.0-m2
github.com/vishvananda/netlink v1.2.1-beta.2
golang.org/x/net v0.24.0
Expand All @@ -27,7 +27,7 @@ require (
k8s.io/client-go v0.29.3
k8s.io/component-helpers v0.29.3
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
sigs.k8s.io/controller-runtime v0.17.2
sigs.k8s.io/controller-runtime v0.17.3
sigs.k8s.io/knftables v0.0.16
sigs.k8s.io/mcs-api v0.1.0
sigs.k8s.io/structured-merge-diff/v4 v4.4.1
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/submariner-io/admiral v0.18.0-m2 h1:SGW+azCnbCAe/K1pAC1NeKJ3kR+poY1d99Gq8ZGAMsY=
github.com/submariner-io/admiral v0.18.0-m2/go.mod h1:yaPz+wX65Ew3iVulB3T7r/JqYUwcT6RxTS/oGnZaNTo=
github.com/submariner-io/admiral v0.18.0-m2.0.20240417062804-5cfcef6bc3ba h1:2ERBixskBiw1BsTtPt2XKKtAiUg/GofmxCwPqNNnL98=
github.com/submariner-io/admiral v0.18.0-m2.0.20240417062804-5cfcef6bc3ba/go.mod h1:GR4J3O4aLA5VXQyWcFgvfqn4ahWbfh93a2TIISPO4k8=
github.com/submariner-io/shipyard v0.18.0-m2 h1:9jnyioyAvotl6GMb/6gHAFoEcOFmjR0ixUl4AqzJGiA=
github.com/submariner-io/shipyard v0.18.0-m2/go.mod h1:gqN3jA+kUYv1CDS95Wopfj8vXCfBJmL/NupebzB8KBc=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
Expand Down Expand Up @@ -783,8 +783,8 @@ k8s.io/api v0.29.3 h1:2ORfZ7+bGC3YJqGpV0KSDDEVf8hdGQ6A03/50vj8pmw=
k8s.io/api v0.29.3/go.mod h1:y2yg2NTyHUUkIoTC+phinTnEa3KFM6RZ3szxt014a80=
k8s.io/apiextensions-apiserver v0.18.2/go.mod h1:q3faSnRGmYimiocj6cHQ1I3WpLqmDgJFlKL37fC4ZvY=
k8s.io/apiextensions-apiserver v0.18.4/go.mod h1:NYeyeYq4SIpFlPxSAB6jHPIdvu3hL0pc36wuRChybio=
k8s.io/apiextensions-apiserver v0.29.0 h1:0VuspFG7Hj+SxyF/Z/2T0uFbI5gb5LRgEyUVE3Q4lV0=
k8s.io/apiextensions-apiserver v0.29.0/go.mod h1:TKmpy3bTS0mr9pylH0nOt/QzQRrW7/h7yLdRForMZwc=
k8s.io/apiextensions-apiserver v0.29.2 h1:UK3xB5lOWSnhaCk0RFZ0LUacPZz9RY4wi/yt2Iu+btg=
k8s.io/apiextensions-apiserver v0.29.2/go.mod h1:aLfYjpA5p3OwtqNXQFkhJ56TB+spV8Gc4wfMhUA3/b8=
k8s.io/apimachinery v0.18.2/go.mod h1:9SnR/e11v5IbyPCGbvJViimtJ0SwHG4nfZFjU77ftcA=
k8s.io/apimachinery v0.18.4/go.mod h1:OaXp26zu/5J7p0f92ASynJa1pZo06YlV9fG7BoWbCko=
k8s.io/apimachinery v0.29.3 h1:2tbx+5L7RNvqJjn7RIuIKu9XTsIZ9Z5wX2G22XAa5EU=
Expand Down Expand Up @@ -819,8 +819,8 @@ k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSn
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.7/go.mod h1:PHgbrJT7lCHcxMU+mDHEm+nx46H4zuuHZkDP6icnhu0=
sigs.k8s.io/controller-runtime v0.6.1/go.mod h1:XRYBPdbf5XJu9kpS84VJiZ7h/u1hF3gEORz0efEja7A=
sigs.k8s.io/controller-runtime v0.17.2 h1:FwHwD1CTUemg0pW2otk7/U5/i5m2ymzvOXdbeGOUvw0=
sigs.k8s.io/controller-runtime v0.17.2/go.mod h1:+MngTvIQQQhfXtwfdGw/UOQ/aIaqsYywfCINOtwMO/s=
sigs.k8s.io/controller-runtime v0.17.3 h1:65QmN7r3FWgTxDMz9fvGnO1kbf2nu+acg9p2R9oYYYk=
sigs.k8s.io/controller-runtime v0.17.3/go.mod h1:N0jpP5Lo7lMTF9aL56Z/B2oWBJjey6StQM0jRbKQXtY=
sigs.k8s.io/controller-tools v0.3.0/go.mod h1:enhtKGfxZD1GFEoMgP8Fdbu+uKQ/cq1/WGJhdVChfvI=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
Expand Down
101 changes: 101 additions & 0 deletions pkg/ipset/ipset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
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 ipset_test

import (
"errors"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
fakecommand "github.com/submariner-io/admiral/pkg/command/fake"
"github.com/submariner-io/submariner/pkg/ipset"
)

const testSet = "test-set"

var IPSetPathMatcher = HaveSuffix(ipset.IPSetCmd)

func TestIPSet(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "IP Set Suite")
}

var _ = Describe("DestroySet", func() {
t := newTestDriver()

It("should invoke the correct command", func() {
err := t.ipSet.DestroySet(testSet)
Expect(err).To(Succeed())
t.cmdExecutor.AwaitCommand(IPSetPathMatcher, "destroy", testSet)
})

When("the set does not exist", func() {
It("should succeed", func() {
t.cmdExecutor.SetupCommandOutputWithError("The set with the given name does not exist",
errors.New("exit status 1"), IPSetPathMatcher, "destroy")
err := t.ipSet.DestroySet(testSet)
Expect(err).To(Succeed())
})
})

When("the set is initially in use", func() {
It("should retry and succeed", func() {
t.cmdExecutor.SetupCommandOutputWithError("Set cannot be destroyed: it is in use by a kernel component",
errors.New("exit status 1"), IPSetPathMatcher, "destroy")
err := t.ipSet.DestroySet(testSet)
Expect(err).To(Succeed())
})
})
})

var _ = Describe("DestroyAllSets", func() {
t := newTestDriver()

It("should invoke the correct command", func() {
err := t.ipSet.DestroyAllSets()
Expect(err).To(Succeed())
t.cmdExecutor.AwaitCommand(IPSetPathMatcher, "destroy")
})

When("a set is initially in use", func() {
It("should retry and succeed", func() {
t.cmdExecutor.SetupCommandOutputWithError("Set cannot be destroyed: it is in use by a kernel component",
errors.New("exit status 1"), IPSetPathMatcher, "destroy")
err := t.ipSet.DestroyAllSets()
Expect(err).To(Succeed())
})
})
})

type testDriver struct {
cmdExecutor *fakecommand.Executor
ipSet ipset.Interface
}

func newTestDriver() *testDriver {
t := &testDriver{}

BeforeEach(func() {
t.cmdExecutor = fakecommand.New()
t.ipSet = ipset.New()
})

return t
}

0 comments on commit 020d4ab

Please sign in to comment.