1package graphite_test
2
3import (
4	"testing"
5	"time"
6
7	"github.com/BurntSushi/toml"
8	"github.com/influxdata/influxdb/services/graphite"
9)
10
11func TestConfig_Parse(t *testing.T) {
12	// Parse configuration.
13	var c graphite.Config
14	if _, err := toml.Decode(`
15bind-address = ":8080"
16database = "mydb"
17retention-policy = "myrp"
18enabled = true
19protocol = "tcp"
20batch-size=100
21batch-pending=77
22batch-timeout="1s"
23consistency-level="one"
24templates=["servers.* .host.measurement*"]
25tags=["region=us-east"]
26`, &c); err != nil {
27		t.Fatal(err)
28	}
29
30	// Validate configuration.
31	if c.BindAddress != ":8080" {
32		t.Fatalf("unexpected bind address: %s", c.BindAddress)
33	} else if c.Database != "mydb" {
34		t.Fatalf("unexpected database selected: %s", c.Database)
35	} else if c.RetentionPolicy != "myrp" {
36		t.Fatalf("unexpected retention policy selected: %s", c.RetentionPolicy)
37	} else if !c.Enabled {
38		t.Fatalf("unexpected graphite enabled: %v", c.Enabled)
39	} else if c.Protocol != "tcp" {
40		t.Fatalf("unexpected graphite protocol: %s", c.Protocol)
41	} else if c.BatchSize != 100 {
42		t.Fatalf("unexpected graphite batch size: %d", c.BatchSize)
43	} else if c.BatchPending != 77 {
44		t.Fatalf("unexpected graphite batch pending: %d", c.BatchPending)
45	} else if time.Duration(c.BatchTimeout) != time.Second {
46		t.Fatalf("unexpected graphite batch timeout: %v", c.BatchTimeout)
47	} else if c.ConsistencyLevel != "one" {
48		t.Fatalf("unexpected graphite consistency setting: %s", c.ConsistencyLevel)
49	}
50
51	if len(c.Templates) != 1 && c.Templates[0] != "servers.* .host.measurement*" {
52		t.Fatalf("unexpected graphite templates setting: %v", c.Templates)
53	}
54	if len(c.Tags) != 1 && c.Tags[0] != "regsion=us-east" {
55		t.Fatalf("unexpected graphite templates setting: %v", c.Tags)
56	}
57}
58
59func TestConfigValidateEmptyTemplate(t *testing.T) {
60	c := &graphite.Config{}
61	c.Templates = []string{""}
62	if err := c.Validate(); err == nil {
63		t.Errorf("config validate expected error. got nil")
64	}
65
66	c.Templates = []string{"     "}
67	if err := c.Validate(); err == nil {
68		t.Errorf("config validate expected error. got nil")
69	}
70}
71
72func TestConfigValidateTooManyField(t *testing.T) {
73	c := &graphite.Config{}
74	c.Templates = []string{"a measurement b c"}
75	if err := c.Validate(); err == nil {
76		t.Errorf("config validate expected error. got nil")
77	}
78}
79
80func TestConfigValidateTemplatePatterns(t *testing.T) {
81	c := &graphite.Config{}
82	c.Templates = []string{"*measurement"}
83	if err := c.Validate(); err == nil {
84		t.Errorf("config validate expected error. got nil")
85	}
86
87	c.Templates = []string{".host.region"}
88	if err := c.Validate(); err == nil {
89		t.Errorf("config validate expected error. got nil")
90	}
91}
92
93func TestConfigValidateFilter(t *testing.T) {
94	c := &graphite.Config{}
95	c.Templates = []string{".server measurement*"}
96	if err := c.Validate(); err == nil {
97		t.Errorf("config validate expected error. got nil")
98	}
99
100	c.Templates = []string{".    .server measurement*"}
101	if err := c.Validate(); err == nil {
102		t.Errorf("config validate expected error. got nil")
103	}
104
105	c.Templates = []string{"server* measurement*"}
106	if err := c.Validate(); err == nil {
107		t.Errorf("config validate expected error. got nil")
108	}
109}
110
111func TestConfigValidateTemplateTags(t *testing.T) {
112	c := &graphite.Config{}
113	c.Templates = []string{"*.server measurement* foo"}
114	if err := c.Validate(); err == nil {
115		t.Errorf("config validate expected error. got nil")
116	}
117
118	c.Templates = []string{"*.server measurement* foo=bar="}
119	if err := c.Validate(); err == nil {
120		t.Errorf("config validate expected error. got nil")
121	}
122
123	c.Templates = []string{"*.server measurement* foo=bar,"}
124	if err := c.Validate(); err == nil {
125		t.Errorf("config validate expected error. got nil")
126	}
127
128	c.Templates = []string{"*.server measurement* ="}
129	if err := c.Validate(); err == nil {
130		t.Errorf("config validate expected error. got nil")
131	}
132}
133
134func TestConfigValidateDefaultTags(t *testing.T) {
135	c := &graphite.Config{}
136	c.Tags = []string{"foo"}
137	if err := c.Validate(); err == nil {
138		t.Errorf("config validate expected error. got nil")
139	}
140
141	c.Tags = []string{"foo=bar="}
142	if err := c.Validate(); err == nil {
143		t.Errorf("config validate expected error. got nil")
144	}
145
146	c.Tags = []string{"foo=bar", ""}
147	if err := c.Validate(); err == nil {
148		t.Errorf("config validate expected error. got nil")
149	}
150
151	c.Tags = []string{"="}
152	if err := c.Validate(); err == nil {
153		t.Errorf("config validate expected error. got nil")
154	}
155}
156
157func TestConfigValidateFilterDuplicates(t *testing.T) {
158	c := &graphite.Config{}
159	c.Templates = []string{"foo measurement*", "foo .host.measurement"}
160	if err := c.Validate(); err == nil {
161		t.Errorf("config validate expected error. got nil")
162	}
163
164	// duplicate default templates
165	c.Templates = []string{"measurement*", ".host.measurement"}
166	if err := c.Validate(); err == nil {
167		t.Errorf("config validate expected error. got nil")
168	}
169
170}
171