From f9fa73abf1488b6b4260e189e399bb8bc6972a65 Mon Sep 17 00:00:00 2001 From: almostinf Date: Tue, 5 Nov 2024 16:36:31 +0300 Subject: [PATCH 1/4] add new heartbeat types --- database/redis/emergency_contact_test.go | 12 ++++++------ database/redis/reply/emergency_contacts_test.go | 4 ++-- datatypes/emergency_contact.go | 10 +++++++--- datatypes/emergency_contact_test.go | 6 +++++- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/database/redis/emergency_contact_test.go b/database/redis/emergency_contact_test.go index 9da23eb0a..cd5a36993 100644 --- a/database/redis/emergency_contact_test.go +++ b/database/redis/emergency_contact_test.go @@ -18,12 +18,12 @@ var ( testEmergencyContact = datatypes.EmergencyContact{ ContactID: testContactID, - HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifierOff}, + HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifier}, } testEmergencyContact2 = datatypes.EmergencyContact{ ContactID: testContactID2, - HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifierOff}, + HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifier}, } testEmergencyContact3 = datatypes.EmergencyContact{ @@ -147,7 +147,7 @@ func TestGetHeartbeatTypeContactIDs(t *testing.T) { Convey("Test GetHeartbeatTypeContactIDs", t, func() { Convey("Without any emergency contacts by heartbeat type", func() { - emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(datatypes.HeartbeatNotifierOff) + emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(datatypes.HeartbeatNotifier) So(err, ShouldBeNil) So(emergencyContactIDs, ShouldBeEmpty) }) @@ -159,7 +159,7 @@ func TestGetHeartbeatTypeContactIDs(t *testing.T) { testEmergencyContact3, }) - emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(datatypes.HeartbeatNotifierOff) + emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(datatypes.HeartbeatNotifier) So(err, ShouldBeNil) assert.ElementsMatch(t, emergencyContactIDs, []string{ testContactID, @@ -197,7 +197,7 @@ func TestSaveEmergencyContact(t *testing.T) { So(err, ShouldBeNil) So(emergencyContacts, ShouldResemble, expectedEmergencyContacts) - emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(datatypes.HeartbeatNotifierOff) + emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(datatypes.HeartbeatNotifier) So(err, ShouldBeNil) So(emergencyContactIDs, ShouldResemble, expectedEmergencyContactIDs) }) @@ -230,7 +230,7 @@ func TestSaveEmergencyContacts(t *testing.T) { So(err, ShouldBeNil) assert.ElementsMatch(t, emergencyContacts, expectedEmergencyContacts) - emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(datatypes.HeartbeatNotifierOff) + emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(datatypes.HeartbeatNotifier) So(err, ShouldBeNil) assert.ElementsMatch(t, emergencyContactIDs, expectedEmergencyContactIDs) }) diff --git a/database/redis/reply/emergency_contacts_test.go b/database/redis/reply/emergency_contacts_test.go index 4916a3c94..15b981d23 100644 --- a/database/redis/reply/emergency_contacts_test.go +++ b/database/redis/reply/emergency_contacts_test.go @@ -10,14 +10,14 @@ import ( ) const ( - testEmergencyContactVal = `{"contact_id":"test-contact-id","heartbeat_types":["notifier_off"]}` + testEmergencyContactVal = `{"contact_id":"test-contact-id","heartbeat_types":["heartbeat_notifier"]}` testEmptyEmergencyContactVal = `{"contact_id":"","heartbeat_types":null}` ) var ( testEmergencyContact = datatypes.EmergencyContact{ ContactID: "test-contact-id", - HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifierOff}, + HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifier}, } testEmptyEmergencyContact = datatypes.EmergencyContact{} ) diff --git a/datatypes/emergency_contact.go b/datatypes/emergency_contact.go index deec88061..1a1bb02d1 100644 --- a/datatypes/emergency_contact.go +++ b/datatypes/emergency_contact.go @@ -4,14 +4,18 @@ package datatypes type HeartbeatType string const ( - HeartbeatTypeNotSet HeartbeatType = "type_not_set" - HeartbeatNotifierOff HeartbeatType = "notifier_off" + HeartbeatTypeNotSet HeartbeatType = "heartbeat_type_not_set" + HeartbeatNotifier HeartbeatType = "heartbeat_notifier" + HeartbeatDatabase HeartbeatType = "heartbeat_database" + HeartbeatLocalChecker HeartbeatType = "heartbeat_local_checker" + HeartbeatRemoteChecker HeartbeatType = "heartbeat_remote_checker" + HeartbeatFilter HeartbeatType = "heartbeat_filter" ) // IsValid checks if such an heartbeat type exists. func (heartbeatType HeartbeatType) IsValid() bool { switch heartbeatType { - case HeartbeatNotifierOff: + case HeartbeatNotifier, HeartbeatDatabase, HeartbeatLocalChecker, HeartbeatRemoteChecker, HeartbeatFilter: return true default: return false diff --git a/datatypes/emergency_contact_test.go b/datatypes/emergency_contact_test.go index ec0389b32..4e9b85b43 100644 --- a/datatypes/emergency_contact_test.go +++ b/datatypes/emergency_contact_test.go @@ -10,7 +10,11 @@ func TestIsValidHeartbeatType(t *testing.T) { Convey("Test IsValid heartbeat type", t, func() { Convey("Test valid cases", func() { testcases := []HeartbeatType{ - HeartbeatNotifierOff, + HeartbeatNotifier, + HeartbeatDatabase, + HeartbeatLocalChecker, + HeartbeatRemoteChecker, + HeartbeatFilter, } for _, testcase := range testcases { From 82fcf1ce956a6840b670b0f6501942c0db74bf4a Mon Sep 17 00:00:00 2001 From: almostinf Date: Tue, 5 Nov 2024 17:44:48 +0300 Subject: [PATCH 2/4] fix api with new heartbeat types --- api/controller/contact_test.go | 2 +- api/controller/emergency_contact_test.go | 6 +++--- api/dto/emergency_contact_test.go | 2 +- api/handler/contact_test.go | 2 +- api/handler/emergency_contact_test.go | 6 +++--- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/api/controller/contact_test.go b/api/controller/contact_test.go index 8144f2cf4..7b2411448 100644 --- a/api/controller/contact_test.go +++ b/api/controller/contact_test.go @@ -890,7 +890,7 @@ func TestRemoveContact(t *testing.T) { Convey("With emergency contact", func() { emergencyContact := datatypes.EmergencyContact{ ContactID: contactID, - HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifierOff}, + HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifier}, } dataBase.EXPECT().GetTeamSubscriptionIDs(teamID).Return(make([]string, 0), nil) dataBase.EXPECT().GetEmergencyContact(contactID).Return(emergencyContact, nil) diff --git a/api/controller/emergency_contact_test.go b/api/controller/emergency_contact_test.go index b0c5c0d14..85b62cd31 100644 --- a/api/controller/emergency_contact_test.go +++ b/api/controller/emergency_contact_test.go @@ -21,7 +21,7 @@ var ( testEmergencyContact = datatypes.EmergencyContact{ ContactID: testContactID, - HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifierOff}, + HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifier}, } testEmergencyContact2 = datatypes.EmergencyContact{ ContactID: testContactID2, @@ -30,7 +30,7 @@ var ( testEmergencyContactDTO = dto.EmergencyContact{ ContactID: testContactID, - HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifierOff}, + HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifier}, } testEmergencyContact2DTO = dto.EmergencyContact{ ContactID: testContactID2, @@ -286,7 +286,7 @@ func TestUpdateEmergencyContact(t *testing.T) { Convey("With empty contact id", func() { emergencyContactDTO := dto.EmergencyContact{ - HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifierOff}, + HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifier}, } database.EXPECT().SaveEmergencyContact(testEmergencyContact).Return(nil) diff --git a/api/dto/emergency_contact_test.go b/api/dto/emergency_contact_test.go index 4e4790dfd..8d0725fae 100644 --- a/api/dto/emergency_contact_test.go +++ b/api/dto/emergency_contact_test.go @@ -12,7 +12,7 @@ var ( testEmergencyContact = datatypes.EmergencyContact{ ContactID: testContactID, - HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifierOff}, + HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifier}, } ) diff --git a/api/handler/contact_test.go b/api/handler/contact_test.go index 8369c504b..e307fb090 100644 --- a/api/handler/contact_test.go +++ b/api/handler/contact_test.go @@ -837,7 +837,7 @@ func TestRemoveContact(t *testing.T) { emergencyContact := datatypes.EmergencyContact{ ContactID: contactID, - HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifierOff}, + HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifier}, } mockDb.EXPECT().GetUserSubscriptionIDs(defaultLogin).Return([]string{"test"}, nil).Times(1) diff --git a/api/handler/emergency_contact_test.go b/api/handler/emergency_contact_test.go index d2ac2774a..ac4bd67b4 100644 --- a/api/handler/emergency_contact_test.go +++ b/api/handler/emergency_contact_test.go @@ -28,7 +28,7 @@ var ( testEmergencyContact = datatypes.EmergencyContact{ ContactID: testContactID, - HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifierOff}, + HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifier}, } testEmergencyContact2 = datatypes.EmergencyContact{ ContactID: testContactID2, @@ -247,7 +247,7 @@ func TestCreateEmergencyContact(t *testing.T) { Convey("Try to create emergency contact without contact id", func() { emergencyContact := datatypes.EmergencyContact{ - HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifierOff}, + HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifier}, } emergencyContactDTO := dto.EmergencyContact(emergencyContact) @@ -473,7 +473,7 @@ func TestUpdateEmergencyContact(t *testing.T) { Convey("Successfully update emergency contact without contact id in dto", func() { emergencyContact := datatypes.EmergencyContact{ - HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifierOff}, + HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifier}, } emergencyContactDTO := dto.EmergencyContact(emergencyContact) From ad85a9771b44718054328b097758c527665f3413 Mon Sep 17 00:00:00 2001 From: almostinf Date: Tue, 5 Nov 2024 18:03:10 +0300 Subject: [PATCH 3/4] fix heartbeats --- go.mod | 9 +-- go.sum | 17 ++++-- notifier/selfstate/heartbeat/database.go | 7 +-- notifier/selfstate/heartbeat/database_test.go | 27 ++------- notifier/selfstate/heartbeat/filter.go | 9 +-- notifier/selfstate/heartbeat/filter_test.go | 27 ++------- notifier/selfstate/heartbeat/heartbeat.go | 7 +-- .../selfstate/heartbeat/heartbeat_test.go | 55 ++++++++++++------- notifier/selfstate/heartbeat/local_checker.go | 9 +-- .../selfstate/heartbeat/local_checker_test.go | 27 ++------- notifier/selfstate/heartbeat/notifier.go | 7 +-- notifier/selfstate/heartbeat/notifier_test.go | 20 +------ .../selfstate/heartbeat/remote_checker.go | 9 +-- .../heartbeat/remote_checker_test.go | 27 ++------- 14 files changed, 88 insertions(+), 169 deletions(-) diff --git a/go.mod b/go.mod index b309808bd..cc875b256 100644 --- a/go.mod +++ b/go.mod @@ -46,7 +46,7 @@ require ( require github.com/prometheus/common v0.37.0 require ( - github.com/go-playground/validator/v10 v10.4.1 + github.com/go-playground/validator/v10 v10.22.1 github.com/hashicorp/golang-lru/v2 v2.0.7 github.com/mattermost/mattermost/server/public v0.1.1 github.com/mitchellh/mapstructure v1.5.0 @@ -173,19 +173,20 @@ require ( github.com/blang/semver/v4 v4.0.0 // indirect github.com/dyatlov/go-opengraph/opengraph v0.0.0-20220524092352-606d7b1e5f8a // indirect github.com/fatih/color v1.16.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/go-openapi/jsonpointer v0.20.0 // indirect github.com/go-openapi/jsonreference v0.20.2 // indirect github.com/go-openapi/spec v0.20.9 // indirect github.com/go-openapi/swag v0.22.4 // indirect - github.com/go-playground/locales v0.13.0 // indirect - github.com/go-playground/universal-translator v0.17.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect github.com/hashicorp/go-hclog v1.6.3 // indirect github.com/hashicorp/go-plugin v1.6.0 // indirect github.com/hashicorp/yamux v0.1.1 // indirect github.com/huandu/xstrings v1.3.3 // indirect github.com/imdario/mergo v0.3.11 // indirect github.com/josharian/intern v1.0.0 // indirect - github.com/leodido/go-urn v1.2.0 // indirect + github.com/leodido/go-urn v1.4.0 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/mitchellh/copystructure v1.0.0 // indirect github.com/mitchellh/go-testing-interface v1.14.1 // indirect diff --git a/go.sum b/go.sum index a013bebd9..30410d294 100644 --- a/go.sum +++ b/go.sum @@ -238,6 +238,8 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4 github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= github.com/go-asn1-ber/asn1-ber v1.5.5 h1:MNHlNMBDgEKD4TcKr36vQN68BA00aDfjIt3/bD50WnA= @@ -279,14 +281,18 @@ github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/ github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= github.com/go-openapi/swag v0.22.4 h1:QLMzNJnMGPRNDCbySlcj1x01tzU8/9LTTL9hZZZogBU= github.com/go-openapi/swag v0.22.4/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= -github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= -github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= -github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= -github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= +github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA= +github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= github.com/go-redis/redis/v7 v7.4.0 h1:7obg6wUoj05T0EpY0o8B59S9w5yeMWql7sw2kwNW1x4= @@ -527,8 +533,9 @@ github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lomik/og-rek v0.0.0-20170411191824-628eefeb8d80 h1:KVyDGUXjVOdHQt24wIgY4ZdGFXHtQHLWw0L/MAK3Kb0= diff --git a/notifier/selfstate/heartbeat/database.go b/notifier/selfstate/heartbeat/database.go index 47b466ffa..592325ef1 100644 --- a/notifier/selfstate/heartbeat/database.go +++ b/notifier/selfstate/heartbeat/database.go @@ -53,14 +53,9 @@ func (heartbeater *databaseHeartbeater) Check() (State, error) { return StateOK, err } -// NeedTurnOffNotifier is a function that checks to see if the notifier needs to be turned off. -func (heartbeater databaseHeartbeater) NeedTurnOffNotifier() bool { - return heartbeater.cfg.NeedTurnOffNotifier -} - // Type is a function that returns the current heartbeat type. func (databaseHeartbeater) Type() datatypes.HeartbeatType { - return datatypes.HeartbeatTypeNotSet + return datatypes.HeartbeatDatabase } // AlertSettings is a function that returns the current settings for alerts. diff --git a/notifier/selfstate/heartbeat/database_test.go b/notifier/selfstate/heartbeat/database_test.go index 99312d892..66d4277f6 100644 --- a/notifier/selfstate/heartbeat/database_test.go +++ b/notifier/selfstate/heartbeat/database_test.go @@ -34,7 +34,11 @@ func TestNewDatabaseHeartbeater(t *testing.T) { }) Convey("Without redis disconnect delay", func() { - cfg := DatabaseHeartbeaterConfig{} + cfg := DatabaseHeartbeaterConfig{ + HeartbeaterBaseConfig: HeartbeaterBaseConfig{ + Enabled: true, + }, + } databaseHeartbeater, err := NewDatabaseHeartbeater(cfg, heartbeaterBase) So(errors.As(err, &validationErr), ShouldBeTrue) @@ -107,25 +111,6 @@ func TestDatabaseHeartbeaterCheck(t *testing.T) { }) } -func TestDatabaseHeartbeaterNeedTurnOffNotifier(t *testing.T) { - _, _, _, heartbeaterBase := heartbeaterHelper(t) - - Convey("Test databaseHeartbeater.TurnOffNotifier", t, func() { - cfg := DatabaseHeartbeaterConfig{ - HeartbeaterBaseConfig: HeartbeaterBaseConfig{ - NeedTurnOffNotifier: true, - }, - RedisDisconnectDelay: defaultRedisDisconnectDelay, - } - - databaseHeartbeater, err := NewDatabaseHeartbeater(cfg, heartbeaterBase) - So(err, ShouldBeNil) - - needTurnOffNotifier := databaseHeartbeater.NeedTurnOffNotifier() - So(needTurnOffNotifier, ShouldBeTrue) - }) -} - func TestDatabaseHeartbeaterType(t *testing.T) { _, _, _, heartbeaterBase := heartbeaterHelper(t) @@ -138,7 +123,7 @@ func TestDatabaseHeartbeaterType(t *testing.T) { So(err, ShouldBeNil) databaseHeartbeaterType := databaseHeartbeater.Type() - So(databaseHeartbeaterType, ShouldResemble, datatypes.HeartbeatTypeNotSet) + So(databaseHeartbeaterType, ShouldResemble, datatypes.HeartbeatDatabase) }) } diff --git a/notifier/selfstate/heartbeat/filter.go b/notifier/selfstate/heartbeat/filter.go index 73ff3fcc3..15a222b2b 100644 --- a/notifier/selfstate/heartbeat/filter.go +++ b/notifier/selfstate/heartbeat/filter.go @@ -19,7 +19,7 @@ var ( type FilterHeartbeaterConfig struct { HeartbeaterBaseConfig - MetricReceivedDelay time.Duration `validate:"required,gt=0"` + MetricReceivedDelay time.Duration `validate:"required_if=Enabled true,gte=0"` } type filterHeartbeater struct { @@ -67,14 +67,9 @@ func (heartbeater *filterHeartbeater) Check() (State, error) { return StateOK, nil } -// NeedTurnOffNotifier is a function that checks to see if the notifier needs to be turned off. -func (heartbeater filterHeartbeater) NeedTurnOffNotifier() bool { - return heartbeater.cfg.NeedTurnOffNotifier -} - // Type is a function that returns the current heartbeat type. func (filterHeartbeater) Type() datatypes.HeartbeatType { - return datatypes.HeartbeatTypeNotSet + return datatypes.HeartbeatFilter } // AlertSettings is a function that returns the current settings for alerts. diff --git a/notifier/selfstate/heartbeat/filter_test.go b/notifier/selfstate/heartbeat/filter_test.go index fe54dc894..52f29ebde 100644 --- a/notifier/selfstate/heartbeat/filter_test.go +++ b/notifier/selfstate/heartbeat/filter_test.go @@ -32,7 +32,11 @@ func TestNewFilterHeartbeater(t *testing.T) { }) Convey("Without metric received delay", func() { - cfg := FilterHeartbeaterConfig{} + cfg := FilterHeartbeaterConfig{ + HeartbeaterBaseConfig: HeartbeaterBaseConfig{ + Enabled: true, + }, + } filterHeartbeater, err := NewFilterHeartbeater(cfg, heartbeaterBase) So(errors.As(err, &validationErr), ShouldBeTrue) @@ -149,25 +153,6 @@ func TestFilterHeartbeaterCheck(t *testing.T) { }) } -func TestFilterHeartbeaterNeedTurnOffNotifier(t *testing.T) { - _, _, _, heartbeaterBase := heartbeaterHelper(t) - - Convey("Test filterHeartbeater.TurnOffNotifier", t, func() { - cfg := FilterHeartbeaterConfig{ - HeartbeaterBaseConfig: HeartbeaterBaseConfig{ - NeedTurnOffNotifier: true, - }, - MetricReceivedDelay: defaultMetricReceivedDelay, - } - - filterHeartbeater, err := NewFilterHeartbeater(cfg, heartbeaterBase) - So(err, ShouldBeNil) - - needTurnOffNotifier := filterHeartbeater.NeedTurnOffNotifier() - So(needTurnOffNotifier, ShouldBeTrue) - }) -} - func TestFilterHeartbeaterType(t *testing.T) { _, _, _, heartbeaterBase := heartbeaterHelper(t) @@ -180,7 +165,7 @@ func TestFilterHeartbeaterType(t *testing.T) { So(err, ShouldBeNil) filterHeartbeaterType := filterHeartbeater.Type() - So(filterHeartbeaterType, ShouldResemble, datatypes.HeartbeatTypeNotSet) + So(filterHeartbeaterType, ShouldResemble, datatypes.HeartbeatFilter) }) } diff --git a/notifier/selfstate/heartbeat/heartbeat.go b/notifier/selfstate/heartbeat/heartbeat.go index ef8d72852..3a85f4094 100644 --- a/notifier/selfstate/heartbeat/heartbeat.go +++ b/notifier/selfstate/heartbeat/heartbeat.go @@ -17,7 +17,7 @@ const ( // IsDegraded checks if the condition has degraded. func (lastState State) IsDegraded(newState State) bool { - return lastState == StateOK && newState == StateError + return newState == StateError } // IsRecovered checks if the condition has recovered. @@ -28,7 +28,6 @@ func (lastState State) IsRecovered(newState State) bool { // Heartbeater is the interface for simplified events verification. type Heartbeater interface { Check() (State, error) - NeedTurnOffNotifier() bool AlertSettings() AlertConfig Type() datatypes.HeartbeatType } @@ -39,12 +38,12 @@ type HeartbeaterBaseConfig struct { NeedTurnOffNotifier bool NeedToCheckOthers bool - AlertCfg AlertConfig `validate:"required_if=Enabled true"` + AlertCfg AlertConfig } // AlertConfig contains the configuration of the alerts that heartbeater sends out. type AlertConfig struct { - Name string `validate:"required_if=Enabled true"` + Name string Desc string } diff --git a/notifier/selfstate/heartbeat/heartbeat_test.go b/notifier/selfstate/heartbeat/heartbeat_test.go index 539c44587..5dd2961e2 100644 --- a/notifier/selfstate/heartbeat/heartbeat_test.go +++ b/notifier/selfstate/heartbeat/heartbeat_test.go @@ -31,8 +31,16 @@ func heartbeaterHelper(t *testing.T) (*mock_moira_alert.MockDatabase, *mock_cloc } func TestStateIsDegradated(t *testing.T) { - Convey("Test state.IsDegradated", t, func() { - Convey("With degradated state", func() { + Convey("Test state.IsDegraded", t, func() { + Convey("With continue degraded", func() { + lastState := StateError + newState := StateError + + degradated := lastState.IsDegraded(newState) + So(degradated, ShouldBeTrue) + }) + + Convey("With degraded state", func() { lastState := StateOK newState := StateError @@ -40,13 +48,21 @@ func TestStateIsDegradated(t *testing.T) { So(degradated, ShouldBeTrue) }) - Convey("Without degradated state", func() { + Convey("Without degraded state", func() { lastState := StateError newState := StateOK degradated := lastState.IsDegraded(newState) So(degradated, ShouldBeFalse) }) + + Convey("With continue recovered", func() { + lastState := StateOK + newState := StateOK + + degradated := lastState.IsDegraded(newState) + So(degradated, ShouldBeFalse) + }) }) } @@ -67,6 +83,22 @@ func TestStateIsRecovered(t *testing.T) { recovered := lastState.IsRecovered(newState) So(recovered, ShouldBeFalse) }) + + Convey("With continue recovered", func() { + lastState := StateOK + newState := StateOK + + recovered := lastState.IsRecovered(newState) + So(recovered, ShouldBeFalse) + }) + + Convey("With continue degraded", func() { + lastState := StateError + newState := StateError + + recovered := lastState.IsRecovered(newState) + So(recovered, ShouldBeFalse) + }) }) } @@ -104,23 +136,6 @@ func TestValidateHeartbeaterBaseConfig(t *testing.T) { So(err, ShouldBeNil) }) - Convey("With just enabled config", func() { - hbCfg := HeartbeaterBaseConfig{ - Enabled: true, - } - err := moira.ValidateStruct(hbCfg) - So(err, ShouldNotBeNil) - }) - - Convey("With enabled config and added alert config", func() { - hbCfg := HeartbeaterBaseConfig{ - Enabled: true, - AlertCfg: AlertConfig{}, - } - err := moira.ValidateStruct(hbCfg) - So(err, ShouldNotBeNil) - }) - Convey("With enabled config, added and filled alert config", func() { hbCfg := HeartbeaterBaseConfig{ Enabled: true, diff --git a/notifier/selfstate/heartbeat/local_checker.go b/notifier/selfstate/heartbeat/local_checker.go index 4ea80c113..9a80b6ba1 100644 --- a/notifier/selfstate/heartbeat/local_checker.go +++ b/notifier/selfstate/heartbeat/local_checker.go @@ -15,7 +15,7 @@ var _ Heartbeater = (*localCheckerHeartbeater)(nil) type LocalCheckerHeartbeaterConfig struct { HeartbeaterBaseConfig - LocalCheckDelay time.Duration `validate:"required,gt=0"` + LocalCheckDelay time.Duration `validate:"required_if=Enabled true,gte=0"` } type localCheckerHeartbeater struct { @@ -63,14 +63,9 @@ func (heartbeater *localCheckerHeartbeater) Check() (State, error) { return StateOK, nil } -// NeedTurnOffNotifier is a function that checks to see if the notifier needs to be turned off. -func (heartbeater localCheckerHeartbeater) NeedTurnOffNotifier() bool { - return heartbeater.cfg.NeedTurnOffNotifier -} - // Type is a function that returns the current heartbeat type. func (localCheckerHeartbeater) Type() datatypes.HeartbeatType { - return datatypes.HeartbeatTypeNotSet + return datatypes.HeartbeatLocalChecker } // AlertSettings is a function that returns the current settings for alerts. diff --git a/notifier/selfstate/heartbeat/local_checker_test.go b/notifier/selfstate/heartbeat/local_checker_test.go index 50eae03e8..cfe720674 100644 --- a/notifier/selfstate/heartbeat/local_checker_test.go +++ b/notifier/selfstate/heartbeat/local_checker_test.go @@ -32,7 +32,11 @@ func TestNewLocalCheckerHeartbeater(t *testing.T) { }) Convey("Without local check delay", func() { - cfg := LocalCheckerHeartbeaterConfig{} + cfg := LocalCheckerHeartbeaterConfig{ + HeartbeaterBaseConfig: HeartbeaterBaseConfig{ + Enabled: true, + }, + } localCheckerHeartbeater, err := NewLocalCheckerHeartbeater(cfg, heartbeaterBase) So(errors.As(err, &validationErr), ShouldBeTrue) @@ -149,25 +153,6 @@ func TestLocalCheckerHeartbeaterCheck(t *testing.T) { }) } -func TestLocalCheckerHeartbeaterNeedTurnOffNotifier(t *testing.T) { - _, _, _, heartbeaterBase := heartbeaterHelper(t) - - Convey("Test localCheckerHeartbeater.TurnOffNotifier", t, func() { - cfg := LocalCheckerHeartbeaterConfig{ - HeartbeaterBaseConfig: HeartbeaterBaseConfig{ - NeedTurnOffNotifier: true, - }, - LocalCheckDelay: defaultLocalCheckDelay, - } - - localCheckerHeartbeater, err := NewLocalCheckerHeartbeater(cfg, heartbeaterBase) - So(err, ShouldBeNil) - - needTurnOffNotifier := localCheckerHeartbeater.NeedTurnOffNotifier() - So(needTurnOffNotifier, ShouldBeTrue) - }) -} - func TestLocalCheckerHeartbeaterType(t *testing.T) { _, _, _, heartbeaterBase := heartbeaterHelper(t) @@ -180,7 +165,7 @@ func TestLocalCheckerHeartbeaterType(t *testing.T) { So(err, ShouldBeNil) localCheckerHeartbeaterType := localCheckerHeartbeater.Type() - So(localCheckerHeartbeaterType, ShouldResemble, datatypes.HeartbeatTypeNotSet) + So(localCheckerHeartbeaterType, ShouldResemble, datatypes.HeartbeatLocalChecker) }) } diff --git a/notifier/selfstate/heartbeat/notifier.go b/notifier/selfstate/heartbeat/notifier.go index 9d26e45c5..6d92a5ef6 100644 --- a/notifier/selfstate/heartbeat/notifier.go +++ b/notifier/selfstate/heartbeat/notifier.go @@ -41,14 +41,9 @@ func (heartbeater *notifierHeartbeater) Check() (State, error) { return StateOK, nil } -// NeedTurnOffNotifier is a function that checks to see if the notifier needs to be turned off. -func (heartbeater *notifierHeartbeater) NeedTurnOffNotifier() bool { - return heartbeater.cfg.NeedTurnOffNotifier -} - // Type is a function that returns the current heartbeat type. func (notifierHeartbeater) Type() datatypes.HeartbeatType { - return datatypes.HeartbeatNotifierOff + return datatypes.HeartbeatNotifier } // AlertSettings is a function that returns the current settings for alerts. diff --git a/notifier/selfstate/heartbeat/notifier_test.go b/notifier/selfstate/heartbeat/notifier_test.go index 9b1767eb5..a0a76bbc2 100644 --- a/notifier/selfstate/heartbeat/notifier_test.go +++ b/notifier/selfstate/heartbeat/notifier_test.go @@ -65,24 +65,6 @@ func TestNotifierHeartbeaterCheck(t *testing.T) { }) } -func TestNotifierHeartbeaterNeedTurnOffNotifier(t *testing.T) { - _, _, _, heartbeaterBase := heartbeaterHelper(t) - - Convey("Test notifierHeartbeater.TurnOffNotifier", t, func() { - cfg := NotifierHeartbeaterConfig{ - HeartbeaterBaseConfig: HeartbeaterBaseConfig{ - NeedTurnOffNotifier: true, - }, - } - - notifierHeartbeater, err := NewNotifierHeartbeater(cfg, heartbeaterBase) - So(err, ShouldBeNil) - - needTurnOffNotifier := notifierHeartbeater.NeedTurnOffNotifier() - So(needTurnOffNotifier, ShouldBeTrue) - }) -} - func TestNotifierHeartbeaterType(t *testing.T) { _, _, _, heartbeaterBase := heartbeaterHelper(t) @@ -93,7 +75,7 @@ func TestNotifierHeartbeaterType(t *testing.T) { So(err, ShouldBeNil) notifierHeartbeaterType := notifierHeartbeater.Type() - So(notifierHeartbeaterType, ShouldResemble, datatypes.HeartbeatNotifierOff) + So(notifierHeartbeaterType, ShouldResemble, datatypes.HeartbeatNotifier) }) } diff --git a/notifier/selfstate/heartbeat/remote_checker.go b/notifier/selfstate/heartbeat/remote_checker.go index 096342378..1253184a8 100644 --- a/notifier/selfstate/heartbeat/remote_checker.go +++ b/notifier/selfstate/heartbeat/remote_checker.go @@ -19,7 +19,7 @@ var ( type RemoteCheckerHeartbeaterConfig struct { HeartbeaterBaseConfig - RemoteCheckDelay time.Duration `validate:"required,gt=0"` + RemoteCheckDelay time.Duration `validate:"required_if=Enabled true,gte=0"` } type remoteCheckerHeartbeater struct { @@ -67,14 +67,9 @@ func (heartbeater remoteCheckerHeartbeater) Check() (State, error) { return StateOK, nil } -// NeedTurnOffNotifier is a function that checks to see if the notifier needs to be turned off. -func (heartbeater remoteCheckerHeartbeater) NeedTurnOffNotifier() bool { - return heartbeater.cfg.NeedTurnOffNotifier -} - // Type is a function that returns the current heartbeat type. func (remoteCheckerHeartbeater) Type() datatypes.HeartbeatType { - return datatypes.HeartbeatTypeNotSet + return datatypes.HeartbeatRemoteChecker } // AlertSettings is a function that returns the current settings for alerts. diff --git a/notifier/selfstate/heartbeat/remote_checker_test.go b/notifier/selfstate/heartbeat/remote_checker_test.go index aac4dbaf9..6d7cf02e6 100644 --- a/notifier/selfstate/heartbeat/remote_checker_test.go +++ b/notifier/selfstate/heartbeat/remote_checker_test.go @@ -32,7 +32,11 @@ func TestNewRemoteCheckerHeartbeater(t *testing.T) { }) Convey("Without remote check delay", func() { - cfg := RemoteCheckerHeartbeaterConfig{} + cfg := RemoteCheckerHeartbeaterConfig{ + HeartbeaterBaseConfig: HeartbeaterBaseConfig{ + Enabled: true, + }, + } remoteCheckerHeartbeater, err := NewRemoteCheckerHeartbeater(cfg, heartbeaterBase) So(errors.As(err, &validationErr), ShouldBeTrue) @@ -149,25 +153,6 @@ func TestRemoteCheckerHeartbeaterCheck(t *testing.T) { }) } -func TestRemoteCheckerHeartbeaterNeedTurnOffNotifier(t *testing.T) { - _, _, _, heartbeaterBase := heartbeaterHelper(t) - - Convey("Test remoteCheckerHeartbeater.TurnOffNotifier", t, func() { - cfg := RemoteCheckerHeartbeaterConfig{ - HeartbeaterBaseConfig: HeartbeaterBaseConfig{ - NeedTurnOffNotifier: true, - }, - RemoteCheckDelay: defaultRemoteCheckDelay, - } - - remoteCheckerHeartbeater, err := NewRemoteCheckerHeartbeater(cfg, heartbeaterBase) - So(err, ShouldBeNil) - - needTurnOffNotifier := remoteCheckerHeartbeater.NeedTurnOffNotifier() - So(needTurnOffNotifier, ShouldBeTrue) - }) -} - func TestRemoteCheckerHeartbeaterType(t *testing.T) { _, _, _, heartbeaterBase := heartbeaterHelper(t) @@ -180,7 +165,7 @@ func TestRemoteCheckerHeartbeaterType(t *testing.T) { So(err, ShouldBeNil) remoteCheckerHeartbeaterType := remoteCheckerHeartbeater.Type() - So(remoteCheckerHeartbeaterType, ShouldResemble, datatypes.HeartbeatTypeNotSet) + So(remoteCheckerHeartbeaterType, ShouldResemble, datatypes.HeartbeatRemoteChecker) }) } From d42af769eee2a486428d0c0a9633e4826abd0f6d Mon Sep 17 00:00:00 2001 From: almostinf Date: Tue, 5 Nov 2024 18:03:59 +0300 Subject: [PATCH 4/4] regenerate mocks --- mock/heartbeat/heartbeat.go | 63 +++++++++++++++---------------------- 1 file changed, 25 insertions(+), 38 deletions(-) diff --git a/mock/heartbeat/heartbeat.go b/mock/heartbeat/heartbeat.go index f0a6f6acf..694bee3c1 100644 --- a/mock/heartbeat/heartbeat.go +++ b/mock/heartbeat/heartbeat.go @@ -12,6 +12,8 @@ package mock_heartbeat import ( reflect "reflect" + datatypes "github.com/moira-alert/moira/datatypes" + heartbeat "github.com/moira-alert/moira/notifier/selfstate/heartbeat" gomock "go.uber.org/mock/gomock" ) @@ -38,60 +40,45 @@ func (m *MockHeartbeater) EXPECT() *MockHeartbeaterMockRecorder { return m.recorder } -// Check mocks base method. -func (m *MockHeartbeater) Check(arg0 int64) (int64, bool, error) { +// AlertSettings mocks base method. +func (m *MockHeartbeater) AlertSettings() heartbeat.AlertConfig { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Check", arg0) - ret0, _ := ret[0].(int64) - ret1, _ := ret[1].(bool) - ret2, _ := ret[2].(error) - return ret0, ret1, ret2 -} - -// Check indicates an expected call of Check. -func (mr *MockHeartbeaterMockRecorder) Check(arg0 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Check", reflect.TypeOf((*MockHeartbeater)(nil).Check), arg0) -} - -// GetErrorMessage mocks base method. -func (m *MockHeartbeater) GetErrorMessage() string { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetErrorMessage") - ret0, _ := ret[0].(string) + ret := m.ctrl.Call(m, "AlertSettings") + ret0, _ := ret[0].(heartbeat.AlertConfig) return ret0 } -// GetErrorMessage indicates an expected call of GetErrorMessage. -func (mr *MockHeartbeaterMockRecorder) GetErrorMessage() *gomock.Call { +// AlertSettings indicates an expected call of AlertSettings. +func (mr *MockHeartbeaterMockRecorder) AlertSettings() *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetErrorMessage", reflect.TypeOf((*MockHeartbeater)(nil).GetErrorMessage)) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AlertSettings", reflect.TypeOf((*MockHeartbeater)(nil).AlertSettings)) } -// NeedToCheckOthers mocks base method. -func (m *MockHeartbeater) NeedToCheckOthers() bool { +// Check mocks base method. +func (m *MockHeartbeater) Check() (heartbeat.State, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "NeedToCheckOthers") - ret0, _ := ret[0].(bool) - return ret0 + ret := m.ctrl.Call(m, "Check") + ret0, _ := ret[0].(heartbeat.State) + ret1, _ := ret[1].(error) + return ret0, ret1 } -// NeedToCheckOthers indicates an expected call of NeedToCheckOthers. -func (mr *MockHeartbeaterMockRecorder) NeedToCheckOthers() *gomock.Call { +// Check indicates an expected call of Check. +func (mr *MockHeartbeaterMockRecorder) Check() *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NeedToCheckOthers", reflect.TypeOf((*MockHeartbeater)(nil).NeedToCheckOthers)) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Check", reflect.TypeOf((*MockHeartbeater)(nil).Check)) } -// NeedTurnOffNotifier mocks base method. -func (m *MockHeartbeater) NeedTurnOffNotifier() bool { +// Type mocks base method. +func (m *MockHeartbeater) Type() datatypes.HeartbeatType { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "NeedTurnOffNotifier") - ret0, _ := ret[0].(bool) + ret := m.ctrl.Call(m, "Type") + ret0, _ := ret[0].(datatypes.HeartbeatType) return ret0 } -// NeedTurnOffNotifier indicates an expected call of NeedTurnOffNotifier. -func (mr *MockHeartbeaterMockRecorder) NeedTurnOffNotifier() *gomock.Call { +// Type indicates an expected call of Type. +func (mr *MockHeartbeaterMockRecorder) Type() *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NeedTurnOffNotifier", reflect.TypeOf((*MockHeartbeater)(nil).NeedTurnOffNotifier)) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Type", reflect.TypeOf((*MockHeartbeater)(nil).Type)) }