Skip to content

Commit

Permalink
fix: HttpPost AlertTemplateFile and RowTemplateFile work with either …
Browse files Browse the repository at this point in the history
…unix- or windows-like paths (influxdata#2767)
  • Loading branch information
TiagoMHCSantana authored Aug 4, 2023
1 parent 2fbf7f1 commit e07d6bb
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 3 deletions.
6 changes: 3 additions & 3 deletions services/httppost/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"text/template"

Expand Down Expand Up @@ -70,15 +70,15 @@ func (c Config) Validate() error {
return errors.New("must specify only one of alert-template and alert-template-file")
}

if c.AlertTemplateFile != "" && !path.IsAbs(c.AlertTemplateFile) {
if c.AlertTemplateFile != "" && !filepath.IsAbs(c.AlertTemplateFile) {
return errors.New("must use an absolute path for alert-template-file")
}

if c.RowTemplate != "" && c.RowTemplateFile != "" {
return errors.New("must specify only one of row-template and row-template-file")
}

if c.RowTemplateFile != "" && !path.IsAbs(c.RowTemplateFile) {
if c.RowTemplateFile != "" && !filepath.IsAbs(c.RowTemplateFile) {
return errors.New("must use an absolute path for row-template-file")
}

Expand Down
71 changes: 71 additions & 0 deletions services/httppost/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//go:build !windows

package httppost

import (
"testing"
)

func Test_Validate_ShouldNotReturnError_WhenAlertTemplateFileIsUnixLikePath(t *testing.T) {
testCases := []struct {
config Config
err error
}{
{
config: Config{
Endpoint: "test",
URLTemplate: "http://localhost:8080/alert",
AlertTemplateFile: "/etc/kapacitor/templates/alert_template.json",
},
err: nil,
},
}

for _, tc := range testCases {
validationErr := tc.config.Validate()

if validationErr != nil {
if tc.err == nil {
t.Errorf("unexpected error: got %v", validationErr)
} else if tc.err.Error() != validationErr.Error() {
t.Errorf("unexpected error message: got %q exp %q", validationErr.Error(), tc.err.Error())
}
} else {
if tc.err != nil {
t.Errorf("expected error: %q got nil", tc.err.Error())
}
}
}
}

func Test_Validate_ShouldNotReturnError_WhenRowTemplateFileIsUnixLikePath(t *testing.T) {
testCases := []struct {
config Config
err error
}{
{
config: Config{
Endpoint: "test",
URLTemplate: "http://localhost:8080/alert",
RowTemplateFile: "/etc/kapacitor/templates/row_template.json",
},
err: nil,
},
}

for _, tc := range testCases {
validationErr := tc.config.Validate()

if validationErr != nil {
if tc.err == nil {
t.Errorf("unexpected error: got %v", validationErr)
} else if tc.err.Error() != validationErr.Error() {
t.Errorf("unexpected error message: got %q exp %q", validationErr.Error(), tc.err.Error())
}
} else {
if tc.err != nil {
t.Errorf("expected error: %q got nil", tc.err.Error())
}
}
}
}
71 changes: 71 additions & 0 deletions services/httppost/config_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//go:build windows

package httppost

import (
"testing"
)

func Test_Validate_ShouldNotReturnError_WhenAlertTemplateFileIsWindowsLikePath(t *testing.T) {
testCases := []struct {
config Config
err error
}{
{
config: Config{
Endpoint: "test",
URLTemplate: "http://localhost:8080/alert",
AlertTemplateFile: "C:\\InfluxData\\kapacitor\\templates\\alert_template.json",
},
err: nil,
},
}

for _, tc := range testCases {
validationErr := tc.config.Validate()

if validationErr != nil {
if tc.err == nil {
t.Errorf("unexpected error: got %v", validationErr)
} else if tc.err.Error() != validationErr.Error() {
t.Errorf("unexpected error message: got %q exp %q", validationErr.Error(), tc.err.Error())
}
} else {
if tc.err != nil {
t.Errorf("expected error: %q got nil", tc.err.Error())
}
}
}
}

func Test_Validate_ShouldNotReturnError_WhenRowTemplateFileIsWindowsLikePath(t *testing.T) {
testCases := []struct {
config Config
err error
}{
{
config: Config{
Endpoint: "test",
URLTemplate: "http://localhost:8080/alert",
RowTemplateFile: "C:\\InfluxData\\kapacitor\\templates\\row_template.json",
},
err: nil,
},
}

for _, tc := range testCases {
validationErr := tc.config.Validate()

if validationErr != nil {
if tc.err == nil {
t.Errorf("unexpected error: got %v", validationErr)
} else if tc.err.Error() != validationErr.Error() {
t.Errorf("unexpected error message: got %q exp %q", validationErr.Error(), tc.err.Error())
}
} else {
if tc.err != nil {
t.Errorf("expected error: %q got nil", tc.err.Error())
}
}
}
}

0 comments on commit e07d6bb

Please sign in to comment.