Example #1
0
func TestSimpleQuery(t *testing.T) {
	backend := backends.NewReadOnlyBackend(
		metrics.NewMetric("foo.bar", 3.14, 10),
		metrics.NewMetric("foo.bar", 42.0, 20),
		metrics.NewMetric("foo.baz", 10, 10),
		metrics.NewMetric("foo.baz", 20, 20),
	)
	backend.Start()
	defer backend.Stop()

	var stringerTests = []struct {
		query    string
		from, to time.Time
		output   []Response
	}{
		// Should match everything
		{"foo.bar", time.Unix(0, 0), time.Unix(30, 0), []Response{}},

		// Should match nothing
		//{"foo.bar", 0, 10, []Response{}},

		// Just a number
		{"42", time.Unix(0, 0), time.Unix(1, 0), []Response{}},

		// Funcntions
		{"scale(foo.bar, 10.1)", time.Unix(0, 0), time.Unix(30, 0), []Response{}},
		{"scale(foo.bar, foo.baz, 10)", time.Unix(0, 0), time.Unix(30, 0), []Response{}},
	}

	for _, tt := range stringerTests {
		// Build a query
		out, err := ParseGraphiteQuery(tt.query)

		if err != nil {
			t.Errorf("Err'd on input %s: %v", tt.query, err)
			continue
		}

		res, err := out.Query(Request{backend, tt.from, tt.to})
		if err != nil {
			t.Errorf("Query() errored: %v", err)
			continue
		}

		// Expect to get some data back
		for i := range res {
			data := res[i].GetAllMetrics()
			if len(data) == 0 {
				t.Errorf("Expected query '%s' [%d, %d] to return some data.", out, tt.from, tt.to)
			}
		}
	}
}
func TestReadOnlyBackend(t *testing.T) {
	backend := NewReadOnlyBackend(
		metrics.NewMetric("foo", 1, 1),
		metrics.NewMetric("foo", 2, 2),
	)

	// Start backend
	backend.Start()
	defer backend.Stop()

	// Read back list of metrics
	metricNames := GetMetricsAsList(backend)
	if len(metricNames) != 1 {
		t.Errorf("Expected to get one metric back, got %d", len(metricNames))
	} else if metricNames[0] != "foo" {
		t.Errorf("Expected the metric name to be 'foo', got '%v'", metricNames[0])
	}

	// Read back the data
	data := GetDataAsList(backend, "foo", time.Unix(0, 0), time.Unix(5, 0))
	if len(data) != 2 {
		t.Fatalf("Expected to get two results, got %d", len(data))
	}

	if data[0].Value != 1 {
		t.Errorf("Expected data[0].Value=1, got '%f'.", data[0].Value)
	}
	if data[0].Time != time.Unix(1, 0) {
		t.Errorf("Expected data[0].Time=1, got '%d'.", data[0].Time)
	}

	if data[1].Value != 2 {
		t.Errorf("Expected data[0].Value=2, got '%f'.", data[1].Value)
	}
	if data[1].Time != time.Unix(2, 0) {
		t.Errorf("Expected data[0].Time=2, got '%d'.", data[1].Time)
	}

	data = GetDataAsList(backend, "foo", time.Unix(-1, 0), time.Unix(0, 0))
	if len(data) != 0 {
		t.Errorf("Expected no data, got %v", data)
	}
}
Example #3
0
func TestTestProxy(t *testing.T) {
	backend := NewTestProxy(NewReadOnlyBackend(
		metrics.NewMetric("foo", 1, 1),
		metrics.NewMetric("foo", 2, 2),
	))

	// Start backend
	backend.Start()
	defer backend.Stop()

	// Read back the data
	data := GetDataAsList(backend, "foo", time.Unix(0, 0), time.Unix(5, 0))
	if len(data) != 2 {
		t.Fatalf("Expected to get two results, got %d", len(data))
	}

	if data[0].Value != 1 {
		t.Errorf("Expected data[0].Value=1, got '%f'.", data[0].Value)
	}
	if data[0].Time != time.Unix(1, 0) {
		t.Errorf("Expected data[0].Time=1, got '%d'.", data[0].Time)
	}

	if data[1].Value != 2 {
		t.Errorf("Expected data[0].Value=2, got '%f'.", data[1].Value)
	}
	if data[1].Time != time.Unix(2, 0) {
		t.Errorf("Expected data[0].Time=2, got '%d'.", data[1].Time)
	}

	data = GetDataAsList(backend, "foo", time.Unix(-1, 0), time.Unix(0, 0))
	if len(data) != 0 {
		t.Errorf("Expected no data, got %v", data)
	}

	// Get some test.X data
	data = GetDataAsList(backend, "test.sin", time.Unix(1, 0), time.Unix(100, 0))
	if len(data) == 0 {
		t.Errorf("Expected some data from test.sin, got none")
	}
	//t.Errorf("%#v", data)
}
Example #4
0
// What about some basic lines
func TestSingleLineParsing(t *testing.T) {
	t.Parallel()
	var linetests = []struct {
		in  string
		out metrics.Metric
	}{
		{
			"foo 1 2",
			*metrics.NewMetric("foo", 1, 2),
		},
		{
			"a.b.c 4.2 42",
			*metrics.NewMetric("a.b.c", 4.2, 42),
		},
	}

	for i, tt := range linetests {
		_, outMetric := parseGraphiteLine(tt.in)
		if outMetric != tt.out {
			t.Errorf("%d. parseGraphiteLine(%s) => %v, want %v", i, tt.in, outMetric, tt.out)
		}
	}
}
func generateTestStoreAndGet(backend Backend, t *testing.T) {
	// Start backend
	backend.Start()
	defer backend.Stop()

	// Load some data and read it back out
	inChan := make(chan metrics.Metric)
	defer close(inChan)
	backend.AddMetricChan(inChan)
	inChan <- *metrics.NewMetric("foo.bar", 3.14, 100)

	time.Sleep(time.Millisecond)

	// Read back list of metrics
	metricNames := GetMetricsAsList(backend)
	if len(metricNames) != 1 {
		t.Errorf("Expected to get one metric back, got %d", len(metricNames))
	} else if metricNames[0] != "foo.bar" {
		t.Errorf("Expected the metric name to be 'foo.bar', got '%v'", metricNames[0])
	}

	// Read back the data
	data := GetDataAsList(backend, "foo.bar", time.Unix(99, 0), time.Unix(101, 0))
	if len(data) != 1 {
		t.Fatalf("Expected to get one result, got %d", len(data))
	}

	if data[0].Value != 3.14 {
		t.Errorf("Expected data[0].Value=3.14, got '%f'.", data[0].Value)
	}
	if data[0].Time != time.Unix(100, 0) {
		t.Errorf("Expected data[0].Time=100, got '%d'.", data[0].Time)
	}

	// Make a query that shouldn't return any data
	data = GetDataAsList(backend, "foo.bar", time.Unix(0, 0), time.Unix(0, 0))
	if len(data) != 0 {
		t.Errorf("Expected no data for query (foo.bar, 0, 0), got %v", data)
	}
}
Example #6
0
func TestGlobHelper(t *testing.T) {
	backend := NewReadOnlyBackend(
		metrics.NewMetric("foo", 1, 1),
		metrics.NewMetric("foo.1.bar", 2, 2),
		metrics.NewMetric("foo.a2.bar", 2, 2),
	)

	// Start backend
	backend.Start()
	defer backend.Stop()

	var globberTests = []struct {
		glob string
		out  []string
	}{
		// Nothing
		{"foo.*", []string{}},
		{"foo.bar", []string{}},

		// Globless
		{"foo", []string{"foo"}},
		{"foo.1.bar", []string{"foo.1.bar"}},

		// Sub matches
		{"foo.*.bar", []string{"foo.1.bar", "foo.a2.bar"}},
		{"foo.?.bar", []string{"foo.1.bar"}},
		{"foo.a*.bar", []string{"foo.a2.bar"}},
		{"foo.a?.bar", []string{"foo.a2.bar"}},
	}

	for _, tt := range globberTests {
		// Build a query
		out, err := GlobMetricsAsList(tt.glob, backend)

		if err != nil {
			t.Errorf("Err'd on input %s: %v", tt.glob, err)
			continue
		}

		if len(out) != len(tt.out) {
			t.Errorf("Expected glob %v to return %v, got %v.", tt.glob, tt.out, out)
		}

		// Put all requred names in a map and remove all available elements
		m := make(map[string]bool)
		for _, s := range tt.out {
			m[s] = false
		}

		for _, s := range out {
			if _, ok := m[s]; !ok {
				t.Errorf("Expected glob %v to include %v. It didn't.", tt.glob, s)
			}

			if m[s] {
				t.Errorf("Glob %v returned duplicate element %v.", tt.glob, s)
			}

			m[s] = true
		}
	}
}