diff --git a/README-ru.md b/README-ru.md index e4b1b43..56c811c 100644 --- a/README-ru.md +++ b/README-ru.md @@ -6,7 +6,7 @@ Gonkey протестирует ваши сервисы, используя их - работает с REST/JSON API - проверка API сервиса на соответствие OpenAPI-спеке -- заполнение БД сервиса данными из фикстур (поддерживается PostgreSQL) +- заполнение БД сервиса данными из фикстур (поддерживается PostgreSQL, MySQL, Aerospike) - моки для имитации внешних сервисов - можно подключить к проекту как библиотеку и запускать вместе с юнит-тестами - запись результата тестов в виде отчета [Allure](http://allure.qatools.ru/) @@ -32,6 +32,7 @@ Gonkey протестирует ваши сервисы, используя их - [Наследование записей](#наследование-записей) - [Связывание записей](#связывание-записей) - [Выражения](#выражения) + - [Aerospike](#aerospike) - [Моки](#моки) - [Запуск моков при использовании gonkey как библиотеки](#запуск-моков-при-использовании-gonkey-как-библиотеки) - [Описание моков в файле с тестом](#описание-моков-в-файле-с-тестом) @@ -42,6 +43,7 @@ Gonkey протестирует ваши сервисы, используя их - [Описание скрипта](#описание-скрипта) - [Запуск скрипта с параметризацией](#запуск-скрипта-с-параметризацией) - [Запрос в Базу данных](#запрос-в-базу-данных) + - [Формат описания запросов](#формат-описания-запросов) - [Описание запроса](#описание-запроса) - [Описание ответа на запрос в Базу данных](#описание-ответа-на-запрос-в-базу-данных) - [Параметризация при запросах в Базу данных](#параметризация-при-запросах-в-базу-данных) @@ -56,7 +58,9 @@ Gonkey протестирует ваши сервисы, используя их - `-spec <...>` путь к файлу или URL со swagger-спецификацией сервиса - `-host <...>` хост:порт сервиса - `-tests <...>` файл или директория с тестами -- `-db_dsn <...>` dsn для вашей тестовой базы данных (бд будет очищена перед наполнением!), поддерживается только PostgreSQL +- `-db-type <...>` - тип базы данных. В данный момент поддерживается PostgreSQL и Aerospike. +- `-db_dsn <...>` dsn для вашей тестовой SQL базы данных (бд будет очищена перед наполнением!), поддерживается только PostgreSQL +- `-aerospike_host <...>` при использовании Aerospike - URL для подключения к нему в формате `host:port/namespace` - `-fixtures <...>` директория с вашими фикстурами - `-allure` генерировать allure-отчет - `-v` подробный вывод @@ -74,8 +78,8 @@ Gonkey протестирует ваши сервисы, используя их ```go import ( - "github.com/lamoda/gonkey/runner" - "github.com/lamoda/gonkey/mocks" + "github.com/lamoda/gonkey/runner" + "github.com/lamoda/gonkey/mocks" ) ``` @@ -86,9 +90,12 @@ func TestFuncCases(t *testing.T) { // проинициализируйте моки, если нужно (подробнее - ниже) //m := mocks.NewNop(...) - // проинициализирйте базу для загрузки фикстур, если нужно (подробнее - ниже) + // проинициализируйте базу для загрузки фикстур, если нужно (подробнее - ниже) //db := ... + // проинициализируйте Aerospike для загрузки фикстур, если нужно (подробнее - ниже) + //aerospikeClient := ... + // создайте экземпляр сервера вашего приложения srv := server.NewServer() defer srv.Close() @@ -99,7 +106,11 @@ func TestFuncCases(t *testing.T) { TestsDir: "cases", Mocks: m, DB: db, - // Тип используемой базы данных, возможные значения fixtures.Postgres, fixtures.Mysql + Aerospike: runner.Aerospike{ + Client: aerospikeClient, + Namespace: "test", + } + // Тип используемой базы данных, возможные значения fixtures.Postgres, fixtures.Mysql, fixtures.Aerospike // Если в параметр DB не пустой, а данный параметр не назначен, будет использоваться тип бд fixtures.Postgresql DbType: fixtures.Postgres, FixturesDir: "fixtures", @@ -592,6 +603,55 @@ tables: - created_at: $eval(NOW()) ``` +### Aerospike + +Для хранилища Aerospike также поддерживается заливка тестовых данных. Для этого важно не забыть при запуске gonkey как CLI-приложение использовать флаг `-db-type aerospike`, а при использовании в качестве библиотеки в конфигурации раннера: `DbType: fixtures.Aerospike`. + +Формат файлов с фикстурами для аэроспайка отличается, но смысл остаётся прежним: +```yaml +sets: + set1: + key1: + bin1: "value1" + bin2: 1 + key2: + bin1: "value2" + bin2: 2 + bin3: 2.569947773654566473 + set2: + key1: + bin4: false + bin5: null + bin1: '"' + key2: + bin1: "'" + bin5: + - 1 + - '2' +``` + +Также поддерживаются шаблоны: +```yaml +templates: + base_tmpl: + bin1: value1 + extended_tmpl: + $extend: base_tmpl + bin2: value2 + +sets: + set1: + key1: + $extend: base_tmpl + bin1: overwritten + set2: + key1: + $extend: extended_tmpl + bin2: overwritten +``` + +Связывание записей и выражения на данный момент не поддерживаются. + ## Моки Чтобы для тестов имитировать ответы от внешних сервисов, применяются моки. @@ -605,12 +665,12 @@ tables: ```go // создаем пустые моки сервисов m := mocks.NewNop( - "cart", - "loyalty", - "catalog", - "madmin", - "okz", - "discounts", + "cart", + "loyalty", + "catalog", + "madmin", + "okz", + "discounts", ) // запускаем моки diff --git a/README.md b/README.md index 7b2e31e..c82e311 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Capabilities: - works with REST/JSON API - tests service API for compliance with OpenAPI-specs -- seeds the DB with fixtures data (supports PostgreSQL) +- seeds the DB with fixtures data (supports PostgreSQL, MySQL, Aerospike) - provides mocks for external services - can be used as a library and ran together with unit-tests - stores the results as an [Allure](http://allure.qatools.ru/) report @@ -34,6 +34,7 @@ Capabilities: - [Record inheritance](#record-inheritance) - [Record linking](#record-linking) - [Expressions](#expressions) + - [Aerospike](#aerospike) - [Mocks](#mocks) - [Running mocks while using gonkey as a library](#running-mocks-while-using-gonkey-as-a-library) - [Mocks definition in the test file](#mocks-definition-in-the-test-file) @@ -44,6 +45,7 @@ Capabilities: - [Script definition](#script-definition) - [Running a script with parameterization](#running-a-script-with-parameterization) - [A DB query](#a-db-query) + - [Test Format](#test-format) - [Query definition](#query-definition) - [Definition of DB request response](#definition-of-db-request-response) - [DB request parameterization](#db-request-parameterization) @@ -58,6 +60,8 @@ To test a service located on a remote host, use gonkey as a console util. - `-spec <...>` path to a file or URL with the swagger-specs for the service - `-host <...>` service host:port - `-tests <...>` test file or directory +- `-db-type <...>` - database type. PostgreSQL and Aerospike are currently supported. +- `-aerospike_host <...>` when using Aerospike - connection URL in form of `host:port/namespace` - `-db_dsn <...>` DSN for the test DB (the DB will be cleared before seeding!), supports only PostgreSQL - `-fixtures <...>` fixtures directory - `-allure` generate an Allure-report @@ -76,8 +80,8 @@ Import gonkey as a dependency to your project in this file. ```go import ( - "github.com/lamoda/gonkey/runner" - "github.com/lamoda/gonkey/mocks" + "github.com/lamoda/gonkey/runner" + "github.com/lamoda/gonkey/mocks" ) ``` @@ -91,6 +95,9 @@ func TestFuncCases(t *testing.T) { // init the DB to load the fixtures if needed (details below) //db := ... + // init Aerospike to load the fixtures if needed (details below) + //aerospikeClient := ... + // create a server instance of your app srv := server.NewServer() defer srv.Close() @@ -101,6 +108,10 @@ func TestFuncCases(t *testing.T) { TestsDir: "cases", Mocks: m, DB: db, + Aerospike: runner.Aerospike{ + Client: aerospikeClient, + Namespace: "test", + } // Type of database, can be fixtures.Postgres or fixtures.Mysql // if DB parameter present, by default uses fixtures.Postgres database type DbType: fixtures.Postgres, @@ -594,6 +605,57 @@ tables: - created_at: $eval(NOW()) ``` +### Aerospike + +Fixtures for Aerospike are also supported. While using gonkey as CLI application do not forget the flag `-db-type aerospike`; add `DbType: fixtures.Aerospike` to runner's configuration if gonkey is used as library. + +Fixtures files format is a bit different, yet the same basic principles applies: + +```yaml +sets: + set1: + key1: + bin1: "value1" + bin2: 1 + key2: + bin1: "value2" + bin2: 2 + bin3: 2.569947773654566473 + set2: + key1: + bin4: false + bin5: null + bin1: '"' + key2: + bin1: "'" + bin5: + - 1 + - '2' +``` + +Fixtures templates are also supported: + +```yaml +templates: + base_tmpl: + bin1: value1 + extended_tmpl: + $extend: base_tmpl + bin2: value2 + +sets: + set1: + key1: + $extend: base_tmpl + bin1: overwritten + set2: + key1: + $extend: extended_tmpl + bin2: overwritten +``` + +Records linking and expressions are currently not supported. + ## Mocks In order to imitate responses from external services, use mocks. @@ -607,12 +669,12 @@ Before running tests, all planned mocks are started. It means that gonkey spins ```go // create empty server mocks m := mocks.NewNop( - "cart", - "loyalty", - "catalog", - "madmin", - "okz", - "discounts", + "cart", + "loyalty", + "catalog", + "madmin", + "okz", + "discounts", ) // spin up mocks diff --git a/checker/addons/openapi2_compliance/checker.go b/checker/addons/openapi2_compliance/checker.go index 313e0a0..5e2de35 100644 --- a/checker/addons/openapi2_compliance/checker.go +++ b/checker/addons/openapi2_compliance/checker.go @@ -15,8 +15,6 @@ import ( ) type ResponseSchemaChecker struct { - checker.CheckerInterface - swagger *spec.Swagger } diff --git a/checker/response_body/response_body.go b/checker/response_body/response_body.go index 82e0c16..f347a24 100644 --- a/checker/response_body/response_body.go +++ b/checker/response_body/response_body.go @@ -11,9 +11,7 @@ import ( "github.com/lamoda/gonkey/models" ) -type ResponseBodyChecker struct { - checker.CheckerInterface -} +type ResponseBodyChecker struct{} func NewChecker() checker.CheckerInterface { return &ResponseBodyChecker{} diff --git a/checker/response_db/response_db.go b/checker/response_db/response_db.go index 384457e..30ba3ea 100644 --- a/checker/response_db/response_db.go +++ b/checker/response_db/response_db.go @@ -15,8 +15,6 @@ import ( ) type ResponseDbChecker struct { - checker.CheckerInterface - db *sql.DB } diff --git a/checker/response_header/response_header.go b/checker/response_header/response_header.go index 504b80f..316d525 100644 --- a/checker/response_header/response_header.go +++ b/checker/response_header/response_header.go @@ -9,9 +9,7 @@ import ( "github.com/lamoda/gonkey/models" ) -type ResponseHeaderChecker struct { - checker.CheckerInterface -} +type ResponseHeaderChecker struct{} func NewChecker() checker.CheckerInterface { return &ResponseHeaderChecker{} diff --git a/compare/compare_test.go b/compare/compare_test.go index 4d21545..f41042f 100644 --- a/compare/compare_test.go +++ b/compare/compare_test.go @@ -3832,7 +3832,7 @@ var complexJson1 = ` "example": false }, "is_autoreserve_allowed": { - "description": "If True, the choosen interval gets to be reserved right after the order is created", + "description": "If True, the chosen interval gets to be reserved right after the order is created", "type": "boolean" }, "has_horizon": { @@ -8744,7 +8744,7 @@ var complexJson2 = ` "example": false }, "is_autoreserve_allowed": { - "description": "If True, the choosen interval gets to be reserved right after the order is created", + "description": "If True, the chosen interval gets to be reserved right after the order is created", "type": "boolean" }, "has_horizon": { diff --git a/examples/with-db-example/Makefile b/examples/with-db-example/Makefile index 500e606..db7e841 100644 --- a/examples/with-db-example/Makefile +++ b/examples/with-db-example/Makefile @@ -1,6 +1,6 @@ .PHONY: setup -setup: - @docker-compose -f docker-compose.yaml up --build -d +setup: teardown + @docker-compose -f docker-compose.yaml up --build --wait -d @curl http://localhost:5000/info/10 .PHONY: teardown @@ -8,6 +8,11 @@ teardown: @docker-compose -f docker-compose.yaml down -v --remove-orphans .PHONY: test -test: setup - gonkey -db_dsn "postgresql://testing_user:testing_password@localhost:5432/testing_db?sslmode=disable" -debug -host http://localhost:5000 -tests ./cases/ +test-postgres: setup + ./gonkey -db_dsn "postgresql://testing_user:testing_password@localhost:5432/testing_db?sslmode=disable" -debug -host http://localhost:5000 -tests ./cases/postgres make teardown + +.PHONY: test-aerospike +test-aerospike: setup + ./gonkey -debug -fixtures ./fixtures/ -db-type aerospike -aerospike_host "localhost:3000/test" -host http://localhost:5000 -tests ./cases/aerospike + make teardown \ No newline at end of file diff --git a/examples/with-db-example/cases/aerospike/aerospike.yaml b/examples/with-db-example/cases/aerospike/aerospike.yaml new file mode 100644 index 0000000..5c317ad --- /dev/null +++ b/examples/with-db-example/cases/aerospike/aerospike.yaml @@ -0,0 +1,7 @@ +- name: Get a number from aerospike + method: GET + path: /aerospike/ + fixtures: + - aerospike.yaml + response: + 200: '{"data": {"bin1": "value1", "bin2": "overwritten"}}' \ No newline at end of file diff --git a/examples/with-db-example/cases/database-with-vars.yaml b/examples/with-db-example/cases/postgres/database-with-vars.yaml similarity index 100% rename from examples/with-db-example/cases/database-with-vars.yaml rename to examples/with-db-example/cases/postgres/database-with-vars.yaml diff --git a/examples/with-db-example/cases/focused-tests.yaml b/examples/with-db-example/cases/postgres/focused-tests.yaml similarity index 100% rename from examples/with-db-example/cases/focused-tests.yaml rename to examples/with-db-example/cases/postgres/focused-tests.yaml diff --git a/examples/with-db-example/cases/skipped-and-broken-tests.yaml b/examples/with-db-example/cases/postgres/skipped-and-broken-tests.yaml similarity index 100% rename from examples/with-db-example/cases/skipped-and-broken-tests.yaml rename to examples/with-db-example/cases/postgres/skipped-and-broken-tests.yaml diff --git a/examples/with-db-example/docker-compose.yaml b/examples/with-db-example/docker-compose.yaml index 3c35560..d1a4c74 100644 --- a/examples/with-db-example/docker-compose.yaml +++ b/examples/with-db-example/docker-compose.yaml @@ -17,6 +17,13 @@ services: healthcheck: test: "pg_isready -U postgres" + aerospike: + image: aerospike/aerospike-server:5.6.0.5 + ports: + - "3000:3000" + environment: + - NAMESPACE=test + svc: build: context: . @@ -31,8 +38,10 @@ services: - APP_POSTGRES_PASS=testing_password - APP_POSTGRES_DB=testing_db depends_on: + aerospike: + condition: service_started postgres: condition: service_healthy volumes: - postgres-db: \ No newline at end of file + postgres-db: diff --git a/examples/with-db-example/fixtures/aerospike.yaml b/examples/with-db-example/fixtures/aerospike.yaml new file mode 100644 index 0000000..34dbb7a --- /dev/null +++ b/examples/with-db-example/fixtures/aerospike.yaml @@ -0,0 +1,16 @@ +templates: + base_tmpl: + bin1: value1 + extended_tmpl: + $extend: base_tmpl + bin2: value2 + +sets: + set1: + key1: + $extend: base_tmpl + bin1: overwritten + set2: + key1: + $extend: extended_tmpl + bin2: overwritten \ No newline at end of file diff --git a/examples/with-db-example/requirements.txt b/examples/with-db-example/requirements.txt new file mode 100644 index 0000000..e7528e2 --- /dev/null +++ b/examples/with-db-example/requirements.txt @@ -0,0 +1,2 @@ +psycopg2-binary==2.9.3 +aerospike==7.0.2 \ No newline at end of file diff --git a/examples/with-db-example/server.dockerfile b/examples/with-db-example/server.dockerfile index 4a9afdd..1dac4e9 100644 --- a/examples/with-db-example/server.dockerfile +++ b/examples/with-db-example/server.dockerfile @@ -1,4 +1,6 @@ -FROM python:3.7.9 +FROM python:3.10.5 + +COPY requirements.txt /app/requirements.txt +RUN pip install -r /app/requirements.txt -RUN pip install -U psycopg2-binary --no-cache-dir COPY server.py /app/server.py diff --git a/examples/with-db-example/server.py b/examples/with-db-example/server.py index 116c123..fed29a8 100644 --- a/examples/with-db-example/server.py +++ b/examples/with-db-example/server.py @@ -1,20 +1,22 @@ import http.server -import random import json import os +import random import socketserver from http import HTTPStatus +import aerospike import psycopg2 class Handler(http.server.SimpleHTTPRequestHandler): - def get_response(self) -> dict: if self.path.startswith('/info/'): response = self.get_info() elif self.path.startswith('/randint/'): response = self.get_rand_num() + elif self.path.startswith('/aerospike/'): + response = self.get_from_aerospike() else: response = {'non-existing': True} @@ -24,12 +26,15 @@ def get_info(self) -> dict: info_id = self.path.split('/')[-1] return { 'result_id': info_id, - 'query_result': storage.get_sql_result('SELECT id, name FROM testing LIMIT 2'), + 'query_result': postgres.get_sql_result('SELECT id, name FROM testing LIMIT 2'), } def get_rand_num(self) -> dict: return {'num': {'generated': str(random.randint(0, 100))}} + def get_from_aerospike(self) -> dict: + return {'data': aerospike.get()} + def do_GET(self): # заголовки ответа self.send_response(HTTPStatus.OK) @@ -53,9 +58,11 @@ def __init__(self): self.cursor = self.conn.cursor() def apply_migrations(self): - self.cursor.execute(""" + self.cursor.execute( + """ CREATE TABLE IF NOT EXISTS testing (id SERIAL PRIMARY KEY, name VARCHAR(200) NOT NULL); - """) + """ + ) self.conn.commit() self.cursor.executemany( "INSERT INTO testing (name) VALUES (%(name)s);", @@ -70,8 +77,22 @@ def get_sql_result(self, sql_str): return query_data -storage = PostgresStorage() -storage.apply_migrations() +class AerospikeStorage: + def __init__(self): + config = {'hosts': [('aerospike', 3000)]} + self.client = aerospike.client(config).connect() + + def get(self): + # Records are addressable via a tuple of (namespace, set, key) + key = ('test', 'set2', 'key1') + key, metadata, record = self.client.get(key) + return record + + +postgres = PostgresStorage() +postgres.apply_migrations() + +aerospike = AerospikeStorage() if __name__ == '__main__': service = socketserver.TCPServer(('', 5000), Handler) diff --git a/fixtures/aerospike/aerospike.go b/fixtures/aerospike/aerospike.go new file mode 100644 index 0000000..e9439b0 --- /dev/null +++ b/fixtures/aerospike/aerospike.go @@ -0,0 +1,285 @@ +package aerospike + +import ( + "encoding/json" + "errors" + "fmt" + "io/ioutil" + "os" + + "gopkg.in/yaml.v2" +) + +type aerospikeClient interface { + Truncate(set string) error + InsertBinMap(set, key string, binMap map[string]interface{}) error +} + +type LoaderAerospike struct { + client aerospikeClient + location string + debug bool +} + +type binMap map[string]interface{} +type set map[string]binMap + +type fixture struct { + Inherits []string + Sets yaml.MapSlice + Templates yaml.MapSlice +} + +type loadedSet struct { + name string + data set +} +type loadContext struct { + files []string + sets []loadedSet + refsDefinition set +} + +func New(client aerospikeClient, location string, debug bool) *LoaderAerospike { + return &LoaderAerospike{ + client: client, + location: location, + debug: debug, + } +} + +func (l *LoaderAerospike) Load(names []string) error { + ctx := loadContext{ + refsDefinition: make(set), + } + + // Gather data from files. + for _, name := range names { + err := l.loadFile(name, &ctx) + if err != nil { + return fmt.Errorf("unable to load fixture %s: %s", name, err.Error()) + } + } + return l.loadSets(&ctx) +} + +func (l *LoaderAerospike) loadFile(name string, ctx *loadContext) error { + candidates := []string{ + l.location + "/" + name, + l.location + "/" + name + ".yml", + l.location + "/" + name + ".yaml", + } + var err error + var file string + for _, candidate := range candidates { + if _, err = os.Stat(candidate); err == nil { + file = candidate + break + } + } + if err != nil { + return err + } + // skip previously loaded files + if inArray(file, ctx.files) { + return nil + } + if l.debug { + fmt.Println("Loading", file) + } + data, err := ioutil.ReadFile(file) + if err != nil { + return err + } + ctx.files = append(ctx.files, file) + return l.loadYml(data, ctx) +} + +func (l *LoaderAerospike) loadYml(data []byte, ctx *loadContext) error { + // read yml into struct + var loadedFixture fixture + if err := yaml.Unmarshal(data, &loadedFixture); err != nil { + return err + } + + // load inherits + for _, inheritFile := range loadedFixture.Inherits { + if err := l.loadFile(inheritFile, ctx); err != nil { + return err + } + } + + // loadedFixture.templates + // yaml.MapSlice{ + // string => yaml.MapSlice{ --- template name + // string => interface{} --- bin name: value + // } + // } + for _, template := range loadedFixture.Templates { + name := template.Key.(string) + if _, ok := ctx.refsDefinition[name]; ok { + return fmt.Errorf("unable to load template %s: duplicating ref name", name) + } + + binMap, err := binMapFromYaml(template) + if err != nil { + return err + } + + if base, ok := binMap["$extend"]; ok { + baseName := base.(string) + baseBinMap, err := l.resolveReference(ctx.refsDefinition, baseName) + if err != nil { + return err + } + for k, v := range binMap { + baseBinMap[k] = v + } + binMap = baseBinMap + } + ctx.refsDefinition[name] = binMap + if l.debug { + marshalled, _ := json.Marshal(binMap) + fmt.Printf("Populating ref %s as %s from template\n", name, string(marshalled)) + } + } + + // loadedFixture.sets + // yaml.MapSlice{ + // string => yaml.MapSlice{ --- set name + // string => yaml.MapSlice{ --- key name + // string => interface{} --- bin name: value + // } + // } + // } + for _, yamlSet := range loadedFixture.Sets { + set, err := setFromYaml(yamlSet) + if err != nil { + return err + } + lt := loadedSet{ + name: yamlSet.Key.(string), + data: set, + } + ctx.sets = append(ctx.sets, lt) + } + return nil +} + +func setFromYaml(mapItem yaml.MapItem) (set, error) { + entries, ok := mapItem.Value.(yaml.MapSlice) + if !ok { + return nil, errors.New("expected map/array as set") + } + + set := make(set, len(entries)) + for _, e := range entries { + key := e.Key.(string) + binmap, err := binMapFromYaml(e) + if err != nil { + return nil, err + } + set[key] = binmap + } + + return set, nil +} + +func binMapFromYaml(mapItem yaml.MapItem) (binMap, error) { + bins, ok := mapItem.Value.(yaml.MapSlice) + if !ok { + return nil, errors.New("expected map/array as binmap") + } + + binmap := make(binMap, len(bins)) + for j := range bins { + binmap[bins[j].Key.(string)] = bins[j].Value + } + + return binmap, nil +} + +func (l *LoaderAerospike) loadSets(ctx *loadContext) error { + // truncate first + truncatedSets := make(map[string]bool) + for _, s := range ctx.sets { + if _, ok := truncatedSets[s.name]; ok { + // already truncated + continue + } + if err := l.truncateSet(s.name); err != nil { + return err + } + truncatedSets[s.name] = true + } + + // then load data + for _, s := range ctx.sets { + if len(s.data) == 0 { + continue + } + if err := l.loadSet(ctx, s); err != nil { + return fmt.Errorf("failed to load set '%s' because:\n%s", s.name, err) + } + } + + return nil +} + +// truncateTable truncates table +func (l *LoaderAerospike) truncateSet(name string) error { + return l.client.Truncate(name) +} + +func (l *LoaderAerospike) loadSet(ctx *loadContext, set loadedSet) error { + // $extend keyword allows to import values from a named row + for key, binMap := range set.data { + if base, ok := binMap["$extend"]; ok { + baseName := base.(string) + baseBinMap, err := l.resolveReference(ctx.refsDefinition, baseName) + if err != nil { + return err + } + for k, v := range binMap { + baseBinMap[k] = v + } + set.data[key] = baseBinMap + } + } + + for key, binmap := range set.data { + err := l.client.InsertBinMap(set.name, key, binmap) + if err != nil { + return err + } + } + + return nil +} + +// resolveReference finds previously stored reference by its name +func (f *LoaderAerospike) resolveReference(refs set, refName string) (binMap, error) { + target, ok := refs[refName] + if !ok { + return nil, fmt.Errorf("undefined reference %s", refName) + } + // make a copy of referencing data to prevent spoiling the source + // by the way removing $-records from base row + targetCopy := make(binMap, len(target)) + for k, v := range target { + if len(k) == 0 || k[0] != '$' { + targetCopy[k] = v + } + } + return targetCopy, nil +} + +// inArray checks whether the needle is present in haystack slice +func inArray(needle string, haystack []string) bool { + for _, e := range haystack { + if needle == e { + return true + } + } + return false +} diff --git a/fixtures/aerospike/aerospike_test.go b/fixtures/aerospike/aerospike_test.go new file mode 100644 index 0000000..6fd71f3 --- /dev/null +++ b/fixtures/aerospike/aerospike_test.go @@ -0,0 +1,122 @@ +package aerospike + +import ( + "io/ioutil" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestLoaderAerospike_loadYml(t *testing.T) { + type args struct { + data []byte + ctx *loadContext + } + tests := []struct { + name string + args args + want loadContext + }{ + { + name: "basic", + args: args{ + data: loadTestData(t, "../testdata/aerospike.yaml"), + ctx: &loadContext{ + refsDefinition: make(set), + }, + }, + want: loadContext{ + refsDefinition: set{}, + sets: []loadedSet{ + { + name: "set1", + data: set{ + "key1": { + "bin1": "value1", + "bin2": 1, + }, + "key2": { + "bin1": "value2", + "bin2": 2, + "bin3": 2.569947773654566473, + }, + }, + }, + { + name: "set2", + data: set{ + "key1": { + "bin1": `"`, + "bin4": false, + "bin5": nil, + }, + "key2": { + "bin1": "'", + "bin5": []interface{}{1, "2"}, + }, + }, + }, + }, + }, + }, + { + name: "extend", + args: args{ + data: loadTestData(t, "../testdata/aerospike_extend.yaml"), + ctx: &loadContext{ + refsDefinition: make(set), + }, + }, + want: loadContext{ + sets: []loadedSet{ + { + name: "set1", + data: set{ + "key1": { + "$extend": "base_tmpl", + "bin1": "overwritten", + }, + }, + }, + { + name: "set2", + data: set{ + "key1": { + "$extend": "extended_tmpl", + "bin2": "overwritten", + }, + }, + }, + }, + refsDefinition: set{ + "base_tmpl": { + "bin1": "value1", + }, + "extended_tmpl": { + "$extend": "base_tmpl", + "bin1": "value1", + "bin2": "value2", + }, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f := &LoaderAerospike{} + if err := f.loadYml(tt.args.data, tt.args.ctx); err != nil { + t.Errorf("LoaderAerospike.loadYml() error = %v", err) + } + + require.Equal(t, tt.want, *tt.args.ctx) + }) + } +} + +func loadTestData(t *testing.T, path string) []byte { + aerospikeYaml, err := ioutil.ReadFile(path) + if err != nil { + t.Error("No aerospike.yaml") + } + return aerospikeYaml +} diff --git a/fixtures/loader.go b/fixtures/loader.go index 50f8a4b..d164cb7 100644 --- a/fixtures/loader.go +++ b/fixtures/loader.go @@ -6,8 +6,10 @@ import ( _ "github.com/lib/pq" + "github.com/lamoda/gonkey/fixtures/aerospike" "github.com/lamoda/gonkey/fixtures/mysql" "github.com/lamoda/gonkey/fixtures/postgres" + aerospikeClient "github.com/lamoda/gonkey/storage/aerospike" ) type DbType int @@ -15,18 +17,21 @@ type DbType int const ( Postgres DbType = iota Mysql + Aerospike ) const ( - PostgresParam = "postgres" - MysqlParam = "mysql" + PostgresParam = "postgres" + MysqlParam = "mysql" + AerospikeParam = "aerospike" ) type Config struct { - DB *sql.DB - DbType DbType - Location string - Debug bool + DB *sql.DB + Aerospike *aerospikeClient.Client + DbType DbType + Location string + Debug bool } type Loader interface { @@ -52,6 +57,12 @@ func NewLoader(cfg *Config) Loader { location, cfg.Debug, ) + case Aerospike: + loader = aerospike.New( + cfg.Aerospike, + location, + cfg.Debug, + ) default: panic("unknown db type") } @@ -65,6 +76,8 @@ func FetchDbType(dbType string) DbType { return Postgres case MysqlParam: return Mysql + case AerospikeParam: + return Aerospike default: panic("unknown db type param") } diff --git a/fixtures/mysql/mysql.go b/fixtures/mysql/mysql.go index 98c6c48..65232cb 100644 --- a/fixtures/mysql/mysql.go +++ b/fixtures/mysql/mysql.go @@ -30,15 +30,14 @@ type table []row type rowsDict map[string]row type fixture struct { - Version string Inherits []string Tables yaml.MapSlice Templates yaml.MapSlice } type loadedTable struct { - Name string - Rows table + name string + rows table } type loadContext struct { @@ -94,7 +93,7 @@ func (l *LoaderMysql) loadFile(name string, ctx *loadContext) error { } // skip previously loaded files - if inArray(file, &(*ctx).files) { + if inArray(file, ctx.files) { return nil } @@ -104,7 +103,7 @@ func (l *LoaderMysql) loadFile(name string, ctx *loadContext) error { if err != nil { return err } - (*ctx).files = append((*ctx).files, file) + ctx.files = append(ctx.files, file) return l.loadYml(data, ctx) } @@ -170,10 +169,10 @@ func (l *LoaderMysql) loadYml(data []byte, ctx *loadContext) error { rows[i] = fields } lt := loadedTable{ - Name: sourceTable.Key.(string), - Rows: rows, + name: sourceTable.Key.(string), + rows: rows, } - (*ctx).tables = append((*ctx).tables, lt) + ctx.tables = append(ctx.tables, lt) } return nil } @@ -188,22 +187,22 @@ func (l *LoaderMysql) loadTables(ctx *loadContext) error { // truncate first truncatedTables := make(map[string]bool) for _, lt := range ctx.tables { - if _, ok := truncatedTables[lt.Name]; ok { + if _, ok := truncatedTables[lt.name]; ok { // already truncated continue } - if err := l.truncateTable(tx, lt.Name); err != nil { + if err := l.truncateTable(tx, lt.name); err != nil { return err } - truncatedTables[lt.Name] = true + truncatedTables[lt.name] = true } // then load data for _, lt := range ctx.tables { - if len(lt.Rows) == 0 { + if len(lt.rows) == 0 { continue } - if err := l.loadTable(tx, ctx, lt.Name, lt.Rows); err != nil { + if err := l.loadTable(tx, ctx, lt.name, lt.rows); err != nil { return err } } @@ -502,8 +501,8 @@ func (l *LoaderMysql) resolveFieldReference(refs rowsDict, ref string) (interfac } // inArray checks whether the needle is present in haystack slice -func inArray(needle string, haystack *[]string) bool { - for _, e := range *haystack { +func inArray(needle string, haystack []string) bool { + for _, e := range haystack { if needle == e { return true } diff --git a/fixtures/mysql/mysql_test.go b/fixtures/mysql/mysql_test.go index 8c20acc..4727d0f 100644 --- a/fixtures/mysql/mysql_test.go +++ b/fixtures/mysql/mysql_test.go @@ -15,7 +15,7 @@ import ( func TestBuildInsertQuery(t *testing.T) { - ymlFile, err := ioutil.ReadFile("../testdata/table.yaml") + ymlFile, err := ioutil.ReadFile("../testdata/sql.yaml") require.NoError(t, err) expected := []string{ @@ -36,7 +36,7 @@ func TestBuildInsertQuery(t *testing.T) { l.loadYml(ymlFile, &ctx), ) - for i, row := range ctx.tables[0].Rows { + for i, row := range ctx.tables[0].rows { query, err := l.buildInsertQuery(&ctx, "table", row) require.NoError(t, err) @@ -45,7 +45,7 @@ func TestBuildInsertQuery(t *testing.T) { } func TestLoadTablesShouldResolveRefs(t *testing.T) { - yml, err := ioutil.ReadFile("../testdata/table_refs.yaml") + yml, err := ioutil.ReadFile("../testdata/sql_refs.yaml") require.NoError(t, err) db, mock, err := sqlmock.New() @@ -111,7 +111,7 @@ func TestLoadTablesShouldResolveRefs(t *testing.T) { } func TestLoadTablesShouldExtendRows(t *testing.T) { - yml, err := ioutil.ReadFile("../testdata/table_extend.yaml") + yml, err := ioutil.ReadFile("../testdata/sql_extend.yaml") require.NoError(t, err) db, mock, err := sqlmock.New() diff --git a/fixtures/postgres/postgres.go b/fixtures/postgres/postgres.go index 089cd75..dd2ff9f 100644 --- a/fixtures/postgres/postgres.go +++ b/fixtures/postgres/postgres.go @@ -28,19 +28,18 @@ type table []row type rowsDict map[string]row type fixture struct { - Version string Inherits []string Tables yaml.MapSlice Templates yaml.MapSlice } type loadedTable struct { - Name tableName - Rows table + name tableName + rows table } type tableName struct { - Name string - Schema string + name string + schema string } func newTableName(source string) tableName { @@ -52,12 +51,12 @@ func newTableName(source string) tableName { case parts[0] == "": parts[0] = "public" } - lt := tableName{Schema: parts[0], Name: parts[1]} + lt := tableName{schema: parts[0], name: parts[1]} return lt } func (t *tableName) getFullName() string { - return fmt.Sprintf("\"%s\".\"%s\"", t.Schema, t.Name) + return fmt.Sprintf("\"%s\".\"%s\"", t.schema, t.name) } type loadContext struct { @@ -108,7 +107,7 @@ func (f *LoaderPostgres) loadFile(name string, ctx *loadContext) error { return err } // skip previously loaded files - if inArray(file, &(*ctx).files) { + if inArray(file, ctx.files) { return nil } if f.debug { @@ -118,7 +117,7 @@ func (f *LoaderPostgres) loadFile(name string, ctx *loadContext) error { if err != nil { return err } - (*ctx).files = append((*ctx).files, file) + ctx.files = append(ctx.files, file) return f.loadYml(data, ctx) } @@ -136,7 +135,7 @@ func (f *LoaderPostgres) loadYml(data []byte, ctx *loadContext) error { } } - // loadedFixture.Templates + // loadedFixture.templates // yaml.MapSlice{ // string => yaml.MapSlice{ // string => interface{} @@ -172,7 +171,7 @@ func (f *LoaderPostgres) loadYml(data []byte, ctx *loadContext) error { } } - // loadedFixture.Tables + // loadedFixture.tables // yaml.MapSlice{ // string => []interface{ // yaml.MapSlice{ @@ -195,10 +194,10 @@ func (f *LoaderPostgres) loadYml(data []byte, ctx *loadContext) error { rows[i] = fields } lt := loadedTable{ - Name: newTableName(sourceTable.Key.(string)), - Rows: rows, + name: newTableName(sourceTable.Key.(string)), + rows: rows, } - (*ctx).tables = append((*ctx).tables, lt) + ctx.tables = append(ctx.tables, lt) } return nil } @@ -213,22 +212,22 @@ func (f *LoaderPostgres) loadTables(ctx *loadContext) error { // truncate first truncatedTables := make(map[string]bool) for _, lt := range ctx.tables { - if _, ok := truncatedTables[lt.Name.getFullName()]; ok { + if _, ok := truncatedTables[lt.name.getFullName()]; ok { // already truncated continue } - if err := f.truncateTable(lt.Name); err != nil { + if err := f.truncateTable(lt.name); err != nil { return err } - truncatedTables[lt.Name.getFullName()] = true + truncatedTables[lt.name.getFullName()] = true } // then load data for _, lt := range ctx.tables { - if len(lt.Rows) == 0 { + if len(lt.rows) == 0 { continue } - if err := f.loadTable(ctx, lt.Name, lt.Rows); err != nil { - return fmt.Errorf("failed to load table '%s' because:\n%s", lt.Name, err) + if err := f.loadTable(ctx, lt.name, lt.rows); err != nil { + return fmt.Errorf("failed to load table '%s' because:\n%s", lt.name, err) } } // alter the sequences so they contain max id + 1 @@ -473,8 +472,8 @@ func (f *LoaderPostgres) resolveFieldReference(refs rowsDict, ref string) (inter } // inArray checks whether the needle is present in haystack slice -func inArray(needle string, haystack *[]string) bool { - for _, e := range *haystack { +func inArray(needle string, haystack []string) bool { + for _, e := range haystack { if needle == e { return true } @@ -519,4 +518,4 @@ func quoteLiteral(s string) string { s = strings.Replace(s, `'`, `''`, -1) s = strings.Replace(s, `\`, `\\`, -1) return p + `'` + s + `'` -} \ No newline at end of file +} diff --git a/fixtures/postgres/postgres_test.go b/fixtures/postgres/postgres_test.go index 82565fb..e03c32c 100644 --- a/fixtures/postgres/postgres_test.go +++ b/fixtures/postgres/postgres_test.go @@ -10,7 +10,7 @@ import ( ) func TestBuildInsertQuery(t *testing.T) { - yml, err := ioutil.ReadFile("../testdata/table.yaml") + yml, err := ioutil.ReadFile("../testdata/sql.yaml") require.NoError(t, err) expected := "INSERT INTO \"public\".\"table\" AS row (\"field1\", \"field2\", \"field3\", \"field4\", \"field5\") VALUES " + @@ -29,7 +29,7 @@ func TestBuildInsertQuery(t *testing.T) { err = l.loadYml(yml, &ctx) require.NoError(t, err) - query, err := l.buildInsertQuery(&ctx, newTableName("table"), ctx.tables[0].Rows) + query, err := l.buildInsertQuery(&ctx, newTableName("table"), ctx.tables[0].rows) if err != nil { t.Error("must not produce error, error:", err.Error()) @@ -43,7 +43,7 @@ func TestBuildInsertQuery(t *testing.T) { } func TestLoadTablesShouldResolveSchema(t *testing.T) { - yml, err := ioutil.ReadFile("../testdata/table_schema.yaml") + yml, err := ioutil.ReadFile("../testdata/sql_schema.yaml") require.NoError(t, err) db, mock, err := sqlmock.New() @@ -124,7 +124,7 @@ func TestLoadTablesShouldResolveSchema(t *testing.T) { } func TestLoadTablesShouldResolveRefs(t *testing.T) { - yml, err := ioutil.ReadFile("../testdata/table_refs.yaml") + yml, err := ioutil.ReadFile("../testdata/sql_refs.yaml") require.NoError(t, err) db, mock, err := sqlmock.New() @@ -205,7 +205,7 @@ func TestLoadTablesShouldResolveRefs(t *testing.T) { } func TestLoadTablesShouldExtendRows(t *testing.T) { - yml, err := ioutil.ReadFile("../testdata/table_extend.yaml") + yml, err := ioutil.ReadFile("../testdata/sql_extend.yaml") require.NoError(t, err) db, mock, err := sqlmock.New() diff --git a/fixtures/testdata/aerospike.yaml b/fixtures/testdata/aerospike.yaml new file mode 100644 index 0000000..abec015 --- /dev/null +++ b/fixtures/testdata/aerospike.yaml @@ -0,0 +1,19 @@ +sets: + set1: + key1: + bin1: "value1" + bin2: 1 + key2: + bin1: "value2" + bin2: 2 + bin3: 2.569947773654566473 + set2: + key1: + bin4: false + bin5: null + bin1: '"' + key2: + bin1: "'" + bin5: + - 1 + - '2' \ No newline at end of file diff --git a/fixtures/testdata/aerospike_extend.yaml b/fixtures/testdata/aerospike_extend.yaml new file mode 100644 index 0000000..34dbb7a --- /dev/null +++ b/fixtures/testdata/aerospike_extend.yaml @@ -0,0 +1,16 @@ +templates: + base_tmpl: + bin1: value1 + extended_tmpl: + $extend: base_tmpl + bin2: value2 + +sets: + set1: + key1: + $extend: base_tmpl + bin1: overwritten + set2: + key1: + $extend: extended_tmpl + bin2: overwritten \ No newline at end of file diff --git a/fixtures/testdata/table.yaml b/fixtures/testdata/sql.yaml similarity index 100% rename from fixtures/testdata/table.yaml rename to fixtures/testdata/sql.yaml diff --git a/fixtures/testdata/table_extend.yaml b/fixtures/testdata/sql_extend.yaml similarity index 100% rename from fixtures/testdata/table_extend.yaml rename to fixtures/testdata/sql_extend.yaml diff --git a/fixtures/testdata/table_refs.yaml b/fixtures/testdata/sql_refs.yaml similarity index 100% rename from fixtures/testdata/table_refs.yaml rename to fixtures/testdata/sql_refs.yaml diff --git a/fixtures/testdata/table_schema.yaml b/fixtures/testdata/sql_schema.yaml similarity index 100% rename from fixtures/testdata/table_schema.yaml rename to fixtures/testdata/sql_schema.yaml diff --git a/go.mod b/go.mod index 91efad1..90fc9e1 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.14 require ( github.com/Masterminds/sprig/v3 v3.2.2 + github.com/aerospike/aerospike-client-go/v5 v5.8.0 github.com/fatih/color v1.7.0 github.com/google/uuid v1.1.1 github.com/huandu/xstrings v1.3.2 // indirect @@ -13,10 +14,13 @@ require ( github.com/lib/pq v1.3.0 github.com/mattn/go-colorable v0.1.12 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect + github.com/onsi/ginkgo v1.16.5 // indirect + github.com/onsi/gomega v1.19.0 // indirect github.com/stretchr/testify v1.7.1 github.com/tidwall/gjson v1.13.0 + github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64 // indirect golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 - gopkg.in/yaml.v2 v2.3.0 + gopkg.in/yaml.v2 v2.4.0 ) diff --git a/go.sum b/go.sum index d9e24f4..f70ed36 100644 --- a/go.sum +++ b/go.sum @@ -4,16 +4,43 @@ github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030I github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= github.com/Masterminds/sprig/v3 v3.2.2 h1:17jRggJu518dr3QaafizSXOjKYp94wKfABxUmyxvxX8= github.com/Masterminds/sprig/v3 v3.2.2/go.mod h1:UoaO7Yp8KlPnJIYWTFkMaqPUYKTfGFPhxNuwnnxkKlk= +github.com/aerospike/aerospike-client-go/v5 v5.8.0 h1:EUV2wG80yIenQqOyUlf5NfyhagPIwoeL09MJIE+xILE= +github.com/aerospike/aerospike-client-go/v5 v5.8.0/go.mod h1:rJ/KpmClE7kiBPfvAPrGw9WuNOiz8v2uKbQaUyYPXtI= +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw= github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= +github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= @@ -33,6 +60,22 @@ github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HK github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= +github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= +github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0= +github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= +github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= +github.com/onsi/ginkgo/v2 v2.1.3 h1:e/3Cwtogj0HA+25nMP1jCMDIf8RtRYbGwGGuBIFztkc= +github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c= +github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/onsi/gomega v1.15.0/go.mod h1:cIuvLEne0aoVhAgh/O6ac0Op8WWw9H6eYCriF+tEHG0= +github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY= +github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw= +github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= @@ -50,33 +93,95 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da/go.mod h1:E1AXubJBdNmFERAOucpDIxNzeGfLzg0mYh+UfMWdChA= +github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64 h1:5mLPGnFdSsevFRFc9q3yYbBkB6tsm4aCwwQV/j1JQAQ= +github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM= golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= +golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc= +golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo= +golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 h1:FVCohIoYO7IJoDDVpV2pdq7SgrMH6wHnuTyrdrxJNoY= gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0/go.mod h1:OdE7CF6DbADk7lN8LIKRzRJTTZXIjtWgA5THM5lhBAw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA= gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 6aeb2e9..01c818f 100644 --- a/main.go +++ b/main.go @@ -6,8 +6,10 @@ import ( "flag" "log" "os" + "strconv" "strings" + "github.com/aerospike/aerospike-client-go/v5" "github.com/joho/godotenv" "github.com/lamoda/gonkey/checker/response_body" @@ -16,104 +18,107 @@ import ( "github.com/lamoda/gonkey/output/allure_report" "github.com/lamoda/gonkey/output/console_colored" "github.com/lamoda/gonkey/runner" + aerospikeAdapter "github.com/lamoda/gonkey/storage/aerospike" "github.com/lamoda/gonkey/testloader/yaml_file" "github.com/lamoda/gonkey/variables" ) +type config struct { + Host string + TestsLocation string + DbDsn string + AerospikeHost string + FixturesLocation string + EnvFile string + Allure bool + Verbose bool + Debug bool + DbType string +} + +type storages struct { + db *sql.DB + aerospike *aerospikeAdapter.Client +} + func main() { - var config struct { - Host string - TestsLocation string - DbDsn string - FixturesLocation string - EnvFile string - Allure bool - Verbose bool - Debug bool - DbType string - } + cfg := getConfig() + validateConfig(&cfg) - flag.StringVar(&config.Host, "host", "", "Target system hostname") - flag.StringVar(&config.TestsLocation, "tests", "", "Path to tests file or directory") - flag.StringVar(&config.DbDsn, "db_dsn", "", "DSN for the fixtures database (WARNING! Db tables will be truncated)") - flag.StringVar(&config.FixturesLocation, "fixtures", "", "Path to fixtures directory") - flag.StringVar(&config.EnvFile, "env-file", "", "Path to env-file") - flag.BoolVar(&config.Allure, "allure", true, "Make Allure report") - flag.BoolVar(&config.Verbose, "v", false, "Verbose output") - flag.BoolVar(&config.Debug, "debug", false, "Debug output") - flag.StringVar( - &config.DbType, - "db-type", - fixtures.PostgresParam, - "Type of database (options: postgres, mysql)", - ) + storages := initStorages(cfg) - flag.Parse() + fixturesLoader := initLoaders(storages, cfg) - if config.Host == "" { - log.Fatal(errors.New("service hostname not provided")) - } else { - if !strings.HasPrefix(config.Host, "http://") && !strings.HasPrefix(config.Host, "https://") { - config.Host = "http://" + config.Host - } - config.Host = strings.TrimRight(config.Host, "/") - } + runner := initRunner(cfg, fixturesLoader) - if config.TestsLocation == "" { - log.Fatal(errors.New("no tests location provided")) - } + addCheckers(runner, storages.db) - var db *sql.DB - if config.DbDsn != "" { - var err error - db, err = sql.Open("postgres", config.DbDsn) - if err != nil { - log.Fatal(err) - } + run(runner, cfg) +} + +func initStorages(cfg config) storages { + db := initDB(cfg) + aerospikeClient := initAerospike(cfg) + return storages{ + db: db, + aerospike: aerospikeClient, } +} +func initLoaders(storages storages, cfg config) fixtures.Loader { var fixturesLoader fixtures.Loader - if db != nil && config.FixturesLocation != "" { + if (storages.db != nil || storages.aerospike != nil) && cfg.FixturesLocation != "" { fixturesLoader = fixtures.NewLoader(&fixtures.Config{ - DB: db, - Location: config.FixturesLocation, - Debug: config.Debug, - DbType: fixtures.FetchDbType(config.DbType), + DB: storages.db, + Aerospike: storages.aerospike, + Location: cfg.FixturesLocation, + Debug: cfg.Debug, + DbType: fixtures.FetchDbType(cfg.DbType), }) - } else if config.FixturesLocation != "" { + } else if cfg.FixturesLocation != "" { log.Fatal(errors.New("you should specify db_dsn to load fixtures")) } + return fixturesLoader +} - if config.EnvFile != "" { - if err := godotenv.Load(config.EnvFile); err != nil { +func validateConfig(cfg *config) { + if cfg.Host == "" { + log.Fatal(errors.New("service hostname not provided")) + } else { + if !strings.HasPrefix(cfg.Host, "http://") && !strings.HasPrefix(cfg.Host, "https://") { + cfg.Host = "http://" + cfg.Host + } + cfg.Host = strings.TrimRight(cfg.Host, "/") + } + + if cfg.TestsLocation == "" { + log.Fatal(errors.New("no tests location provided")) + } + + if cfg.EnvFile != "" { + if err := godotenv.Load(cfg.EnvFile); err != nil { log.Println(errors.New("can't load .env file"), err) } } +} - r := runner.New( - &runner.Config{ - Host: config.Host, - FixturesLoader: fixturesLoader, - Variables: variables.New(), - }, - yaml_file.NewLoader(config.TestsLocation), - ) +func addCheckers(r *runner.Runner, db *sql.DB) { + r.AddCheckers(response_body.NewChecker()) + if db != nil { + r.AddCheckers(response_db.NewChecker(db)) + } +} - consoleOutput := console_colored.NewOutput(config.Verbose) +func run(r *runner.Runner, cfg config) { + consoleOutput := console_colored.NewOutput(cfg.Verbose) r.AddOutput(consoleOutput) var allureOutput *allure_report.AllureReportOutput - if config.Allure { + if cfg.Allure { allureOutput = allure_report.NewOutput("Gonkey", "./allure-results") r.AddOutput(allureOutput) } - r.AddCheckers(response_body.NewChecker()) - - if db != nil { - r.AddCheckers(response_db.NewChecker(db)) - } - summary, err := r.Run() if err != nil { log.Fatal(err) @@ -129,3 +134,81 @@ func main() { os.Exit(1) } } + +func initRunner(cfg config, fixturesLoader fixtures.Loader) *runner.Runner { + return runner.New( + &runner.Config{ + Host: cfg.Host, + FixturesLoader: fixturesLoader, + Variables: variables.New(), + }, + yaml_file.NewLoader(cfg.TestsLocation), + ) +} + +func initAerospike(cfg config) *aerospikeAdapter.Client { + if cfg.AerospikeHost != "" { + address, port, namespace := parseAerospikeHost(cfg.AerospikeHost) + client, err := aerospike.NewClient(address, port) + if err != nil { + log.Fatal("Couldn't connect to aerospike: ", err) + } + return aerospikeAdapter.New(client, namespace) + } + + return nil +} + +func initDB(cfg config) *sql.DB { + if cfg.DbDsn != "" { + var err error + db, err := sql.Open("postgres", cfg.DbDsn) + if err != nil { + log.Fatal(err) + } + return db + } + + return nil +} + +func getConfig() config { + cfg := config{} + + flag.StringVar(&cfg.Host, "host", "", "Target system hostname") + flag.StringVar(&cfg.TestsLocation, "tests", "", "Path to tests file or directory") + flag.StringVar(&cfg.DbDsn, "db_dsn", "", "DSN for the fixtures database (WARNING! Db tables will be truncated)") + flag.StringVar(&cfg.AerospikeHost, "aerospike_host", "", "Aerospike host for fixtures in form of 'host:port/namespace' (WARNING! Aerospike sets will be truncated)") + flag.StringVar(&cfg.FixturesLocation, "fixtures", "", "Path to fixtures directory") + flag.StringVar(&cfg.EnvFile, "env-file", "", "Path to env-file") + flag.BoolVar(&cfg.Allure, "allure", true, "Make Allure report") + flag.BoolVar(&cfg.Verbose, "v", false, "Verbose output") + flag.BoolVar(&cfg.Debug, "debug", false, "Debug output") + flag.StringVar( + &cfg.DbType, + "db-type", + fixtures.PostgresParam, + "Type of database (options: postgres, mysql, aerospike)", + ) + + flag.Parse() + return cfg +} + +func parseAerospikeHost(dsn string) (address string, port int, namespace string) { + parts := strings.Split(dsn, "/") + if len(parts) != 2 { + log.Fatalf("couldn't parse aerospike host %v, should be in form of host:port/namespace", dsn) + } + namespace = parts[1] + + host := parts[0] + hostParts := strings.Split(host, ":") + address = hostParts[0] + port, err := strconv.Atoi(hostParts[1]) + if err != nil { + log.Fatal("couldn't parse port: " + parts[1]) + } + + return +} diff --git a/mocks/reply_strategy.go b/mocks/reply_strategy.go index deb7042..18634ee 100644 --- a/mocks/reply_strategy.go +++ b/mocks/reply_strategy.go @@ -19,8 +19,6 @@ type contextAwareStrategy interface { } type constantReply struct { - replyStrategy - replyBody []byte statusCode int headers map[string]string @@ -64,16 +62,13 @@ func (s *constantReply) HandleRequest(w http.ResponseWriter, r *http.Request) [] return nil } -type failReply struct { -} +type failReply struct{} func (s *failReply) HandleRequest(w http.ResponseWriter, r *http.Request) []error { return unhandledRequestError(r) } -type nopReply struct { - replyStrategy -} +type nopReply struct{} func (s *nopReply) HandleRequest(w http.ResponseWriter, r *http.Request) []error { w.WriteHeader(http.StatusNoContent) @@ -81,9 +76,6 @@ func (s *nopReply) HandleRequest(w http.ResponseWriter, r *http.Request) []error } type uriVaryReply struct { - replyStrategy - contextAwareStrategy - basePath string variants map[string]*definition } @@ -120,9 +112,6 @@ func (s *uriVaryReply) EndRunningContext() []error { } type methodVaryReply struct { - replyStrategy - contextAwareStrategy - variants map[string]*definition } @@ -198,9 +187,6 @@ func (s *sequentialReply) HandleRequest(w http.ResponseWriter, r *http.Request) type basedOnRequestReply struct { sync.Mutex - replyStrategy - contextAwareStrategy - variants []*definition } diff --git a/mocks/request_constraint.go b/mocks/request_constraint.go index 37d10ba..29a8387 100644 --- a/mocks/request_constraint.go +++ b/mocks/request_constraint.go @@ -22,9 +22,7 @@ type verifier interface { Verify(r *http.Request) []error } -type nopConstraint struct { - verifier -} +type nopConstraint struct {} func (c *nopConstraint) Verify(r *http.Request) []error { return nil @@ -68,8 +66,6 @@ func (c *bodyMatchesXMLConstraint) Verify(r *http.Request) []error { } type bodyMatchesJSONConstraint struct { - verifier - expectedBody interface{} compareParams compare.CompareParams } @@ -151,8 +147,6 @@ func (c *bodyJSONFieldMatchesJSONConstraint) Verify(r *http.Request) []error { } type methodConstraint struct { - verifier - method string } @@ -164,8 +158,6 @@ func (c *methodConstraint) Verify(r *http.Request) []error { } type headerConstraint struct { - verifier - header string value string regexp *regexp.Regexp @@ -293,8 +285,6 @@ func (c *queryRegexpConstraint) Verify(r *http.Request) (errors []error) { } type pathConstraint struct { - verifier - path string regexp *regexp.Regexp } @@ -327,8 +317,6 @@ func (c *pathConstraint) Verify(r *http.Request) []error { } type bodyMatchesTextConstraint struct { - verifier - body string regexp *regexp.Regexp } diff --git a/output/allure_report/allure_report.go b/output/allure_report/allure_report.go index e13d7e2..a0c52d8 100644 --- a/output/allure_report/allure_report.go +++ b/output/allure_report/allure_report.go @@ -8,12 +8,9 @@ import ( "time" "github.com/lamoda/gonkey/models" - "github.com/lamoda/gonkey/output" ) type AllureReportOutput struct { - output.OutputInterface - reportLocation string allure Allure } @@ -54,7 +51,7 @@ func (o *AllureReportOutput) Process(t models.TestInterface, result *models.Resu "txt") o.allure.AddAttachment( *bytes.NewBufferString(fmt.Sprintf("Db Response #%d", i+1)), - *bytes.NewBufferString(fmt.Sprintf(`Respone: %s`, dbresult.Response)), + *bytes.NewBufferString(fmt.Sprintf(`Response: %s`, dbresult.Response)), "txt") } } diff --git a/output/console_colored/console_colored.go b/output/console_colored/console_colored.go index 291b724..798b4d6 100644 --- a/output/console_colored/console_colored.go +++ b/output/console_colored/console_colored.go @@ -6,14 +6,11 @@ import ( "github.com/fatih/color" "github.com/lamoda/gonkey/models" - "github.com/lamoda/gonkey/output" ) const dotsPerLine = 80 type ConsoleColoredOutput struct { - output.OutputInterface - verbose bool dots int coloredPrintf func(format string, a ...interface{}) diff --git a/output/testing/testing.go b/output/testing/testing.go index 1ae8768..0f61089 100644 --- a/output/testing/testing.go +++ b/output/testing/testing.go @@ -6,11 +6,9 @@ import ( "text/template" "github.com/lamoda/gonkey/models" - "github.com/lamoda/gonkey/output" ) type TestingOutput struct { - output.OutputInterface testing *testing.T } diff --git a/runner/runner_testing.go b/runner/runner_testing.go index 6a2a2c2..bb03ef3 100644 --- a/runner/runner_testing.go +++ b/runner/runner_testing.go @@ -6,6 +6,7 @@ import ( "os" "testing" + "github.com/aerospike/aerospike-client-go/v5" "github.com/joho/godotenv" "github.com/lamoda/gonkey/checker" @@ -17,16 +18,23 @@ import ( "github.com/lamoda/gonkey/output" "github.com/lamoda/gonkey/output/allure_report" testingOutput "github.com/lamoda/gonkey/output/testing" + aerospikeAdapter "github.com/lamoda/gonkey/storage/aerospike" "github.com/lamoda/gonkey/testloader/yaml_file" "github.com/lamoda/gonkey/variables" ) +type Aerospike struct { + *aerospike.Client + Namespace string +} + type RunWithTestingParams struct { Server *httptest.Server TestsDir string Mocks *mocks.Mocks FixturesDir string DB *sql.DB + Aerospike Aerospike // If DB parameter present, used to recognize type of database, if not set, by default uses Postgres DbType fixtures.DbType EnvFilePath string @@ -51,19 +59,33 @@ func RunWithTesting(t *testing.T, params *RunWithTestingParams) { debug := os.Getenv("GONKEY_DEBUG") != "" var fixturesLoader fixtures.Loader - if params.DB != nil { + if params.DB != nil || params.Aerospike.Client != nil { fixturesLoader = fixtures.NewLoader(&fixtures.Config{ - Location: params.FixturesDir, - DB: params.DB, - Debug: debug, - DbType: params.DbType, + Location: params.FixturesDir, + DB: params.DB, + Aerospike: aerospikeAdapter.New(params.Aerospike.Client, params.Aerospike.Namespace), + Debug: debug, + DbType: params.DbType, }) } + runner := initRunner(params, mocksLoader, fixturesLoader) + + setupOutputs(runner, params, t) + + addCheckers(runner, params) + + _, err := runner.Run() + if err != nil { + t.Fatal(err) + } +} + +func initRunner(params *RunWithTestingParams, mocksLoader *mocks.Loader, fixturesLoader fixtures.Loader) *Runner { yamlLoader := yaml_file.NewLoader(params.TestsDir) yamlLoader.SetFileFilter(os.Getenv("GONKEY_FILE_FILTER")) - r := New( + runner := New( &Config{ Host: params.Server.URL, Mocks: params.Mocks, @@ -73,7 +95,21 @@ func RunWithTesting(t *testing.T, params *RunWithTestingParams) { }, yamlLoader, ) + return runner +} + +func addCheckers(runner *Runner, params *RunWithTestingParams) { + runner.AddCheckers(response_body.NewChecker()) + runner.AddCheckers(response_header.NewChecker()) + if params.DB != nil { + runner.AddCheckers(response_db.NewChecker(params.DB)) + } + + runner.AddCheckers(params.Checkers...) +} + +func setupOutputs(r *Runner, params *RunWithTestingParams, t *testing.T) { if params.OutputFunc != nil { r.AddOutput(params.OutputFunc) } else { @@ -85,18 +121,4 @@ func RunWithTesting(t *testing.T, params *RunWithTestingParams) { defer allureOutput.Finalize() r.AddOutput(allureOutput) } - - r.AddCheckers(response_body.NewChecker()) - r.AddCheckers(response_header.NewChecker()) - - if params.DB != nil { - r.AddCheckers(response_db.NewChecker(params.DB)) - } - - r.AddCheckers(params.Checkers...) - - _, err := r.Run() - if err != nil { - t.Fatal(err) - } } diff --git a/storage/aerospike/aerospike.go b/storage/aerospike/aerospike.go new file mode 100644 index 0000000..8467d3d --- /dev/null +++ b/storage/aerospike/aerospike.go @@ -0,0 +1,43 @@ +package aerospike + +import ( + "github.com/aerospike/aerospike-client-go/v5" +) + +type Client struct { + *aerospike.Client + namespace string +} + +func New(client *aerospike.Client, namespace string) *Client { + + return &Client{ + Client: client, + namespace: namespace, + } +} + +func (c *Client) Truncate(set string) error { + return c.Client.Truncate(nil, c.namespace, set, nil) +} + +func (c *Client) InsertBinMap(set string, key string, binMap map[string]interface{}) error { + aerospikeKey, err := aerospike.NewKey(c.namespace, set, key) + if err != nil { + return err + } + bins := prepareBins(binMap) + + return c.PutBins(nil, aerospikeKey, bins...) +} + +func prepareBins(binmap map[string]interface{}) []*aerospike.Bin { + var bins []*aerospike.Bin + for binName, binData := range binmap { + if binName == "$extend" { + continue + } + bins = append(bins, aerospike.NewBin(binName, binData)) + } + return bins +} diff --git a/testloader/yaml_file/test.go b/testloader/yaml_file/test.go index af1fcb8..023e900 100644 --- a/testloader/yaml_file/test.go +++ b/testloader/yaml_file/test.go @@ -17,8 +17,6 @@ func (c *dbCheck) SetDbQueryString(q string) { c.query = q } func (c *dbCheck) SetDbResponseJson(r []string) { c.response = r } type Test struct { - models.TestInterface - TestDefinition Filename string diff --git a/testloader/yaml_file/yaml_file.go b/testloader/yaml_file/yaml_file.go index 51552e3..07a7718 100644 --- a/testloader/yaml_file/yaml_file.go +++ b/testloader/yaml_file/yaml_file.go @@ -6,12 +6,9 @@ import ( "strings" "github.com/lamoda/gonkey/models" - "github.com/lamoda/gonkey/testloader" ) type YamlFileLoader struct { - testloader.LoaderInterface - testsLocation string fileFilter string }