-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sensors: test intermediate states ({,un}loading)
This patch tests the intermediate states that were introduced in the previous patches. Specifically, it tests whether ListTracingPolicies call observes loading/unloading states for the corresponding sensors. It does so by a TestDelayedSensor that implements the SensorIface interface. Signed-off-by: Kornilios Kourtis <kornilios@isovalent.com>
- Loading branch information
Showing
2 changed files
with
175 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Tetragon | ||
|
||
package sensors | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
// A sensor to test the intermediate policy states (loading / unloading) | ||
|
||
func makeTestDelayedSensor(t *testing.T) *TestDelayedSensor { | ||
s := &TestDelayedSensor{ | ||
name: "test-delayed-sensor", | ||
loaded: false, | ||
ch: make(chan struct{}), | ||
} | ||
RegisterPolicyHandlerAtInit("dummy-policyhandler", &dummyHandler{s: s}) | ||
t.Cleanup(func() { | ||
delete(registeredPolicyHandlers, "dummy-policyhandler") | ||
}) | ||
|
||
return s | ||
} | ||
|
||
type TestDelayedSensor struct { | ||
name string | ||
loaded bool | ||
ch chan struct{} | ||
} | ||
|
||
func (tds *TestDelayedSensor) GetName() string { | ||
return tds.name | ||
} | ||
|
||
func (tds *TestDelayedSensor) IsLoaded() bool { | ||
return tds.loaded | ||
} | ||
|
||
func (tds *TestDelayedSensor) Load(_ string) error { | ||
select { | ||
case <-tds.ch: | ||
|
||
case <-time.After(1 * time.Second): | ||
return fmt.Errorf("TestDelayedSensor/Load timeout when waiting for unblocking") | ||
} | ||
tds.loaded = true | ||
return nil | ||
} | ||
|
||
func (tds *TestDelayedSensor) Unload() error { | ||
select { | ||
case <-tds.ch: | ||
|
||
case <-time.After(1 * time.Second): | ||
return fmt.Errorf("TestDelayedSensor/Unload timeout when waiting for unblocking") | ||
} | ||
tds.loaded = false | ||
return nil | ||
} | ||
|
||
func (tds *TestDelayedSensor) Destroy() { | ||
tds.loaded = false | ||
} | ||
|
||
func (tds *TestDelayedSensor) unblock(_ *testing.T) { | ||
tds.ch <- struct{}{} | ||
} |
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