diff --git a/cmd/root.go b/cmd/root.go index 1ee40c2..916f5b7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,5 +1,5 @@ /* -Copyright 2023-2024 API Testing Authors. +Copyright 2023-2025 API Testing Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/e2e/entrypoint.sh b/e2e/entrypoint.sh index e3d6608..4d135a0 100755 --- a/e2e/entrypoint.sh +++ b/e2e/entrypoint.sh @@ -17,4 +17,6 @@ kind=orm target=greptimedb:4002 driver=greptime dbname=public atest run -p testi kind=orm target=tdengine:6041 driver=tdengine password=taosdata dbname=information_schema atest run -p testing-data-query.yaml kind=orm target=postgres driver=postgres atest run -p testing-data-query.yaml +kind=orm target=mysql driver=mysql atest run -p testing-data-query-mysql.yaml + cat /root/.config/atest/stores.yaml diff --git a/e2e/start.sh b/e2e/start.sh index 89c4c2a..e2efab6 100755 --- a/e2e/start.sh +++ b/e2e/start.sh @@ -8,4 +8,5 @@ fi docker compose version docker compose -f "$file" down -docker compose -f "$file" up --build testing --exit-code-from testing --remove-orphans +docker compose -f "$file" build extension +docker compose -f "$file" up --exit-code-from testing --remove-orphans diff --git a/e2e/testing-data-query-mysql.yaml b/e2e/testing-data-query-mysql.yaml new file mode 100644 index 0000000..fa8952e --- /dev/null +++ b/e2e/testing-data-query-mysql.yaml @@ -0,0 +1,75 @@ +#!api-testing +# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-schema.json +name: atest +api: | + {{default "http://localhost:8080" (env "SERVER")}}/api/v1 +param: + store: "{{randAlpha 3}}" + server: | + {{default "http://localhost:8080" (env "SERVER")}} +items: +- name: CreateStore + before: + items: + - httpReady("{{.param.server}}/healthz", 2400) + request: + api: /stores + method: POST + body: | + { + "name": "{{.param.store}}", + "url": "{{env "target"}}", + "username": "{{default "root" (env "username")}}", + "password": "{{default "root" (env "password")}}", + "kind": { + "name": "atest-store-{{env "kind"}}" + }, + "properties": [{ + "key": "driver", + "value": "{{default "mysql" (env "driver")}}" + }, { + "key": "database", + "value": "{{default "atest" (env "dbname")}}" + }, { + "key": "bucket", + "value": "bucket" + }, { + "key": "region", + "value": "cn" + }, { + "key": "disablessl", + "value": "true" + }, { + "key": "targetPath", + "value": "api-testing" + }] + } +- name: query + before: + items: + - sleep(3) + request: + api: /data/query + method: POST + header: + X-Store-Name: "{{.param.store}}" + body: | + { + "sql": "", + "key": "" + } +- name: invalid-sql + request: + api: /data/query + method: POST + header: + X-Store-Name: "{{.param.store}}" + body: | + { + "sql": "selec * from user where id <> 1", + "key": "" + } + expect: + statusCode: 500 + bodyFieldsExpect: + code: 2 \ No newline at end of file diff --git a/main.go b/main.go index 5900101..79015cd 100644 --- a/main.go +++ b/main.go @@ -1,26 +1,17 @@ /* -* -MIT License +Copyright 2023 API Testing Authors. -Copyright (c) 2023 API Testing Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: + http://www.apache.org/licenses/LICENSE-2.0 -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ package main diff --git a/pkg/data_query.go b/pkg/data_query.go index 9d75f02..3ca8de0 100644 --- a/pkg/data_query.go +++ b/pkg/data_query.go @@ -52,8 +52,9 @@ func (s *dbserver) Query(ctx context.Context, query *server.DataQuery) (result * go func() { defer wg.Done() // query database and tables - if result.Meta.Databases, err = dbQuery.GetDatabases(ctx); err != nil { - log.Printf("failed to query databases: %v\n", err) + var innerErr error + if result.Meta.Databases, innerErr = dbQuery.GetDatabases(ctx); innerErr != nil { + log.Printf("failed to query databases: %v\n", innerErr) } }() @@ -61,13 +62,15 @@ func (s *dbserver) Query(ctx context.Context, query *server.DataQuery) (result * go func() { defer wg.Done() if result.Meta.CurrentDatabase = query.Key; query.Key == "" { - if result.Meta.CurrentDatabase, err = dbQuery.GetCurrentDatabase(); err != nil { - log.Printf("failed to query current database: %v\n", err) + var queryDBErr error + if result.Meta.CurrentDatabase, queryDBErr = dbQuery.GetCurrentDatabase(); queryDBErr != nil { + log.Printf("failed to query current database: %v\n", queryDBErr) } } - if result.Meta.Tables, err = dbQuery.GetTables(ctx, result.Meta.CurrentDatabase); err != nil { - log.Printf("failed to query tables: %v\n", err) + var queryTableErr error + if result.Meta.Tables, queryTableErr = dbQuery.GetTables(ctx, result.Meta.CurrentDatabase); err != nil { + log.Printf("failed to query tables: %v\n", queryTableErr) } }() @@ -97,6 +100,8 @@ func (s *dbserver) Query(ctx context.Context, query *server.DataQuery) (result * wg.Wait() result.Meta.Labels = append(result.Meta.Labels, dataResult.Meta.Labels...) + } else { + wg.Wait() } return } @@ -140,7 +145,8 @@ func sqlQuery(ctx context.Context, sqlText string, db *gorm.DB) (result *server. } } - columns, err := rows.Columns() + var columns []string + columns, err = rows.Columns() if err != nil { return }