-
Notifications
You must be signed in to change notification settings - Fork 23
/
connections_and_endpoints.go
147 lines (122 loc) · 5.6 KB
/
connections_and_endpoints.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package suites
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/stackrox/collector/integration-tests/pkg/collector"
"github.com/stackrox/collector/integration-tests/pkg/common"
"github.com/stackrox/collector/integration-tests/pkg/config"
"github.com/stackrox/collector/integration-tests/pkg/types"
"github.com/stretchr/testify/assert"
)
type Container struct {
Name string
Cmd string
ContainerID string
IP string
ExpectedNetwork []types.NetworkInfo
ExpectedEndpoints []types.EndpointInfo
}
type ConnectionsAndEndpointsTestSuite struct {
IntegrationTestSuiteBase
Server Container
Client Container
}
func (s *ConnectionsAndEndpointsTestSuite) SetupSuite() {
s.RegisterCleanup(s.Server.Name, s.Client.Name)
s.StartContainerStats()
collectorOptions := collector.StartupOptions{
Env: map[string]string{
"ROX_PROCESSES_LISTENING_ON_PORT": "true",
"ROX_ENABLE_AFTERGLOW": "false",
},
Config: map[string]any{
"turnOffScrape": false,
},
}
s.StartCollector(false, &collectorOptions)
socatImage := config.Images().QaImageByKey("qa-socat")
serverName := s.Server.Name
clientName := s.Client.Name
longContainerID, err := s.launchContainer(serverName, "--entrypoint", "/bin/sh", socatImage, "-c", "/bin/sleep 300")
s.Server.ContainerID = common.ContainerShortID(longContainerID)
s.Require().NoError(err)
longContainerID, err = s.launchContainer(clientName, "--entrypoint", "/bin/sh", socatImage, "-c", "/bin/sleep 300")
s.Require().NoError(err)
s.Client.ContainerID = common.ContainerShortID(longContainerID)
s.Server.IP, err = s.getIPAddress(serverName)
s.Require().NoError(err)
s.Client.IP, err = s.getIPAddress(clientName)
s.Require().NoError(err)
serverCmd := strings.Replace(s.Server.Cmd, "CLIENT_IP", s.Client.IP, -1)
_, err = s.execContainer(serverName, []string{"/bin/sh", "-c", serverCmd})
s.Require().NoError(err)
time.Sleep(3 * time.Second)
clientCmd := strings.Replace(s.Client.Cmd, "SERVER_IP", s.Server.IP, -1)
_, err = s.execContainer(clientName, []string{"/bin/sh", "-c", clientCmd})
s.Require().NoError(err)
time.Sleep(6 * time.Second)
}
func (s *ConnectionsAndEndpointsTestSuite) TearDownSuite() {
s.StopCollector()
s.cleanupContainers(s.Server.Name, s.Client.Name)
s.WritePerfResults()
}
func (s *ConnectionsAndEndpointsTestSuite) TestConnectionsAndEndpoints() {
// TODO If ExpectedNetwork is nil the test should check that it is actually nil
if s.Client.ExpectedNetwork != nil {
clientNetworks := s.Sensor().Connections(s.Client.ContainerID)
nNetwork := len(clientNetworks)
nExpectedNetwork := len(s.Client.ExpectedNetwork)
// TODO Get this assert to pass reliably for these tests. Don't just do the asserts for the last connection. https://issues.redhat.com/browse/ROX-17964
// assert.Equal(s.T(), nClientNetwork, nExpectedClientNetwork)
if nExpectedNetwork != nNetwork {
fmt.Println("WARNING: Expected " + strconv.Itoa(nExpectedNetwork) + " client network connections but found " + strconv.Itoa(nNetwork))
}
lastNetwork := clientNetworks[nNetwork-1]
lastExpectedNetwork := s.Client.ExpectedNetwork[nExpectedNetwork-1]
expectedLocalAddress := strings.Replace(lastExpectedNetwork.LocalAddress, "CLIENT_IP", s.Client.IP, -1)
expectedRemoteAddress := strings.Replace(lastExpectedNetwork.RemoteAddress, "SERVER_IP", s.Server.IP, -1)
assert.Equal(s.T(), expectedLocalAddress, lastNetwork.LocalAddress)
assert.Equal(s.T(), expectedRemoteAddress, lastNetwork.RemoteAddress)
assert.Equal(s.T(), "ROLE_CLIENT", lastNetwork.Role)
assert.Equal(s.T(), lastExpectedNetwork.SocketFamily, lastNetwork.SocketFamily)
}
if s.Client.ExpectedEndpoints != nil {
fmt.Println("Expected client endpoint should be nil")
}
endpoints := s.Sensor().Endpoints(s.Client.ContainerID)
assert.Equal(s.T(), 0, len(endpoints))
// TODO If ExpectedNetwork is nil the test should check that it is actually nil
if s.Server.ExpectedNetwork != nil {
serverNetworks := s.Sensor().Connections(s.Server.ContainerID)
nNetwork := len(serverNetworks)
nExpectedNetwork := len(s.Server.ExpectedNetwork)
// TODO Get this assert to pass reliably for these tests. Don't just do the asserts for the last connection. https://issues.redhat.com/browse/ROX-18803
// assert.Equal(s.T(), nServerNetwork, nExpectedServerNetwork)
if nExpectedNetwork != nNetwork {
fmt.Println("WARNING: Expected " + strconv.Itoa(nExpectedNetwork) + " server network connections but found " + strconv.Itoa(nNetwork))
}
lastNetwork := serverNetworks[nNetwork-1]
lastExpectedNetwork := s.Server.ExpectedNetwork[nExpectedNetwork-1]
expectedLocalAddress := strings.Replace(lastExpectedNetwork.LocalAddress, "SERVER_IP", s.Server.IP, -1)
expectedRemoteAddress := strings.Replace(lastExpectedNetwork.RemoteAddress, "CLIENT_IP", s.Client.IP, -1)
assert.Equal(s.T(), expectedLocalAddress, lastNetwork.LocalAddress)
assert.Equal(s.T(), expectedRemoteAddress, lastNetwork.RemoteAddress)
assert.Equal(s.T(), "ROLE_SERVER", lastNetwork.Role)
assert.Equal(s.T(), lastExpectedNetwork.SocketFamily, lastNetwork.SocketFamily)
}
serverEndpoints := s.Sensor().Endpoints(s.Server.ContainerID)
if s.Server.ExpectedEndpoints != nil {
assert.Equal(s.T(), len(s.Server.ExpectedEndpoints), len(serverEndpoints))
types.SortEndpoints(s.Server.ExpectedEndpoints)
types.SortEndpoints(serverEndpoints)
for idx := range serverEndpoints {
assert.Equal(s.T(), s.Server.ExpectedEndpoints[idx].Protocol, serverEndpoints[idx].Protocol)
assert.Equal(s.T(), s.Server.ExpectedEndpoints[idx].Address, serverEndpoints[idx].Address)
}
} else {
assert.Equal(s.T(), 0, len(serverEndpoints))
}
}