|
| 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 | +} |
0 commit comments