Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add unit tests for crons_monitor_data #79

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions crons_monitor_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,20 @@ func (c *CronsMonitorData) addJob(job *batchv1.Job, checkinId sentry.EventID) er
return nil
}

// wrapper struct over crons monitor map that
// handles syncrhonization
// Wrapper struct over crons monitor map that
// handles synchronization
type CronsMetaData struct {
mutex *sync.RWMutex
cronsMonitorDataMap map[string]*CronsMonitorData
}

func NewCronsMetaData() *CronsMetaData {
return &CronsMetaData{
mutex: &sync.RWMutex{},
cronsMonitorDataMap: make(map[string]*CronsMonitorData),
}
}

func (c *CronsMetaData) addCronsMonitorData(cronjobName string, newCronsMonitorData *CronsMonitorData) {
c.mutex.Lock()
defer c.mutex.Unlock()
Expand All @@ -88,10 +95,3 @@ func (c *CronsMetaData) getCronsMonitorData(cronjobName string) (*CronsMonitorDa
cronsMonitorData, ok := c.cronsMonitorDataMap[cronjobName]
return cronsMonitorData, ok
}

func NewCronsMetaData() *CronsMetaData {
return &CronsMetaData{
mutex: &sync.RWMutex{},
cronsMonitorDataMap: make(map[string]*CronsMonitorData),
}
}
163 changes: 163 additions & 0 deletions crons_monitor_data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package main

import (
"testing"

"github.com/getsentry/sentry-go"
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestNewCronsJobData(t *testing.T) {

fakeId := "080181f33ca343f89b0bf55d50abfeee"

cronsJobData := NewCronsJobData(sentry.EventID(fakeId))
if cronsJobData == nil {
t.Errorf("Failed to create cronsJobData")
return
}
if cronsJobData.CheckinId != sentry.EventID(fakeId) {
t.Errorf("The cronsJobData set to incorrect ID")
}
}

func TestGetCheckinId(t *testing.T) {

fakeId := "080181f33ca343f89b0bf55d50abfeee"

cronsJobData := NewCronsJobData(sentry.EventID(fakeId))
if cronsJobData == nil {
t.Errorf("Failed to create cronsJobData")
return
}
if cronsJobData.getCheckinId() != sentry.EventID(fakeId) {
t.Errorf("Retrieved incorrect checkin ID")
}
}

func TestNewCronsMonitorData(t *testing.T) {

fakeMonitorSlug := "cronjob-slug"
fakeSchedule := "* * * * *"
var fakeCompletions int32 = 3

cronsMonitorData := NewCronsMonitorData(fakeMonitorSlug, fakeSchedule, &fakeCompletions)

if cronsMonitorData.MonitorSlug != fakeMonitorSlug {
t.Errorf("The monitor slug is incorrect")
}
if cronsMonitorData.monitorConfig.Schedule != sentry.CrontabSchedule(fakeSchedule) {
t.Errorf("The schedule is incorrect")
}
if cronsMonitorData.JobDatas == nil {
t.Errorf("Failed to create jobDatas map")
}
if cronsMonitorData.requiredCompletions != fakeCompletions {
t.Errorf("The completions is incorrect")
}
}

func TestAddJob(t *testing.T) {

fakeId := "080181f33ca343f89b0bf55d50abfeee"
fakeMonitorSlug := "cronjob-slug"
fakeSchedule := "* * * * *"
var fakeCompletions int32 = 3

cronsMonitorData := NewCronsMonitorData(fakeMonitorSlug, fakeSchedule, &fakeCompletions)

jobObj := &batchv1.Job{
ObjectMeta: v1.ObjectMeta{
Name: "TestAddJobJob",
},
}
cronsMonitorData.addJob(jobObj, sentry.EventID(fakeId))

jobData, ok := cronsMonitorData.JobDatas["TestAddJobJob"]
if !ok {
t.Errorf("Failed to add job data")
}
if jobData.CheckinId != sentry.EventID(fakeId) {
t.Errorf("Incorrect checkin ID")
}
}

func TestNewCronsMetaData(t *testing.T) {

cronsMetaData := NewCronsMetaData()
if cronsMetaData.cronsMonitorDataMap == nil {
t.Errorf("Failed to create cronsMonitorDataMap")
}
}

func TestAddCronsMonitorData(t *testing.T) {

cronsMetaData := NewCronsMetaData()
if cronsMetaData.cronsMonitorDataMap == nil {
t.Errorf("Failed to create cronsMonitorDataMap")
}

fakeMonitorSlug := "TestAddCronsMonitorDataCronJob"
fakeSchedule := "* * * * *"
var fakeCompletions int32 = 3
cronsMonitorData := NewCronsMonitorData(fakeMonitorSlug, fakeSchedule, &fakeCompletions)

cronsMetaData.addCronsMonitorData("TestAddCronsMonitorDataCronJob", cronsMonitorData)

retCronsMonitorData, ok := cronsMetaData.cronsMonitorDataMap["TestAddCronsMonitorDataCronJob"]
if !ok {
t.Errorf("Failed to add cronsMonitorData to map")
}
if retCronsMonitorData != cronsMonitorData {
t.Errorf("Failed to add correct cronsMonitorData to map")
}
}

func TestDeleteCronsMonitorData(t *testing.T) {

cronsMetaData := NewCronsMetaData()
if cronsMetaData.cronsMonitorDataMap == nil {
t.Errorf("Failed to create cronsMonitorDataMap")
}

fakeMonitorSlug := "TestAddCronsMonitorDataCronJob"
fakeSchedule := "* * * * *"
var fakeCompletions int32 = 3
cronsMonitorData := NewCronsMonitorData(fakeMonitorSlug, fakeSchedule, &fakeCompletions)

cronsMetaData.cronsMonitorDataMap["TestAddCronsMonitorDataCronJob"] = cronsMonitorData

cronsMetaData.deleteCronsMonitorData("TestAddCronsMonitorDataCronJob")

_, ok := cronsMetaData.cronsMonitorDataMap["TestAddCronsMonitorDataCronJob"]

if ok {
t.Errorf("Failed to delete cronsMonitorData from map")
}

}

func TestGetCronsMonitorData(t *testing.T) {

cronsMetaData := NewCronsMetaData()
if cronsMetaData.cronsMonitorDataMap == nil {
t.Errorf("Failed to create cronsMonitorDataMap")
}

fakeMonitorSlug := "TestAddCronsMonitorDataCronJob"
fakeSchedule := "* * * * *"
var fakeCompletions int32 = 3
cronsMonitorData := NewCronsMonitorData(fakeMonitorSlug, fakeSchedule, &fakeCompletions)

cronsMetaData.cronsMonitorDataMap["TestAddCronsMonitorDataCronJob"] = cronsMonitorData

retCronsMonitorData, ok := cronsMetaData.getCronsMonitorData("TestAddCronsMonitorDataCronJob")
if !ok {
t.Errorf("Failed to get cronsMonitorData to map")
}
if retCronsMonitorData != cronsMonitorData {
t.Errorf("Failed to get correct cronsMonitorData to map")
}

}