Skip to content

Commit 5d43322

Browse files
committed
Init action tests
Signed-off-by: Ruben Vargas <ruben.vp8510@gmail.com>
1 parent 755aca5 commit 5d43322

File tree

7 files changed

+292
-16
lines changed

7 files changed

+292
-16
lines changed

cmd/es-rollover/app/init/action.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,32 @@ func (c Action) Do() error {
7979
return nil
8080
}
8181

82+
func createIndexIfNotExist(c client.IndexAPI, index string) error {
83+
err := c.CreateIndex(index)
84+
if err != nil {
85+
if esErr, ok := err.(client.ResponseError); ok {
86+
if esErr.StatusCode != http.StatusBadRequest || esErr.Body == nil {
87+
return err
88+
}
89+
// check for the reason of the error
90+
jsonError := map[string]interface{}{}
91+
err := json.Unmarshal(esErr.Body, &jsonError)
92+
if err != nil {
93+
// return unmarshal error
94+
return err
95+
}
96+
errorMap := jsonError["error"].(map[string]interface{})
97+
// check for reason, ignore already exist error
98+
if strings.Contains("resource_already_exists_exception", errorMap["type"].(string)) {
99+
return nil
100+
}
101+
}
102+
// Return any other error unrelated the he response
103+
return err
104+
}
105+
return nil
106+
}
107+
82108
func (c Action) init(version uint, indexset app.IndexOption) error {
83109
mapping, err := c.getMapping(version, indexset.TemplateName)
84110
if err != nil {
@@ -89,22 +115,11 @@ func (c Action) init(version uint, indexset app.IndexOption) error {
89115
if err != nil {
90116
return err
91117
}
92-
index := indexset.InitialRolloverIndex()
93118

94-
err = c.IndicesClient.CreateIndex(index)
95-
if esErr, ok := err.(client.ResponseError); ok {
96-
if esErr.StatusCode == http.StatusBadRequest && esErr.Body != nil {
97-
jsonError := map[string]interface{}{}
98-
err := json.Unmarshal(esErr.Body, &jsonError)
99-
if err != nil {
100-
return err
101-
}
102-
errorMap := jsonError["error"].(map[string]interface{})
103-
// Ignore already exist error
104-
if !strings.Contains("resource_already_exists_exception", errorMap["type"].(string)) {
105-
return err
106-
}
107-
}
119+
index := indexset.InitialRolloverIndex()
120+
err = createIndexIfNotExist(c.IndicesClient, index)
121+
if err != nil {
122+
return err
108123
}
109124

110125
jaegerIndices, err := c.IndicesClient.GetJaegerIndices(c.Config.IndexPrefix)
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
// Copyright (c) 2021 The Jaeger Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package init
16+
17+
import (
18+
"errors"
19+
"testing"
20+
21+
"github.com/crossdock/crossdock-go/assert"
22+
"github.com/stretchr/testify/mock"
23+
24+
"github.com/jaegertracing/jaeger/cmd/es-rollover/app"
25+
"github.com/jaegertracing/jaeger/pkg/es/client"
26+
"github.com/jaegertracing/jaeger/pkg/es/client/mocks"
27+
)
28+
29+
func TestRolloverAction(t *testing.T) {
30+
tests := []struct {
31+
name string
32+
setupCallExpectations func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI)
33+
config Config
34+
expectedErr error
35+
}{
36+
37+
{
38+
name: "Unsupported version",
39+
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) {
40+
clusterClient.On("Version").Return(uint(5), nil)
41+
},
42+
config: Config{
43+
Config: app.Config{
44+
Archive: true,
45+
UseILM: true,
46+
},
47+
},
48+
expectedErr: errors.New("ILM is supported only for ES version 7+"),
49+
},
50+
{
51+
name: "error getting version",
52+
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) {
53+
clusterClient.On("Version").Return(uint(0), errors.New("version error"))
54+
},
55+
expectedErr: errors.New("version error"),
56+
config: Config{
57+
Config: app.Config{
58+
Archive: true,
59+
UseILM: true,
60+
},
61+
},
62+
},
63+
{
64+
name: "ilm doesnt exist",
65+
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) {
66+
clusterClient.On("Version").Return(uint(7), nil)
67+
ilmClient.On("Exists", "myilmpolicy").Return(false, nil)
68+
},
69+
expectedErr: errors.New("ILM policy myilmpolicy doesn't exist in Elasticsearch. Please create it and re-run init"),
70+
config: Config{
71+
Config: app.Config{
72+
Archive: true,
73+
UseILM: true,
74+
ILMPolicyName: "myilmpolicy",
75+
},
76+
},
77+
},
78+
{
79+
name: "fail get ilm policy",
80+
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) {
81+
clusterClient.On("Version").Return(uint(7), nil)
82+
ilmClient.On("Exists", "myilmpolicy").Return(false, errors.New("error getting ilm policy"))
83+
},
84+
expectedErr: errors.New("error getting ilm policy"),
85+
config: Config{
86+
Config: app.Config{
87+
Archive: true,
88+
UseILM: true,
89+
ILMPolicyName: "myilmpolicy",
90+
},
91+
},
92+
},
93+
{
94+
name: "fail to create template",
95+
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) {
96+
clusterClient.On("Version").Return(uint(7), nil)
97+
indexClient.On("CreateTemplate", mock.Anything, "jaeger-span").Return(errors.New("error creating template"))
98+
},
99+
expectedErr: errors.New("error creating template"),
100+
config: Config{
101+
Config: app.Config{
102+
Archive: true,
103+
UseILM: false,
104+
},
105+
},
106+
},
107+
{
108+
name: "fail to get jaeger indices",
109+
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) {
110+
clusterClient.On("Version").Return(uint(7), nil)
111+
indexClient.On("CreateTemplate", mock.Anything, "jaeger-span").Return(nil)
112+
indexClient.On("CreateIndex", "jaeger-span-archive-000001").Return(nil)
113+
indexClient.On("GetJaegerIndices", "").Return([]client.Index{}, errors.New("error getting jaeger indices"))
114+
},
115+
expectedErr: errors.New("error getting jaeger indices"),
116+
config: Config{
117+
Config: app.Config{
118+
Archive: true,
119+
UseILM: false,
120+
},
121+
},
122+
},
123+
{
124+
name: "fail to create alias",
125+
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) {
126+
clusterClient.On("Version").Return(uint(7), nil)
127+
indexClient.On("CreateTemplate", mock.Anything, "jaeger-span").Return(nil)
128+
indexClient.On("CreateIndex", "jaeger-span-archive-000001").Return(nil)
129+
indexClient.On("GetJaegerIndices", "").Return([]client.Index{}, nil)
130+
indexClient.On("CreateAlias", []client.Alias{
131+
{Index: "jaeger-span-archive-000001", Name: "jaeger-span-archive-read", IsWriteIndex: false},
132+
{Index: "jaeger-span-archive-000001", Name: "jaeger-span-archive-write", IsWriteIndex: true},
133+
}).Return(errors.New("error creating aliases"))
134+
},
135+
expectedErr: errors.New("error creating aliases"),
136+
config: Config{
137+
Config: app.Config{
138+
Archive: true,
139+
UseILM: false,
140+
},
141+
},
142+
},
143+
{
144+
name: "create rollover index",
145+
setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) {
146+
clusterClient.On("Version").Return(uint(7), nil)
147+
indexClient.On("CreateTemplate", mock.Anything, "jaeger-span").Return(nil)
148+
indexClient.On("CreateIndex", "jaeger-span-archive-000001").Return(nil)
149+
indexClient.On("GetJaegerIndices", "").Return([]client.Index{}, nil)
150+
indexClient.On("CreateAlias", []client.Alias{
151+
{Index: "jaeger-span-archive-000001", Name: "jaeger-span-archive-read", IsWriteIndex: false},
152+
{Index: "jaeger-span-archive-000001", Name: "jaeger-span-archive-write", IsWriteIndex: true},
153+
}).Return(nil)
154+
155+
},
156+
expectedErr: nil,
157+
config: Config{
158+
Config: app.Config{
159+
Archive: true,
160+
UseILM: false,
161+
},
162+
},
163+
},
164+
}
165+
166+
for _, test := range tests {
167+
t.Run(test.name, func(t *testing.T) {
168+
indexClient := &mocks.MockIndexAPI{}
169+
clusterClient := &mocks.MockClusterAPI{}
170+
ilmClient := &mocks.MockILMAPI{}
171+
initAction := Action{
172+
Config: test.config,
173+
IndicesClient: indexClient,
174+
ClusterClient: clusterClient,
175+
ILMClient: ilmClient,
176+
}
177+
178+
test.setupCallExpectations(indexClient, clusterClient, ilmClient)
179+
180+
err := initAction.Do()
181+
if test.expectedErr != nil {
182+
assert.Error(t, err)
183+
assert.Equal(t, test.expectedErr, err)
184+
}
185+
186+
indexClient.AssertExpectations(t)
187+
clusterClient.AssertExpectations(t)
188+
ilmClient.AssertExpectations(t)
189+
})
190+
}
191+
}

cmd/es-rollover/app/rollover/action_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"testing"
2020

2121
"github.com/crossdock/crossdock-go/assert"
22+
2223
"github.com/jaegertracing/jaeger/cmd/es-rollover/app"
2324
"github.com/jaegertracing/jaeger/pkg/es/client"
2425
"github.com/jaegertracing/jaeger/pkg/es/client/mocks"

pkg/es/client/interfaces.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright (c) 2021 The Jaeger Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package client
216

317
type IndexAPI interface {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2021 The Jaeger Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package mocks
16+
17+
import (
18+
"github.com/stretchr/testify/mock"
19+
)
20+
21+
type MockClusterAPI struct {
22+
mock.Mock
23+
}
24+
25+
func (c *MockClusterAPI) Version() (uint, error) {
26+
ret := c.Called()
27+
return ret.Get(0).(uint), ret.Error(1)
28+
}

pkg/es/client/mocks/ilm_client.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2021 The Jaeger Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package mocks
16+
17+
import "github.com/stretchr/testify/mock"
18+
19+
type MockILMAPI struct {
20+
mock.Mock
21+
}
22+
23+
func (c *MockILMAPI) Exists(name string) (bool, error) {
24+
ret := c.Called(name)
25+
return ret.Get(0).(bool), ret.Error(1)
26+
}

pkg/es/client/mocks/index_client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
package mocks
1616

1717
import (
18-
"github.com/jaegertracing/jaeger/pkg/es/client"
1918
"github.com/stretchr/testify/mock"
19+
20+
"github.com/jaegertracing/jaeger/pkg/es/client"
2021
)
2122

2223
type MockIndexAPI struct {

0 commit comments

Comments
 (0)