Skip to content

Commit

Permalink
[NO-ISSUE] Fix capturing log for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brusdev committed Sep 16, 2024
1 parent f0f2a8c commit 4be587f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 46 deletions.
2 changes: 1 addition & 1 deletion controllers/activemqartemis_controller2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ var _ = Describe("artemis controller 2", func() {
}, timeout, interval).Should(Succeed())

By("finding no Updating v1.Route ")
matches, err := FindAllFromCapturedLog(`Updating \*v1.Route`)
matches, err := FindAllInCapturingLog(`Updating \*v1.Route`)
Expect(err).To(BeNil())
Expect(len(matches)).To(Equal(0))

Expand Down
8 changes: 4 additions & 4 deletions controllers/activemqartemis_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1350,11 +1350,11 @@ var _ = Describe("artemis controller", func() {
g.Expect(newVarFound).To(BeTrue())
}, timeout, interval).Should(Succeed())

hasMatch, matchErr := MatchCapturedLog("Failed to create new \\*v1\\.Secret")
hasMatch, matchErr := MatchInCapturingLog("Failed to create new \\*v1\\.Secret")
Expect(matchErr).To(BeNil())
Expect(hasMatch).To(BeFalse())

hasMatch, matchErr = MatchCapturedLog("The secret " + secret.Name + " is ignored because its onwer references doesn't include ActiveMQArtemis/" + brokerCr.Name)
hasMatch, matchErr = MatchInCapturingLog("The secret " + secret.Name + " is ignored because its onwer references doesn't include ActiveMQArtemis/" + brokerCr.Name)
Expect(matchErr).To(BeNil())
Expect(hasMatch).To(BeTrue())

Expand Down Expand Up @@ -3241,7 +3241,7 @@ var _ = Describe("artemis controller", func() {
g.Expect(meta.IsStatusConditionTrue(deployedCrd.Status.Conditions, brokerv1beta1.ReadyConditionType)).Should(BeTrue())
}, existingClusterTimeout, existingClusterInterval).Should(Succeed())

unequalEntries, _ := FindAllFromCapturedLog("Unequal")
unequalEntries, _ := FindAllInCapturingLog("Unequal")
Expect(len(unequalEntries)).Should(BeNumerically("==", 0))

Expect(k8sClient.Delete(ctx, deployedCrd)).Should(Succeed())
Expand Down Expand Up @@ -8062,7 +8062,7 @@ var _ = Describe("artemis controller", func() {
g.Expect(len(createdCrd.Status.PodStatus.Stopped)).Should(BeEquivalentTo(1))
}, timeout, interval).Should(Succeed())

Expect(MatchCapturedLog("Failed to create new \\*v1.Secret")).Should(BeFalse())
Expect(MatchInCapturingLog("Failed to create new \\*v1.Secret")).Should(BeFalse())

// cleanup
k8sClient.Delete(ctx, &crd)
Expand Down
6 changes: 3 additions & 3 deletions controllers/activemqartemissecurity_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ var _ = Describe("security controller", func() {

By("checking the log")
Eventually(func(g Gomega) {
isMatch, err := MatchPattern(TestLogWrapper.unbufferedWriter.String(), "resource successfully reconciled")
isMatch, err := MatchInCapturingLog("resource successfully reconciled")
g.Expect(err).To(BeNil())
g.Expect(isMatch).To(BeTrue())
}, timeout, interval).Should(Succeed())
hasMatch, matchErr := MatchPattern(TestLogWrapper.unbufferedWriter.String(), okDefaultPwd)
hasMatch, matchErr := MatchInCapturingLog(okDefaultPwd)
Expect(matchErr).To(BeNil())
Expect(hasMatch).To(BeFalse(), TestLogWrapper.unbufferedWriter.String())
Expect(hasMatch).To(BeFalse())

CleanResource(createdBrokerCr, createdBrokerCr.Name, defaultNamespace)
CleanResource(createdSecurityCr, createdSecurityCr.Name, defaultNamespace)
Expand Down
27 changes: 0 additions & 27 deletions controllers/common_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"os"
"os/exec"
"path"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -81,32 +80,6 @@ func GetHelmCmd() string {
return "helm"
}

type TestLogWriter struct {
unbufferedWriter bytes.Buffer
}

func (w *TestLogWriter) Write(p []byte) (n int, err error) {
num, err := w.unbufferedWriter.Write(p)
if err != nil {
return num, err
}
return GinkgoWriter.Write(p)
}

func (w *TestLogWriter) StartLogging() {
w.unbufferedWriter = *bytes.NewBuffer(nil)
}

func (w *TestLogWriter) StopLogging() {
w.unbufferedWriter.Reset()
}

var TestLogWrapper = TestLogWriter{}

func MatchPattern(content string, pattern string) (matched bool, err error) {
return regexp.Match(pattern, []byte(content))
}

func randStringWithPrefix(prefix string) string {
rand.Seed(time.Now().UnixNano())
length := 6
Expand Down
20 changes: 9 additions & 11 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ var (
}
managerChannel chan struct{}

testWriter = common.BufferWriter{}
capturingLogWriter = common.BufferWriter{}

artemisGvk = schema.GroupVersionKind{Group: "broker", Version: "v1beta1", Kind: "ActiveMQArtemis"}

Expand Down Expand Up @@ -769,7 +769,7 @@ func setUpK8sClient() {
var _ = BeforeSuite(func() {
opts := zap.Options{
Development: true,
DestWriter: &TestLogWrapper,
DestWriter: GinkgoWriter,
TimeEncoder: zapcore.ISO8601TimeEncoder,
}

Expand All @@ -779,7 +779,7 @@ var _ = BeforeSuite(func() {
GinkgoWriter.TeeTo(os.Stderr)
}

GinkgoWriter.TeeTo(testWriter)
GinkgoWriter.TeeTo(&capturingLogWriter)

ctx, cancel = context.WithCancel(context.TODO())

Expand Down Expand Up @@ -870,25 +870,23 @@ func uninstallCRDs() {
}

func StartCapturingLog() {
testWriter.Buffer = bytes.NewBuffer(nil)
TestLogWrapper.StartLogging()
capturingLogWriter.Buffer = bytes.NewBuffer(nil)
}

func MatchCapturedLog(pattern string) (matched bool, err error) {
return regexp.Match(pattern, TestLogWrapper.unbufferedWriter.Bytes())
func MatchInCapturingLog(pattern string) (matched bool, err error) {
return regexp.Match(pattern, capturingLogWriter.Buffer.Bytes())
}

func FindAllFromCapturedLog(pattern string) ([]string, error) {
func FindAllInCapturingLog(pattern string) ([]string, error) {
re, err := regexp.Compile(pattern)
if err == nil {
return re.FindAllString(TestLogWrapper.unbufferedWriter.String(), -1), nil
return re.FindAllString(capturingLogWriter.Buffer.String(), -1), nil
}
return nil, err
}

func StopCapturingLog() {
testWriter.Buffer = nil
TestLogWrapper.StopLogging()
capturingLogWriter.Buffer = nil
}

func BeforeEachSpec() {
Expand Down

0 comments on commit 4be587f

Please sign in to comment.