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