-
Notifications
You must be signed in to change notification settings - Fork 23
/
async_connections.go
93 lines (73 loc) · 2.6 KB
/
async_connections.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
package suites
import (
"fmt"
"strconv"
"time"
"github.com/stretchr/testify/assert"
"github.com/stackrox/collector/integration-tests/pkg/collector"
"github.com/stackrox/collector/integration-tests/pkg/common"
"github.com/stackrox/collector/integration-tests/pkg/config"
)
type AsyncConnectionTestSuite struct {
IntegrationTestSuiteBase
DisableConnectionStatusTracking bool
BlockConnection bool
ExpectToSeeTheConnection bool
clientContainer string
serverContainer string
serverIP string
}
/*
* This suite has 2 main cases:
* - BlockConnection=false: verify that a successful asynchronous connection gets reported
* We provide curl-test with the IP of a TCP server to isntruct it to connect to this server.
* - BlockConnection=true : verify that a failed aynchronous connection is not
* No parameter to curl-test, and in this case, it simulates a firewall rule by connecting to
* a dummy address.
*/
func (s *AsyncConnectionTestSuite) SetupSuite() {
s.RegisterCleanup("server", "client")
s.StartContainerStats()
collectorOptions := collector.StartupOptions{
Env: map[string]string{
"ROX_COLLECT_CONNECTION_STATUS": strconv.FormatBool(!s.DisableConnectionStatusTracking),
},
Config: map[string]any{
"turnOffScrape": true,
// we need a short scrape interval to have it trigger
// after the connection is closed.
"scrapeInterval": 2,
},
}
s.StartCollector(false, &collectorOptions)
image_store := config.Images()
containerID, err := s.launchContainer("server", image_store.ImageByKey("nginx"))
s.Require().NoError(err)
s.serverContainer = common.ContainerShortID(containerID)
s.serverIP, err = s.getIPAddress("server")
s.Require().NoError(err)
time.Sleep(5 * time.Second) // TODO use the endpoint declaration
target := s.serverIP
if s.BlockConnection {
target = "10.255.255.1"
}
containerID, err = s.launchContainer("client", image_store.QaImageByKey("qa-alpine-curl"), "curl", "--connect-timeout", "5", fmt.Sprintf("http://%s/", target))
s.Require().NoError(err)
s.clientContainer = common.ContainerShortID(containerID)
time.Sleep(10 * time.Second) // give some time to the connection to fail
}
func (s *AsyncConnectionTestSuite) TearDownSuite() {
s.StopCollector()
s.cleanupContainers("server", "client")
s.WritePerfResults()
}
func (s *AsyncConnectionTestSuite) TestNetworkFlows() {
networkInfos := s.Sensor().Connections(s.clientContainer)
if s.ExpectToSeeTheConnection {
// expect one connection
assert.Equal(s.T(), 1, len(networkInfos))
} else {
// expect no connections at all
assert.Equal(s.T(), 0, len(networkInfos))
}
}