Move tests from HTTP handlers to integration

pull/1788/head
Philip O'Toole 2015-03-03 14:02:53 -08:00
parent 1ba6d8a4f9
commit 0d03998dc3
2 changed files with 86 additions and 226 deletions

View File

@ -242,6 +242,7 @@ func runTestsData(t *testing.T, testName string, nodes Cluster, database, retent
query string // If equal to the blank string, no query is executed. query string // If equal to the blank string, no query is executed.
expected string // If 'query' is equal to the blank string, this is ignored. expected string // If 'query' is equal to the blank string, this is ignored.
}{ }{
// Data read and write tests
{ {
reset: true, reset: true,
name: "single point with timestamp", name: "single point with timestamp",
@ -254,6 +255,86 @@ func runTestsData(t *testing.T, testName string, nodes Cluster, database, retent
query: `SELECT * FROM "%DB%"."%RP%".cpu WHERE time < now()`, query: `SELECT * FROM "%DB%"."%RP%".cpu WHERE time < now()`,
expected: `{"results":[{"series":[{"name":"cpu","columns":["time","value"],"values":[["2015-02-28T01:03:36.703820946Z",100]]}]}]}`, expected: `{"results":[{"series":[{"name":"cpu","columns":["time","value"],"values":[["2015-02-28T01:03:36.703820946Z",100]]}]}]}`,
}, },
{
name: "measurement not found",
query: `SELECT value FROM "%DB%"."%RP%".foobarbaz`,
expected: `{"results":[{"error":"measurement not found"}]}`,
},
{
name: "field not found",
query: `SELECT abc FROM "%DB%"."%RP%".cpu WHERE time < now()`,
expected: `{"results":[{"error":"field not found: abc"}]}`,
},
// User control tests
{
name: "show users, no actual users",
query: `SHOW USERS`,
expected: `{"results":[{"series":[{"columns":["user","admin"]}]}]}`,
},
{
query: `CREATE USER jdoe WITH PASSWORD '1337'`,
expected: `{"results":[{}]}`,
},
{
name: "show users, 1 existing user",
query: `SHOW USERS`,
expected: `{"results":[{"series":[{"columns":["user","admin"],"values":[["jdoe",false]]}]}]}`,
},
{
query: `GRANT ALL PRIVILEGES TO jdoe`,
expected: `{"results":[{}]}`,
},
{
name: "show users, existing user as admin",
query: `SHOW USERS`,
expected: `{"results":[{"series":[{"columns":["user","admin"],"values":[["jdoe",true]]}]}]}`,
},
{
name: "grant DB privileges to user",
query: `GRANT READ ON %DB% TO jdoe`,
expected: `{"results":[{}]}`,
},
{
query: `REVOKE ALL PRIVILEGES FROM jdoe`,
expected: `{"results":[{}]}`,
},
{
name: "bad create user request",
query: `CREATE USER 0xBAD WITH PASSWORD pwd1337`,
expected: `{"error":"error parsing query: found 0, expected identifier at line 1, char 13"}`,
},
{
name: "bad create user request, no name",
query: `CREATE USER WITH PASSWORD pwd1337`,
expected: `{"error":"error parsing query: found WITH, expected identifier at line 1, char 13"}`,
},
{
name: "bad create user request, no password",
query: `CREATE USER jdoe`,
expected: `{"error":"error parsing query: found EOF, expected WITH at line 1, char 18"}`,
},
{
query: `DROP USER jdoe`,
expected: `{"results":[{}]}`,
},
{
name: "delete non existing user",
query: `DROP USER noone`,
expected: `{"results":[{"error":"user not found"}]}`,
},
// Continuous query control.
{
name: "create continuous query",
query: `CREATE CONTINUOUS QUERY myquery ON %DB% BEGIN SELECT count() INTO measure1 FROM myseries GROUP BY time(10m) END`,
expected: `{"results":[{}]}`,
},
{
query: `SHOW CONTINUOUS QUERIES`,
expected: `{"results":[{"series":[{"name":"%DB%","columns":["name","query"],"values":[["myquery","CREATE CONTINUOUS QUERY myquery ON %DB% BEGIN SELECT count() INTO measure1 FROM myseries GROUP BY time(10m) END"]]}]}]}`,
},
} }
for _, tt := range tests { for _, tt := range tests {
@ -271,7 +352,11 @@ func runTestsData(t *testing.T, testName string, nodes Cluster, database, retent
if tt.query != "" { if tt.query != "" {
got, ok := query(t, nodes, rewriteDbRp(tt.query, database, retention), rewriteDbRp(tt.expected, database, retention)) got, ok := query(t, nodes, rewriteDbRp(tt.query, database, retention), rewriteDbRp(tt.expected, database, retention))
if !ok { if !ok {
t.Errorf(`Test "%s" failed, expected: %s, got: %s`, tt.name, rewriteDbRp(tt.expected, database, retention), got) name := tt.name
if name == "" {
name = tt.query
}
t.Errorf(`Test "%s" failed, expected: %s, got: %s`, name, rewriteDbRp(tt.expected, database, retention), got)
} }
} }
} }

View File

@ -252,55 +252,6 @@ func TestHandler_DropDatabase_NotFound(t *testing.T) {
} }
} }
func TestHandler_SelectMeasurement_NotFound(t *testing.T) {
srvr := OpenAuthlessServer(NewMessagingClient())
srvr.CreateDatabase("foo")
s := NewHTTPServer(srvr)
defer s.Close()
query := map[string]string{"q": "CREATE RETENTION POLICY bar ON foo DURATION 1h REPLICATION 1 DEFAULT"}
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)
}
status, body = MustHTTP("GET", s.URL+`/query`, map[string]string{"q": "SELECT value FROM foobarbaz", "db": "foo"}, nil, "")
if status != http.StatusOK {
t.Fatalf("unexpected status: %d", status)
} else if body != `{"results":[{"error":"measurement not found"}]}` {
t.Fatalf("unexpected body: %s", body)
}
}
func TestHandler_SelectField_NotFound(t *testing.T) {
srvr := OpenAuthlessServer(NewMessagingClient())
srvr.CreateDatabase("foo")
s := NewHTTPServer(srvr)
defer s.Close()
query := map[string]string{"q": "CREATE RETENTION POLICY bar ON foo DURATION 1h REPLICATION 1 DEFAULT"}
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)
}
// 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}}]}`)
status, body = MustHTTP("GET", s.URL+`/query`, map[string]string{"q": "SELECT abc FROM cpu", "db": "foo"}, nil, "")
if status != http.StatusOK {
t.Fatalf("unexpected status: %d", status)
} else if body != `{"results":[{"error":"field not found: abc"}]}` {
t.Fatalf("unexpected body: %s", body)
}
}
func TestHandler_RetentionPolicies(t *testing.T) { func TestHandler_RetentionPolicies(t *testing.T) {
srvr := OpenAuthlessServer(NewMessagingClient()) srvr := OpenAuthlessServer(NewMessagingClient())
srvr.CreateDatabase("foo") srvr.CreateDatabase("foo")
@ -695,36 +646,6 @@ func TestHandler_PingHead(t *testing.T) {
} }
} }
func TestHandler_Users_NoUsers(t *testing.T) {
srvr := OpenAuthlessServer(NewMessagingClient())
srvr.CreateDatabase("foo")
s := NewHTTPServer(srvr)
defer s.Close()
query := map[string]string{"q": "SHOW USERS"}
status, body := MustHTTP("GET", s.URL+`/query`, query, nil, "")
if status != http.StatusOK {
t.Fatalf("unexpected status: %d", status)
} else if body != `{"results":[{"series":[{"columns":["user","admin"]}]}]}` {
t.Fatalf("unexpected body: %s", body)
}
}
func TestHandler_Users_OneUser(t *testing.T) {
srvr := OpenAuthlessServer(NewMessagingClient())
srvr.CreateUser("jdoe", "1337", true)
s := NewHTTPServer(srvr)
defer s.Close()
query := map[string]string{"q": "SHOW USERS"}
status, body := MustHTTP("GET", s.URL+`/query`, query, nil, "")
if status != http.StatusOK {
t.Fatalf("unexpected status: %d", status)
} else if body != `{"results":[{"series":[{"columns":["user","admin"],"values":[["jdoe",true]]}]}]}` {
t.Fatalf("unexpected body: %s", body)
}
}
func TestHandler_Users_MultipleUsers(t *testing.T) { func TestHandler_Users_MultipleUsers(t *testing.T) {
srvr := OpenAuthlessServer(NewMessagingClient()) srvr := OpenAuthlessServer(NewMessagingClient())
srvr.CreateUser("jdoe", "1337", false) srvr.CreateUser("jdoe", "1337", false)
@ -742,62 +663,6 @@ func TestHandler_Users_MultipleUsers(t *testing.T) {
} }
} }
func TestHandler_CreateUser(t *testing.T) {
srvr := OpenAuthlessServer(NewMessagingClient())
s := NewHTTPServer(srvr)
defer s.Close()
query := map[string]string{"q": `CREATE USER testuser WITH PASSWORD '1337'`}
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)
}
}
func TestHandler_CreateUser_BadRequest(t *testing.T) {
srvr := OpenAuthlessServer(NewMessagingClient())
s := NewHTTPServer(srvr)
defer s.Close()
query := map[string]string{"q": "CREATE USER 0xBAD WITH PASSWORD pwd1337"}
status, body := MustHTTP("GET", s.URL+`/query`, query, nil, "")
if status != http.StatusBadRequest {
t.Fatalf("unexpected status: %d", status)
} else if body != `{"error":"error parsing query: found 0, expected identifier at line 1, char 13"}` {
t.Fatalf("unexpected body: %s", body)
}
}
func TestHandler_CreateUser_BadRequest_NoName(t *testing.T) {
srvr := OpenAuthlessServer(NewMessagingClient())
s := NewHTTPServer(srvr)
defer s.Close()
query := map[string]string{"q": "CREATE USER WITH PASSWORD pwd1337"}
status, body := MustHTTP("GET", s.URL+`/query`, query, nil, "")
if status != http.StatusBadRequest {
t.Fatalf("unexpected status: %d", status)
} else if body != `{"error":"error parsing query: found WITH, expected identifier at line 1, char 13"}` {
t.Fatalf("unexpected body: %s", body)
}
}
func TestHandler_CreateUser_BadRequest_NoPassword(t *testing.T) {
srvr := OpenAuthlessServer(NewMessagingClient())
s := NewHTTPServer(srvr)
defer s.Close()
query := map[string]string{"q": "CREATE USER jdoe"}
status, body := MustHTTP("GET", s.URL+`/query`, query, nil, "")
if status != http.StatusBadRequest {
t.Fatalf("unexpected status: %d", status)
} else if body != `{"error":"error parsing query: found EOF, expected WITH at line 1, char 18"}` {
t.Fatalf("unexpected body: %s", body)
}
}
func TestHandler_UpdateUser(t *testing.T) { func TestHandler_UpdateUser(t *testing.T) {
t.Skip() t.Skip()
srvr := OpenAuthlessServer(NewMessagingClient()) srvr := OpenAuthlessServer(NewMessagingClient())
@ -834,35 +699,6 @@ func TestHandler_UpdateUser_PasswordBadRequest(t *testing.T) {
} }
} }
func TestHandler_DeleteUser(t *testing.T) {
srvr := OpenAuthlessServer(NewMessagingClient())
srvr.CreateUser("jdoe", "1337", false)
s := NewHTTPServer(srvr)
defer s.Close()
query := map[string]string{"q": "DROP USER jdoe"}
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)
}
}
func TestHandler_DeleteUser_UserNotFound(t *testing.T) {
srvr := OpenAuthlessServer(NewMessagingClient())
s := NewHTTPServer(srvr)
defer s.Close()
query := map[string]string{"q": "DROP USER jdoe"}
status, body := MustHTTP("GET", s.URL+`/query`, query, nil, "")
if status != http.StatusInternalServerError {
t.Fatalf("unexpected status: %d", status)
} else if body != `{"results":[{"error":"user not found"}]}` {
t.Fatalf("unexpected body: %s", body)
}
}
func TestHandler_DataNodes(t *testing.T) { func TestHandler_DataNodes(t *testing.T) {
t.Skip() t.Skip()
srvr := OpenUninitializedServer(NewMessagingClient()) srvr := OpenUninitializedServer(NewMessagingClient())
@ -1041,37 +877,6 @@ func TestHandler_AuthenticatedDatabases_UnauthorizedBasicAuth(t *testing.T) {
} }
} }
func TestHandler_GrantAdmin(t *testing.T) {
srvr := OpenAuthenticatedServer(NewMessagingClient())
// Create a cluster admin that will grant admin to "john".
srvr.CreateUser("lisa", "password", true)
// Create user that will be granted cluster admin.
srvr.CreateUser("john", "password", false)
s := NewAuthenticatedHTTPServer(srvr)
defer s.Close()
auth := make(map[string]string)
auth["Authorization"] = "Basic " + base64.StdEncoding.EncodeToString([]byte("lisa:password"))
query := map[string]string{"q": "GRANT ALL PRIVILEGES TO john"}
status, _ := MustHTTP("GET", s.URL+`/query`, query, auth, "")
if status != http.StatusOK {
t.Fatalf("unexpected status: %d", status)
}
if u := srvr.User("john"); !u.Admin {
t.Fatal(`expected user "john" to be admin`)
}
// Make sure update persists after server restart.
srvr.Restart()
if u := srvr.User("john"); !u.Admin {
t.Fatal(`expected user "john" to be admin after server restart`)
}
}
func TestHandler_GrantDBPrivilege(t *testing.T) { func TestHandler_GrantDBPrivilege(t *testing.T) {
srvr := OpenAuthenticatedServer(NewMessagingClient()) srvr := OpenAuthenticatedServer(NewMessagingClient())
// Create a cluster admin that will grant privilege to "john". // Create a cluster admin that will grant privilege to "john".
@ -1175,36 +980,6 @@ func TestHandler_RevokeDBPrivilege(t *testing.T) {
} }
} }
func TestHandler_ShowContinuousQueries(t *testing.T) {
srvr := OpenAuthlessServer(NewMessagingClient())
srvr.CreateDatabase("foo")
srvr.CreateRetentionPolicy("foo", influxdb.NewRetentionPolicy("bar"))
srvr.SetDefaultRetentionPolicy("foo", "bar")
// create and check
q := "CREATE CONTINUOUS QUERY myquery ON foo BEGIN SELECT count() INTO measure1 FROM myseries GROUP BY time(10m) END"
stmt, err := influxql.NewParser(strings.NewReader(q)).ParseStatement()
if err != nil {
t.Fatalf("error parsing query %s", err.Error())
}
cq := stmt.(*influxql.CreateContinuousQueryStatement)
if err := srvr.CreateContinuousQuery(cq); err != nil {
t.Fatalf("error creating continuous query %s", err.Error())
}
s := NewHTTPServer(srvr)
defer s.Close()
query := map[string]string{"q": "SHOW CONTINUOUS QUERIES"}
status, body := MustHTTP("GET", s.URL+`/query`, query, nil, "")
if status != http.StatusOK {
t.Fatalf("unexpected status: %d", status)
} else if body != `{"results":[{"series":[{"name":"foo","columns":["name","query"],"values":[["myquery","CREATE CONTINUOUS QUERY myquery ON foo BEGIN SELECT count() INTO measure1 FROM myseries GROUP BY time(10m) END"]]}]}]}` {
t.Fatalf("unexpected body: %s", body)
}
}
func TestHandler_DropSeries(t *testing.T) { func TestHandler_DropSeries(t *testing.T) {
srvr := OpenAuthlessServer(NewMessagingClient()) srvr := OpenAuthlessServer(NewMessagingClient())
srvr.CreateDatabase("foo") srvr.CreateDatabase("foo")