Skip to content

Commit

Permalink
Merge pull request #14 from woblerr/separate_stderr_stdout
Browse files Browse the repository at this point in the history
Use two separate buffers instead of CombinedOutput.
  • Loading branch information
woblerr committed Sep 23, 2021
2 parents 659a00e + 54f8635 commit f9d68f5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
36 changes: 30 additions & 6 deletions backrest/backrest_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

var (
mockStdout string
mockExit int
mockStdout, mockStderr string
mockExit int
)

func TestSetPromPortandPath(t *testing.T) {
Expand All @@ -39,6 +39,7 @@ func TestGetPgBackRestInfo(t *testing.T) {
name string
args args
mockStdout string
mockStderr string
mockExit int
testText string
}{
Expand All @@ -54,33 +55,54 @@ func TestGetPgBackRestInfo(t *testing.T) {
`"system-id":6970977677138971135,"version":"13"}],"name":"demo","repo":[{"cipher":"none",` +
`"key":1,"status":{"code":0,"message":"ok"}}],"status":{"code":0,"lock":{"backup":` +
`{"held":false}},"message":"ok"}}]`,
``,
0,
""},
{"GetPgBackRestInfoGoodDataReturnWithWarn",
args{"", "", []string{""}, []string{""}, false},
`[{"archive":[{"database":{"id":1,"repo-key":1},"id":"13-1",` +
`"max":"000000010000000000000002","min":"000000010000000000000001"}],` +
`"backup":[{"archive":{"start":"000000010000000000000002","stop":"000000010000000000000002"},` +
`"backrest":{"format":5,"version":"2.34"},"database":{"id":1,"repo-key":1},` +
`"info":{"delta":24316343,"repository":{"delta":2969512,"size":2969512},"size":24316343},` +
`"label":"20210614-213200F","prior":null,"reference":null,"timestamp":{"start":1623706320,` +
`"stop":1623706322},"type":"full"}],"cipher":"none","db":[{"id":1,"repo-key":1,` +
`"system-id":6970977677138971135,"version":"13"}],"name":"demo","repo":[{"cipher":"none",` +
`"key":1,"status":{"code":0,"message":"ok"}}],"status":{"code":0,"lock":{"backup":` +
`{"held":false}},"message":"ok"}}]`,
`WARN: environment contains invalid option 'test'`,
0,
"[ERROR] pgBackRest message: WARN: environment contains invalid option 'test'"},
{"GetPgBackRestInfoBadDataReturn",
args{"", "", []string{""}, []string{""}, false},
`Forty two`,
1,
"[ERROR] pgBackRest error: Forty two"},
``,
`ERROR: [029]: missing '=' in key/value at line 9: test`,
29,
"[ERROR] Get data from pgBackRest failed, exit status 29"},
{"GetPgBackRestInfoZeroDataReturn",
args{"", "", []string{""}, []string{""}, false},
`[]`,
``,
0,
"[WARN] No backup data returned"},
{"GetPgBackRestInfoJsonUnmarshalFail",
args{"", "", []string{""}, []string{""}, false},
`[{}`,
``,
0,
"[ERROR] Parse JSON failed, unexpected end of JSON input"},
{"GetPgBackRestInfoEqualIncludeExcludeLists",
args{"", "", []string{"demo"}, []string{"demo"}, false},
`[{}`,
``,
``,
0,
"[WARN] Stanza demo is specified in include and exclude lists"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ResetMetrics()
mockStdout = tt.mockStdout
mockStderr = tt.mockStderr
execCommand = fakeExecCommand
mockExit = tt.mockExit
defer func() { execCommand = exec.Command }()
Expand All @@ -107,6 +129,7 @@ func fakeExecCommand(command string, args ...string) *exec.Cmd {
es := strconv.Itoa(mockExit)
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1",
"STDOUT=" + mockStdout,
"STDERR=" + mockStderr,
"EXIT_STATUS=" + es}
return cmd
}
Expand All @@ -116,6 +139,7 @@ func TestExecCommandHelper(t *testing.T) {
return
}
fmt.Fprintf(os.Stdout, "%s", os.Getenv("STDOUT"))
fmt.Fprintf(os.Stderr, "%s", os.Getenv("STDERR"))
i, _ := strconv.Atoi(os.Getenv("EXIT_STATUS"))
os.Exit(i)
}
19 changes: 14 additions & 5 deletions backrest/backrest_parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package backrest

import (
"bytes"
"encoding/json"
"errors"
"log"
Expand Down Expand Up @@ -213,16 +214,24 @@ func getAllInfoData(config, configIncludePath, stanza string) ([]byte, error) {
returnConfigExecArgs(config, configIncludePath),
returnConfigStanzaArgs(stanza),
}
// Finally arguments for exec command
// Finally arguments for exec command.
concatArgs := concatExecArgs(args)
out, err := execCommand(app, concatArgs...).CombinedOutput()
// If error occurs - write error from pgBackRest to log and
cmd := execCommand(app, concatArgs...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
// If stderr from pgBackRest is not empty,
// write message from pgBackRest to log.
if stderr.Len() > 0 {
log.Printf("[ERROR] pgBackRest message: %v", stderr.String())
}
// If error occurs,
// return nil for stanza data.
if err != nil {
log.Printf("[ERROR] pgBackRest error: %v", string(out))
return nil, err
}
return out, err
return stdout.Bytes(), err
}

func parseResult(output []byte) ([]stanza, error) {
Expand Down

0 comments on commit f9d68f5

Please sign in to comment.