-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ruben Vargas <ruben.vp8510@gmail.com>
- Loading branch information
1 parent
b9ae2d5
commit c48c9c7
Showing
5 changed files
with
281 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// 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 lookback | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/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 TestLookBackAction(t *testing.T) { | ||
nowTime := time.Date(2021, 10, 12, 10, 10, 10, 10, time.Local) | ||
indices := []client.Index{ | ||
{ | ||
Index: "jaeger-span-archive-0000", | ||
Aliases: map[string]bool{ | ||
"jaeger-span-archive-other-alias": true, | ||
}, | ||
CreationTime: time.Date(2021, 10, 10, 10, 10, 10, 10, time.Local), | ||
}, | ||
{ | ||
Index: "jaeger-span-archive-0001", | ||
Aliases: map[string]bool{ | ||
"jaeger-span-archive-read": true, | ||
}, | ||
CreationTime: time.Date(2021, 10, 10, 10, 10, 10, 10, time.Local), | ||
}, | ||
{ | ||
Index: "jaeger-span-archive-0002", | ||
Aliases: map[string]bool{ | ||
"jaeger-span-archive-read": true, | ||
"jaeger-span-archive-write": true, | ||
}, | ||
CreationTime: time.Date(2021, 10, 11, 10, 10, 10, 10, time.Local), | ||
}, | ||
{ | ||
Index: "jaeger-span-archive-0002", | ||
Aliases: map[string]bool{ | ||
"jaeger-span-archive-read": true, | ||
}, | ||
CreationTime: nowTime, | ||
}, | ||
{ | ||
Index: "jaeger-span-archive-0004", | ||
Aliases: map[string]bool{ | ||
"jaeger-span-archive-read": true, | ||
"jaeger-span-archive-write": true, | ||
}, | ||
CreationTime: nowTime, | ||
}, | ||
} | ||
timeNow = func() time.Time { | ||
return nowTime | ||
} | ||
tests := []struct { | ||
name string | ||
setupCallExpectations func(indexClient *mocks.MockIndexAPI) | ||
config Config | ||
expectedErr error | ||
}{ | ||
|
||
{ | ||
name: "success", | ||
setupCallExpectations: func(indexClient *mocks.MockIndexAPI) { | ||
indexClient.On("GetJaegerIndices", "").Return(indices, nil) | ||
indexClient.On("DeleteAlias", []client.Alias{ | ||
{ | ||
Index: "jaeger-span-archive-0001", | ||
Name: "jaeger-span-archive-read", | ||
}, | ||
}).Return(nil) | ||
|
||
}, | ||
config: Config{ | ||
Unit: "days", | ||
UnitCount: 1, | ||
Config: app.Config{ | ||
Archive: true, | ||
UseILM: true, | ||
}, | ||
}, | ||
expectedErr: nil, | ||
}, | ||
{ | ||
name: "get indices error", | ||
setupCallExpectations: func(indexClient *mocks.MockIndexAPI) { | ||
indexClient.On("GetJaegerIndices", "").Return(indices, errors.New("get indices error")) | ||
}, | ||
config: Config{ | ||
Unit: "days", | ||
UnitCount: 1, | ||
Config: app.Config{ | ||
Archive: true, | ||
UseILM: true, | ||
}, | ||
}, | ||
expectedErr: errors.New("get indices error"), | ||
}, | ||
{ | ||
name: "empty indices error", | ||
setupCallExpectations: func(indexClient *mocks.MockIndexAPI) { | ||
indexClient.On("GetJaegerIndices", "").Return([]client.Index{}, nil) | ||
}, | ||
config: Config{ | ||
Unit: "days", | ||
UnitCount: 1, | ||
Config: app.Config{ | ||
Archive: true, | ||
UseILM: true, | ||
}, | ||
}, | ||
expectedErr: errors.New("no indices to remove from alias jaeger-span-archive-read"), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
indexClient := &mocks.MockIndexAPI{} | ||
lookbackAction := Action{ | ||
Config: test.config, | ||
IndicesClient: indexClient, | ||
} | ||
|
||
test.setupCallExpectations(indexClient) | ||
|
||
err := lookbackAction.Do() | ||
if test.expectedErr != nil { | ||
assert.Error(t, err) | ||
assert.Equal(t, test.expectedErr, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// 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 lookback | ||
|
||
import "time" | ||
|
||
func getTimeReference(currentTime time.Time, units string, unitCount int) time.Time { | ||
switch units { | ||
case "minutes": | ||
return currentTime.Truncate(time.Minute).Add(-time.Duration(unitCount) * time.Minute) | ||
case "hours": | ||
return currentTime.Truncate(time.Hour).Add(-time.Duration(unitCount) * time.Hour) | ||
case "days": | ||
year, month, day := currentTime.Date() | ||
tomorrowMidnight := time.Date(year, month, day, 0, 0, 0, 0, currentTime.Location()).AddDate(0, 0, 1) | ||
return tomorrowMidnight.Add(-time.Hour * 24 * time.Duration(unitCount)) | ||
case "weeks": | ||
year, month, day := currentTime.Date() | ||
tomorrowMidnight := time.Date(year, month, day, 0, 0, 0, 0, currentTime.Location()).AddDate(0, 0, 1) | ||
return tomorrowMidnight.Add(-time.Hour * 24 * time.Duration(7*unitCount)) | ||
case "months": | ||
year, month, day := currentTime.Date() | ||
return time.Date(year, month, day, 0, 0, 0, 0, currentTime.Location()).AddDate(0, -1*unitCount, 0) | ||
case "years": | ||
year, month, day := currentTime.Date() | ||
return time.Date(year, month, day, 0, 0, 0, 0, currentTime.Location()).AddDate(-1*unitCount, 0, 0) | ||
} | ||
|
||
return currentTime.Truncate(time.Second).Add(-time.Duration(unitCount) * time.Second) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// 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 lookback | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/crossdock/crossdock-go/assert" | ||
) | ||
|
||
func TestGetTimeReference(t *testing.T) { | ||
now := time.Date(2021, time.October, 10, 10, 10, 10, 10, time.UTC) | ||
|
||
tests := []struct { | ||
name string | ||
unit string | ||
unitCount int | ||
expectedTime time.Time | ||
}{ | ||
{ | ||
name: "seconds unit", | ||
unit: "seconds", | ||
unitCount: 30, | ||
expectedTime: time.Date(2021, time.October, 10, 10, 9, 40, 00, time.UTC), | ||
}, | ||
{ | ||
name: "minutes unit", | ||
unit: "minutes", | ||
unitCount: 30, | ||
expectedTime: time.Date(2021, time.October, 10, 9, 40, 00, 00, time.UTC), | ||
}, | ||
{ | ||
name: "hours unit", | ||
unit: "hours", | ||
unitCount: 2, | ||
expectedTime: time.Date(2021, time.October, 10, 8, 00, 00, 00, time.UTC), | ||
}, | ||
{ | ||
name: "days unit", | ||
unit: "days", | ||
unitCount: 2, | ||
expectedTime: time.Date(2021, 10, 9, 0, 0, 0, 0, time.UTC), | ||
}, | ||
{ | ||
name: "weeks unit", | ||
unit: "weeks", | ||
unitCount: 2, | ||
expectedTime: time.Date(2021, time.September, 27, 0, 0, 0, 0, time.UTC), | ||
}, | ||
{ | ||
name: "months unit", | ||
unit: "months", | ||
unitCount: 2, | ||
expectedTime: time.Date(2021, time.August, 10, 0, 0, 0, 0, time.UTC), | ||
}, | ||
{ | ||
name: "years unit", | ||
unit: "years", | ||
unitCount: 2, | ||
expectedTime: time.Date(2019, time.October, 10, 0, 0, 0, 0, time.UTC), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
ref := getTimeReference(now, test.unit, test.unitCount) | ||
assert.Equal(t, test.expectedTime, ref) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters