This repository was archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathportmap_test.go
81 lines (66 loc) · 1.93 KB
/
portmap_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// This is the e2e package to run tests for Ignite
// Currently, we do local e2e tests only
// we have to wait until the CI setup to allow Ignite to run with sudo and in a KVM environment.
//
// How to run tests:
// sudo IGNITE_E2E_HOME=$PWD $(which go) test ./e2e/. -v -count 1 -run Test
//
package e2e
import (
"fmt"
"os/exec"
"testing"
"github.com/weaveworks/ignite/e2e/util"
"gotest.tools/assert"
)
func TestPortmapCleanup(t *testing.T) {
assert.Assert(t, e2eHome != "", "IGNITE_E2E_HOME should be set")
vmName := "e2e-test-ignite-portmap-cleanup"
mappedPort := 4242
igniteCmd := util.NewCommand(t, igniteBin)
igniteCmd.New().
WithNetwork("cni").
With("run").
With("--name=" + vmName).
With("--ssh").
With("--ports=" + fmt.Sprintf("%d:%d", mappedPort, mappedPort)).
With(util.DefaultVMImage).
Run()
// Get the VM ID
idCmd := igniteCmd.New().
With("ps").
With("--filter").
With(fmt.Sprintf("{{.ObjectMeta.Name}}=%s", vmName)).
With("--quiet")
idOut, idErr := idCmd.Cmd.CombinedOutput()
assert.Check(t, idErr, fmt.Sprintf("vm id not found: \n%q\n%s", idCmd.Cmd, idOut))
vmID := string(idOut[:len(idOut)-2])
// Check that the IPtable rules are installed
grepOut, grepErr := grepIPTables(vmID)
assert.NilError(t, grepErr, fmt.Sprintf("unable to match iptable rules:\n %s", grepOut))
igniteCmd.New().
With("rm", "-f", vmName).
Run()
// Check that the IPtable rules are removed
grepOut, grepErr = grepIPTables(vmID)
assert.Error(t, grepErr, "exit status 1", fmt.Sprintf("unexpected output in grep : \n%s", grepOut))
assert.Equal(t, len(grepOut), 0, fmt.Sprintf("leftover iptable rules detected:\n%s", string(grepOut)))
}
func grepIPTables(search string) ([]byte, error) {
grepCmd := exec.Command(
"grep",
search,
)
natCmd := exec.Command(
"iptables",
"-t",
"nat",
"-nL",
)
pipe, _ := natCmd.StdoutPipe()
defer pipe.Close()
grepCmd.Stdin = pipe
_ = natCmd.Start()
return grepCmd.Output()
}