Skip to content

Commit

Permalink
Add lookback tests
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 7, 2021
1 parent b9ae2d5 commit c48c9c7
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 28 deletions.
30 changes: 3 additions & 27 deletions cmd/es-rollover/app/lookback/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/jaegertracing/jaeger/pkg/es/filter"
)

var timeNow func() time.Time = time.Now

// Action holds the configuration and clients for lookback action
type Action struct {
Config
Expand All @@ -49,7 +51,7 @@ func (a *Action) lookback(indexSet app.IndexOption) error {
readAliasName := indexSet.ReadAliasName()
readAliasIndices := filter.ByAlias(jaegerIndicex, []string{readAliasName})
excludedWriteIndex := filter.ByAliasExclude(readAliasIndices, []string{indexSet.WriteAliasName()})
finalIndices := filter.ByDate(excludedWriteIndex, getTimeReference(time.Now(), a.Unit, a.UnitCount))
finalIndices := filter.ByDate(excludedWriteIndex, getTimeReference(timeNow(), a.Unit, a.UnitCount))
if len(finalIndices) == 0 {
return fmt.Errorf("no indices to remove from alias %s", readAliasName)
}
Expand All @@ -65,29 +67,3 @@ func (a *Action) lookback(indexSet app.IndexOption) error {
return a.IndicesClient.DeleteAlias(aliases)

}

func getTimeReference(now time.Time, units string, unitCount int) time.Time {
switch units {
case "minutes":
return now.Truncate(time.Minute).Add(time.Minute).Add(-time.Duration(unitCount) * time.Minute)
case "hours":
return now.Truncate(time.Minute).Add(time.Hour).Add(-time.Duration(unitCount) * time.Hour)
case "days":
year, month, day := time.Now().Date()
return time.Date(year, month, day, 0, 0, 0, 0, now.Location()).AddDate(0, 0, -1*unitCount)
case "weeks":
diff := int(now.Weekday()) - int(time.Saturday)
year, month, day := time.Now().Date()
weekEnd := time.Date(year, month, day, 0, 0, 0, 0, now.Location()).AddDate(0, 0, diff)
return weekEnd.Add(-time.Hour * 24 * 7 * time.Duration(unitCount))
case "months":
year, month, day := time.Now().Date()
return time.Date(year, month, day, 0, 0, 0, 0, now.Location()).AddDate(0, -1*unitCount, 0)
case "years":
year, month, day := time.Now().Date()
return time.Date(year, month, day, 0, 0, 0, 0, now.Location()).AddDate(1*unitCount, 0, 0)
}

return now.Add(-time.Duration(unitCount) * time.Second)

}
151 changes: 151 additions & 0 deletions cmd/es-rollover/app/lookback/action_test.go
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)
}
})
}
}
43 changes: 43 additions & 0 deletions cmd/es-rollover/app/lookback/time_reference.go
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)

}
83 changes: 83 additions & 0 deletions cmd/es-rollover/app/lookback/time_reference_test.go
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)
})
}
}
2 changes: 1 addition & 1 deletion pkg/es/client/cluster_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (c *ClusterClient) Version() (uint, error) {
return 0, fmt.Errorf("invalid version format: %w", versionField)
}
version := strings.Split(versionNumber, ".")
major, err := strconv.ParseUint(version[0], 10, 64)
major, err := strconv.ParseUint(version[0], 10, 32)
if err != nil {
return 0, fmt.Errorf("invalid version format: %s", version[0])
}
Expand Down

0 comments on commit c48c9c7

Please sign in to comment.