-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement kubectl helper integration tests (#342)
* Refactor k3s utils for reuse * Export NewFromConfig function * Implement kubectl integration tests --------- Signed-off-by: Pablo Chacin <[email protected]>
- Loading branch information
1 parent
ce51386
commit 8aaeff7
Showing
5 changed files
with
113 additions
and
72 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,57 @@ | ||
// Package k3sutils implements helper functions for tests using TestContainer's k3s module | ||
package k3sutils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"regexp" | ||
"time" | ||
|
||
"github.com/testcontainers/testcontainers-go" | ||
) | ||
|
||
// regexMatcher process lines from logs and notifies when a match is found | ||
type regexMatcher struct { | ||
exp *regexp.Regexp | ||
found chan (bool) | ||
} | ||
|
||
// Accept implements the LogConsumer interface | ||
func (a *regexMatcher) Accept(log testcontainers.Log) { | ||
if a.exp.MatchString(string(log.Content)) { | ||
a.found <- true | ||
} | ||
} | ||
|
||
// WaitForRegex waits until a match for the given regex is found in the log produced by the container | ||
// This utility function is a workaround for https://github.com/testcontainers/testcontainers-go/issues/1541 | ||
func WaitForRegex(ctx context.Context, container testcontainers.Container, exp string, timeout time.Duration) error { | ||
regexp, err := regexp.Compile(exp) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
found := make(chan bool) | ||
container.FollowOutput(®exMatcher{ | ||
exp: regexp, | ||
found: found, | ||
}) | ||
|
||
err = container.StartLogProducer(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
_ = container.StopLogProducer() | ||
}() | ||
|
||
expired := time.After(timeout) | ||
select { | ||
case <-found: | ||
return nil | ||
case <-expired: | ||
return fmt.Errorf("timeout waiting for a match") | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
} | ||
} |