diff --git a/internal/cmd/integration_test.go b/internal/cmd/integration_test.go index 038bcdd..2780d32 100644 --- a/internal/cmd/integration_test.go +++ b/internal/cmd/integration_test.go @@ -2,13 +2,18 @@ package cmd_test import ( "bytes" + "errors" + "io" "net/http" "net/http/httptest" + "os" + "path/filepath" "testing" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/spf13/cobra" + "go.octolab.org/unsafe" ) var ( @@ -19,8 +24,34 @@ var ( var ( _ = BeforeSuite(func() { - grafana = httptest.NewServer(http.NewServeMux()) - graphite = httptest.NewServer(http.NewServeMux()) + grafanaAPI := http.NewServeMux() + grafana = httptest.NewUnstartedServer(grafanaAPI) + grafana.Start() + + graphiteAPI := http.NewServeMux() + graphiteAPI.HandleFunc("/metrics/find", func(rw http.ResponseWriter, req *http.Request) { + if err := req.ParseForm(); err != nil { + http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) + return + } + + name := filepath.Join("testdata", "metrics", req.FormValue("query")) + ".json" + f, err := os.Open(name) + if errors.Is(err, os.ErrNotExist) { + http.Error(rw, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + if err != nil { + http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + + unsafe.DoSilent(io.Copy(rw, f)) + unsafe.Ignore(f.Close()) + }) + graphite = httptest.NewUnstartedServer(graphiteAPI) + graphite.Start() + buffer = bytes.NewBuffer(make([]byte, 0, 1024)) }) diff --git a/internal/cmd/lookup.go b/internal/cmd/lookup.go index 9c56fa1..0d2fcab 100644 --- a/internal/cmd/lookup.go +++ b/internal/cmd/lookup.go @@ -13,7 +13,7 @@ import ( // NewCacheLookupCommand returns command to lookup cache. func NewCacheLookupCommand( config *cnf.Config, - logger *logrus.Logger, + _ *logrus.Logger, ) *cobra.Command { command := cobra.Command{ Use: "cache-lookup", diff --git a/internal/cmd/metrics_test.go b/internal/cmd/metrics_test.go index f5c5e0a..f229dd2 100644 --- a/internal/cmd/metrics_test.go +++ b/internal/cmd/metrics_test.go @@ -1,6 +1,8 @@ package cmd_test import ( + "strings" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -39,5 +41,26 @@ var _ = Describe("fetch queries", func() { }) }) - When("correct usage", func() {}) + metrics := strings.TrimSpace(` +a.b.c +a.b.d +a.b.e +a.f.g +a.f.h +a.i.j +a.i.k`) + + When("correct usage", func() { + It("returns metrics if all work well", func() { + root.SetArgs([]string{ + "metrics", + "--graphite", graphite.URL, + "-m", "a", + "-f", "tsv", + "--no-cache", + }) + Expect(root.Execute()).ToNot(HaveOccurred()) + Expect(buffer.String()).To(ContainSubstring(metrics)) + }) + }) }) diff --git a/internal/cmd/queries_test.go b/internal/cmd/queries_test.go index 83da75c..7b1650e 100644 --- a/internal/cmd/queries_test.go +++ b/internal/cmd/queries_test.go @@ -40,5 +40,7 @@ var _ = Describe("fetch queries", func() { }) }) - When("correct usage", func() {}) + When("correct usage", func() { + // TODO:implement + }) }) diff --git a/internal/cmd/testdata/metrics/a.b.*.json b/internal/cmd/testdata/metrics/a.b.*.json new file mode 100644 index 0000000..6168c73 --- /dev/null +++ b/internal/cmd/testdata/metrics/a.b.*.json @@ -0,0 +1,17 @@ +[ + { + "id": "a.b.c", + "text": "c", + "leaf": 1 + }, + { + "id": "a.b.d", + "text": "d", + "leaf": 1 + }, + { + "id": "a.b.e", + "text": "e", + "leaf": 1 + } +] diff --git a/internal/cmd/testdata/metrics/a.f.*.json b/internal/cmd/testdata/metrics/a.f.*.json new file mode 100644 index 0000000..41bb206 --- /dev/null +++ b/internal/cmd/testdata/metrics/a.f.*.json @@ -0,0 +1,12 @@ +[ + { + "id": "a.f.g", + "text": "g", + "leaf": 1 + }, + { + "id": "a.f.h", + "text": "h", + "leaf": 1 + } +] diff --git a/internal/cmd/testdata/metrics/a.i.*.json b/internal/cmd/testdata/metrics/a.i.*.json new file mode 100644 index 0000000..395c4b2 --- /dev/null +++ b/internal/cmd/testdata/metrics/a.i.*.json @@ -0,0 +1,12 @@ +[ + { + "id": "a.i.j", + "text": "b", + "leaf": 1 + }, + { + "id": "a.i.k", + "text": "f", + "leaf": 1 + } +] diff --git a/internal/cmd/testdata/metrics/a.json b/internal/cmd/testdata/metrics/a.json new file mode 100644 index 0000000..69262e5 --- /dev/null +++ b/internal/cmd/testdata/metrics/a.json @@ -0,0 +1,17 @@ +[ + { + "id": "a.b", + "text": "b", + "leaf": 0 + }, + { + "id": "a.f", + "text": "f", + "leaf": 0 + }, + { + "id": "a.i", + "text": "i", + "leaf": 0 + } +]