Created
June 6, 2024 07:04
-
-
Save mabhi/3c0460db11035d65d881a18f0aa26789 to your computer and use it in GitHub Desktop.
Check log message for a string
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
/* | |
Ex. output | |
{"File":"xyx.xyz/../../../some.go","Function":"xyz.xyz/../../package.FunctionName,"Line":36,"level":"error","msg":"Unsupported base image","time":"2024-06-06T12:29:40.032+0530"} | |
*/ | |
func checkInvalidImage(c *qt.C, imageName, expected string) { | |
// Capture stderr via a pipe | |
stdErrC := make(chan *bytes.Buffer) | |
pR, pW, err := os.Pipe() | |
c.Assert(err, qt.IsNil) | |
go func() { | |
var buf bytes.Buffer | |
io.Copy(&buf, pR) | |
pR.Close() | |
stdErrC <- &buf | |
}() | |
// Save and redirect stderr | |
var savedStderr = os.Stderr | |
defer func() { | |
os.Stderr = savedStderr | |
log.SetOutput(log.StderrSink) | |
}() | |
os.Stderr = pW | |
log.SetOutput(log.StderrSink) | |
var result = GetKanisterImage(imageName) | |
// Close pipe writer and capture stderr output | |
pW.Close() | |
stderr := <-stdErrC | |
c.Assert(result, qt.Equals, expected) | |
re := regexp.MustCompile(`"msg"\s*:\s*"([^"]+)"`) | |
matches := re.FindAllStringSubmatch(stderr.String(), -1) | |
c.Assert(len(matches), qt.Equals, 1) | |
c.Assert(matches[0][1], qt.Equals, unsupportedErrorMsg) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment