Skip to content

Commit

Permalink
Increase coverage of rollover action
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 11, 2021
1 parent 2f2b644 commit 038d84f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 27 deletions.
6 changes: 3 additions & 3 deletions cmd/es-rollover/app/rollover/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func (a *Action) rollover(indexSet app.IndexOption) error {
Name: readAlias,
})
}
if len(aliases) > 0 {
return a.IndicesClient.CreateAlias(aliases)
if len(aliases) == 0 {
return nil
}
return nil
return a.IndicesClient.CreateAlias(aliases)
}
82 changes: 58 additions & 24 deletions cmd/es-rollover/app/rollover/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,87 @@ func TestRolloverAction(t *testing.T) {
}

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
}{
type testCase struct {
name string
conditions string
unmarshalErrExpected bool
getJaegerIndicesErr error
rolloverErr error
createAliasErr error
expectedError bool
indices []client.Index
setupCallExpectations func(indexClient *mocks.MockIndexAPI, t *testCase)
}

tests := []testCase{
{
name: "success",
conditions: "{\"max_age\": \"2d\"}",
expectedError: false,
indices: readIndices,
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, test *testCase) {
indexClient.On("GetJaegerIndices", "").Return(test.indices, test.getJaegerIndicesErr)
indexClient.On("CreateAlias", aliasToCreate).Return(test.createAliasErr)
indexClient.On("Rollover", "jaeger-span-archive-write", map[string]interface{}{"max_age": "2d"}).Return(test.rolloverErr)
},
},
{
name: "no alias write alias",
conditions: "{\"max_age\": \"2d\"}",
expectedError: false,
indices: []client.Index{
{
Index: "jaeger-read-span",
Aliases: map[string]bool{
"jaeger-span-archive-read": true,
},
},
},
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, test *testCase) {
indexClient.On("GetJaegerIndices", "").Return(test.indices, test.getJaegerIndicesErr)
indexClient.On("Rollover", "jaeger-span-archive-write", map[string]interface{}{"max_age": "2d"}).Return(test.rolloverErr)
},
},
{
name: "get jaeger indices error",
conditions: "{\"max_age\": \"2d\"}",
expectedError: true,
getJaegerIndicesErr: errors.New("unable to get indices"),
indices: readIndices,
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, test *testCase) {
indexClient.On("Rollover", "jaeger-span-archive-write", map[string]interface{}{"max_age": "2d"}).Return(test.rolloverErr)
indexClient.On("GetJaegerIndices", "").Return(test.indices, test.getJaegerIndicesErr)
},
},
{
name: "rollover error",
conditions: "{\"max_age\": \"2d\"}",
expectedError: true,
rolloverErr: errors.New("unable to rollover"),
indices: readIndices,
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, test *testCase) {
indexClient.On("Rollover", "jaeger-span-archive-write", map[string]interface{}{"max_age": "2d"}).Return(test.rolloverErr)
},
},
{
name: "create alias error",
conditions: "{\"max_age\": \"2d\"}",
expectedError: true,
createAliasErr: errors.New("unable to create alias"),
indices: readIndices,
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, test *testCase) {
indexClient.On("GetJaegerIndices", "").Return(test.indices, test.getJaegerIndicesErr)
indexClient.On("CreateAlias", aliasToCreate).Return(test.createAliasErr)
indexClient.On("Rollover", "jaeger-span-archive-write", map[string]interface{}{"max_age": "2d"}).Return(test.rolloverErr)
},
},
{
name: "unmarshal conditions error",
conditions: "{\"max_age\" \"2d\"},",
unmarshalErrExpected: 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"),
indices: readIndices,
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, test *testCase) {},
},
}

Expand All @@ -89,17 +133,7 @@ func TestRolloverAction(t *testing.T) {
},
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)
}

test.setupCallExpectations(indexClient, &test)
err := rolloverAction.Do()
if test.expectedError || test.unmarshalErrExpected {
assert.Error(t, err)
Expand Down

0 comments on commit 038d84f

Please # to comment.