Skip to content

Commit

Permalink
test rollover action
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben Vargas <[email protected]>
  • Loading branch information
rubenvp8510 committed Sep 6, 2021
1 parent 1793c4a commit 755aca5
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 10 deletions.
6 changes: 3 additions & 3 deletions cmd/es-rollover/app/init/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ const ilmVersionSupport = 7
// Action holds the configuration and clients for init action
type Action struct {
Config Config
ClusterClient client.ClusterClient
IndicesClient client.IndicesClient
ILMClient client.ILMClient
ClusterClient client.ClusterAPI
IndicesClient client.IndexAPI
ILMClient client.IndexManagementLifecycleAPI
}

func (c Action) getMapping(version uint, templateName string) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/es-rollover/app/lookback/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// Action holds the configuration and clients for lookback action
type Action struct {
Config
IndicesClient client.IndicesClient
IndicesClient client.IndexAPI
}

// Do the lookback action
Expand Down
2 changes: 1 addition & 1 deletion cmd/es-rollover/app/rollover/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
// Action holds the configuration and clients for rollover action
type Action struct {
Config
IndicesClient client.IndicesClient
IndicesClient client.IndexAPI
}

// Do the rollover action
Expand Down
112 changes: 112 additions & 0 deletions cmd/es-rollover/app/rollover/action_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// 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 rollover

import (
"errors"
"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"
)

func TestRolloverAction(t *testing.T) {
readIndices := []client.Index{
{
Index: "jaeger-read-span",
Aliases: map[string]bool{
"jaeger-span-archive-write": true,
},
},
}

aliasToCreate := []client.Alias{{Index: "jaeger-read-span", Name: "jaeger-span-archive-read", IsWriteIndex: false}}
tests := []struct {
name string
conditions string
unmarshalErrExpected bool
getJaegerIndicesErr error
rolloverErr error
createAliasErr error
expectedError bool
}{
{
name: "success",
conditions: "{\"max_age\": \"2d\"}",
expectedError: false,
},
{
name: "get jaeger indices error",
conditions: "{\"max_age\": \"2d\"}",
expectedError: true,
getJaegerIndicesErr: errors.New("unable to get indices"),
},
{
name: "rollover error",
conditions: "{\"max_age\": \"2d\"}",
expectedError: true,
rolloverErr: errors.New("unable to rollover"),
},
{
name: "create alias error",
conditions: "{\"max_age\": \"2d\"}",
expectedError: true,
createAliasErr: errors.New("unable to create alias"),
},
{
name: "unmarshal conditions error",
conditions: "{\"max_age\" \"2d\"},",
unmarshalErrExpected: true,
createAliasErr: errors.New("unable to create alias"),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
indexClient := &mocks.MockIndexAPI{}

rolloverAction := Action{
Config: Config{
Conditions: test.conditions,
Config: app.Config{
Archive: true,
},
},
IndicesClient: indexClient,
}

if !test.unmarshalErrExpected {
if test.rolloverErr == nil {
indexClient.On("GetJaegerIndices", "").Return(readIndices, test.getJaegerIndicesErr)
if test.getJaegerIndicesErr == nil {
indexClient.On("CreateAlias", aliasToCreate).Return(test.createAliasErr)
}
}
indexClient.On("Rollover", "jaeger-span-archive-write", map[string]interface{}{"max_age": "2d"}).Return(test.rolloverErr)
}

err := rolloverAction.Do()
if test.expectedError || test.unmarshalErrExpected {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
indexClient.AssertExpectations(t)
})
}

}
10 changes: 5 additions & 5 deletions cmd/es-rollover/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ func main() {
}, func(c client.Client, cfg app.Config) app.Action {
initCfg.Config = cfg
initCfg.InitFromViper(v)
indicesClient := client.IndicesClient{
indicesClient := &client.IndicesClient{
Client: c,
MasterTimeoutSeconds: initCfg.Timeout,
}
clusterClient := client.ClusterClient{
clusterClient := &client.ClusterClient{
Client: c,
}
ilmClient := client.ILMClient{
ilmClient := &client.ILMClient{
Client: c,
}
return &initialize.Action{
Expand Down Expand Up @@ -97,7 +97,7 @@ func main() {
}, func(c client.Client, cfg app.Config) app.Action {
rolloverCfg.Config = cfg
rolloverCfg.InitFromViper(v)
indicesClient := client.IndicesClient{
indicesClient := &client.IndicesClient{
Client: c,
MasterTimeoutSeconds: rolloverCfg.Timeout,
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func main() {
}, func(c client.Client, cfg app.Config) app.Action {
lookbackCfg.Config = cfg
lookbackCfg.InitFromViper(v)
indicesClient := client.IndicesClient{
indicesClient := &client.IndicesClient{
Client: c,
MasterTimeoutSeconds: lookbackCfg.Timeout,
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/es/client/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package client

type IndexAPI interface {
GetJaegerIndices(prefix string) ([]Index, error)
DeleteIndices(indices []Index) error
CreateIndex(index string) error
CreateAlias(aliases []Alias) error
DeleteAlias(aliases []Alias) error
CreateTemplate(template, name string) error
Rollover(rolloverTarget string, conditions map[string]interface{}) error
}

type ClusterAPI interface {
Version() (uint, error)
}

type IndexManagementLifecycleAPI interface {
Exists(name string) (bool, error)
}
Empty file added pkg/es/client/mocks/.nocover
Empty file.
15 changes: 15 additions & 0 deletions pkg/es/client/mocks/empty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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
53 changes: 53 additions & 0 deletions pkg/es/client/mocks/index_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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/jaegertracing/jaeger/pkg/es/client"
"github.com/stretchr/testify/mock"
)

type MockIndexAPI struct {
mock.Mock
}

func (c *MockIndexAPI) GetJaegerIndices(prefix string) ([]client.Index, error) {
ret := c.Called(prefix)
return ret.Get(0).([]client.Index), ret.Error(1)
}
func (c *MockIndexAPI) DeleteIndices(indices []client.Index) error {
ret := c.Called(indices)
return ret.Error(0)
}
func (c *MockIndexAPI) CreateIndex(index string) error {
ret := c.Called(index)
return ret.Error(0)
}
func (c *MockIndexAPI) CreateAlias(aliases []client.Alias) error {
ret := c.Called(aliases)
return ret.Error(0)
}
func (c *MockIndexAPI) DeleteAlias(aliases []client.Alias) error {
ret := c.Called(aliases)
return ret.Error(0)
}
func (c *MockIndexAPI) CreateTemplate(template, name string) error {
ret := c.Called(template, name)
return ret.Error(0)
}
func (c *MockIndexAPI) Rollover(rolloverTarget string, conditions map[string]interface{}) error {
ret := c.Called(rolloverTarget, conditions)
return ret.Error(0)
}

0 comments on commit 755aca5

Please sign in to comment.