Skip to content

Commit

Permalink
chore: add more tests for retention
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Dec 6, 2023
1 parent 9f30224 commit 02f0553
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion scrapers/runscrapers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,16 @@ var _ = Describe("Scrapers test", Ordered, func() {
ConfigClass: "Test",
ScraperID: &dummyScraper.ID,
}
configItemID2 := uuid.New().String()
dummyCI2 := models.ConfigItem{
ID: configItemID2,
ConfigClass: "Test",
ScraperID: &dummyScraper.ID,
}
err = db.DefaultDB().Create(&dummyCI).Error
Expect(err).To(BeNil())
err = db.DefaultDB().Create(&dummyCI2).Error
Expect(err).To(BeNil())

twoDaysAgo := time.Now().Add(-2 * 24 * time.Hour)
fiveDaysAgo := time.Now().Add(-5 * 24 * time.Hour)
Expand All @@ -262,14 +270,16 @@ var _ = Describe("Scrapers test", Ordered, func() {
{ConfigID: configItemID, ChangeType: "TestDiff", CreatedAt: &fiveDaysAgo, ExternalChangeId: uuid.New().String()},
{ConfigID: configItemID, ChangeType: "TestDiff", CreatedAt: &tenDaysAgo, ExternalChangeId: uuid.New().String()},
{ConfigID: configItemID, ChangeType: "TestDiff", CreatedAt: &tenDaysAgo, ExternalChangeId: uuid.New().String()},
{ConfigID: configItemID2, ChangeType: "TestDiff", ExternalChangeId: uuid.New().String()},
{ConfigID: configItemID2, ChangeType: "TestDiff", ExternalChangeId: uuid.New().String()},
}

err = db.DefaultDB().Table("config_changes").Create(&configChanges).Error
Expect(err).To(BeNil())

var currentCount int
err = db.DefaultDB().
Raw(`SELECT COUNT(*) FROM config_changes WHERE change_type = ? AND config_id = ?`, "TestDiff", configItemID).
Raw(`SELECT COUNT(*) FROM config_changes WHERE change_type = ?`, "TestDiff").
Scan(&currentCount).
Error
Expect(err).To(BeNil())
Expand All @@ -288,6 +298,15 @@ var _ = Describe("Scrapers test", Ordered, func() {
Expect(err).To(BeNil())
Expect(count1).To(Equal(15))

// The other config item should not be touched
var otherCount1 int
err = db.DefaultDB().
Raw(`SELECT COUNT(*) FROM config_changes WHERE change_type = ? AND config_id = ?`, "TestDiff", configItemID2).
Scan(&otherCount1).
Error
Expect(err).To(BeNil())
Expect(otherCount1).To(Equal(2))

// Only keep latest 12 config changes
err = ProcessChangeRetention(ctx, dummyScraper.ID, v1.ChangeRetentionSpec{Name: "TestDiff", Count: 12})
Expect(err).To(BeNil())
Expand All @@ -299,6 +318,15 @@ var _ = Describe("Scrapers test", Ordered, func() {
Expect(err).To(BeNil())
Expect(count2).To(Equal(12))

// The other config item should not be touched
var otherCount2 int
err = db.DefaultDB().
Raw(`SELECT COUNT(*) FROM config_changes WHERE change_type = ? AND config_id = ?`, "TestDiff", configItemID2).
Scan(&otherCount2).
Error
Expect(err).To(BeNil())
Expect(otherCount2).To(Equal(2))

// Keep config changes which are newer than 3 days and max count can be 10
err = ProcessChangeRetention(ctx, dummyScraper.ID, v1.ChangeRetentionSpec{Name: "TestDiff", Age: "3d", Count: 10})
Expect(err).To(BeNil())
Expand All @@ -313,6 +341,16 @@ var _ = Describe("Scrapers test", Ordered, func() {
// No params in ChangeRetentionSpec should fail
err = ProcessChangeRetention(ctx, dummyScraper.ID, v1.ChangeRetentionSpec{Name: "TestDiff"})
Expect(err).ToNot(BeNil())

// The other config item should not be touched
var otherCount3 int
err = db.DefaultDB().
Raw(`SELECT COUNT(*) FROM config_changes WHERE change_type = ? AND config_id = ?`, "TestDiff", configItemID2).
Scan(&otherCount3).
Error
Expect(err).To(BeNil())
Expect(otherCount3).To(Equal(2))

})
})
})
Expand Down

0 comments on commit 02f0553

Please sign in to comment.