1
0
mirror of https://github.com/gluster/glusterd2.git synced 2026-02-05 12:45:38 +01:00

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>
This commit is contained in:
Prashanth Pai
2018-09-14 14:51:32 +05:30
parent 19bca18f92
commit 31f09bfd7d
3 changed files with 20 additions and 13 deletions

View File

@@ -163,7 +163,7 @@ func main() {
}
case unix.SIGUSR1:
log.Info("Received SIGUSR1. Dumping statedump")
utils.WriteStatedump()
utils.WriteStatedump(config.GetString("rundir"))
default:
continue
}

View File

@@ -11,12 +11,12 @@ import (
"time"
log "github.com/sirupsen/logrus"
config "github.com/spf13/viper"
)
// WriteStatedump writes statedump information to a file. The file name is
// of the format glusterd2.<pid>.dump.<timestamp>
func WriteStatedump() {
// 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()
@@ -27,10 +27,9 @@ func WriteStatedump() {
return
}
dumpDir := config.GetString("rundir")
dumpFileName := fmt.Sprintf("glusterd2.%s.dump.%s",
strconv.Itoa(os.Getpid()), strconv.Itoa(int(time.Now().Unix())))
dumpPath := path.Join(dumpDir, dumpFileName)
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")

View File

@@ -2,18 +2,26 @@ package utils
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWriteStatedump(t *testing.T) {
filename := fmt.Sprintf("glusterd2.%s.dump.%s",
strconv.Itoa(os.Getpid()), strconv.Itoa(int(time.Now().Unix())))
WriteStatedump()
assert.FileExists(t, filename)
os.Remove(filename)
r := require.New(t)
dir, err := ioutil.TempDir("", t.Name())
r.Nil(err)
defer os.RemoveAll(dir)
WriteStatedump(dir)
filePattern := fmt.Sprintf("glusterd2.%s.dump.*", strconv.Itoa(os.Getpid()))
matches, err := filepath.Glob(filepath.Join(dir, filePattern))
r.Nil(err)
r.NotEmpty(matches)
}