func TestConfigValidateTooManyField(t *testing.T) { c := graphite.NewConfig() c.Templates = []string{"a measurement b c"} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } }
func TestConfigValidateTemplatePatterns(t *testing.T) { c := graphite.NewConfig() c.Templates = []string{"*measurement"} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } c.Templates = []string{".host.region"} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } }
func TestConfigValidateEmptyTemplate(t *testing.T) { c := graphite.NewConfig() c.Templates = []string{""} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } c.Templates = []string{" "} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } }
func TestConfigValidateFilterDuplicates(t *testing.T) { c := graphite.NewConfig() c.Templates = []string{"foo measurement*", "foo .host.measurement"} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } // duplicate default templates c.Templates = []string{"measurement*", ".host.measurement"} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } }
func TestConfigValidateFilter(t *testing.T) { c := graphite.NewConfig() c.Templates = []string{".server measurement*"} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } c.Templates = []string{". .server measurement*"} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } c.Templates = []string{"server* measurement*"} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } }
// NewConfig returns an instance of Config with reasonable defaults. func NewConfig() *Config { c := &Config{} c.Meta = meta.NewConfig() c.Data = tsdb.NewConfig() c.Cluster = cluster.NewConfig() c.Precreator = precreator.NewConfig() c.Admin = admin.NewConfig() c.HTTPD = httpd.NewConfig() c.Collectd = collectd.NewConfig() c.OpenTSDB = opentsdb.NewConfig() c.Graphites = append(c.Graphites, graphite.NewConfig()) c.Monitoring = monitor.NewConfig() c.ContinuousQuery = continuous_querier.NewConfig() c.Retention = retention.NewConfig() c.HintedHandoff = hh.NewConfig() return c }
func TestConfigValidateDefaultTags(t *testing.T) { c := graphite.NewConfig() c.Tags = []string{"foo"} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } c.Tags = []string{"foo=bar="} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } c.Tags = []string{"foo=bar", ""} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } c.Tags = []string{"="} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } }
func TestConfigValidateTemplateTags(t *testing.T) { c := graphite.NewConfig() c.Templates = []string{"*.server measurement* foo"} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } c.Templates = []string{"*.server measurement* foo=bar="} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } c.Templates = []string{"*.server measurement* foo=bar,"} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } c.Templates = []string{"*.server measurement* ="} if err := c.Validate(); err == nil { t.Errorf("config validate expected error. got nil") } }
func Test_ServerGraphiteUDP(t *testing.T) { t.Parallel() now := time.Now().UTC().Round(time.Second) config := graphite.NewConfig() config.Database = "graphitedb" config.BatchSize = 0 // No batching. config.BatchTimeout = toml.Duration(time.Second) config.BindAddress = ":10000" config.Protocol = "udp" service, err := graphite.NewService(config) if err != nil { t.Fatalf("failed to create Graphite service: %s", err.Error()) } // Allow test to wait until points are written. var wg sync.WaitGroup wg.Add(1) pointsWriter := PointsWriter{ WritePointsFn: func(req *cluster.WritePointsRequest) error { defer wg.Done() if req.Database != "graphitedb" { t.Fatalf("unexpected database: %s", req.Database) } else if req.RetentionPolicy != "" { t.Fatalf("unexpected retention policy: %s", req.RetentionPolicy) } else if !reflect.DeepEqual(req.Points, []tsdb.Point{ tsdb.NewPoint( "cpu", map[string]string{}, map[string]interface{}{"value": 23.456}, time.Unix(now.Unix(), 0), ), }) { spew.Dump(req.Points) t.Fatalf("unexpected points: %#v", req.Points) } return nil }, } service.PointsWriter = &pointsWriter dbCreator := DatabaseCreator{} service.MetaStore = &dbCreator if err := service.Open(); err != nil { t.Fatalf("failed to open Graphite service: %s", err.Error()) } if !dbCreator.Created { t.Fatalf("failed to create target database") } // Connect to the graphite endpoint we just spun up _, port, _ := net.SplitHostPort(service.Addr().String()) conn, err := net.Dial("udp", "127.0.0.1:"+port) if err != nil { t.Fatal(err) } data := []byte(`cpu 23.456 `) data = append(data, []byte(fmt.Sprintf("%d", now.Unix()))...) data = append(data, '\n') _, err = conn.Write(data) if err != nil { t.Fatal(err) } wg.Wait() conn.Close() }