Skip to content

Commit

Permalink
Broke setExtenalIpsEnabled into smaller functions. Using built in fun…
Browse files Browse the repository at this point in the history
…ctions for createDir and removeFile
  • Loading branch information
JoukoVirtanen committed Dec 6, 2024
1 parent 9a30908 commit 596a2e3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
14 changes: 14 additions & 0 deletions integration-tests/pkg/types/runtime_config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package types

import (
"gopkg.in/yaml.v3"
)

type RuntimeConfig struct {
Networking struct {
ExternalIps struct {
Expand All @@ -11,3 +15,13 @@ type RuntimeConfig struct {
func (n *RuntimeConfig) Equal(other RuntimeConfig) bool {
return n.Networking.ExternalIps.Enable == other.Networking.ExternalIps.Enable
}

func (s *RuntimeConfig) GetRuntimeConfigStr() (string, error) {
yamlBytes, err := yaml.Marshal(s)

if err != nil {
return "", err
}

return string(yamlBytes), err
}
14 changes: 10 additions & 4 deletions integration-tests/suites/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,13 +459,19 @@ func (s *IntegrationTestSuiteBase) execShellCommand(command string) error {
}

func (s *IntegrationTestSuiteBase) createDirectory(dir string) {
cmd := "mkdir " + dir
s.execShellCommand(cmd)
if _, err := os.Stat(dir); err == nil {
return
}
err := os.Mkdir(dir, os.ModePerm)
s.Require().NoError(err)
}

func (s *IntegrationTestSuiteBase) deleteFile(file string) {
cmd := "rm " + file
s.execShellCommand(cmd)
if _, err := os.Stat(file); os.IsNotExist(err) {
return
}
err := os.Remove(file)
s.Require().NoError(err)
}

func (s *IntegrationTestSuiteBase) waitForFileToBeDeleted(file string) error {
Expand Down
21 changes: 12 additions & 9 deletions integration-tests/suites/runtime_config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package suites

import (
"fmt"
"gopkg.in/yaml.v3"
"path/filepath"
"time"

Expand Down Expand Up @@ -66,15 +65,19 @@ func (s *RuntimeConfigFileTestSuite) setRuntimeConfig(runtimeConfigFile string,
s.execShellCommand(cmd)
}

func (s *RuntimeConfigFileTestSuite) setExternalIpsEnable(runtimeConfigFile string, enable bool) {
func (s *RuntimeConfigFileTestSuite) getRuntimeConfigEnabledStr(enabled bool) string {
var runtimeConfig types.RuntimeConfig
runtimeConfig.Networking.ExternalIps.Enable = enable
runtimeConfig.Networking.ExternalIps.Enable = enabled

yamlBytes, err := yaml.Marshal(runtimeConfig)
configStr, err := runtimeConfig.GetRuntimeConfigStr()
s.Require().NoError(err)

configStr := string(yamlBytes)
s.setRuntimeConfig(runtimeConfigFile, configStr)
return configStr
}

func (s *RuntimeConfigFileTestSuite) setExternalIpsEnabled(runtimeConfigFile string, enabled bool) {
runtimeConfigStr := s.getRuntimeConfigEnabledStr(enabled)
s.setRuntimeConfig(runtimeConfigFile, runtimeConfigStr)
}

// Launches collector and creates the directory for runtime configuration.
Expand Down Expand Up @@ -123,7 +126,7 @@ func (s *RuntimeConfigFileTestSuite) TestRuntimeConfigFileEnable() {
// External IPs enabled.
// Normalized connection must be reported as inactive
// Unnormalized connection will now be reported.
s.setExternalIpsEnable(runtimeConfigFile, true)
s.setExternalIpsEnabled(runtimeConfigFile, true)
assert.AssertExternalIps(s.T(), true, collectorIP)
expectedConnections = append(expectedConnections, activeUnnormalizedConnection, inactiveNormalizedConnection)
connectionSuccess = s.Sensor().ExpectSameElementsConnections(s.T(), s.ClientContainer, 10*time.Second, expectedConnections...)
Expand All @@ -138,7 +141,7 @@ func (s *RuntimeConfigFileTestSuite) TestRuntimeConfigFileEnable() {
s.Require().True(connectionSuccess)

// Back to having external IPs enabled.
s.setExternalIpsEnable(runtimeConfigFile, true)
s.setExternalIpsEnabled(runtimeConfigFile, true)
assert.AssertExternalIps(s.T(), true, collectorIP)
expectedConnections = append(expectedConnections, activeUnnormalizedConnection, inactiveNormalizedConnection)
connectionSuccess = s.Sensor().ExpectSameElementsConnections(s.T(), s.ClientContainer, 10*time.Second, expectedConnections...)
Expand All @@ -156,7 +159,7 @@ func (s *RuntimeConfigFileTestSuite) TestRuntimeConfigFileDisable() {

// The runtime config file is created, but external IPs is disables.
// There is no change in the state, so there are no changes to the connections
s.setExternalIpsEnable(runtimeConfigFile, false)
s.setExternalIpsEnabled(runtimeConfigFile, false)
assert.AssertExternalIps(s.T(), false, collectorIP)
common.Sleep(3 * time.Second) // Sleep so that collector has a chance to report connections
connectionSuccess = s.Sensor().ExpectSameElementsConnections(s.T(), s.ClientContainer, 10*time.Second, expectedConnections...)
Expand Down

0 comments on commit 596a2e3

Please sign in to comment.