Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Enable tests with real grafana server #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
33 changes: 33 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
TEST?=$$(go list ./... |grep -v 'vendor')
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
PKG_NAME=github.com/nytm/go-grafana-api
GRAFANA_VERSION ?= latest
GO_VERSION ?= 1.10
GRAFANA_CONTAINER_NAME ?= grafana-dev
GRAFANA_ADMIN_PWD ?= pwd4test

default: test

test: start-grafana test-in-local

start-grafana:
CONTAINER_COUNT=$$(docker ps -a -f "Name=$(GRAFANA_CONTAINER_NAME)" | wc -l) ;\
if [[ $$CONTAINER_COUNT -lt 1 ]]; then \
docker run --name $(GRAFANA_CONTAINER_NAME) -d -p localhost:3000:3000 -e "GF_SECURITY_ADMIN_PASSWORD=$(GRAFANA_ADMIN_PWD)" "grafana/grafana:$(GRAFANA_VERSION)"; \
else \
docker start $(GRAFANA_CONTAINER_NAME) ;\
fi \

test-in-local:
GRAFANA_AUTH=admin:$(GRAFANA_ADMIN_PWD) \
go test \

test-in-docker:
docker run -it -v `pwd`:/go/src/$(PKG_NAME) \
--link $$GRAFANA_CONTAINER_ID:grafana \
--workdir /go/src/$(PKG_NAME) \
-e "GRAFANA_AUTH=admin:$(GRAFANA_ADMIN_PWD)" \
-e "GRAFANA_URL=http://grafana:3000" "golang:$(GO_VERSION)" go test

fmt:
gofmt -w $(GOFMT_FILES)
6 changes: 6 additions & 0 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package: github.com/nytm/go-grafana-api
import: []
testImport:
- package: github.com/gobs/pretty
25 changes: 25 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gapi

import (
"os"
"testing"
)

var realClient *Client

func TestMain(m *testing.M) {
var err error
grafanaAuth := os.Getenv("GRAFANA_AUTH")
grafanaUrl := os.Getenv("GRAFANA_URL")
if grafanaAuth == "" {
grafanaAuth = "admin:pwd4test"
}
if grafanaUrl == "" {
grafanaUrl = "http://localhost:3000"
}
realClient, err = New(grafanaAuth, grafanaUrl)
if err != nil {
panic(err)
}
m.Run()
}
20 changes: 4 additions & 16 deletions user_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package gapi

import (
"github.com/gobs/pretty"
"testing"

"github.com/gobs/pretty"
)

const (
Expand All @@ -11,25 +12,12 @@ const (
)

func TestUsers(t *testing.T) {
server, client := gapiTestTools(200, getUsersJSON)
defer server.Close()

resp, err := client.Users()
resp, err := realClient.Users()
if err != nil {
t.Error(err)
}

t.Log(pretty.PrettyFormat(resp))

user := User{
Id: 1,
Email: "admin@localhost",
Name: "",
Login: "admin",
IsAdmin: true,
}

if len(resp) != 1 || resp[0] != user {
if len(resp) == 0 {
t.Error("Not correctly parsing returned users.")
}
}
Expand Down
22 changes: 22 additions & 0 deletions vendor/github.com/gobs/pretty/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions vendor/github.com/gobs/pretty/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/gobs/pretty/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

186 changes: 186 additions & 0 deletions vendor/github.com/gobs/pretty/pretty.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading