Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix directory existence test on Windows #424

Merged
merged 3 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions common/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package common

import (
"os"
"path"
"path/filepath"

"github.com/Graylog2/collector-sidecar/logger"
Expand Down Expand Up @@ -47,11 +46,11 @@ func IsDir(filePath string) bool {

}

func CreatePathToFile(filepath string) error {
dir := path.Dir(filepath)
func CreatePathToFile(path string) error {
dir := filepath.Dir(path)
_, err := os.Open(dir)
if err != nil {
log.Info("Trying to create directory for: ", filepath)
log.Info("Trying to create directory for: ", path)
err = os.MkdirAll(dir, 0750)
if err != nil {
log.Error("Not able to create directory path: ", dir)
Expand Down
33 changes: 22 additions & 11 deletions common/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,7 @@ func GetCollectorId(collectorId string) string {
id := collectorId
if strings.HasPrefix(collectorId, "file:") {
filePath := strings.SplitAfterN(collectorId, ":", 2)[1]
err := FileExists(filePath)
if err != nil {
log.Info("node-id file doesn't exist, generating a new one")
CreatePathToFile(filePath)
ioutil.WriteFile(filePath, []byte(RandomUuid()), 0644)
}
file, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatal("Can not read node-id file: ", err)
}
id = strings.Trim(string(file), " \n")
id = idFromFile(filePath)
}

if id != "" && !cfgfile.ValidateConfig() {
Expand All @@ -99,6 +89,27 @@ func GetCollectorId(collectorId string) string {
return id
}

func idFromFile(filePath string) string {
err := FileExists(filePath)
if err != nil {
log.Info("node-id file doesn't exist, generating a new one")
err = CreatePathToFile(filePath)
if err == nil {
err = ioutil.WriteFile(filePath, []byte(RandomUuid()), 0644)
if err != nil {
log.Error("Can not write node-id file: ", err)
}
}
}

file, err := ioutil.ReadFile(filePath)
if err != nil {
log.Error("Can not read node-id file: ", err)
return ""
}
return strings.Trim(string(file), " \n")
}

func RandomUuid() string {
return uuid.NewRandom().String()
}
Expand Down
40 changes: 40 additions & 0 deletions common/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,46 @@ func TestGetCollectorIdFromNonExistingFile(t *testing.T) {
}
}

func TestGetCollectorIdFromNonExistingPath(t *testing.T) {
dir, err := ioutil.TempDir("", "test-node-id")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

tmpfile := filepath.Join(dir, "subdir", "node-id")
result := GetCollectorId("file:/" + tmpfile)
match, err := regexp.Match("^[0-9a-f]{8}-", []byte(result))
if err != nil {
t.Fatal(err)
}

if !match {
t.Fail()
}
}

func TestCollectorIdFileNotWritable(t *testing.T) {
dir, err := ioutil.TempDir("", "test-node-id")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

nodeIdDir := filepath.Join(dir, "non-writable")

err = os.MkdirAll(nodeIdDir, 0500)
if err != nil {
t.Fatal(err)
}

tmpfile := filepath.Join(nodeIdDir, "node-id")
result := GetCollectorId("file:/" + tmpfile)
if result != "" {
t.Fatalf("Unwritable node-id file should result in empty node-id")
}
}

func TestEncloseWithWithoutAction(t *testing.T) {
content := "/some regex/"
result := EncloseWith(content, "/")
Expand Down