Skip to content

Commit 2d55c8a

Browse files
yhabteabjulianbrost
authored andcommitted
Add test cases for object.RestoreMutedObjects()
1 parent 683ea69 commit 2d55c8a

File tree

3 files changed

+116
-13
lines changed

3 files changed

+116
-13
lines changed

internal/incident/incidents_test.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package incident
22

33
import (
44
"context"
5-
"crypto/rand"
6-
"fmt"
75
"github.com/icinga/icinga-go-library/database"
86
"github.com/icinga/icinga-go-library/logging"
97
"github.com/icinga/icinga-go-library/types"
@@ -138,10 +136,10 @@ func makeIncident(ctx context.Context, db *database.DB, t *testing.T, recovered
138136
ev := &event.Event{
139137
Time: time.Time{},
140138
SourceId: 1,
141-
Name: makeRandomString(t),
139+
Name: testutils.MakeRandomString(t),
142140
Tags: map[string]string{ // Always generate unique object tags not to produce same object ID!
143-
"host": makeRandomString(t),
144-
"service": makeRandomString(t),
141+
"host": testutils.MakeRandomString(t),
142+
"service": testutils.MakeRandomString(t),
145143
},
146144
ExtraTags: map[string]string{
147145
"hostgroup/database-server": "",
@@ -167,11 +165,3 @@ func makeIncident(ctx context.Context, db *database.DB, t *testing.T, recovered
167165

168166
return i
169167
}
170-
171-
func makeRandomString(t *testing.T) string {
172-
buf := make([]byte, 20)
173-
_, err := rand.Read(buf)
174-
require.NoError(t, err, "failed to generate random string")
175-
176-
return fmt.Sprintf("%x", buf)
177-
}

internal/object/objects_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package object
2+
3+
import (
4+
"context"
5+
"github.com/icinga/icinga-go-library/database"
6+
"github.com/icinga/icinga-go-library/types"
7+
"github.com/icinga/icinga-notifications/internal/event"
8+
"github.com/icinga/icinga-notifications/internal/testutils"
9+
"github.com/icinga/icinga-notifications/internal/utils"
10+
"github.com/jmoiron/sqlx"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
"testing"
14+
"time"
15+
)
16+
17+
func TestRestoreMutedObjects(t *testing.T) {
18+
ctx := context.Background()
19+
db := testutils.GetTestDB(ctx, t)
20+
21+
var sourceID int64
22+
err := utils.RunInTx(ctx, db, func(tx *sqlx.Tx) error {
23+
args := map[string]interface{}{
24+
"type": "notifications",
25+
"name": "Icinga Notifications",
26+
"insecure": "n",
27+
}
28+
// We can't use config.Source here unfortunately due to cyclic import error!
29+
id, err := utils.InsertAndFetchId(ctx, tx, `INSERT INTO source (type, name, icinga2_insecure_tls) VALUES (:type, :name, :insecure)`, args)
30+
require.NoError(t, err, "populating source table should not fail")
31+
32+
sourceID = id
33+
return nil
34+
})
35+
require.NoError(t, err, "utils.RunInTx() should not fail")
36+
37+
ClearCache()
38+
39+
// Just to make sure that there are no objects that have already been muted.
40+
require.NoError(t, RestoreMutedObjects(ctx, db), "restoring muted objects shouldn't fail")
41+
require.Len(t, cache, 0, "found mysterious muted objects")
42+
43+
testObjects := map[string]*Object{}
44+
for i := 0; i < 20; i++ {
45+
o := makeObject(ctx, db, t, sourceID, true)
46+
testObjects[o.ID.String()] = o
47+
if i%2 == 0 { // Insert also some unmuted objects
48+
makeObject(ctx, db, t, sourceID, false)
49+
}
50+
}
51+
ClearCache()
52+
53+
require.NoError(t, RestoreMutedObjects(ctx, db), "restoring muted objects shouldn't fail")
54+
assert.Len(t, cache, len(testObjects), "all muted objects should be restored")
55+
56+
for _, o := range testObjects {
57+
objFromCache := GetFromCache(o.ID)
58+
assert.NotNilf(t, objFromCache, "muted object %q was not restored correctly", o.DisplayName())
59+
60+
if objFromCache != nil {
61+
assert.True(t, objFromCache.IsMuted(), "object should be muted")
62+
assert.Equal(t, o.Name, objFromCache.Name, "objects name should match")
63+
assert.Equal(t, o.URL, objFromCache.URL, "objects url should match")
64+
65+
assert.Equal(t, o.Tags, objFromCache.Tags, "objects tags should match")
66+
assert.Equal(t, o.ExtraTags, objFromCache.ExtraTags, "objects tags should match")
67+
}
68+
69+
// Purge all newly created objects and its relations not mes up local database tests.
70+
_, err = db.NamedExecContext(ctx, `DELETE FROM object_id_tag WHERE object_id = :id`, o)
71+
assert.NoError(t, err, "deleting object id tags should not fail")
72+
73+
_, err = db.NamedExecContext(ctx, `DELETE FROM object_extra_tag WHERE object_id = :id`, o)
74+
assert.NoError(t, err, "deleting object extra tags should not fail")
75+
76+
_, err = db.NamedExecContext(ctx, `DELETE FROM object WHERE id = :id`, o)
77+
assert.NoError(t, err, "deleting object should not fail")
78+
}
79+
}
80+
81+
func makeObject(ctx context.Context, db *database.DB, t *testing.T, sourceID int64, mute bool) *Object {
82+
ev := &event.Event{
83+
Time: time.Time{},
84+
SourceId: sourceID,
85+
Name: testutils.MakeRandomString(t),
86+
Mute: types.Bool{Valid: true, Bool: mute},
87+
MuteReason: "Just for testing",
88+
Tags: map[string]string{ // Always generate unique object tags not to produce same object ID!
89+
"host": testutils.MakeRandomString(t),
90+
"service": testutils.MakeRandomString(t),
91+
},
92+
ExtraTags: map[string]string{
93+
"hostgroup/database-server": "",
94+
"servicegroup/webserver": "",
95+
},
96+
}
97+
98+
o, err := FromEvent(ctx, db, ev)
99+
require.NoError(t, err)
100+
101+
return o
102+
}

internal/testutils/testutils.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package testutils
22

33
import (
44
"context"
5+
"crypto/rand"
6+
"fmt"
57
"github.com/creasty/defaults"
68
"github.com/icinga/icinga-go-library/database"
79
"github.com/icinga/icinga-go-library/logging"
@@ -55,3 +57,12 @@ func GetTestDB(ctx context.Context, t *testing.T) *database.DB {
5557

5658
return db
5759
}
60+
61+
// MakeRandomString returns a 20 byte random hex string.
62+
func MakeRandomString(t *testing.T) string {
63+
buf := make([]byte, 20)
64+
_, err := rand.Read(buf)
65+
require.NoError(t, err, "failed to generate random string")
66+
67+
return fmt.Sprintf("%x", buf)
68+
}

0 commit comments

Comments
 (0)