Skip to content

Commit

Permalink
Creates a container with an external connection and adds tests for it…
Browse files Browse the repository at this point in the history
…. Also made changes so that external connections can be checked for
  • Loading branch information
JoukoVirtanen committed Oct 20, 2024
1 parent 91ee35e commit ae8bf37
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
17 changes: 14 additions & 3 deletions integration-tests/pkg/mock_sensor/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,11 @@ func (m *MockSensor) HasConnection(containerID string, conn types.NetworkInfo) b
defer m.networkMutex.Unlock()

if conns, ok := m.connections[containerID]; ok {
_, exists := conns[conn]
return exists
for connection := range conns {
if connection.Equal(conn) {
return true
}
}
}

return false
Expand Down Expand Up @@ -489,8 +492,16 @@ func (m *MockSensor) pushEndpoint(containerID string, endpoint *sensorAPI.Networ
// translateAddress is a helper function for converting binary representations
// of network addresses (in the signals) to usable forms for testing
func (m *MockSensor) translateAddress(addr *sensorAPI.NetworkAddress) string {
address := utils.IPFromBytes(addr.GetAddressData())
if (address == utils.IPAddress{}) {
ipNetworkData := addr.GetIpNetwork()
if len(ipNetworkData) > 0 {
ipNetworkData = ipNetworkData[:len(ipNetworkData)-1]
address = utils.IPFromBytes(ipNetworkData)
}
}
ipPortPair := utils.NetworkPeerID{
Address: utils.IPFromBytes(addr.GetAddressData()),
Address: address,
Port: uint16(addr.GetPort()),
}
return ipPortPair.String()
Expand Down
8 changes: 8 additions & 0 deletions integration-tests/pkg/types/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ func (n *NetworkInfo) IsActive() bool {
// no close timestamp means the connection is open, and active
return n.CloseTimestamp == NilTimestamp
}

func (n *NetworkInfo) Equal(other NetworkInfo) bool {
return n.LocalAddress == other.LocalAddress &&
n.RemoteAddress == other.RemoteAddress &&
n.Role == other.Role &&
n.SocketFamily == other.SocketFamily &&
n.IsActive() == other.IsActive()
}
51 changes: 49 additions & 2 deletions integration-tests/suites/runtime_config_file.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package suites

import (
"fmt"
"path/filepath"
"strconv"
"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/introspection_endpoints"
"github.com/stackrox/collector/integration-tests/pkg/types"
)
Expand Down Expand Up @@ -39,12 +42,27 @@ func (s *RuntimeConfigFileTestSuite) setExternalIpsEnable(runtimeConfigFile stri
// the config introspection endpoint to make sure that the configuration is
// correct.
func (s *RuntimeConfigFileTestSuite) SetupSuite() {
s.RegisterCleanup("external-connection")

s.StartContainerStats()

normalizedIp := "255.255.255.255"
externalIp := "8.8.8.8"
serverPort := 53
externalUrl := fmt.Sprintf("http://%s:%d", externalIp, serverPort)
image_store := config.Images()
containerID, err := s.Executor().StartContainer(
config.ContainerStartConfig{
Name: "external-connection",
Image: image_store.QaImageByKey("qa-alpine-curl"),
Command: []string{"sh", "-c", "while true; do curl " + externalUrl + "; sleep 1; done"},
})
s.Require().NoError(err)
clientContainer := common.ContainerShortID(containerID)

collectorOptions := collector.StartupOptions{
Env: map[string]string{
"ROX_AFTERGLOW_PERIOD": "0",
"ROX_ENABLE_AFTERGLOW": "false",
"ROX_AFTERGLOW_PERIOD": "2",
"ROX_COLLECTOR_INTROSPECTION_ENABLE": "true",
},
}
Expand All @@ -65,21 +83,49 @@ func (s *RuntimeConfigFileTestSuite) SetupSuite() {
},
}

activeNormalizedConnection := types.NetworkInfo{
LocalAddress: "",
RemoteAddress: fmt.Sprintf("%s:%d", normalizedIp, serverPort),
Role: "ROLE_CLIENT",
SocketFamily: "SOCKET_FAMILY_UNKNOWN",
CloseTimestamp: types.NilTimestamp,
}

activeUnnormalizedConnection := types.NetworkInfo{
LocalAddress: "",
RemoteAddress: fmt.Sprintf("%s:%d", externalIp, serverPort),
Role: "ROLE_CLIENT",
SocketFamily: "SOCKET_FAMILY_UNKNOWN",
CloseTimestamp: types.NilTimestamp,
}

inactiveNormalizedConnection := activeNormalizedConnection
inactiveNormalizedConnection.CloseTimestamp = "Not nill time"

inactiveUnnormalizedConnection := activeUnnormalizedConnection
inactiveUnnormalizedConnection.CloseTimestamp = "Not nill time"

s.StartCollector(false, &collectorOptions)
runtimeConfigDir := "/etc/stackrox"
runtimeConfigFile := filepath.Join(runtimeConfigDir, "/runtime_config.yaml")
s.createDir(runtimeConfigDir)

runtimeConfigSuccess := introspection_endpoints.ExpectRuntimeConfig(s.T(), 30*time.Second, externalIpsFalse)
s.Require().True(runtimeConfigSuccess)
expectedConnections := []types.NetworkInfo{activeNormalizedConnection}
s.Sensor().ExpectConnections(s.T(), clientContainer, 5*time.Second, expectedConnections...)

s.setExternalIpsEnable(runtimeConfigFile, true)
runtimeConfigSuccess = introspection_endpoints.ExpectRuntimeConfig(s.T(), 30*time.Second, externalIpsTrue)
s.Require().True(runtimeConfigSuccess)
expectedConnections = append(expectedConnections, activeUnnormalizedConnection, inactiveNormalizedConnection)
s.Sensor().ExpectConnections(s.T(), clientContainer, 5*time.Second, expectedConnections...)

s.setExternalIpsEnable(runtimeConfigFile, false)
runtimeConfigSuccess = introspection_endpoints.ExpectRuntimeConfig(s.T(), 30*time.Second, externalIpsFalse)
s.Require().True(runtimeConfigSuccess)
expectedConnections = append(expectedConnections, activeNormalizedConnection, inactiveNormalizedConnection)
//s.Sensor().ExpectConnections(s.T(), clientContainer, 5*time.Second, expectedConnections...)

s.deleteFile(runtimeConfigFile)
runtimeConfigSuccess = introspection_endpoints.ExpectRuntimeConfig(s.T(), 30*time.Second, externalIpsFalse)
Expand All @@ -105,6 +151,7 @@ func (s *RuntimeConfigFileTestSuite) SetupSuite() {

func (s *RuntimeConfigFileTestSuite) TearDownSuite() {
s.StopCollector()
s.cleanupContainers("external-connection")
s.WritePerfResults()
}

Expand Down

0 comments on commit ae8bf37

Please sign in to comment.