-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jv integration tests for runtime config #1899
Open
JoukoVirtanen
wants to merge
25
commits into
master
Choose a base branch
from
jv-integration-tests-for-runtime-config
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
b1cdc47
X-Smart-Branch-Parent: master
JoukoVirtanen 6364aff
X-Smart-Squash: Squashed 14 commits:
JoukoVirtanen dade64c
More error handling for reading the configuration. Using a volume mount
JoukoVirtanen 95c001b
Fixed lint error
JoukoVirtanen ec61352
The runtime config file is only modified on the host machine
JoukoVirtanen 0f0be47
There is now only one source code file for runtime config introspection
JoukoVirtanen a9f5c3c
Minor fixes for k8s
JoukoVirtanen e410b11
Added comments and sleeps when config does not change so we get a scr…
JoukoVirtanen 9cdf634
Increased the afterglow period
JoukoVirtanen 7dfd521
Update collector/lib/ConfigLoader.cpp
JoukoVirtanen 7f1fa8a
Renamed unit test. Integration tests use a dedicated directory
JoukoVirtanen a30b703
Created a new assert package
JoukoVirtanen 29f768f
Apply suggestions from code review
JoukoVirtanen a144acd
Split test into smaller tests
JoukoVirtanen 4862733
Apply suggestions from code review
JoukoVirtanen 0ae580b
Removed unneeded sort
JoukoVirtanen 087c326
Using slices.ContainsFunc in HasConnection
JoukoVirtanen b70f822
Made timings in AssertRepeated configurable
JoukoVirtanen c724053
Always sort the connections when comparing
JoukoVirtanen b01f751
Not using assertMismatch
JoukoVirtanen 678fc86
Updated comments
JoukoVirtanen f092de4
Apply suggestions from code review
JoukoVirtanen 11fd614
Sorting connections inside Connections instead of SortedConnections
JoukoVirtanen 30a7ad8
Created ElementsMatchFunc in assert pkg
JoukoVirtanen 766747d
Using spew for logging
JoukoVirtanen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package assert | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/davecgh/go-spew/spew" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/stackrox/collector/integration-tests/pkg/collector" | ||
"github.com/stackrox/collector/integration-tests/pkg/types" | ||
) | ||
|
||
func AssertExternalIps(t *testing.T, enable bool, collectorIP string) { | ||
tickTime := 1 * time.Second | ||
timeout := 3 * time.Minute | ||
AssertRepeated(t, tickTime, timeout, 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) { | ||
tickTime := 1 * time.Second | ||
timeout := 3 * time.Minute | ||
AssertRepeated(t, tickTime, timeout, func() bool { | ||
body, err := collector.IntrospectionQuery(collectorIP, "/state/config") | ||
assert.NoError(t, err) | ||
return strings.TrimSpace(string(body)) == "{}" | ||
}) | ||
} | ||
|
||
func AssertRepeated(t *testing.T, tickTime time.Duration, timeout time.Duration, condition func() bool) { | ||
tick := time.NewTicker(tickTime) | ||
timer := time.After(timeout) | ||
|
||
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() | ||
} | ||
} | ||
} | ||
|
||
func ElementsMatchFunc[N any](expected []N, actual []N, equal func(a, b N) bool) bool { | ||
if len(expected) != len(actual) { | ||
return false | ||
} | ||
|
||
for i := range expected { | ||
if !equal(expected[i], actual[i]) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func ListsToAssertMsg[N any](expected []N, actual []N) string { | ||
var expectedBuf, actualBuf bytes.Buffer | ||
spew.Fdump(&expectedBuf, expected) | ||
spew.Fdump(&actualBuf, actual) | ||
return fmt.Sprintf( | ||
"Expected elements:\n%s\n\nActual elements:\n%s\n", | ||
expectedBuf.String(), | ||
actualBuf.String(), | ||
) | ||
} | ||
|
||
func AssertElementsMatchFunc[N any](t *testing.T, expected []N, actual []N, equal func(a, b N) bool) bool { | ||
match := ElementsMatchFunc(expected, actual, equal) | ||
if !match { | ||
assertMsg := ListsToAssertMsg(expected, actual) | ||
assert.True(t, match, assertMsg) | ||
} | ||
return match | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"log" | ||
"net" | ||
"os" | ||
"slices" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
@@ -32,7 +33,6 @@ const ( | |
// us to use any comparable type as the key) | ||
type ProcessMap map[types.ProcessInfo]interface{} | ||
type LineageMap map[types.ProcessLineage]interface{} | ||
type ConnMap map[types.NetworkInfo]interface{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changes in this file were made here https://github.com/stackrox/collector/pull/1902/files |
||
type EndpointMap map[types.EndpointInfo]interface{} | ||
|
||
type MockSensor struct { | ||
|
@@ -47,7 +47,7 @@ type MockSensor struct { | |
processLineages map[string]LineageMap | ||
processMutex sync.Mutex | ||
|
||
connections map[string]ConnMap | ||
connections map[string][]types.NetworkInfo | ||
endpoints map[string]EndpointMap | ||
networkMutex sync.Mutex | ||
|
||
|
@@ -65,7 +65,7 @@ func NewMockSensor(test string) *MockSensor { | |
testName: test, | ||
processes: make(map[string]ProcessMap), | ||
processLineages: make(map[string]LineageMap), | ||
connections: make(map[string]ConnMap), | ||
connections: make(map[string][]types.NetworkInfo), | ||
endpoints: make(map[string]EndpointMap), | ||
} | ||
} | ||
|
@@ -155,11 +155,10 @@ func (m *MockSensor) Connections(containerID string) []types.NetworkInfo { | |
defer m.networkMutex.Unlock() | ||
|
||
if connections, ok := m.connections[containerID]; ok { | ||
keys := make([]types.NetworkInfo, 0, len(connections)) | ||
for k := range connections { | ||
keys = append(keys, k) | ||
} | ||
return keys | ||
conns := make([]types.NetworkInfo, len(connections)) | ||
copy(conns, connections) | ||
types.SortConnections(conns) | ||
return conns | ||
} | ||
return make([]types.NetworkInfo, 0) | ||
} | ||
|
@@ -171,8 +170,9 @@ 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 | ||
return slices.ContainsFunc(conns, func(c types.NetworkInfo) bool { | ||
return c.Equal(conn) | ||
}) | ||
} | ||
|
||
return false | ||
|
@@ -271,7 +271,7 @@ func (m *MockSensor) Stop() { | |
|
||
m.processes = make(map[string]ProcessMap) | ||
m.processLineages = make(map[string]LineageMap) | ||
m.connections = make(map[string]ConnMap) | ||
m.connections = make(map[string][]types.NetworkInfo) | ||
m.endpoints = make(map[string]EndpointMap) | ||
|
||
m.processChannel.Stop() | ||
|
@@ -432,11 +432,10 @@ func (m *MockSensor) pushConnection(containerID string, connection *sensorAPI.Ne | |
CloseTimestamp: connection.GetCloseTimestamp().String(), | ||
} | ||
|
||
if connections, ok := m.connections[containerID]; ok { | ||
connections[conn] = true | ||
if _, ok := m.connections[containerID]; ok { | ||
m.connections[containerID] = append(m.connections[containerID], conn) | ||
} else { | ||
connections := ConnMap{conn: true} | ||
m.connections[containerID] = connections | ||
m.connections[containerID] = []types.NetworkInfo{conn} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes in this file were made here https://github.com/stackrox/collector/pull/1902/files