diff --git a/indexers/elastic.go b/indexers/elastic.go index 724d861..52090a7 100644 --- a/indexers/elastic.go +++ b/indexers/elastic.go @@ -32,8 +32,6 @@ import ( "github.com/elastic/go-elasticsearch/v7/esutil" ) -const elastic = "elastic" - // Elastic ElasticSearch instance type Elastic struct { index string @@ -44,7 +42,7 @@ var ESClient *elasticsearch.Client // Init function func init() { - indexerMap[elastic] = &Elastic{} + indexerMap[ElasticIndexer] = &Elastic{} } // Returns new indexer for elastic search @@ -140,7 +138,7 @@ func (esIndexer *Elastic) Index(documents []interface{}, opts IndexingOpts) (str for stat, val := range indexerStats { statString += fmt.Sprintf(" %s=%d", stat, val) } - if(redundantSkipped > 0){ + if redundantSkipped > 0 { statString += fmt.Sprintf(" redundantskipped=%d", redundantSkipped) } return fmt.Sprintf("Indexing finished in %v:%v", dur.Truncate(time.Millisecond), statString), nil diff --git a/indexers/local.go b/indexers/local.go index 542838e..dcabe85 100644 --- a/indexers/local.go +++ b/indexers/local.go @@ -21,8 +21,6 @@ import ( "path" ) -const local = "local" - // Local indexer instance type Local struct { metricsDirectory string @@ -30,7 +28,7 @@ type Local struct { // Init function func init() { - indexerMap[local] = &Local{} + indexerMap[LocalIndexer] = &Local{} } // Prepares local indexing directory @@ -45,6 +43,9 @@ func (l *Local) new(indexerConfig IndexerConfig) error { // Index uses generates a local file with the given name and metrics func (l *Local) Index(documents []interface{}, opts IndexingOpts) (string, error) { + if len(documents) == 0 { + return "", fmt.Errorf("Empty document list in %v", opts.MetricName) + } if opts.MetricName == "" { return "", fmt.Errorf("MetricName shouldn't be empty") } diff --git a/indexers/local_test.go b/indexers/local_test.go index 762249e..9bce27e 100644 --- a/indexers/local_test.go +++ b/indexers/local_test.go @@ -2,8 +2,10 @@ package indexers import ( "errors" + "fmt" "log" "os" + "path" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -19,9 +21,8 @@ var _ = Describe("Tests for local.go", func() { var localIndexer Local BeforeEach(func() { testcase = newtestcase{ - indexerconfig: IndexerConfig{Type: "elastic", + indexerconfig: IndexerConfig{Type: "local", Servers: []string{""}, - Index: "go-commons-test", InsecureSkipVerify: true, MetricsDirectory: "", }, @@ -41,7 +42,7 @@ var _ = Describe("Tests for local.go", func() { }) Context("Default behaviour of local.go, Index()", func() { - var testcase indexMethodTestcase + var testcase, emtpyTestCase indexMethodTestcase var indexer Local indexer.metricsDirectory = "placeholder" BeforeEach(func() { @@ -71,6 +72,12 @@ var _ = Describe("Tests for local.go", func() { MetricName: "placeholder", }, } + emtpyTestCase = indexMethodTestcase{ + documents: []interface{}{}, + opts: IndexingOpts{ + MetricName: "empty", + }, + } }) AfterEach(func() { err := os.RemoveAll(indexer.metricsDirectory) @@ -79,13 +86,10 @@ var _ = Describe("Tests for local.go", func() { } }) - It("No err is returned", func() { + It("Metric file is created", func() { _, err := indexer.Index(testcase.documents, testcase.opts) Expect(err).To(BeNil()) - }) - - It("No err is returned", func() { - _, err := indexer.Index(testcase.documents, testcase.opts) + _, err = os.Stat(path.Join(indexer.metricsDirectory, testcase.opts.MetricName+".json")) Expect(err).To(BeNil()) }) @@ -100,5 +104,9 @@ var _ = Describe("Tests for local.go", func() { _, err := indexer.Index(testcase.documents, testcase.opts) Expect(err).To(BeEquivalentTo(errors.New("JSON encoding error: json: unsupported type: chan string"))) }) + It("returns err no empty document list", func() { + _, err := indexer.Index(emtpyTestCase.documents, emtpyTestCase.opts) + Expect(err).To(BeEquivalentTo(fmt.Errorf("Empty document list in %v", emtpyTestCase.opts.MetricName))) + }) }) }) diff --git a/indexers/opensearch.go b/indexers/opensearch.go index 40e227a..35fde7b 100644 --- a/indexers/opensearch.go +++ b/indexers/opensearch.go @@ -32,8 +32,6 @@ import ( opensearchutil "github.com/opensearch-project/opensearch-go/opensearchutil" ) -const indexer = "opensearch" - // OSClient OpenSearch client instance var OSClient *opensearch.Client @@ -44,7 +42,7 @@ type OpenSearch struct { // Init function func init() { - indexerMap[indexer] = &OpenSearch{} + indexerMap[OpenSearchIndexer] = &OpenSearch{} } // Returns new indexer for OpenSearch @@ -140,7 +138,7 @@ func (OpenSearchIndexer *OpenSearch) Index(documents []interface{}, opts Indexin for stat, val := range indexerStats { statString += fmt.Sprintf(" %s=%d", stat, val) } - if(redundantSkipped > 0){ + if redundantSkipped > 0 { statString += fmt.Sprintf(" redundantskipped=%d", redundantSkipped) } return fmt.Sprintf("Indexing finished in %v:%v", dur.Truncate(time.Millisecond), statString), nil