Add guard to storage fingerprint so CWD will be used if AllocDir is not specified -- fixes windows tests

This commit is contained in:
Chris Bednarski
2015-08-27 16:03:09 -07:00
parent b691c5a089
commit 104ba36550
2 changed files with 17 additions and 22 deletions

View File

@@ -3,6 +3,7 @@ package fingerprint
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
@@ -40,10 +41,20 @@ func (f *StorageFingerprint) Fingerprint(cfg *config.Config, node *structs.Node)
node.Resources = &structs.Resources{}
}
if runtime.GOOS == "windows" {
path, err := filepath.Abs(cfg.AllocDir)
// Guard against unset AllocDir
storageDir := cfg.AllocDir
if storageDir == "" {
var err error
storageDir, err = os.Getwd()
if err != nil {
return false, fmt.Errorf("Failed to detect volume for storage directory %s: %s", cfg.AllocDir, err)
return false, fmt.Errorf("Unable to get CWD from filesystem: %s", err)
}
}
if runtime.GOOS == "windows" {
path, err := filepath.Abs(storageDir)
if err != nil {
return false, fmt.Errorf("Failed to detect volume for storage directory %s: %s", storageDir, err)
}
volume := filepath.VolumeName(path)
node.Attributes["storage.volume"] = volume
@@ -78,9 +89,9 @@ func (f *StorageFingerprint) Fingerprint(cfg *config.Config, node *structs.Node)
return false, fmt.Errorf("Failed to parse output from fsutil")
}
} else {
path, err := filepath.Abs(cfg.AllocDir)
path, err := filepath.Abs(storageDir)
if err != nil {
return false, fmt.Errorf("Failed to determine absolute path for %s", cfg.AllocDir)
return false, fmt.Errorf("Failed to determine absolute path for %s", storageDir)
}
// Use -k to standardize the output values between darwin and linux

View File

@@ -1,11 +1,9 @@
package fingerprint
import (
"os"
"strconv"
"testing"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/nomad/structs"
)
@@ -15,21 +13,7 @@ func TestStorageFingerprint(t *testing.T) {
Attributes: make(map[string]string),
}
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get test working directory: %s", err)
}
cfg := &config.Config{
AllocDir: cwd,
}
ok, err := fp.Fingerprint(cfg, node)
if err != nil {
t.Fatalf("Failed to fingerprint: `%s`", err)
}
if !ok {
t.Fatal("Failed to apply node attributes")
}
assertFingerprintOK(t, fp, node)
assertNodeAttributeContains(t, node, "storage.volume")
assertNodeAttributeContains(t, node, "storage.bytestotal")