Exemplo n.º 1
0
func TestHandler_serveWriteSeriesFieldTypeConflict(t *testing.T) {
	srvr := OpenAuthlessServer(NewMessagingClient())
	srvr.CreateDatabase("foo")
	srvr.CreateRetentionPolicy("foo", influxdb.NewRetentionPolicy("bar"))
	srvr.SetDefaultRetentionPolicy("foo", "bar")

	s := NewHTTPServer(srvr)
	defer s.Close()

	status, _ := MustHTTP("POST", s.URL+`/write`, nil, nil, `{"database" : "foo", "retentionPolicy" : "bar", "points": [{"name": "cpu", "tags": {"host": "server01"},"fields": {"value": 100}}]}`)
	if status != http.StatusOK {
		t.Fatalf("unexpected status: %d", status)
	}

	status, body := MustHTTP("POST", s.URL+`/write`, nil, nil, `{"database" : "foo", "retentionPolicy" : "bar", "points": [{"name": "cpu", "tags": {"host": "server01"},"fields": {"value": "foo"}}]}`)
	if status != http.StatusInternalServerError {
		t.Errorf("unexpected status: %d", status)
	}

	r := &influxdb.Results{}
	if err := json.Unmarshal([]byte(body), r); err != nil {
		t.Log(body)
		t.Error(err)
	}
	if len(r.Results) != 0 {
		t.Fatalf("unexpected results count")
	}
	if r.Err.Error() != "field \"value\" is type string, mapped as type number" {
		t.Fatalf("unexpected error returned, actual: %s", r.Err.Error())
	}
}
Exemplo n.º 2
0
func TestHandler_UpdateRetentionPolicy(t *testing.T) {
	srvr := OpenAuthlessServer(NewMessagingClient())
	srvr.CreateDatabase("foo")
	srvr.CreateRetentionPolicy("foo", influxdb.NewRetentionPolicy("bar"))
	s := NewHTTPServer(srvr)
	defer s.Close()

	query := map[string]string{"q": "ALTER RETENTION POLICY bar ON foo REPLICATION 42 DURATION 2h DEFAULT"}
	status, body := MustHTTP("GET", s.URL+`/query`, query, nil, "")

	// Verify updated policy.
	p, _ := srvr.RetentionPolicy("foo", "bar")
	if status != http.StatusOK {
		t.Fatalf("unexpected status: %d", status)
	} else if body != `{"results":[{}]}` {
		t.Fatalf("unexpected body: %s", body)
	} else if p.ReplicaN != 42 {
		t.Fatalf("unexpected replication factor: %d", p.ReplicaN)
	}

	// Make sure retention policy has been set as default.
	if p, err := srvr.DefaultRetentionPolicy("foo"); err != nil {
		t.Fatal(err)
	} else if p == nil {
		t.Fatal("default retention policy not set")
	} else if p.Name != "bar" {
		t.Fatal(`expected default retention policy to be "bar"`)
	}
}
Exemplo n.º 3
0
func TestHandler_serveShowMeasurements(t *testing.T) {
	srvr := OpenServer(NewMessagingClient())
	srvr.CreateDatabase("foo")
	srvr.CreateRetentionPolicy("foo", influxdb.NewRetentionPolicy("bar"))
	s := NewHTTPServer(srvr)
	defer s.Close()

	status, body := MustHTTP("POST", s.URL+`/write`, nil, nil, `{"database" : "foo", "retentionPolicy" : "bar", "points": [
		{"name": "cpu", "tags": {"host": "server01"},"timestamp": "2009-11-10T23:00:00Z","values": {"value": 100}},
		{"name": "cpu", "tags": {"host": "server01", "region": "uswest"},"timestamp": "2009-11-10T23:00:00Z","values": {"value": 100}},
		{"name": "cpu", "tags": {"host": "server01", "region": "useast"},"timestamp": "2009-11-10T23:00:00Z","values": {"value": 100}},
		{"name": "cpu", "tags": {"host": "server02", "region": "useast"},"timestamp": "2009-11-10T23:00:00Z","values": {"value": 100}},
		{"name": "gpu", "tags": {"host": "server02", "region": "useast"},"timestamp": "2009-11-10T23:00:00Z","values": {"value": 100}}
		]}`)

	if status != http.StatusOK {
		t.Log(body)
		t.Fatalf("unexpected status after write: %d", status)
	}

	var tests = []struct {
		q   string
		r   string
		err string
	}{
		// SHOW SERIES
		{
			q: `SHOW MEASUREMENTS LIMIT 2`,
			r: `{"results":[{"rows":[{"name":"cpu","columns":["host","region"]},{"name":"gpu","columns":["host","region"]}]}]}`,
		},
	}

	for i, tt := range tests {
		query := map[string]string{"db": "foo", "q": tt.q}
		status, body = MustHTTP("GET", s.URL+`/query`, query, nil, "")

		if status != http.StatusOK {
			t.Logf("query #%d: %s", i, tt.q)
			t.Log(body)
			t.Errorf("unexpected status: %d", status)
		}

		r := &influxdb.Results{}
		if err := json.Unmarshal([]byte(body), r); err != nil {
			t.Logf("query #%d: %s", i, tt.q)
			t.Log(body)
			t.Error("error marshaling result: ", err)
		}

		if body != tt.r {
			t.Errorf("result mismatch\n  exp: %s\n  got: %s\n", tt.r, body)
		}
	}
}
Exemplo n.º 4
0
func TestHandler_serveWriteSeriesZeroTime(t *testing.T) {
	srvr := OpenAuthlessServer(NewMessagingClient())
	srvr.CreateDatabase("foo")
	srvr.CreateRetentionPolicy("foo", influxdb.NewRetentionPolicy("bar"))
	srvr.SetDefaultRetentionPolicy("foo", "bar")

	s := NewHTTPServer(srvr)
	defer s.Close()

	now := time.Now()

	status, _ := MustHTTP("POST", s.URL+`/write`, nil, nil, `{"database" : "foo", "retentionPolicy" : "bar", "points": [{"name": "cpu", "tags": {"host": "server01"},"fields": {"value": 100}}]}`)

	if status != http.StatusOK {
		t.Fatalf("unexpected status: %d", status)
	}
	time.Sleep(100 * time.Millisecond) // Ensure data node picks up write.

	srvr.Restart() // Ensure data is queryable across restarts.

	query := map[string]string{"db": "foo", "q": "select value from cpu"}
	status, body := MustHTTP("GET", s.URL+`/query`, query, nil, "")

	if status != http.StatusOK {
		t.Logf("query %s\n", query)
		t.Log(body)
		t.Errorf("unexpected status: %d", status)
	}

	r := &influxdb.Results{}
	if err := json.Unmarshal([]byte(body), r); err != nil {
		t.Logf("query : %s\n", query)
		t.Log(body)
		t.Error(err)
	}

	if len(r.Results) != 1 {
		t.Fatalf("unexpected results count")
	}
	result := r.Results[0]
	if len(result.Series) != 1 {
		t.Fatalf("unexpected row count, expected: %d, actual: %d", 1, len(result.Series))
	}
	row := result.Series[0]
	timestamp, _ := time.Parse(time.RFC3339Nano, row.Values[0][0].(string))
	if timestamp.IsZero() {
		t.Fatalf("failed to write a default time, actual: %v", row.Values[0][0])
	}
	if !timestamp.After(now) {
		t.Fatalf("time was not valid. expected something after %v, actual: %v", now, timestamp)
	}
}
Exemplo n.º 5
0
func TestHandler_serveWriteSeries(t *testing.T) {
	srvr := OpenAuthenticatedServer(NewMessagingClient())
	srvr.CreateDatabase("foo")
	srvr.CreateRetentionPolicy("foo", influxdb.NewRetentionPolicy("bar"))
	s := NewHTTPServer(srvr)
	defer s.Close()

	status, _ := MustHTTP("POST", s.URL+`/write`, nil, nil, `{"database" : "foo", "retentionPolicy" : "bar", "points": [{"name": "cpu", "tags": {"host": "server01"},"timestamp": "2009-11-10T23:00:00Z","fields": {"value": 100}}]}`)

	if status != http.StatusOK {
		t.Fatalf("unexpected status: %d", status)
	}
}
Exemplo n.º 6
0
func TestHandler_UpdateRetentionPolicy_NotFound(t *testing.T) {
	srvr := OpenAuthlessServer(NewMessagingClient())
	srvr.CreateDatabase("foo")
	srvr.CreateRetentionPolicy("foo", influxdb.NewRetentionPolicy("bar"))
	s := NewHTTPServer(srvr)
	defer s.Close()

	query := map[string]string{"q": "ALTER RETENTION POLICY qux ON foo DURATION 1h REPLICATION 1"}
	status, _ := MustHTTP("GET", s.URL+`/query`, query, nil, "")

	// Verify response.
	if status != http.StatusInternalServerError {
		t.Fatalf("unexpected status: %d", status)
	}
}
Exemplo n.º 7
0
func TestHandler_RetentionPolicies(t *testing.T) {
	srvr := OpenAuthlessServer(NewMessagingClient())
	srvr.CreateDatabase("foo")
	srvr.CreateRetentionPolicy("foo", influxdb.NewRetentionPolicy("bar"))
	s := NewHTTPServer(srvr)
	defer s.Close()

	status, body := MustHTTP("GET", s.URL+`/query`, map[string]string{"q": "SHOW RETENTION POLICIES foo"}, nil, "")

	if status != http.StatusOK {
		t.Fatalf("unexpected status: %d", status)
	} else if body != `{"results":[{"series":[{"columns":["name","duration","replicaN","default"],"values":[["bar","168h0m0s",1,false]]}]}]}` {
		t.Fatalf("unexpected body: %s", body)
	}
}
Exemplo n.º 8
0
func TestHandler_DeleteRetentionPolicy(t *testing.T) {
	srvr := OpenAuthlessServer(NewMessagingClient())
	srvr.CreateDatabase("foo")
	srvr.CreateRetentionPolicy("foo", influxdb.NewRetentionPolicy("bar"))
	s := NewHTTPServer(srvr)
	defer s.Close()

	query := map[string]string{"q": "DROP RETENTION POLICY bar ON foo"}
	status, body := MustHTTP("GET", s.URL+`/query`, query, nil, "")

	if status != http.StatusOK {
		t.Fatalf("unexpected status: %d", status)
	} else if body != `{"results":[{}]}` {
		t.Fatalf("unexpected body: %s", body)
	}
}
Exemplo n.º 9
0
func TestHandler_WaitIncrement(t *testing.T) {
	srvr := OpenAuthlessServer(NewMessagingClient())
	srvr.CreateDatabase("foo")
	srvr.CreateRetentionPolicy("foo", influxdb.NewRetentionPolicy("bar"))

	s := NewHTTPServer(srvr)
	defer s.Close()

	status, _ := MustHTTP("GET", s.URL+`/wait/2`, map[string]string{"timeout": "200"}, nil, "")

	// Write some data
	_, _ = MustHTTP("POST", s.URL+`/write`, nil, nil, `{"database" : "foo", "retentionPolicy" : "bar", "points": [{"name": "cpu", "tags": {"host": "server01"},"timestamp": "2009-11-10T23:00:00Z","fields": {"value": 100}}]}`)

	if status != http.StatusOK {
		t.Fatalf("unexpected status, expected:  %d, actual: %d", http.StatusOK, status)
	}
}
Exemplo n.º 10
0
func TestHandler_serveWriteSeriesWithNoFields(t *testing.T) {
	srvr := OpenAuthenticatedServer(NewMessagingClient())
	srvr.CreateDatabase("foo")
	srvr.CreateRetentionPolicy("foo", influxdb.NewRetentionPolicy("bar"))
	s := NewHTTPServer(srvr)
	defer s.Close()

	status, body := MustHTTP("POST", s.URL+`/write`, nil, nil, `{"database" : "foo", "retentionPolicy" : "bar", "points": [{"name": "cpu", "tags": {"host": "server01"},"timestamp": "2009-11-10T23:00:00Z"}]}`)

	expected := fmt.Sprintf(`{"error":"%s"}`, influxdb.ErrFieldsRequired.Error())

	if status != http.StatusInternalServerError {
		t.Fatalf("unexpected status: %d", status)
	} else if body != expected {
		t.Fatalf("result mismatch:\n\texp=%s\n\tgot=%s\n", expected, body)
	}
}
Exemplo n.º 11
0
func TestHandler_serveWriteSeriesWithAuthNilUser(t *testing.T) {
	srvr := OpenAuthenticatedServer(NewMessagingClient())
	srvr.CreateDatabase("foo")
	srvr.CreateRetentionPolicy("foo", influxdb.NewRetentionPolicy("bar"))
	s := NewAuthenticatedHTTPServer(srvr)
	defer s.Close()

	status, body := MustHTTP("POST", s.URL+`/write`, nil, nil, `{"database" : "foo", "retentionPolicy" : "bar", "points": [{"name": "cpu", "tags": {"host": "server01"},"timestamp": "2009-11-10T23:00:00Z","fields": {"value": 100}}]}`)

	if status != http.StatusUnauthorized {
		t.Fatalf("unexpected status: %d", status)
	}

	response := `{"error":"user is required to write to database \"foo\""}`
	if body != response {
		t.Fatalf("unexpected body: expected %s, actual %s", response, body)
	}
}
Exemplo n.º 12
0
func TestHandler_UpdateRetentionPolicy(t *testing.T) {
	t.Skip()
	srvr := OpenServer(NewMessagingClient())
	srvr.CreateDatabase("foo")
	srvr.CreateRetentionPolicy("foo", influxdb.NewRetentionPolicy("bar"))
	s := NewHTTPServer(srvr)
	defer s.Close()

	query := map[string]string{"q": "ALTER RETENTION POLICY bar ON foo REPLICATION 42 DURATION 1m"}
	status, body := MustHTTP("GET", s.URL+`/query`, query, nil, "")

	// Verify updated policy.
	p, _ := srvr.RetentionPolicy("foo", "bar")
	if status != http.StatusOK {
		t.Fatalf("unexpected status: %d", status)
	} else if body != "" {
		t.Fatalf("unexpected body: %s", body)
	} else if p.ReplicaN != 42 {
		t.Fatalf("unexpected replication factor: %d", p.ReplicaN)
	}
}
Exemplo n.º 13
0
func TestHandler_serveWriteSeriesBatch(t *testing.T) {
	srvr := OpenAuthlessServer(NewMessagingClient())
	srvr.CreateDatabase("foo")
	srvr.CreateRetentionPolicy("foo", influxdb.NewRetentionPolicy("bar"))
	srvr.SetDefaultRetentionPolicy("foo", "bar")

	s := NewHTTPServer(srvr)
	defer s.Close()

	batch := `
{
    "database": "foo",
    "retentionPolicy": "bar",
    "points": [
        {
            "name": "disk",
            "timestamp": "2009-11-10T23:00:00Z",
            "tags": {
                "host": "server01"
            },
            "fields": {
                "full": false
            }
        },
        {
            "name": "disk",
            "timestamp": "2009-11-10T23:00:01Z",
            "tags": {
                "host": "server01"
            },
            "fields": {
                "full": true
            }
        },
        {
            "name": "disk",
            "timestamp": "2009-11-10T23:00:02Z",
            "tags": {
                "host": "server02"
            },
            "fields": {
                "full_pct": 64
            }
        }
    ]
}
`
	status, body := MustHTTP("POST", s.URL+`/write`, nil, nil, batch)
	if status != http.StatusOK {
		t.Log(body)
		t.Fatalf("unexpected status: %d", status)
	}
	time.Sleep(200 * time.Millisecond) // Ensure data node picks up write.

	query := map[string]string{"db": "foo", "q": "select * from disk"}
	status, body = MustHTTP("GET", s.URL+`/query`, query, nil, "")
	if status != http.StatusOK {
		t.Logf("query %s\n", query)
		t.Log(body)
		t.Errorf("unexpected status: %d", status)
	}

	r := &influxdb.Results{}
	if err := json.Unmarshal([]byte(body), r); err != nil {
		t.Logf("query : %s\n", query)
		t.Log(body)
		t.Error(err)
	}
	if len(r.Results) != 1 {
		t.Fatalf("unexpected results count")
	}
	result := r.Results[0]
	if len(result.Series) != 1 {
		t.Fatalf("unexpected row count, expected: %d, actual: %d", 1, len(result.Series))
	}
	if len(result.Series[0].Columns) != 3 {
		t.Fatalf("unexpected column count, expected: %d, actual: %d", 3, len(result.Series[0].Columns))
	}
	if len(result.Series[0].Values) != 3 {
		t.Fatalf("unexpected values count, expected: %d, actual: %d", 3, len(result.Series[0].Values))
	}
}
Exemplo n.º 14
0
func TestHandler_serveShowSeries(t *testing.T) {
	srvr := OpenServer(NewMessagingClient())
	srvr.CreateDatabase("foo")
	srvr.CreateRetentionPolicy("foo", influxdb.NewRetentionPolicy("bar"))
	srvr.SetDefaultRetentionPolicy("foo", "bar")
	s := NewHTTPServer(srvr)
	defer s.Close()

	status, body := MustHTTP("POST", s.URL+`/write`, nil, nil, `{"database" : "foo", "retentionPolicy" : "bar", "points": [
		{"name": "cpu", "tags": {"host": "server01"},"timestamp": "2009-11-10T23:00:00Z","values": {"value": 100}},
		{"name": "cpu", "tags": {"host": "server01", "region": "uswest"},"timestamp": "2009-11-10T23:00:00Z","values": {"value": 100}},
		{"name": "cpu", "tags": {"host": "server01", "region": "useast"},"timestamp": "2009-11-10T23:00:00Z","values": {"value": 100}},
		{"name": "cpu", "tags": {"host": "server02", "region": "useast"},"timestamp": "2009-11-10T23:00:00Z","values": {"value": 100}},
		{"name": "gpu", "tags": {"host": "server02", "region": "useast"},"timestamp": "2009-11-10T23:00:00Z","values": {"value": 100}}
		]}`)

	if status != http.StatusOK {
		t.Log(body)
		t.Fatalf("unexpected status after write: %d", status)
	}

	var tests = []struct {
		q   string
		r   *influxdb.Results
		err string
	}{
		// SHOW SERIES
		{
			q: `SHOW SERIES`,
			r: &influxdb.Results{
				Results: []*influxdb.Result{
					&influxdb.Result{
						Rows: []*influxql.Row{
							&influxql.Row{
								Name:    "cpu",
								Columns: []string{"host", "region"},
								Values: [][]interface{}{
									str2iface([]string{"server01", ""}),
									str2iface([]string{"server01", "uswest"}),
									str2iface([]string{"server01", "useast"}),
									str2iface([]string{"server02", "useast"}),
								},
							},
							&influxql.Row{
								Name:    "gpu",
								Columns: []string{"host", "region"},
								Values: [][]interface{}{
									str2iface([]string{"server02", "useast"}),
								},
							},
						},
					},
				},
			},
		},
		// SHOW SERIES ... LIMIT
		// {
		// 	q: `SHOW SERIES LIMIT 1`,
		// 	r: &influxdb.Results{
		// 		Results: []*influxdb.Result{
		// 			&influxdb.Result{
		// 				Rows: []*influxql.Row{
		// 					&influxql.Row{
		// 						Name:    "cpu",
		// 						Columns: []string{"host", "region"},
		// 						Values: [][]interface{}{
		// 							str2iface([]string{"server01", ""}),
		// 							str2iface([]string{"server01", "uswest"}),
		// 							str2iface([]string{"server01", "useast"}),
		// 							str2iface([]string{"server02", "useast"}),
		// 						},
		// 					},
		// 				},
		// 			},
		// 		},
		// 	},
		// },
		// SHOW SERIES FROM
		{
			q: `SHOW SERIES FROM cpu`,
			r: &influxdb.Results{
				Results: []*influxdb.Result{
					&influxdb.Result{
						Rows: []*influxql.Row{
							&influxql.Row{
								Name:    "cpu",
								Columns: []string{"host", "region"},
								Values: [][]interface{}{
									str2iface([]string{"server01", ""}),
									str2iface([]string{"server01", "uswest"}),
									str2iface([]string{"server01", "useast"}),
									str2iface([]string{"server02", "useast"}),
								},
							},
						},
					},
				},
			},
		},
		// SHOW SERIES WHERE
		{
			q: `SHOW SERIES WHERE region = 'uswest'`,
			r: &influxdb.Results{
				Results: []*influxdb.Result{
					&influxdb.Result{
						Rows: []*influxql.Row{
							&influxql.Row{
								Name:    "cpu",
								Columns: []string{"host", "region"},
								Values: [][]interface{}{
									str2iface([]string{"server01", "uswest"}),
								},
							},
						},
					},
				},
			},
		},
		// SHOW SERIES FROM ... WHERE
		{
			q: `SHOW SERIES FROM cpu WHERE region = 'useast'`,
			r: &influxdb.Results{
				Results: []*influxdb.Result{
					&influxdb.Result{
						Rows: []*influxql.Row{
							&influxql.Row{
								Name:    "cpu",
								Columns: []string{"host", "region"},
								Values: [][]interface{}{
									str2iface([]string{"server01", "useast"}),
									str2iface([]string{"server02", "useast"}),
								},
							},
						},
					},
				},
			},
		},
	}

	for i, tt := range tests {
		query := map[string]string{"db": "foo", "q": tt.q}
		status, body = MustHTTP("GET", s.URL+`/query`, query, nil, "")

		if status != http.StatusOK {
			t.Logf("query #%d: %s", i, tt.q)
			t.Log(body)
			t.Errorf("unexpected status: %d", status)
		}

		r := &influxdb.Results{}
		if err := json.Unmarshal([]byte(body), r); err != nil {
			t.Logf("query #%d: %s", i, tt.q)
			t.Log(body)
			t.Error(err)
		}

		if !reflect.DeepEqual(tt.err, errstring(r.Err)) {
			t.Errorf("%d. %s: error mismatch:\n  exp=%s\n  got=%s\n\n", i, tt.q, tt.err, r.Err)
		} else if tt.err == "" && !reflect.DeepEqual(tt.r, r) {
			t.Log(body)
			t.Errorf("%d. %s: result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.q, tt.r, r)
		}
	}
}