Skip to content

Commit

Permalink
Created a new assert package
Browse files Browse the repository at this point in the history
  • Loading branch information
JoukoVirtanen committed Nov 7, 2024
1 parent 353f4b9 commit bdedcb4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 69 deletions.
53 changes: 53 additions & 0 deletions integration-tests/pkg/assert/assert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package assert

import (
"encoding/json"
"github.com/stackrox/collector/integration-tests/pkg/collector"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/stackrox/collector/integration-tests/pkg/types"
)

func AssertExternalIps(t *testing.T, enable bool, collectorIP string) {
AssertRepeated(t, func() bool {
body, err := collector.IntrospectionQuery(collectorIP, "/state/config")
assert.NoError(t, err)
var response types.RuntimeConfig
err = json.Unmarshal(body, &response)
assert.NoError(t, err)

return response.Networking.ExternalIps.Enable == enable
})
}

func AssertNoRuntimeConfig(t *testing.T, collectorIP string) {
AssertRepeated(t, func() bool {
body, err := collector.IntrospectionQuery(collectorIP, "/state/config")
assert.NoError(t, err)
return strings.TrimSpace(string(body)) == "{}"
})
}

func AssertRepeated(t *testing.T, condition func() bool) {
tick := time.NewTicker(1 * time.Second)
timer := time.After(3 * time.Minute)

for {
select {
case <-tick.C:
if condition() {
// Condition has been met
return
}

case <-timer:
// TODO: This message should be passed in rather than hard coded here
t.Log("Timeout reached: Runtime configuration was not updated")
t.FailNow()
}
}
}
54 changes: 0 additions & 54 deletions integration-tests/pkg/collector/introspection.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package collector

import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/stackrox/collector/integration-tests/pkg/log"
"github.com/stackrox/collector/integration-tests/pkg/types"
)

func IntrospectionQuery(collectorIP string, endpoint string) ([]byte, error) {
Expand All @@ -30,50 +23,3 @@ func IntrospectionQuery(collectorIP string, endpoint string) ([]byte, error) {
body, err := io.ReadAll(resp.Body)
return body, err
}

func QueryConfig(t *testing.T, collectorIP string) []byte {
log.Info("Querying: /state/config")
body, err := IntrospectionQuery(collectorIP, "/state/config")
assert.NoError(t, err)
log.Info("Response: %q", body)
return body
}

func AssertExternalIps(t *testing.T, enable bool, collectorIP string) {
AssertRepeated(t, func() bool {
body := QueryConfig(t, collectorIP)
var response types.RuntimeConfig
err := json.Unmarshal(body, &response)
assert.NoError(t, err)

return response.Networking.ExternalIps.Enable == enable
})
}

func AssertNoRuntimeConfig(t *testing.T, collectorIP string) {
AssertRepeated(t, func() bool {
body := QueryConfig(t, collectorIP)
return strings.TrimSpace(string(body)) == "{}"
})
}

// TODO: This should be in its own package
func AssertRepeated(t *testing.T, condition func() bool) {
tick := time.NewTicker(1 * time.Second)
timer := time.After(3 * time.Minute)

for {
select {
case <-tick.C:
if condition() {
// Condition has been met
return
}

case <-timer:
// TODO: This message should be passed in rather than hard coded here
t.Log("Timeout reached: Runtime configuration was not updated")
t.FailNow()
}
}
}
15 changes: 8 additions & 7 deletions integration-tests/suites/k8s/config_reload.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package k8s

import (
"github.com/stackrox/collector/integration-tests/pkg/assert"
"github.com/stackrox/collector/integration-tests/pkg/collector"
"github.com/stackrox/collector/integration-tests/pkg/log"

Expand Down Expand Up @@ -50,7 +51,7 @@ func (k *K8sConfigReloadTestSuite) TestCreateConfigurationAfterStart() {
})

log.Info("Checking runtime configuration is not in use")
collector.AssertNoRuntimeConfig(k.T(), k.Collector().IP())
assert.AssertNoRuntimeConfig(k.T(), k.Collector().IP())

log.Info("Checking external IPs is enabled")
configMap := coreV1.ConfigMap{
Expand All @@ -63,21 +64,21 @@ func (k *K8sConfigReloadTestSuite) TestCreateConfigurationAfterStart() {
},
}
k.createConfigMap(&configMap)
collector.AssertExternalIps(k.T(), true, k.Collector().IP())
assert.AssertExternalIps(k.T(), true, k.Collector().IP())

log.Info("Checking external IPs is disabled")
configMap.Data["runtime_config.yaml"] = EXT_IP_DISABLE
k.updateConfigMap(&configMap)
collector.AssertExternalIps(k.T(), false, k.Collector().IP())
assert.AssertExternalIps(k.T(), false, k.Collector().IP())

log.Info("Checking runtime configuration is not in use")
k.deleteConfigMap(CONFIG_MAP_NAME)
collector.AssertNoRuntimeConfig(k.T(), k.Collector().IP())
assert.AssertNoRuntimeConfig(k.T(), k.Collector().IP())

log.Info("Checking external IPs is enabled again")
configMap.Data["runtime_config.yaml"] = EXT_IP_ENABLE
k.createConfigMap(&configMap)
collector.AssertExternalIps(k.T(), true, k.Collector().IP())
assert.AssertExternalIps(k.T(), true, k.Collector().IP())
}

func (k *K8sConfigReloadTestSuite) TestConfigurationReload() {
Expand All @@ -98,10 +99,10 @@ func (k *K8sConfigReloadTestSuite) TestConfigurationReload() {
"ROX_COLLECTOR_INTROSPECTION_ENABLE": "true",
},
})
collector.AssertExternalIps(k.T(), true, k.Collector().IP())
assert.AssertExternalIps(k.T(), true, k.Collector().IP())

log.Info("Checking external IPs is disabled")
configMap.Data["runtime_config.yaml"] = EXT_IP_DISABLE
k.updateConfigMap(&configMap)
collector.AssertExternalIps(k.T(), false, k.Collector().IP())
assert.AssertExternalIps(k.T(), false, k.Collector().IP())
}
17 changes: 9 additions & 8 deletions integration-tests/suites/runtime_config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"time"

"github.com/stackrox/collector/integration-tests/pkg/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"
Expand Down Expand Up @@ -97,55 +98,55 @@ func (s *RuntimeConfigFileTestSuite) SetupSuite() {
s.StartCollector(false, &collectorOptions)

// The runtime config file was deleted before starting collector so there should not be any config
collector.AssertNoRuntimeConfig(s.T(), collectorIP)
assert.AssertNoRuntimeConfig(s.T(), collectorIP)
// Since there is no config the default is used, which means external IPs is disabled and we should
// expect a normalized connection
expectedConnections := []types.NetworkInfo{activeNormalizedConnection}
connectionSuccess := s.Sensor().ExpectSameElementsConnections(s.T(), clientContainer, 10*time.Second, expectedConnections...)
s.Require().True(connectionSuccess)

s.setExternalIpsEnable(runtimeConfigFile, true)
collector.AssertExternalIps(s.T(), true, collectorIP)
assert.AssertExternalIps(s.T(), true, collectorIP)
// When external IPs is enabled the normalized connection is reported as inactive and the unnormalized connection
// is reported as active
expectedConnections = append(expectedConnections, activeUnnormalizedConnection, inactiveNormalizedConnection)
connectionSuccess = s.Sensor().ExpectSameElementsConnections(s.T(), clientContainer, 10*time.Second, expectedConnections...)
s.Require().True(connectionSuccess)

s.setExternalIpsEnable(runtimeConfigFile, false)
collector.AssertExternalIps(s.T(), false, collectorIP)
assert.AssertExternalIps(s.T(), false, collectorIP)
// When external IPs is disabled the normalized connection is reported as active and the unnormalized connection
// is reported as inactive
expectedConnections = append(expectedConnections, activeNormalizedConnection, inactiveUnnormalizedConnection)
connectionSuccess = s.Sensor().ExpectSameElementsConnections(s.T(), clientContainer, 10*time.Second, expectedConnections...)
s.Require().True(connectionSuccess)

s.deleteFile(runtimeConfigFile)
collector.AssertNoRuntimeConfig(s.T(), collectorIP)
assert.AssertNoRuntimeConfig(s.T(), collectorIP)
// When the config file is deleted the config is reset and external IPs should continue to be disabled. There is no
// change in the expected connections.
common.Sleep(3 * time.Second) // Sleep so that collector has a chance to report connections
connectionSuccess = s.Sensor().ExpectSameElementsConnections(s.T(), clientContainer, 10*time.Second, expectedConnections...)
s.Require().True(connectionSuccess)

s.setExternalIpsEnable(runtimeConfigFile, true)
collector.AssertExternalIps(s.T(), true, collectorIP)
assert.AssertExternalIps(s.T(), true, collectorIP)
// The runtime config file is recreated with external IPs enabled. The unnormalized connection is reported as being active
// and the normalized connection is reported as being inactive.
expectedConnections = append(expectedConnections, activeUnnormalizedConnection, inactiveNormalizedConnection)
connectionSuccess = s.Sensor().ExpectSameElementsConnections(s.T(), clientContainer, 10*time.Second, expectedConnections...)
s.Require().True(connectionSuccess)

s.deleteFile(runtimeConfigFile)
collector.AssertNoRuntimeConfig(s.T(), collectorIP)
assert.AssertNoRuntimeConfig(s.T(), collectorIP)
// The runtime config file is deleted again. This time there should be a change as external IPs transitions from being
// enabled to disabled. The normalized connection should be active and the unnormalized connection shoul be inactive.
expectedConnections = append(expectedConnections, activeNormalizedConnection, inactiveUnnormalizedConnection)
connectionSuccess = s.Sensor().ExpectSameElementsConnections(s.T(), clientContainer, 10*time.Second, expectedConnections...)
s.Require().True(connectionSuccess)

s.setExternalIpsEnable(runtimeConfigFile, false)
collector.AssertExternalIps(s.T(), false, collectorIP)
assert.AssertExternalIps(s.T(), false, collectorIP)
// The runtime config file is created one more time, this time with external IPs disabled. Since there is no change in the
// state, there should not be a change in the connections reported
common.Sleep(3 * time.Second) // Sleep so that collector has a chance to report connections
Expand All @@ -154,7 +155,7 @@ func (s *RuntimeConfigFileTestSuite) SetupSuite() {

invalidConfig := "asdf"
s.setRuntimeConfig(runtimeConfigFile, invalidConfig)
collector.AssertExternalIps(s.T(), false, collectorIP)
assert.AssertExternalIps(s.T(), false, collectorIP)
// Testing an invalid configuration. There should not be a change in the configuration or reported connections
common.Sleep(3 * time.Second) // Sleep so that collector has a chance to report connections
connectionSuccess = s.Sensor().ExpectSameElementsConnections(s.T(), clientContainer, 10*time.Second, expectedConnections...)
Expand Down

0 comments on commit bdedcb4

Please sign in to comment.