Files
teleport/lib/modules/test.go
T
Jim Bishopp 22e043c430 Add TestModules (#10369)
Allows tests to set fake values to be returned from modules.GetModules()
2022-02-15 21:54:40 +00:00

79 lines
2.2 KiB
Go

/*
Copyright 2022 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package modules
import "testing"
// TestModules implements the Modules interface for testing.
//
// Setting Test* fields will return those values from interface
// methods. IsBoringBinary and PrintVersion functions return the
// same values from default modules.
//
// See SetTestModules for an example.
type TestModules struct {
// TestBuildType is returned from the BuiltType function.
TestBuildType string
// TestFeatures is returned from the Features function.
TestFeatures Features
defaultModules
}
// SetTestModules sets the value returned from GetModules to testModules
// and reverts the change in the test cleanup function.
// It must not be used in parallel tests.
//
// func TestWithFakeModules(t *testing.T) {
// modules.SetTestModules(t, &modules.TestModules{
// TestBuildType: modules.BuildEnterprise,
// TestFeatures: modules.Features{
// Cloud: true,
// },
// })
//
// // test implementation
//
// // cleanup will revert module changes after test completes
// }
//
func SetTestModules(t *testing.T, testModules *TestModules) {
defaultModules := GetModules()
t.Cleanup(func() { SetModules(defaultModules) })
SetModules(testModules)
}
// PrintVersion prints teleport version
func (m *TestModules) PrintVersion() {
m.defaultModules.PrintVersion()
}
// IsBoringBinary checks if the binary was compiled with BoringCrypto.
func (m *TestModules) IsBoringBinary() bool {
return m.defaultModules.IsBoringBinary()
}
// Features returns supported features.
func (m *TestModules) Features() Features {
return m.TestFeatures
}
// BuildType returns build type (OSS or Enterprise).
func (m *TestModules) BuildType() string {
return m.TestBuildType
}