Documentation
¶
Overview ¶
Package testproject implements locking of Keboola Projects for E2E parallel tests.
Project is locked: - at locker level (redislock) OR - at the host level (flock.Flock) AND at the goroutines level (sync.Mutex)
Only one test can access the project at a time. See GetTestProject function. If there is no unlocked project, the function waits until a project is released.
Package can be safely used in parallel tests that run on a single host. Use GetTestProjectForTest function to get a testing project in a test. Project lock is automatically released at the end of the test.
Locking between multiple hosts is not provided.
The state of the project does not change automatically, if you need an empty project use storageapi.CleanProjectRequest.
Index ¶
Examples ¶
Constants ¶
const ( StagingStorageABS = "abs" StagingStorageGCS = "gcs" StagingStorageS3 = "s3" BackendSnowflake = "snowflake" BackendBigQuery = "bigquery" TestKbcProjectsFileKey = "TEST_KBC_PROJECTS_FILE" TestKbcProjectsLockDirNameKey = "TEST_KBC_PROJECTS_LOCK_DIR_NAME" TestKbcProjectsLockHostKey = "TEST_KBC_PROJECTS_LOCK_HOST" TestKbcProjectsLockPasswordKey = "TEST_KBC_PROJECTS_LOCK_PASSWORD" TestKbcProjectsLockTLSKey = "TEST_KBC_PROJECTS_LOCK_TLS" )
const (
TTL = 2 * time.Minute
)
Variables ¶
This section is empty.
Functions ¶
func GetTestProject ¶
GetTestProject locks and returns a testing project specified in TEST_KBC_PROJECTS environment variable. The returned UnlockFn function must be called to free project, when the project is no longer used (e.g. defer unlockFn()) If no project is available, the function waits until a project is released.
Example ¶
// Note: For real use call the "GetTestProject" function, // to get a testing project from the "TEST_KBC_PROJECTS" environment variable. // Here, the "projects.GetTestProject" method is called to make it testable and without global variables. projects := MustGetProjectsFrom(projectsForTest()) // Acquire exclusive access to the project. project1, unlockFn1, _ := projects.GetTestProject() defer unlockFn1() fmt.Printf("Project %d locked.\n", project1.ID()) project2, unlockFn2, _ := projects.GetTestProject() defer unlockFn2() fmt.Printf("Project %d locked.\n", project2.ID()) project3, unlockFn3, _ := projects.GetTestProject() defer unlockFn3() fmt.Printf("Project %d locked.\n", project3.ID()) // The returned UnlockFn function must be called to free project, when the project is no longer used (e.g. defer unlockFn()) // See also ExampleGetTestProjectForTest for usage in a test.
Output: Project 1234 locked. Project 3456 locked. Project 5678 locked.
Example (Second) ¶
// Note: For real use call the "GetTestProject" function, // to get a testing project from the "TEST_KBC_PROJECTS" environment variable. // Provide also "TEST_KBC_PROJECTS_LOCK_HOST" and "TEST_KBC_PROJECTS_LOCK_PASSWORD" variables to connect into redis. // Here, the "projects.GetTestProject" method is called to make it testable and without global variables. //os.Setenv(TestKbcProjectsLockHostKey, "redis:6379") // os.Setenv(TestKbcProjectsLockPasswordKey, "testing") //defer func() { // os.Unsetenv(TestKbcProjectsLockHostKey) // os.Unsetenv(TestKbcProjectsLockPasswordKey) //}() projects, err := GetProjectsFrom(projectsForTest()) if err != nil { fmt.Println("redis is not up.") return } // Acquire exclusive access to the project. project1, unlockFn1, _ := projects.GetTestProject() defer unlockFn1() fmt.Printf("Project %d locked.\n", project1.ID()) project2, unlockFn2, _ := projects.GetTestProject() defer unlockFn2() fmt.Printf("Project %d locked.\n", project2.ID()) project3, unlockFn3, _ := projects.GetTestProject() defer unlockFn3() fmt.Printf("Project %d locked.\n", project3.ID()) // The returned UnlockFn function must be called to free project, when the project is no longer used (e.g. defer unlockFn())
Output: Project 1234 locked. Project 3456 locked. Project 5678 locked.
Types ¶
type Definition ¶ added in v0.6.0
type Definition struct { Host string `json:"host" validate:"required"` Token string `json:"token" validate:"required"` StagingStorage string `json:"stagingStorage" validate:"required"` Backend string `json:"backend" validate:"required"` ProjectID int `json:"project" validate:"required"` LegacyTransformation bool `json:"legacyTransformation"` IsGuest bool `json:"isGuest,omitempty"` }
Definition is project Definition parsed from the ENV.
type Option ¶ added in v0.6.0
type Option func(c *config)
Option for the GetTestProjectForTest and GetTestProject functions.
func WithBigQueryBackend ¶ added in v0.8.4
func WithBigQueryBackend() Option
func WithIsGuest ¶ added in v1.2.0
func WithIsGuest() Option
func WithLegacyTransformation ¶ added in v0.9.0
func WithLegacyTransformation() Option
func WithSnowflakeBackend ¶ added in v0.8.4
func WithSnowflakeBackend() Option
func WithStagingStorage ¶ added in v0.6.0
Example ¶
// Note: For real use call the "GetTestProject" function, // to get a testing project from the "TEST_KBC_PROJECTS" environment variable. // Here, the "projects.GetTestProject" method is called to make it testable and without global variables. projects := MustGetProjectsFrom(projectsForTest()) // Acquire exclusive access to the project. project, unlockFn, _ := projects.GetTestProject(WithStagingStorage("abs")) defer unlockFn() fmt.Printf("Project %d locked.\n", project.ID()) fmt.Printf("Staging storage: %s.\n", project.StagingStorage())
Output: Project 3456 locked. Staging storage: abs.
func WithStagingStorageABS ¶ added in v0.6.1
func WithStagingStorageABS() Option
func WithStagingStorageGCS ¶ added in v0.6.2
func WithStagingStorageGCS() Option
func WithStagingStorageS3 ¶ added in v0.6.1
func WithStagingStorageS3() Option
type Project ¶
type Project struct {
// contains filtered or unexported fields
}
Project represents a testing project for E2E tests.
func GetTestProjectForTest ¶ added in v0.3.0
func GetTestProjectForTest(t TInterface, opts ...Option) (*Project, error)
GetTestProjectForTest locks and returns a testing project specified in TEST_KBC_PROJECTS environment variable. Project lock is automatically released at the end of the test. If no project is available, the function waits until a project is released.
Example ¶
t := &mockedT{} // Note: For real use call the "GetTestProject" function, // to get a testing project from the "TEST_KBC_PROJECTS" environment variable. // Here, the "projects.GetTestProject" method is called to make it testable and without global variables. projects := MustGetProjectsFrom(projectsForTest()) // Acquire exclusive access to the project. project1, _ := projects.GetTestProjectForTest(t) fmt.Printf("Project %d locked.\n", project1.ID()) //nolint:forbidigo project2, _ := projects.GetTestProjectForTest(t) fmt.Printf("Project %d locked.\n", project2.ID()) //nolint:forbidigo project3, _ := projects.GetTestProjectForTest(t) fmt.Printf("Project %d locked.\n", project3.ID()) //nolint:forbidigo // Project lock will be automatically released at the end of the test. for _, f := range t.cleanup { f() }
Output: Project 1234 locked. Project 3456 locked. Project 5678 locked.
func (*Project) LegacyTransformation ¶ added in v0.9.0
LegacyTransformation returns support of legacy transformations of the project Definition.
func (*Project) StagingStorage ¶ added in v0.6.0
StagingStorage returns staging storage of the project Definition.
func (*Project) StorageAPIHost ¶
StorageAPIHost returns Storage API host of the project stack.
func (*Project) StorageAPIToken ¶
StorageAPIToken returns Storage API token of the project.
type ProjectsPool ¶ added in v0.6.0
type ProjectsPool []*Project
ProjectsPool a group of testing projects.
func GetProjectsFrom ¶ added in v0.6.0
func GetProjectsFrom(str string) (ProjectsPool, error)
func MustGetProjectsFrom ¶ added in v0.6.0
func MustGetProjectsFrom(str string) ProjectsPool
func (ProjectsPool) GetTestProject ¶ added in v0.6.0
func (v ProjectsPool) GetTestProject(opts ...Option) (*Project, UnlockFn, error)
GetTestProject locks and returns a testing project specified in TEST_KBC_PROJECTS environment variable. The returned UnlockFn function must be called to free project, when the project is no longer used (e.g. defer unlockFn()) If no project is available, the function waits until a project is released.
func (ProjectsPool) GetTestProjectForTest ¶ added in v0.6.0
func (v ProjectsPool) GetTestProjectForTest(t TInterface, opts ...Option) (*Project, error)
GetTestProjectForTest locks and returns a testing project specified in TEST_KBC_PROJECTS environment variable. Project lock is automatically released at the end of the test. If no project is available, the function waits until a project is released.
type TInterface ¶ added in v0.6.0
type TInterface interface {
Cleanup(f func())
}
TInterface is cleanup part of the *testing.T.