Skip to content
Open
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
3 changes: 2 additions & 1 deletion reload/server/bizlogic/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func NewServer(promURL *url.URL, watchDir string, needStoreFileToStorePath bool,
func (s *server) ListConfigs(c *gin.Context) {
list, err := s.getConfigs()
if err != nil {
c.JSON(http.StatusBadRequest, utils.NewErrorResponse(err.Error()))
slog.Error("cannot list configs", "error", err)
c.JSON(http.StatusBadRequest, utils.NewErrorResponse("cannot list configs"))
} else {
c.JSON(http.StatusOK, list)
}
Expand Down
28 changes: 28 additions & 0 deletions reload/server/bizlogic/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,34 @@ func TestConfigFileBoundary(t *testing.T) {
}
}

func TestListConfigsResponse(t *testing.T) {
dir := t.TempDir()
rule := filepath.Join(dir, "rules.yml")
if err := os.WriteFile(rule, []byte("groups: []"), 0600); err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
name string
dir string
code int
body string
}{
{"success", dir, http.StatusOK, `["rules.yml"]`},
{"missing directory", filepath.Join(dir, "private-missing-directory"), http.StatusBadRequest, `{"errorCode":400,"errorMessage":"cannot list configs"}`},
{"not a directory", rule, http.StatusBadRequest, `{"errorCode":400,"errorMessage":"cannot list configs"}`},
} {
t.Run(tc.name, func(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodGet, "/monitoring/configs", nil)
newConfigTestServer(nil, tc.dir, false, "").ListConfigs(c)
if w.Code != tc.code || w.Body.String() != tc.body {
t.Fatalf("unexpected response: %d %s", w.Code, w.Body)
}
})
}
}

func TestListConfigsFileBoundary(t *testing.T) {
dir := t.TempDir()
outside := filepath.Join(t.TempDir(), "outside.yml")
Expand Down
Loading