From c1559c55fa5a4a211d0755289a097f1fce42d0df Mon Sep 17 00:00:00 2001 From: Iuga Mihai Date: Tue, 20 Feb 2024 14:14:26 +0200 Subject: [PATCH] fix unit tests and linter issues --- client/elasticClientCommon.go | 2 +- integrationtests/valuesIndex_test.go | 2 ++ process/elasticproc/accounts/serialize_test.go | 8 ++++---- process/elasticproc/elasticProcessor.go | 8 ++++++-- process/elasticproc/templatesAndPolicies/noKibana_test.go | 2 +- process/factory/indexerFactory_test.go | 3 --- 6 files changed, 14 insertions(+), 11 deletions(-) diff --git a/client/elasticClientCommon.go b/client/elasticClientCommon.go index 00850f7d..41ed1034 100644 --- a/client/elasticClientCommon.go +++ b/client/elasticClientCommon.go @@ -96,7 +96,7 @@ func elasticBulkRequestResponseHandler(res *esapi.Response) error { return fmt.Errorf("%s", res.String()) } - bodyBytes, err := ioutil.ReadAll(res.Body) + bodyBytes, err := io.ReadAll(res.Body) if err != nil { return fmt.Errorf("%w cannot read elastic response body bytes", err) } diff --git a/integrationtests/valuesIndex_test.go b/integrationtests/valuesIndex_test.go index 6a066946..f76835f7 100644 --- a/integrationtests/valuesIndex_test.go +++ b/integrationtests/valuesIndex_test.go @@ -1,3 +1,5 @@ +//go:build integrationtests + package integrationtests import ( diff --git a/process/elasticproc/accounts/serialize_test.go b/process/elasticproc/accounts/serialize_test.go index 52b0dbf4..341c6241 100644 --- a/process/elasticproc/accounts/serialize_test.go +++ b/process/elasticproc/accounts/serialize_test.go @@ -54,7 +54,7 @@ func TestSerializeAccounts(t *testing.T) { require.Equal(t, 1, len(buffSlice.Buffers())) expectedRes := `{ "update" : {"_index": "accounts", "_id" : "addr1" } } -{"scripted_upsert": true, "script": {"source": "if ('create' == ctx.op) {ctx._source = params.account} else {if (ctx._source.containsKey('timestamp')) {if (ctx._source.timestamp <= params.account.timestamp) {ctx._source = params.account}} else {ctx._source = params.account}}","lang": "painless","params": { "account": {"address":"addr1","nonce":1,"balance":"50","balanceNum":0.1,"shardID":0} }},"upsert": {}} +{"scripted_upsert": true, "script": {"source": "if ('create' == ctx.op) {ctx._source = params.account} else {if ((!ctx._source.containsKey('timestamp')) || (ctx._source.timestamp <= params.account.timestamp) ) {params.account.forEach((key, value) -> {ctx._source[key] = value;});}}","lang": "painless","params": { "account": {"address":"addr1","nonce":1,"balance":"50","balanceNum":0.1,"shardID":0} }},"upsert": {}} ` require.Equal(t, expectedRes, buffSlice.Buffers()[0].String()) } @@ -81,7 +81,7 @@ func TestSerializeAccountsESDTNonceZero(t *testing.T) { require.Equal(t, 1, len(buffSlice.Buffers())) expectedRes := `{ "update" : {"_index": "accountsesdt", "_id" : "addr1-token-abcd-00" } } -{"scripted_upsert": true, "script": {"source": "if ('create' == ctx.op) {ctx._source = params.account} else {if (ctx._source.containsKey('timestamp')) {if (ctx._source.timestamp <= params.account.timestamp) {ctx._source = params.account}} else {ctx._source = params.account}}","lang": "painless","params": { "account": {"address":"addr1","nonce":1,"balance":"10000000000000","balanceNum":1,"token":"token-abcd","properties":"000","timestamp":123,"shardID":0} }},"upsert": {}} +{"scripted_upsert": true, "script": {"source": "if ('create' == ctx.op) {ctx._source = params.account} else {if ((!ctx._source.containsKey('timestamp')) || (ctx._source.timestamp <= params.account.timestamp) ) {params.account.forEach((key, value) -> {ctx._source[key] = value;});}}","lang": "painless","params": { "account": {"address":"addr1","nonce":1,"balance":"10000000000000","balanceNum":1,"token":"token-abcd","properties":"000","timestamp":123,"shardID":0} }},"upsert": {}} ` require.Equal(t, expectedRes, buffSlice.Buffers()[0].String()) } @@ -107,7 +107,7 @@ func TestSerializeAccountsESDT(t *testing.T) { require.Equal(t, 1, len(buffSlice.Buffers())) expectedRes := `{ "update" : {"_index": "accountsesdt", "_id" : "addr1-token-0001-05" } } -{"scripted_upsert": true, "script": {"source": "if ('create' == ctx.op) {ctx._source = params.account} else {if (ctx._source.containsKey('timestamp')) {if (ctx._source.timestamp <= params.account.timestamp) {ctx._source = params.account}} else {ctx._source = params.account}}","lang": "painless","params": { "account": {"address":"addr1","nonce":1,"balance":"10000000000000","balanceNum":1,"token":"token-0001","tokenNonce":5,"properties":"000","shardID":0} }},"upsert": {}} +{"scripted_upsert": true, "script": {"source": "if ('create' == ctx.op) {ctx._source = params.account} else {if ((!ctx._source.containsKey('timestamp')) || (ctx._source.timestamp <= params.account.timestamp) ) {params.account.forEach((key, value) -> {ctx._source[key] = value;});}}","lang": "painless","params": { "account": {"address":"addr1","nonce":1,"balance":"10000000000000","balanceNum":1,"token":"token-0001","tokenNonce":5,"properties":"000","shardID":0} }},"upsert": {}} ` require.Equal(t, expectedRes, buffSlice.Buffers()[0].String()) } @@ -147,7 +147,7 @@ func TestSerializeAccountsNFTWithMedaData(t *testing.T) { require.Equal(t, 1, len(buffSlice.Buffers())) expectedRes := `{ "update" : {"_index": "accountsesdt", "_id" : "addr1-token-0001-16" } } -{"scripted_upsert": true, "script": {"source": "if ('create' == ctx.op) {ctx._source = params.account} else {if (ctx._source.containsKey('timestamp')) {if (ctx._source.timestamp <= params.account.timestamp) {ctx._source = params.account}} else {ctx._source = params.account}}","lang": "painless","params": { "account": {"address":"addr1","nonce":1,"balance":"10000000000000","balanceNum":1,"token":"token-0001","identifier":"token-0001-5","tokenNonce":22,"properties":"000","data":{"name":"nft","creator":"010101","royalties":1,"hash":"aGFzaA==","uris":["dXJp"],"tags":["test","free","fun"],"attributes":"dGFnczp0ZXN0LGZyZWUsZnVuO2Rlc2NyaXB0aW9uOlRoaXMgaXMgYSB0ZXN0IGRlc2NyaXB0aW9uIGZvciBhbiBhd2Vzb21lIG5mdA==","metadata":"metadata-test","nonEmptyURIs":true,"whiteListedStorage":false},"shardID":0} }},"upsert": {}} +{"scripted_upsert": true, "script": {"source": "if ('create' == ctx.op) {ctx._source = params.account} else {if ((!ctx._source.containsKey('timestamp')) || (ctx._source.timestamp <= params.account.timestamp) ) {params.account.forEach((key, value) -> {ctx._source[key] = value;});}}","lang": "painless","params": { "account": {"address":"addr1","nonce":1,"balance":"10000000000000","balanceNum":1,"token":"token-0001","identifier":"token-0001-5","tokenNonce":22,"properties":"000","data":{"name":"nft","creator":"010101","royalties":1,"hash":"aGFzaA==","uris":["dXJp"],"tags":["test","free","fun"],"attributes":"dGFnczp0ZXN0LGZyZWUsZnVuO2Rlc2NyaXB0aW9uOlRoaXMgaXMgYSB0ZXN0IGRlc2NyaXB0aW9uIGZvciBhbiBhd2Vzb21lIG5mdA==","metadata":"metadata-test","nonEmptyURIs":true,"whiteListedStorage":false},"shardID":0} }},"upsert": {}} ` require.Equal(t, expectedRes, buffSlice.Buffers()[0].String()) } diff --git a/process/elasticproc/elasticProcessor.go b/process/elasticproc/elasticProcessor.go index 99c6eb51..c38fad5a 100644 --- a/process/elasticproc/elasticProcessor.go +++ b/process/elasticproc/elasticProcessor.go @@ -139,13 +139,17 @@ func (ei *elasticProcessor) init(useKibana bool, indexTemplates, _ map[string]*b } func (ei *elasticProcessor) indexVersion(version string) error { - id := fmt.Sprintf("%s", versionStr) + if version == "" { + log.Debug("ei.elasticProcessor indexer version is empty") + return nil + } + keyValueObj := &data.KeyValueObj{ Key: versionStr, Value: version, } - meta := []byte(fmt.Sprintf(`{ "index" : { "_index":"%s", "_id" : "%s" } }%s`, elasticIndexer.ValuesIndex, id, "\n")) + meta := []byte(fmt.Sprintf(`{ "index" : { "_index":"%s", "_id" : "%s" } }%s`, elasticIndexer.ValuesIndex, versionStr, "\n")) keyValueObjBytes, err := json.Marshal(keyValueObj) if err != nil { return err diff --git a/process/elasticproc/templatesAndPolicies/noKibana_test.go b/process/elasticproc/templatesAndPolicies/noKibana_test.go index dd000be2..db0b12a0 100644 --- a/process/elasticproc/templatesAndPolicies/noKibana_test.go +++ b/process/elasticproc/templatesAndPolicies/noKibana_test.go @@ -14,5 +14,5 @@ func TestTemplatesAndPolicyReaderNoKibana_GetElasticTemplatesAndPolicies(t *test templates, policies, err := reader.GetElasticTemplatesAndPolicies() require.Nil(t, err) require.Len(t, policies, 0) - require.Len(t, templates, 21) + require.Len(t, templates, 22) } diff --git a/process/factory/indexerFactory_test.go b/process/factory/indexerFactory_test.go index b6b35110..dfad0558 100644 --- a/process/factory/indexerFactory_test.go +++ b/process/factory/indexerFactory_test.go @@ -116,7 +116,4 @@ func TestIndexerFactoryCreate_ElasticIndexer(t *testing.T) { err = elasticIndexer.Close() require.NoError(t, err) - - err = elasticIndexer.Close() - require.NoError(t, err) }