-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sensors: improve sensor manager testing
Currently, the observer uses a global variable for the sensor manager. Tests may use the observer or create their own sensor manager. For the latter case, a StartTestSensorManager was created. The idea behind the test functions is that they make it easier to write tests by failing when an error happens and registering cleanup functions when the test finishes. Some tests, however, require having an observer and others do not. This patch: - Makes the sensor manager global variable in the observer a private one, and introduces access functions for it - This allows us to check whether the global variable is set twice. - Changes the StartTestSensorManager to GetTestSensorManager that first checks if the global variable is set. If it is, it just returns the existing sensor manager. Signed-off-by: Kornilios Kourtis <kornilios@isovalent.com>
- Loading branch information
Showing
10 changed files
with
76 additions
and
28 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
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,40 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Tetragon | ||
|
||
package observer | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/cilium/tetragon/pkg/sensors" | ||
) | ||
|
||
var ( | ||
// SensorManager handles dynamic sensors loading / unloading, and is a global variable for | ||
// now | ||
sensorManager *sensors.Manager | ||
sensorManagerMu sync.Mutex | ||
) | ||
|
||
// ResetSensorManager resets the global sensorManager variable to nil. Intended only for testing. | ||
func ResetSensorManager() { | ||
sensorManager = nil | ||
} | ||
|
||
func SetSensorManager(sm *sensors.Manager) error { | ||
sensorManagerMu.Lock() | ||
defer sensorManagerMu.Unlock() | ||
|
||
if sensorManager != nil { | ||
return fmt.Errorf("observer sensorManager already set") | ||
} | ||
sensorManager = sm | ||
return nil | ||
} | ||
|
||
func GetSensorManager() *sensors.Manager { | ||
sensorManagerMu.Lock() | ||
defer sensorManagerMu.Unlock() | ||
return sensorManager | ||
} |
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
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