1
0
mirror of https://github.com/gluster/glusterd2.git synced 2026-02-05 12:45:38 +01:00
Files
glusterd2/pkg/utils/statedump.go
Prashanth Pai 31f09bfd7d Fix TestWriteStatedump test
The unit test (TestWriteStatedump) uses a filename derived from current
timestamp and the actual code (WriteStatedump) being tested also uses
filename of the same pattern but that one may have another timestamp as
that code executes slightly later. Hence, it can so happen that the file
name written by WriteStatedump() is different than the filename expected
by the test. As these two timestamps are at seconds level granularity,
we didn't see it fail earlier.

This change modifies the test to look for a pattern that resembles
statedump file.

Signed-off-by: Prashanth Pai <ppai@redhat.com>
2018-09-14 17:11:38 +05:30

40 lines
1.1 KiB
Go

package utils
import (
"expvar"
"fmt"
"io/ioutil"
"net/http/httptest"
"os"
"path"
"strconv"
"time"
log "github.com/sirupsen/logrus"
)
// WriteStatedump writes statedump information to a file. The file name is
// of the format glusterd2.<pid>.dump.<timestamp>. This file will be
// written to the directory passed.
func WriteStatedump(dirpath string) {
// Run the expvar http handler
w := httptest.NewRecorder()
expvar.Handler().ServeHTTP(w, httptest.NewRequest("GET", "/statedump", nil))
respBody, err := ioutil.ReadAll(w.Result().Body)
if err != nil {
log.WithError(err).Error("Failed to fetch statedump details from expvar handler")
return
}
dumpFileName := fmt.Sprintf("glusterd2.%s.dump.%s",
strconv.Itoa(os.Getpid()), strconv.Itoa(int(time.Now().Unix())))
dumpPath := path.Join(dirpath, dumpFileName)
if err := ioutil.WriteFile(dumpPath, respBody, 0644); err != nil {
log.WithError(err).WithField("file", dumpPath).Error("Failed to write statedump to file")
return
}
log.WithField("file", dumpPath).Info("Statedump written to file")
}