-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9058d1
commit 4c8745f
Showing
3 changed files
with
216 additions
and
7 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
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,197 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/moira-alert/moira/database/redis" | ||
logging "github.com/moira-alert/moira/logging/zerolog_adapter" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
var testTeams = []teamWithID{ | ||
{ | ||
teamStorageElement: teamStorageElement{ | ||
Name: "First team", | ||
Description: "first desc", | ||
}, | ||
ID: "team1", | ||
}, | ||
{ | ||
teamStorageElement: teamStorageElement{ | ||
Name: "Second team", | ||
Description: "second desc", | ||
}, | ||
ID: "team2", | ||
}, | ||
{ | ||
teamStorageElement: teamStorageElement{ | ||
Name: "Third team", | ||
Description: "third desc", | ||
}, | ||
ID: "team3", | ||
}, | ||
{ | ||
teamStorageElement: teamStorageElement{ | ||
Name: "Fourth team", | ||
Description: "fourth desc", | ||
}, | ||
ID: "team4", | ||
}, | ||
} | ||
|
||
func Test_fillTeamNamesHash(t *testing.T) { | ||
Convey("Test filling \"moira-teams-by-names\" redis hash", t, func() { | ||
conf := getDefault() | ||
logger, err := logging.ConfigureLog(conf.LogFile, conf.LogLevel, "test", conf.LogPrettyFormat) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
db := redis.NewTestDatabase(logger) | ||
db.Flush() | ||
defer db.Flush() | ||
|
||
ctx := context.Background() | ||
client := db.Client() | ||
|
||
Convey("with empty database", func() { | ||
err = fillTeamNamesHash(logger, db) | ||
So(err, ShouldBeNil) | ||
|
||
res, existErr := client.Exists(ctx, teamsByNamesKey).Result() | ||
So(existErr, ShouldBeNil) | ||
So(res, ShouldEqual, 0) | ||
}) | ||
|
||
Convey("with teams which have unique names", func() { | ||
defer db.Flush() | ||
|
||
var teamNames, actualTeamNames map[string]string | ||
|
||
teamNames = make(map[string]string, len(testTeams)) | ||
|
||
for _, team := range testTeams { | ||
var teamBytes []byte | ||
|
||
teamBytes, err = getTeamBytes(team) | ||
So(err, ShouldBeNil) | ||
|
||
err = client.HSet(ctx, teamsKey, team.ID, teamBytes).Err() | ||
So(err, ShouldBeNil) | ||
|
||
teamNames[strings.ToLower(team.Name)] = team.ID | ||
} | ||
|
||
err = fillTeamNamesHash(logger, db) | ||
So(err, ShouldBeNil) | ||
|
||
actualTeamNames, err = client.HGetAll(ctx, teamsByNamesKey).Result() | ||
So(err, ShouldBeNil) | ||
So(actualTeamNames, ShouldResemble, teamNames) | ||
}) | ||
|
||
Convey("with teams no unique names", func() { | ||
defer db.Flush() | ||
|
||
testTeams[0].Name = "Team name" | ||
testTeams[1].Name = "teaM name" | ||
testTeams[2].Name = "Team name" | ||
|
||
for _, team := range testTeams { | ||
var teamBytes []byte | ||
|
||
teamBytes, err = getTeamBytes(team) | ||
So(err, ShouldBeNil) | ||
|
||
err = client.HSet(ctx, teamsKey, team.ID, teamBytes).Err() | ||
So(err, ShouldBeNil) | ||
} | ||
|
||
err = fillTeamNamesHash(logger, db) | ||
So(err, ShouldBeNil) | ||
|
||
var actualTeamNames map[string]string | ||
|
||
actualTeamNames, err = client.HGetAll(ctx, teamsByNamesKey).Result() | ||
So(err, ShouldBeNil) | ||
So(actualTeamNames, ShouldHaveLength, len(testTeams)) | ||
|
||
expectedLowercasedTeamNames := []string{"team name", "team name1", "team name2", strings.ToLower(testTeams[3].Name)} | ||
for _, name := range expectedLowercasedTeamNames { | ||
_, ok := actualTeamNames[name] | ||
So(ok, ShouldBeTrue) | ||
} | ||
|
||
for i, team := range testTeams { | ||
Convey(fmt.Sprintf("for team %v fields ok", i), func() { | ||
res, err := client.HGet(ctx, teamsKey, team.ID).Result() | ||
So(err, ShouldBeNil) | ||
|
||
actualTeam, err := unmarshalTeam(team.ID, []byte(res)) | ||
So(err, ShouldBeNil) | ||
So(actualTeam.ID, ShouldEqual, team.ID) | ||
So(actualTeam.Description, ShouldEqual, team.Description) | ||
if i < 3 { | ||
So(actualTeam.Name, ShouldBeIn, []string{team.Name, team.Name + "1", team.Name + "2"}) | ||
} else { | ||
So(actualTeam.Name, ShouldEqual, team.Name) | ||
} | ||
}) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
func Test_removeTeamNamesHash(t *testing.T) { | ||
Convey("Test removing \"moira-teams-by-names\" hash", t, func() { | ||
conf := getDefault() | ||
logger, err := logging.ConfigureLog(conf.LogFile, conf.LogLevel, "test", conf.LogPrettyFormat) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
db := redis.NewTestDatabase(logger) | ||
db.Flush() | ||
defer db.Flush() | ||
|
||
ctx := context.Background() | ||
client := db.Client() | ||
|
||
Convey("with empty database", func() { | ||
err = removeTeamNamesHash(logger, db) | ||
So(err, ShouldBeNil) | ||
|
||
res, existErr := client.Exists(ctx, teamsByNamesKey).Result() | ||
So(existErr, ShouldBeNil) | ||
So(res, ShouldEqual, 0) | ||
}) | ||
|
||
Convey("with filled teams and teams by names hashes", func() { | ||
defer db.Flush() | ||
|
||
for _, team := range testTeams { | ||
var teamBytes []byte | ||
|
||
teamBytes, err = getTeamBytes(team) | ||
So(err, ShouldBeNil) | ||
|
||
err = client.HSet(ctx, teamsKey, team.ID, teamBytes).Err() | ||
So(err, ShouldBeNil) | ||
|
||
err = client.HSet(ctx, teamsByNamesKey, strings.ToLower(team.Name), team.ID).Err() | ||
So(err, ShouldBeNil) | ||
} | ||
|
||
err = removeTeamNamesHash(logger, db) | ||
So(err, ShouldBeNil) | ||
|
||
res, existErr := client.Exists(ctx, teamsByNamesKey).Result() | ||
So(existErr, ShouldBeNil) | ||
So(res, ShouldEqual, 0) | ||
}) | ||
}) | ||
} |