diff --git a/ci/it/testcases/base.go b/ci/it/testcases/base.go index 7346a746e..9294c04fb 100644 --- a/ci/it/testcases/base.go +++ b/ci/it/testcases/base.go @@ -9,6 +9,7 @@ import ( "database/sql" "fmt" "github.com/ClickHouse/clickhouse-go/v2" + "io" "net/http" "testing" "time" @@ -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) { diff --git a/ci/it/testcases/test_dual_write_and_common_table.go b/ci/it/testcases/test_dual_write_and_common_table.go index 4d0e52dda..0270d07e5 100644 --- a/ci/it/testcases/test_dual_write_and_common_table.go +++ b/ci/it/testcases/test_dual_write_and_common_table.go @@ -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'" @@ -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'" @@ -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'" @@ -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'" @@ -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) diff --git a/ci/it/testcases/test_reading_clickhouse_tables.go b/ci/it/testcases/test_reading_clickhouse_tables.go index 08a7947f7..978cba625 100644 --- a/ci/it/testcases/test_reading_clickhouse_tables.go +++ b/ci/it/testcases/test_reading_clickhouse_tables.go @@ -7,7 +7,6 @@ import ( "context" "encoding/json" "github.com/stretchr/testify/assert" - "io" "net/http" "testing" ) @@ -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) } @@ -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")) } @@ -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) diff --git a/ci/it/testcases/test_transparent_proxy.go b/ci/it/testcases/test_transparent_proxy.go index 78924cc14..822c9a257 100644 --- a/ci/it/testcases/test_transparent_proxy.go +++ b/ci/it/testcases/test_transparent_proxy.go @@ -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 { diff --git a/ci/it/testcases/test_two_pipelines.go b/ci/it/testcases/test_two_pipelines.go index c6c695c09..53310e4eb 100644 --- a/ci/it/testcases/test_two_pipelines.go +++ b/ci/it/testcases/test_two_pipelines.go @@ -7,7 +7,6 @@ import ( "context" "encoding/json" "github.com/stretchr/testify/assert" - "io" "net/http" "testing" ) @@ -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) } @@ -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) @@ -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")) @@ -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"))