Navigation Menu

Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-helmich committed Jan 5, 2018
1 parent 3aa966b commit 194e3b5
Show file tree
Hide file tree
Showing 77 changed files with 18,975 additions and 4 deletions.
20 changes: 19 additions & 1 deletion Gopkg.lock

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

4 changes: 4 additions & 0 deletions Gopkg.toml
Expand Up @@ -36,3 +36,7 @@

[[constraint]]
name = "github.com/satyrius/gonx"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.2.0"
4 changes: 4 additions & 0 deletions config/loader_flags_test.go
Expand Up @@ -17,6 +17,8 @@ func configFromFlags(t *testing.T, flags StartupFlags) Config {
}

func TestConfigContainsPortFromFlags(t *testing.T) {
t.Parallel()

cfg := configFromFlags(t, StartupFlags{
ListenPort: 1234,
})
Expand All @@ -27,6 +29,8 @@ func TestConfigContainsPortFromFlags(t *testing.T) {
}

func TestConfigContainsFilenamesFromFlags(t *testing.T) {
t.Parallel()

sf := []string{"/foo.log", "/bar.log"}
cfg := configFromFlags(t, StartupFlags{
ListenPort: 1234,
Expand Down
94 changes: 94 additions & 0 deletions config/loader_test.go
@@ -0,0 +1,94 @@
package config

import (
"testing"
"bytes"
"github.com/stretchr/testify/assert"
)

const HCL_INPUT = `
listen {
address = "10.0.0.1"
port = 4040
}
consul {
enable = true
address = "localhost:8500"
datacenter = "dc1"
scheme = "https"
token = "asdfasfdasf"
service {
id = "nginx-exporter"
name = "nginx-exporter"
tags = ["foo", "bar"]
}
}
namespace "nginx" {
source_files = [
"test.log",
"foo.log"
]
format = "$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" \"$http_x_forwarded_for\""
labels {
app = "magicapp"
foo = "bar"
}
relabel "user" {
from = "remote_user"
whitelist = ["-", "user1", "user2"]
}
relabel "request_uri" {
from = "request"
split = 2
match "^/users/[0-9]+" {
replacement = "/users/:id"
}
match "^/profile" {
replacement = "/profile"
}
}
}
`

func TestLoadsHCLConfigFile(t *testing.T) {
t.Parallel()

buf := bytes.NewBufferString(HCL_INPUT)
cfg := Config{}

err := LoadConfigFromStream(&cfg, buf, TYPE_HCL)
assert.Nil(t, err, "unexpected error: %v", err)

assert.Equal(t, "10.0.0.1", cfg.Listen.Address)
assert.Equal(t, 4040, cfg.Listen.Port)

assert.True(t, cfg.Consul.Enable)
assert.Equal(t, "localhost:8500", cfg.Consul.Address)
assert.Equal(t, "nginx-exporter", cfg.Consul.Service.ID)
assert.Equal(t, "nginx-exporter", cfg.Consul.Service.Name)
assert.Equal(t, "dc1", cfg.Consul.Datacenter)
assert.Equal(t, "https", cfg.Consul.Scheme)
assert.Equal(t, "asdfasfdasf", cfg.Consul.Token)

assert.Len(t, cfg.Namespaces, 1)

n := cfg.Namespaces[0]
assert.Equal(t, "nginx", n.Name)
assert.Equal(t, "$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" \"$http_x_forwarded_for\"", n.Format)
assert.Equal(t, []string{"test.log", "foo.log"}, n.SourceFiles)
assert.Equal(t, "magicapp", n.Labels["app"])

assert.Len(t, n.RelabelConfigs, 2)
assert.Equal(t, "user", n.RelabelConfigs[0].TargetLabel)
assert.Equal(t, "request_uri", n.RelabelConfigs[1].TargetLabel)

assert.Len(t, n.RelabelConfigs[1].Matches, 2)
assert.Equal(t, "^/users/[0-9]+", n.RelabelConfigs[1].Matches[0].RegexpString)
}
6 changes: 3 additions & 3 deletions example-config.hcl
Expand Up @@ -5,12 +5,12 @@ listen {
consul {
enable = true
address = "localhost:8500"
datacenter = "dc1"
scheme = "http"
token = ""
service {
id = "nginx-exporter"
name = "nginx-exporter"
datacenter = "dc1"
scheme = "http"
token = ""
tags = ["foo", "bar"]
}
}
Expand Down
22 changes: 22 additions & 0 deletions vendor/github.com/davecgh/go-spew/.gitignore

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

14 changes: 14 additions & 0 deletions vendor/github.com/davecgh/go-spew/.travis.yml

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

15 changes: 15 additions & 0 deletions vendor/github.com/davecgh/go-spew/LICENSE

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

0 comments on commit 194e3b5

Please sign in to comment.