Skip to content

Commit

Permalink
Merge pull request #43 from lidofinance/fix-tests
Browse files Browse the repository at this point in the history
feat: fix web page, tests
  • Loading branch information
sergeyWh1te authored Oct 18, 2024
2 parents 6802bd6 + 7ec5804 commit 68c3339
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 62 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ WORKDIR /app
RUN apk add --no-cache ca-certificates

COPY --from=builder /go/src/app/bin .
COPY --from=builder /go/src/app/web /app/web/

USER nobody
2 changes: 1 addition & 1 deletion cmd/forwarder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {

log := logger.New(&cfg.AppConfig)

notificationConfig, err := env.ReadNotificationConfig(cfg.AppConfig.Env)
notificationConfig, err := env.ReadNotificationConfig(cfg.AppConfig.Env, `notification.yaml`)
if err != nil {
log.Error(fmt.Sprintf("Error loading consumer's config: %v", err))
return
Expand Down
3 changes: 1 addition & 2 deletions internal/env/notify_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ type NotificationConfig struct {
Consumers []*Consumer `mapstructure:"consumers"`
}

func ReadNotificationConfig(env string) (*NotificationConfig, error) {
func ReadNotificationConfig(env string, configPath string) (*NotificationConfig, error) {
v := viper.New()

configPath := `notification.yaml`
if env != `local` {
configPath = `/etc/forwarder/notification.yaml`
}
Expand Down
8 changes: 4 additions & 4 deletions internal/pkg/notifiler/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ func NewDiscord(webhookURL string, httpClient *http.Client, metricsStore *metric
}
}

const maxDiscordMsgLength = 2000
const warningDiscordMessage = "Warn: Msg >=2000, pls review description message"
const MaxDiscordMsgLength = 2000
const WarningDiscordMessage = "Warn: Msg >=2000, pls review description message"

func (d *Discord) SendFinding(ctx context.Context, alert *databus.FindingDtoJson) error {
message := TruncateMessageWithAlertID(
fmt.Sprintf("%s\n\n%s", alert.Name, FormatAlert(alert, d.source)),
maxDiscordMsgLength,
warningDiscordMessage,
MaxDiscordMsgLength,
WarningDiscordMessage,
)

return d.send(ctx, message)
Expand Down
31 changes: 20 additions & 11 deletions internal/pkg/notifiler/discord_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package notifiler
package notifiler_test

import (
"context"
"net/http"
"testing"

"github.com/prometheus/client_golang/prometheus"

"github.com/lidofinance/onchain-mon/generated/databus"
"github.com/lidofinance/onchain-mon/internal/connectors/metrics"
"github.com/lidofinance/onchain-mon/internal/env"
"github.com/prometheus/client_golang/prometheus"
"github.com/lidofinance/onchain-mon/internal/pkg/notifiler"
"github.com/lidofinance/onchain-mon/internal/utils/pointers"
)

const Name = `[INFO] ℹ️ Lido: BotToken rebased`
Expand Down Expand Up @@ -49,6 +52,12 @@ func Test_usecase_SendFinding(t *testing.T) {
promRegistry := prometheus.NewRegistry()
metricsStore := metrics.New(promRegistry, cfg.AppConfig.MetricsPrefix, cfg.AppConfig.Name, cfg.AppConfig.Env)

notifcationConfig, err := env.ReadNotificationConfig(cfg.AppConfig.Env, "../../../notification.yaml")
if err != nil {
t.Errorf("Read notification config error: %s", err.Error())
return
}

type fields struct {
webhookURL string
httpClient *http.Client
Expand All @@ -67,7 +76,7 @@ func Test_usecase_SendFinding(t *testing.T) {
{
name: "Success",
fields: fields{
webhookURL: cfg.AppConfig.DiscordWebHookURL,
webhookURL: notifcationConfig.DiscordChannels[0].WebhookURL,
httpClient: &http.Client{},
metricsStore: metricsStore,
},
Expand All @@ -78,9 +87,9 @@ func Test_usecase_SendFinding(t *testing.T) {
Description: "L1 token rate: 1.1808\nBridge balances:\n\tLDO:\n\t\tL1: 1231218.4603 LDO\n\t\tL2: 1230730.9530 LDO\n\t\n\twstETH:\n\t\tL1: 84477.0663 wstETH\n\t\tL2: 81852.1638 wstETH\n\nWithdrawals:\n\twstETH: 1664.1363 (in 5 transactions)",
Severity: databus.SeverityInfo,
AlertId: `DIGEST`,
BlockTimestamp: intPtr(1727965236),
BlockNumber: intPtr(20884540),
TxHash: stringPtr("0x714a6c2109c8af671c8a6df594bd9f1f3ba9f11b73a1e54f5f128a3447fa0bdf"),
BlockTimestamp: pointers.IntPtr(1727965236),
BlockNumber: pointers.IntPtr(20884540),
TxHash: pointers.StringPtr("0x714a6c2109c8af671c8a6df594bd9f1f3ba9f11b73a1e54f5f128a3447fa0bdf"),
BotName: `Test`,
Team: `Protocol`,
},
Expand All @@ -90,7 +99,7 @@ func Test_usecase_SendFinding(t *testing.T) {
{
name: "Too long message",
fields: fields{
webhookURL: cfg.AppConfig.DiscordWebHookURL,
webhookURL: notifcationConfig.DiscordChannels[0].WebhookURL,
httpClient: &http.Client{},
metricsStore: metricsStore,
},
Expand All @@ -101,9 +110,9 @@ func Test_usecase_SendFinding(t *testing.T) {
Description: ParadiseLost,
Severity: databus.SeverityInfo,
AlertId: `DIGEST`,
BlockTimestamp: intPtr(1727965236),
BlockNumber: intPtr(20884540),
TxHash: stringPtr("0x714a6c2109c8af671c8a6df594bd9f1f3ba9f11b73a1e54f5f128a3447fa0bdf"),
BlockTimestamp: pointers.IntPtr(1727965236),
BlockNumber: pointers.IntPtr(20884540),
TxHash: pointers.StringPtr("0x714a6c2109c8af671c8a6df594bd9f1f3ba9f11b73a1e54f5f128a3447fa0bdf"),
BotName: `Test`,
Team: `Protocol`,
},
Expand All @@ -113,7 +122,7 @@ func Test_usecase_SendFinding(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := NewDiscord(
u := notifiler.NewDiscord(
tt.fields.webhookURL,
tt.fields.httpClient,
metricsStore,
Expand Down
33 changes: 21 additions & 12 deletions internal/pkg/notifiler/opsgenie_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package notifiler
package notifiler_test

import (
"context"
"net/http"
"testing"

"github.com/prometheus/client_golang/prometheus"

"github.com/lidofinance/onchain-mon/generated/databus"
"github.com/lidofinance/onchain-mon/internal/connectors/metrics"
"github.com/lidofinance/onchain-mon/internal/env"
"github.com/prometheus/client_golang/prometheus"
"github.com/lidofinance/onchain-mon/internal/pkg/notifiler"
"github.com/lidofinance/onchain-mon/internal/utils/pointers"
)

const NameCritical = `[CRITICAL] 🚨🚨🚨 ZkSync bridge balance mismatch 🚨🚨🚨`
Expand All @@ -30,6 +33,12 @@ func Test_opsGenie_SendMessage(t *testing.T) {
promRegistry := prometheus.NewRegistry()
metricsStore := metrics.New(promRegistry, cfg.AppConfig.MetricsPrefix, cfg.AppConfig.Name, cfg.AppConfig.Env)

notifcationConfig, err := env.ReadNotificationConfig(cfg.AppConfig.Env, "../../../notification.yaml")
if err != nil {
t.Errorf("Read notification config error: %s", err.Error())
return
}

type fields struct {
opsGenieKey string
httpClient *http.Client
Expand All @@ -48,7 +57,7 @@ func Test_opsGenie_SendMessage(t *testing.T) {
{
name: "Success",
fields: fields{
opsGenieKey: cfg.AppConfig.OpsGenieAPIKey,
opsGenieKey: notifcationConfig.OpsGenieChannels[0].APIKey,
httpClient: &http.Client{},
metricsStore: metricsStore,
},
Expand All @@ -59,9 +68,9 @@ func Test_opsGenie_SendMessage(t *testing.T) {
Description: DescriptionCritical,
Severity: databus.SeverityHigh,
AlertId: `TEST-CRITICAL-ID`,
BlockTimestamp: intPtr(1727965236),
BlockNumber: intPtr(20884540),
TxHash: stringPtr("0x714a6c2109c8af671c8a6df594bd9f1f3ba9f11b73a1e54f5f128a3447fa0bdf"),
BlockTimestamp: pointers.IntPtr(1727965236),
BlockNumber: pointers.IntPtr(20884540),
TxHash: pointers.StringPtr("0x714a6c2109c8af671c8a6df594bd9f1f3ba9f11b73a1e54f5f128a3447fa0bdf"),
BotName: `Test`,
Team: `Protocol`,
},
Expand All @@ -71,12 +80,12 @@ func Test_opsGenie_SendMessage(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := &opsGenie{
opsGenieKey: tt.fields.opsGenieKey,
httpClient: tt.fields.httpClient,
metrics: metricsStore,
source: `local`,
}
u := notifiler.NewOpsgenie(
tt.fields.opsGenieKey,
tt.fields.httpClient,
metricsStore,
`local`,
)
if err := u.SendFinding(tt.args.ctx, tt.args.alert); (err != nil) != tt.wantErr {
t.Errorf("SendMessage() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/pkg/notifiler/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ func NewTelegram(botToken, chatID string, httpClient *http.Client, metricsStore
}
}

const maxTelegramMessageLength = 4096
const warningTelegramMessage = "Warn: Msg >=4096, pls review description message"
const MaxTelegramMessageLength = 4096
const WarningTelegramMessage = "Warn: Msg >=4096, pls review description message"

func (u *Telegram) SendFinding(ctx context.Context, alert *databus.FindingDtoJson) error {
message := TruncateMessageWithAlertID(
fmt.Sprintf("%s\n\n%s", alert.Name, FormatAlert(alert, u.source)),
maxTelegramMessageLength,
warningTelegramMessage,
MaxTelegramMessageLength,
WarningTelegramMessage,
)

if alert.Severity != databus.SeverityUnknown {
Expand Down
54 changes: 26 additions & 28 deletions internal/pkg/notifiler/telegram_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package notifiler
package notifiler_test

import (
"context"
Expand All @@ -10,18 +10,10 @@ import (
"github.com/lidofinance/onchain-mon/generated/databus"
"github.com/lidofinance/onchain-mon/internal/connectors/metrics"
"github.com/lidofinance/onchain-mon/internal/env"
"github.com/lidofinance/onchain-mon/internal/pkg/notifiler"
"github.com/lidofinance/onchain-mon/internal/utils/pointers"
)

// Helper function to create pointers for strings
func stringPtr(s string) *string {
return &s
}

// Helper function to create pointers for ints
func intPtr(i int) *int {
return &i
}

const ParadiseLost = `
Of Mans First Disobedience, and the Fruit
Of that Forbidden Tree, whose mortal taste
Expand Down Expand Up @@ -193,6 +185,12 @@ func Test_SendFinding(t *testing.T) {
return
}

notifcationConfig, err := env.ReadNotificationConfig(cfg.AppConfig.Env, "../../../notification.yaml")
if err != nil {
t.Errorf("Read notification config error: %s", err.Error())
return
}

promRegistry := prometheus.NewRegistry()
metricsStore := metrics.New(promRegistry, cfg.AppConfig.MetricsPrefix, cfg.AppConfig.Name, cfg.AppConfig.Env)

Expand All @@ -215,8 +213,8 @@ func Test_SendFinding(t *testing.T) {
{
name: "Send_Test_Message_to_telegram",
fields: fields{
botToken: cfg.AppConfig.DeliveryConfig.TelegramBotToken,
chatID: cfg.AppConfig.DeliveryConfig.TelegramErrorsChatID,
botToken: notifcationConfig.TelegramChannels[0].BotToken,
chatID: notifcationConfig.TelegramChannels[0].ChatID,
httpClient: &http.Client{},
metricsStore: metricsStore,
},
Expand All @@ -235,9 +233,9 @@ Withdrawals info:
`,
Severity: databus.SeverityLow,
AlertId: `LIDO-TOKEN-REBASED`,
BlockTimestamp: intPtr(1727965236),
BlockNumber: intPtr(20884540),
TxHash: stringPtr("0x714a6c2109c8af671c8a6df594bd9f1f3ba9f11b73a1e54f5f128a3447fa0bdf"),
BlockTimestamp: pointers.IntPtr(1727965236),
BlockNumber: pointers.IntPtr(20884540),
TxHash: pointers.StringPtr("0x714a6c2109c8af671c8a6df594bd9f1f3ba9f11b73a1e54f5f128a3447fa0bdf"),
BotName: `FF-telegram-unit-test`,
Team: `Protocol`,
},
Expand All @@ -247,8 +245,8 @@ Withdrawals info:
{
name: "Send_Test_Message_to_telegram",
fields: fields{
botToken: cfg.AppConfig.DeliveryConfig.TelegramBotToken,
chatID: cfg.AppConfig.DeliveryConfig.TelegramErrorsChatID,
botToken: notifcationConfig.TelegramChannels[0].BotToken,
chatID: notifcationConfig.TelegramChannels[0].ChatID,
httpClient: &http.Client{},
metricsStore: metricsStore,
},
Expand All @@ -259,9 +257,9 @@ Withdrawals info:
Description: "L1 token rate: 1.1808\nBridge balances:\n\tLDO:\n\t\tL1: 1231218.4603 LDO\n\t\tL2: 1230730.9530 LDO\n\t\n\twstETH:\n\t\tL1: 84477.0663 wstETH\n\t\tL2: 81852.1638 wstETH\n\nWithdrawals:\n\twstETH: 1664.1363 (in 5 transactions)",
Severity: databus.SeverityInfo,
AlertId: `DIGEST`,
BlockTimestamp: intPtr(1727965236),
BlockNumber: intPtr(20884540),
TxHash: stringPtr("0x714a6c2109c8af671c8a6df594bd9f1f3ba9f11b73a1e54f5f128a3447fa0bdf"),
BlockTimestamp: pointers.IntPtr(1727965236),
BlockNumber: pointers.IntPtr(20884540),
TxHash: pointers.StringPtr("0x714a6c2109c8af671c8a6df594bd9f1f3ba9f11b73a1e54f5f128a3447fa0bdf"),
BotName: `Test`,
Team: `Protocol`,
},
Expand All @@ -271,8 +269,8 @@ Withdrawals info:
{
name: "Send_4096_symbol_to_telegram",
fields: fields{
botToken: cfg.AppConfig.DeliveryConfig.TelegramBotToken,
chatID: cfg.AppConfig.DeliveryConfig.TelegramErrorsChatID,
botToken: notifcationConfig.TelegramChannels[0].BotToken,
chatID: notifcationConfig.TelegramChannels[0].ChatID,
httpClient: &http.Client{},
metricsStore: metricsStore,
},
Expand All @@ -283,9 +281,9 @@ Withdrawals info:
Description: ParadiseLost,
Severity: databus.SeverityInfo,
AlertId: `DIGEST`,
BlockTimestamp: intPtr(1727965236),
BlockNumber: intPtr(20884540),
TxHash: stringPtr("0x714a6c2109c8af671c8a6df594bd9f1f3ba9f11b73a1e54f5f128a3447fa0bdf"),
BlockTimestamp: pointers.IntPtr(1727965236),
BlockNumber: pointers.IntPtr(20884540),
TxHash: pointers.StringPtr("0x714a6c2109c8af671c8a6df594bd9f1f3ba9f11b73a1e54f5f128a3447fa0bdf"),
BotName: `Test`,
Team: `Protocol`,
},
Expand All @@ -295,7 +293,7 @@ Withdrawals info:
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := NewTelegram(
u := notifiler.NewTelegram(
tt.fields.botToken,
tt.fields.chatID,
tt.fields.httpClient,
Expand Down Expand Up @@ -444,7 +442,7 @@ Block timestamp: 17:20:36.000 MSK`,
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := TruncateMessageWithAlertID(tt.args.message, maxTelegramMessageLength, warningTelegramMessage); got != tt.want {
if got := notifiler.TruncateMessageWithAlertID(tt.args.message, notifiler.MaxTelegramMessageLength, notifiler.WarningTelegramMessage); got != tt.want {
t.Errorf("TruncateMessageWithAlertID() = %v, want %v", got, tt.want)
}
})
Expand Down
11 changes: 11 additions & 0 deletions internal/utils/pointers/pointers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pointers

// Helper function to create pointers for strings
func StringPtr(s string) *string {
return &s
}

// Helper function to create pointers for ints
func IntPtr(i int) *int {
return &i
}

0 comments on commit 68c3339

Please sign in to comment.