Skip to content

Commit

Permalink
Init action tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben Vargas <ruben.vp8510@gmail.com>
  • Loading branch information
rubenvp8510 committed Sep 6, 2021
1 parent 755aca5 commit b9ae2d5
Show file tree
Hide file tree
Showing 7 changed files with 353 additions and 16 deletions.
45 changes: 30 additions & 15 deletions cmd/es-rollover/app/init/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,32 @@ func (c Action) Do() error {
return nil
}

func createIndexIfNotExist(c client.IndexAPI, index string) error {
err := c.CreateIndex(index)
if err != nil {
if esErr, ok := err.(client.ResponseError); ok {
if esErr.StatusCode != http.StatusBadRequest || esErr.Body == nil {
return esErr.Err
}
// check for the reason of the error
jsonError := map[string]interface{}{}
err := json.Unmarshal(esErr.Body, &jsonError)
if err != nil {
// return unmarshal error
return err
}
errorMap := jsonError["error"].(map[string]interface{})
// check for reason, ignore already exist error
if strings.Contains("resource_already_exists_exception", errorMap["type"].(string)) {
return nil
}
}
// Return any other error unrelated to the response
return err
}
return nil
}

func (c Action) init(version uint, indexset app.IndexOption) error {
mapping, err := c.getMapping(version, indexset.TemplateName)
if err != nil {
Expand All @@ -89,22 +115,11 @@ func (c Action) init(version uint, indexset app.IndexOption) error {
if err != nil {
return err
}
index := indexset.InitialRolloverIndex()

err = c.IndicesClient.CreateIndex(index)
if esErr, ok := err.(client.ResponseError); ok {
if esErr.StatusCode == http.StatusBadRequest && esErr.Body != nil {
jsonError := map[string]interface{}{}
err := json.Unmarshal(esErr.Body, &jsonError)
if err != nil {
return err
}
errorMap := jsonError["error"].(map[string]interface{})
// Ignore already exist error
if !strings.Contains("resource_already_exists_exception", errorMap["type"].(string)) {
return err
}
}
index := indexset.InitialRolloverIndex()
err = createIndexIfNotExist(c.IndicesClient, index)
if err != nil {
return err
}

jaegerIndices, err := c.IndicesClient.GetJaegerIndices(c.Config.IndexPrefix)
Expand Down
252 changes: 252 additions & 0 deletions cmd/es-rollover/app/init/action_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
// Copyright (c) 2021 The Jaeger 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 init

import (
"errors"
"net/http"
"strings"
"testing"

"github.com/crossdock/crossdock-go/assert"
"github.com/stretchr/testify/mock"

"github.com/jaegertracing/jaeger/cmd/es-rollover/app"
"github.com/jaegertracing/jaeger/pkg/es/client"
"github.com/jaegertracing/jaeger/pkg/es/client/mocks"
)

func TestIndexCreateIfNotExist(t *testing.T) {
const esErrResponse = `{"error":{"root_cause":[{"type":"resource_already_exists_exception","reason":"]"}],"type":"resource_already_exists_exception","reason":"request [/jaeger-*] contains unrecognized parameter: [help]"},"status":400}`

tests := []struct {
name string
returnErr error
expectedErr error
containsError string
}{
{
name: "success",
},
{
name: "generic error",
returnErr: errors.New("may be an http error?"),
expectedErr: errors.New("may be an http error?"),
},
{
name: "response error",
returnErr: client.ResponseError{
Err: errors.New("x"),
StatusCode: http.StatusForbidden,
},
expectedErr: errors.New("x"),
},
{
name: "unmarshal error",
returnErr: client.ResponseError{
Err: errors.New("x"),
StatusCode: http.StatusBadRequest,
Body: []byte("blablabla"),
},
containsError: "invalid character",
},
{
name: "existing error",
returnErr: client.ResponseError{
Err: errors.New("x"),
StatusCode: http.StatusBadRequest,
Body: []byte(esErrResponse),
},
expectedErr: nil,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
indexClient := &mocks.MockIndexAPI{}
indexClient.On("CreateIndex", "jaeger-span").Return(test.returnErr)
err := createIndexIfNotExist(indexClient, "jaeger-span")
if test.containsError != "" {
assert.True(t, strings.Contains(err.Error(), test.containsError))
} else {
assert.Equal(t, test.expectedErr, err)
}
})
}

}
func TestRolloverAction(t *testing.T) {
tests := []struct {
name string
setupCallExpectations func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI)
config Config
expectedErr error
}{

{
name: "Unsupported version",
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) {
clusterClient.On("Version").Return(uint(5), nil)
},
config: Config{
Config: app.Config{
Archive: true,
UseILM: true,
},
},
expectedErr: errors.New("ILM is supported only for ES version 7+"),
},
{
name: "error getting version",
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) {
clusterClient.On("Version").Return(uint(0), errors.New("version error"))
},
expectedErr: errors.New("version error"),
config: Config{
Config: app.Config{
Archive: true,
UseILM: true,
},
},
},
{
name: "ilm doesnt exist",
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) {
clusterClient.On("Version").Return(uint(7), nil)
ilmClient.On("Exists", "myilmpolicy").Return(false, nil)
},
expectedErr: errors.New("ILM policy myilmpolicy doesn't exist in Elasticsearch. Please create it and re-run init"),
config: Config{
Config: app.Config{
Archive: true,
UseILM: true,
ILMPolicyName: "myilmpolicy",
},
},
},
{
name: "fail get ilm policy",
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) {
clusterClient.On("Version").Return(uint(7), nil)
ilmClient.On("Exists", "myilmpolicy").Return(false, errors.New("error getting ilm policy"))
},
expectedErr: errors.New("error getting ilm policy"),
config: Config{
Config: app.Config{
Archive: true,
UseILM: true,
ILMPolicyName: "myilmpolicy",
},
},
},
{
name: "fail to create template",
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) {
clusterClient.On("Version").Return(uint(7), nil)
indexClient.On("CreateTemplate", mock.Anything, "jaeger-span").Return(errors.New("error creating template"))
},
expectedErr: errors.New("error creating template"),
config: Config{
Config: app.Config{
Archive: true,
UseILM: false,
},
},
},
{
name: "fail to get jaeger indices",
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) {
clusterClient.On("Version").Return(uint(7), nil)
indexClient.On("CreateTemplate", mock.Anything, "jaeger-span").Return(nil)
indexClient.On("CreateIndex", "jaeger-span-archive-000001").Return(nil)
indexClient.On("GetJaegerIndices", "").Return([]client.Index{}, errors.New("error getting jaeger indices"))
},
expectedErr: errors.New("error getting jaeger indices"),
config: Config{
Config: app.Config{
Archive: true,
UseILM: false,
},
},
},
{
name: "fail to create alias",
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) {
clusterClient.On("Version").Return(uint(7), nil)
indexClient.On("CreateTemplate", mock.Anything, "jaeger-span").Return(nil)
indexClient.On("CreateIndex", "jaeger-span-archive-000001").Return(nil)
indexClient.On("GetJaegerIndices", "").Return([]client.Index{}, nil)
indexClient.On("CreateAlias", []client.Alias{
{Index: "jaeger-span-archive-000001", Name: "jaeger-span-archive-read", IsWriteIndex: false},
{Index: "jaeger-span-archive-000001", Name: "jaeger-span-archive-write", IsWriteIndex: true},
}).Return(errors.New("error creating aliases"))
},
expectedErr: errors.New("error creating aliases"),
config: Config{
Config: app.Config{
Archive: true,
UseILM: false,
},
},
},
{
name: "create rollover index",
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) {
clusterClient.On("Version").Return(uint(7), nil)
indexClient.On("CreateTemplate", mock.Anything, "jaeger-span").Return(nil)
indexClient.On("CreateIndex", "jaeger-span-archive-000001").Return(nil)
indexClient.On("GetJaegerIndices", "").Return([]client.Index{}, nil)
indexClient.On("CreateAlias", []client.Alias{
{Index: "jaeger-span-archive-000001", Name: "jaeger-span-archive-read", IsWriteIndex: false},
{Index: "jaeger-span-archive-000001", Name: "jaeger-span-archive-write", IsWriteIndex: true},
}).Return(nil)

},
expectedErr: nil,
config: Config{
Config: app.Config{
Archive: true,
UseILM: false,
},
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
indexClient := &mocks.MockIndexAPI{}
clusterClient := &mocks.MockClusterAPI{}
ilmClient := &mocks.MockILMAPI{}
initAction := Action{
Config: test.config,
IndicesClient: indexClient,
ClusterClient: clusterClient,
ILMClient: ilmClient,
}

test.setupCallExpectations(indexClient, clusterClient, ilmClient)

err := initAction.Do()
if test.expectedErr != nil {
assert.Error(t, err)
assert.Equal(t, test.expectedErr, err)
}

indexClient.AssertExpectations(t)
clusterClient.AssertExpectations(t)
ilmClient.AssertExpectations(t)
})
}
}
1 change: 1 addition & 0 deletions cmd/es-rollover/app/rollover/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"

"github.com/crossdock/crossdock-go/assert"

"github.com/jaegertracing/jaeger/cmd/es-rollover/app"
"github.com/jaegertracing/jaeger/pkg/es/client"
"github.com/jaegertracing/jaeger/pkg/es/client/mocks"
Expand Down
14 changes: 14 additions & 0 deletions pkg/es/client/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright (c) 2021 The Jaeger 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 client

type IndexAPI interface {
Expand Down
28 changes: 28 additions & 0 deletions pkg/es/client/mocks/cluter_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2021 The Jaeger 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 mocks

import (
"github.com/stretchr/testify/mock"
)

type MockClusterAPI struct {
mock.Mock
}

func (c *MockClusterAPI) Version() (uint, error) {
ret := c.Called()
return ret.Get(0).(uint), ret.Error(1)
}
26 changes: 26 additions & 0 deletions pkg/es/client/mocks/ilm_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2021 The Jaeger 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 mocks

import "github.com/stretchr/testify/mock"

type MockILMAPI struct {
mock.Mock
}

func (c *MockILMAPI) Exists(name string) (bool, error) {
ret := c.Called(name)
return ret.Get(0).(bool), ret.Error(1)
}
Loading

0 comments on commit b9ae2d5

Please sign in to comment.