Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
fix #43: implement success test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Oct 27, 2020
1 parent 6ce028d commit 7b5a2aa
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 5 deletions.
35 changes: 33 additions & 2 deletions internal/cmd/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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))
})

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 24 additions & 1 deletion internal/cmd/metrics_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd_test

import (
"strings"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

Expand Down Expand Up @@ -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))
})
})
})
4 changes: 3 additions & 1 deletion internal/cmd/queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ var _ = Describe("fetch queries", func() {
})
})

When("correct usage", func() {})
When("correct usage", func() {
// TODO:implement
})
})
17 changes: 17 additions & 0 deletions internal/cmd/testdata/metrics/a.b.*.json
Original file line number Diff line number Diff line change
@@ -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
}
]
12 changes: 12 additions & 0 deletions internal/cmd/testdata/metrics/a.f.*.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"id": "a.f.g",
"text": "g",
"leaf": 1
},
{
"id": "a.f.h",
"text": "h",
"leaf": 1
}
]
12 changes: 12 additions & 0 deletions internal/cmd/testdata/metrics/a.i.*.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"id": "a.i.j",
"text": "b",
"leaf": 1
},
{
"id": "a.i.k",
"text": "f",
"leaf": 1
}
]
17 changes: 17 additions & 0 deletions internal/cmd/testdata/metrics/a.json
Original file line number Diff line number Diff line change
@@ -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
}
]

0 comments on commit 7b5a2aa

Please sign in to comment.