Skip to content

Commit

Permalink
ITs: RequestToQuesma boilerplate cleanup (#910)
Browse files Browse the repository at this point in the history
Since I'll be adding new integration tests, let's do some cleanup of
boilerplate of existing tests to make it nicer to write new tests
(avoiding further code duplication/boilerplace).

This PR tackles `RequestToQuesma`: move error handling/reading of body
into `RequestToQuesma` function itself - no need to duplicate this logic
all over the test code.
  • Loading branch information
avelanarius authored Oct 23, 2024
1 parent b520aca commit 5349c23
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 127 deletions.
14 changes: 12 additions & 2 deletions ci/it/testcases/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"database/sql"
"fmt"
"github.com/ClickHouse/clickhouse-go/v2"
"io"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -113,9 +114,18 @@ func (tc *IntegrationTestcaseBase) ExecuteClickHouseStatement(ctx context.Contex
return res, nil
}

func (tc *IntegrationTestcaseBase) RequestToQuesma(ctx context.Context, method, uri string, body []byte) (*http.Response, error) {
func (tc *IntegrationTestcaseBase) RequestToQuesma(ctx context.Context, t *testing.T, method, uri string, requestBody []byte) (*http.Response, []byte) {
endpoint := tc.getQuesmaEndpoint()
return tc.doRequest(ctx, method, endpoint+uri, body, nil)
resp, err := tc.doRequest(ctx, method, endpoint+uri, requestBody, nil)
if err != nil {
t.Fatalf("Error sending %s request to the endpoint '%s': %s", method, uri, err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body of %s request to the endpoint '%s': %s", method, uri, err)
}
return resp, responseBody
}

func (tc *IntegrationTestcaseBase) RequestToElasticsearch(ctx context.Context, method, uri string, body []byte) (*http.Response, error) {
Expand Down
63 changes: 9 additions & 54 deletions ci/it/testcases/test_dual_write_and_common_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,11 @@ func (a *DualWriteAndCommonTableTestcase) RunTests(ctx context.Context, t *testi
}

func (a *DualWriteAndCommonTableTestcase) testBasicRequest(ctx context.Context, t *testing.T) {
resp, err := a.RequestToQuesma(ctx, "GET", "/", nil)
if err != nil {
t.Fatalf("Failed to make GET request: %s", err)
}
defer resp.Body.Close()
resp, _ := a.RequestToQuesma(ctx, t, "GET", "/", nil)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
func (a *DualWriteAndCommonTableTestcase) testIngestToCommonTableWorks(ctx context.Context, t *testing.T) {
resp, err := a.RequestToQuesma(ctx, "POST", "/logs-4/_doc", []byte(`{"name": "Przemyslaw", "age": 31337}`))
if err != nil {
t.Fatalf("Failed to insert document: %s", err)
}
defer resp.Body.Close()
resp, _ := a.RequestToQuesma(ctx, t, "POST", "/logs-4/_doc", []byte(`{"name": "Przemyslaw", "age": 31337}`))
assert.Equal(t, http.StatusOK, resp.StatusCode)

chQuery := "SELECT * FROM 'quesma_common_table'"
Expand Down Expand Up @@ -101,27 +93,14 @@ func (a *DualWriteAndCommonTableTestcase) testIngestToCommonTableWorks(ctx conte
assert.Equal(t, 31337, age)
assert.Equal(t, "logs-4", quesmaIndexName)

resp, err = a.RequestToQuesma(ctx, "GET", "/logs-4/_search", []byte(`{"query": {"match_all": {}}}`))
if err != nil {
t.Fatalf("Failed to make GET request: %s", err)
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body: %s", err)
}

resp, bodyBytes := a.RequestToQuesma(ctx, t, "GET", "/logs-4/_search", []byte(`{"query": {"match_all": {}}}`))
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Contains(t, string(bodyBytes), "Przemyslaw")
assert.Contains(t, "Clickhouse", resp.Header.Get("X-Quesma-Source"))
}

func (a *DualWriteAndCommonTableTestcase) testDualQueryReturnsDataFromClickHouse(ctx context.Context, t *testing.T) {
resp, err := a.RequestToQuesma(ctx, "POST", "/logs-dual-query/_doc", []byte(`{"name": "Przemyslaw", "age": 31337}`))
if err != nil {
t.Fatalf("Failed to insert document: %s", err)
}
defer resp.Body.Close()
resp, _ := a.RequestToQuesma(ctx, t, "POST", "/logs-dual-query/_doc", []byte(`{"name": "Przemyslaw", "age": 31337}`))
assert.Equal(t, http.StatusOK, resp.StatusCode)

chQuery := "SELECT * FROM 'logs-dual-query'"
Expand Down Expand Up @@ -166,27 +145,14 @@ func (a *DualWriteAndCommonTableTestcase) testDualQueryReturnsDataFromClickHouse
t.Fatalf("Failed to make DELETE request: %s", err)
}
// FINAL TEST - WHETHER QUESMA RETURNS DATA FROM CLICKHOUSE
resp, err = a.RequestToQuesma(ctx, "GET", "/logs-dual-query/_search", []byte(`{"query": {"match_all": {}}}`))
if err != nil {
t.Fatalf("Failed to make GET request: %s", err)
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body: %s", err)
}

resp, bodyBytes := a.RequestToQuesma(ctx, t, "GET", "/logs-dual-query/_search", []byte(`{"query": {"match_all": {}}}`))
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Contains(t, string(bodyBytes), "Przemyslaw")
assert.Contains(t, "Clickhouse", resp.Header.Get("X-Quesma-Source"))
}

func (a *DualWriteAndCommonTableTestcase) testIngestToClickHouseWorks(ctx context.Context, t *testing.T) {
resp, err := a.RequestToQuesma(ctx, "POST", "/logs-2/_doc", []byte(`{"name": "Przemyslaw", "age": 31337}`))
if err != nil {
t.Fatalf("Failed to insert document: %s", err)
}
defer resp.Body.Close()
resp, _ := a.RequestToQuesma(ctx, t, "POST", "/logs-2/_doc", []byte(`{"name": "Przemyslaw", "age": 31337}`))
assert.Equal(t, http.StatusOK, resp.StatusCode)

chQuery := "SELECT * FROM 'logs-2'"
Expand Down Expand Up @@ -241,11 +207,7 @@ func (a *DualWriteAndCommonTableTestcase) testIngestToClickHouseWorks(ctx contex
}

func (a *DualWriteAndCommonTableTestcase) testDualWritesWork(ctx context.Context, t *testing.T) {
resp, err := a.RequestToQuesma(ctx, "POST", "/logs-3/_doc", []byte(`{"name": "Przemyslaw", "age": 31337}`))
if err != nil {
t.Fatalf("Failed to insert document: %s", err)
}
defer resp.Body.Close()
resp, _ := a.RequestToQuesma(ctx, t, "POST", "/logs-3/_doc", []byte(`{"name": "Przemyslaw", "age": 31337}`))
assert.Equal(t, http.StatusOK, resp.StatusCode)

chQuery := "SELECT * FROM 'logs-3'"
Expand Down Expand Up @@ -313,15 +275,8 @@ func (a *DualWriteAndCommonTableTestcase) testWildcardGoesToElastic(ctx context.
t.Fatalf("Failed to refresh index: %s", err)
}
// When Quesma searches for that document
resp, err := a.RequestToQuesma(ctx, "POST", "/unmentioned_index/_search", []byte(`{"query": {"match_all": {}}}`))
if err != nil {
t.Fatalf("Failed to make GET request: %s", err)
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body: %s", err)
}
resp, bodyBytes := a.RequestToQuesma(ctx, t, "POST", "/unmentioned_index/_search", []byte(`{"query": {"match_all": {}}}`))

var jsonResponse map[string]interface{}
if err := json.Unmarshal(bodyBytes, &jsonResponse); err != nil {
t.Fatalf("Failed to unmarshal response body: %s", err)
Expand Down
23 changes: 3 additions & 20 deletions ci/it/testcases/test_reading_clickhouse_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"encoding/json"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"testing"
)
Expand Down Expand Up @@ -41,11 +40,7 @@ func (a *ReadingClickHouseTablesIntegrationTestcase) RunTests(ctx context.Contex
}

func (a *ReadingClickHouseTablesIntegrationTestcase) testBasicRequest(ctx context.Context, t *testing.T) {
resp, err := a.RequestToQuesma(ctx, "GET", "/", nil)
if err != nil {
t.Fatalf("Failed to make GET request: %s", err)
}
defer resp.Body.Close()
resp, _ := a.RequestToQuesma(ctx, t, "GET", "/", nil)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

Expand All @@ -63,11 +58,7 @@ func (a *ReadingClickHouseTablesIntegrationTestcase) testRandomThing(ctx context
// This returns 500 Internal Server Error, but will be tackled in separate PR.
// (The table has not yet been discovered by Quesma )
// ERR quesma/quesma/quesma.go:198 > quesma request failed: Q2002: Missing table. Table: test_table: can't load test_table table opaque_id= path=/test_table/_search reason="Missing table." request_id=01926654-b214-7e1d-944a-a7545cd7d419
resp, err := a.RequestToQuesma(ctx, "GET", "/test_table/_search", []byte(`{"query": {"match_all": {}}}`))
if err != nil {
t.Fatalf("Failed to make GET request: %s", err)
}
defer resp.Body.Close()
resp, _ := a.RequestToQuesma(ctx, t, "GET", "/test_table/_search", []byte(`{"query": {"match_all": {}}}`))
assert.Equal(t, "Clickhouse", resp.Header.Get("X-Quesma-Source"))
}

Expand All @@ -84,15 +75,7 @@ func (a *ReadingClickHouseTablesIntegrationTestcase) testWildcardGoesToElastic(c
t.Fatalf("Failed to refresh index: %s", err)
}
// When Quesma searches for that document
resp, err := a.RequestToQuesma(ctx, "POST", "/extra_index/_search", []byte(`{"query": {"match_all": {}}}`))
if err != nil {
t.Fatalf("Failed to make GET request: %s", err)
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body: %s", err)
}
resp, bodyBytes := a.RequestToQuesma(ctx, t, "POST", "/extra_index/_search", []byte(`{"query": {"match_all": {}}}`))
var jsonResponse map[string]interface{}
if err := json.Unmarshal(bodyBytes, &jsonResponse); err != nil {
t.Fatalf("Failed to unmarshal response body: %s", err)
Expand Down
20 changes: 4 additions & 16 deletions ci/it/testcases/test_transparent_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,20 @@ func (a *TransparentProxyIntegrationTestcase) RunTests(ctx context.Context, t *t
}

func (a *TransparentProxyIntegrationTestcase) testBasicRequest(ctx context.Context, t *testing.T) {
resp, err := a.RequestToQuesma(ctx, "GET", "/", nil)
if err != nil {
t.Fatalf("Failed to make GET request: %s", err)
}
defer resp.Body.Close()
resp, _ := a.RequestToQuesma(ctx, t, "GET", "/", nil)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func (a *TransparentProxyIntegrationTestcase) testIfCatHealthRequestReachesElasticsearch(ctx context.Context, t *testing.T) {
resp, err := a.RequestToQuesma(ctx, "GET", "/_cat/health", nil)
if err != nil {
t.Fatalf("Failed to make GET request: %s", err)
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body: %s", err)
}
resp, bodyBytes := a.RequestToQuesma(ctx, t, "GET", "/_cat/health", nil)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "Elasticsearch", resp.Header.Get("X-elastic-product"))
assert.Contains(t, string(bodyBytes), "green")
}

func (a *TransparentProxyIntegrationTestcase) testIfIndexCreationWorks(ctx context.Context, t *testing.T) {
_, err := a.RequestToQuesma(ctx, "PUT", "/index_1", nil)
_, err = a.RequestToQuesma(ctx, "PUT", "/index_2", nil)
_, _ = a.RequestToQuesma(ctx, t, "PUT", "/index_1", nil)
_, _ = a.RequestToQuesma(ctx, t, "PUT", "/index_2", nil)

resp, err := a.RequestToElasticsearch(ctx, "GET", "/_cat/indices", nil)
if err != nil {
Expand Down
39 changes: 4 additions & 35 deletions ci/it/testcases/test_two_pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"encoding/json"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"testing"
)
Expand Down Expand Up @@ -42,11 +41,7 @@ func (a *QueryAndIngestPipelineTestcase) RunTests(ctx context.Context, t *testin
}

func (a *QueryAndIngestPipelineTestcase) testBasicRequest(ctx context.Context, t *testing.T) {
resp, err := a.RequestToQuesma(ctx, "GET", "/", nil)
if err != nil {
t.Fatalf("Failed to make GET request: %s", err)
}
defer resp.Body.Close()
resp, _ := a.RequestToQuesma(ctx, t, "GET", "/", nil)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

Expand All @@ -63,15 +58,7 @@ func (a *QueryAndIngestPipelineTestcase) testWildcardGoesToElastic(ctx context.C
t.Fatalf("Failed to refresh index: %s", err)
}
// When Quesma searches for that document
resp, err := a.RequestToQuesma(ctx, "POST", "/unmentioned_index/_search", []byte(`{"query": {"match_all": {}}}`))
if err != nil {
t.Fatalf("Failed to make GET request: %s", err)
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body: %s", err)
}
resp, bodyBytes := a.RequestToQuesma(ctx, t, "POST", "/unmentioned_index/_search", []byte(`{"query": {"match_all": {}}}`))
var jsonResponse map[string]interface{}
if err := json.Unmarshal(bodyBytes, &jsonResponse); err != nil {
t.Fatalf("Failed to unmarshal response body: %s", err)
Expand All @@ -88,16 +75,7 @@ func (a *QueryAndIngestPipelineTestcase) testWildcardGoesToElastic(ctx context.C
}

func (a *QueryAndIngestPipelineTestcase) testEmptyTargetDoc(ctx context.Context, t *testing.T) {
resp, err := a.RequestToQuesma(ctx, "POST", "/logs_disabled/_doc", []byte(`{"name": "Alice"}`))
if err != nil {
t.Fatalf("Error sending POST request: %s", err)
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body: %s", err)
}

resp, bodyBytes := a.RequestToQuesma(ctx, t, "POST", "/logs_disabled/_doc", []byte(`{"name": "Alice"}`))
assert.Contains(t, string(bodyBytes), "index_closed_exception")
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "Clickhouse", resp.Header.Get("X-Quesma-Source"))
Expand All @@ -112,16 +90,7 @@ func (a *QueryAndIngestPipelineTestcase) testEmptyTargetBulk(ctx context.Context
{ "name": "Bob", "age": 25 }
`)
resp, err := a.RequestToQuesma(ctx, "POST", "/_bulk", bulkPayload)
if err != nil {
t.Fatalf("Error sending POST request: %s", err)
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body: %s", err)
}

resp, bodyBytes := a.RequestToQuesma(ctx, t, "POST", "/_bulk", bulkPayload)
assert.Contains(t, string(bodyBytes), "index_closed_exception")
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "Clickhouse", resp.Header.Get("X-Quesma-Source"))
Expand Down

0 comments on commit 5349c23

Please sign in to comment.