Skip to content

Commit

Permalink
Merge pull request #748 from forta-network/caner/forta-1001-add-e2e-t…
Browse files Browse the repository at this point in the history
…est-case-to-cover-bot-logs

Add E2E test to cover bot logs feature
  • Loading branch information
canercidam authored Jun 7, 2023
2 parents c9b9a4a + eb66bc8 commit de4627b
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/supervisor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func initServices(ctx context.Context, cfg config.Config) ([]services.Service, e
cfg.Registry.JsonRpc.Url = utils.ConvertToDockerHostURL(cfg.Registry.JsonRpc.Url)
cfg.Registry.IPFS.APIURL = utils.ConvertToDockerHostURL(cfg.Registry.IPFS.APIURL)
cfg.Registry.IPFS.GatewayURL = utils.ConvertToDockerHostURL(cfg.Registry.IPFS.GatewayURL)
cfg.AgentLogsConfig.URL = utils.ConvertToDockerHostURL(cfg.AgentLogsConfig.URL)

passphrase, err := security.ReadPassphrase()
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ type AutoUpdateConfig struct {
}

type AgentLogsConfig struct {
URL string `yaml:"url" json:"url" default:"https://alerts.forta.network/logs/agents" validate:"url"`
Disable bool `yaml:"disable" json:"disable"`
URL string `yaml:"url" json:"url" default:"https://alerts.forta.network/logs/agents" validate:"url"`
Disable bool `yaml:"disable" json:"disable"`
SendIntervalSeconds int `yaml:"sendIntervalSeconds" json:"sendIntervalSeconds" default:"60"`
}

type ContainerRegistryConfig struct {
Expand Down
5 changes: 3 additions & 2 deletions services/supervisor/agent_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const (
)

func (sup *SupervisorService) syncAgentLogs() {
ticker := time.NewTicker(defaultAgentLogSendInterval)
interval := time.Duration(sup.botLifecycleConfig.Config.AgentLogsConfig.SendIntervalSeconds) * time.Second
ticker := time.NewTicker(interval)
for range ticker.C {
err := sup.doSyncAgentLogs()
sup.lastAgentLogsRequest.Set()
Expand All @@ -41,7 +42,7 @@ func (sup *SupervisorService) doSyncAgentLogs() error {

botContainers, err := sup.botLifecycle.BotClient.LoadBotContainers(sup.ctx)
if err != nil {
log.WithError(err).Warn("failed to sync bot logs")
return fmt.Errorf("failed to load the bot containers: %v", err)
}

for _, container := range botContainers {
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/.forta/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ telemetry:
disable: true

agentLogs:
disable: true
url: http://localhost:9090/logs/agents
sendIntervalSeconds: 1

log:
level: trace
2 changes: 2 additions & 0 deletions tests/e2e/agents/txdetectoragent/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ FROM alpine AS base
COPY test-bot-txdetector /main
EXPOSE 50051

LABEL "network.forta.settings.agent-logs.enable"="true"

ENTRYPOINT [ "/main" ]
10 changes: 9 additions & 1 deletion tests/e2e/agents/txdetectoragent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"os"
"strings"
"time"

"github.com/ethereum/go-ethereum/ethclient"
"github.com/forta-network/forta-core-go/protocol"
Expand Down Expand Up @@ -38,6 +39,13 @@ func main() {
ethClient: ethClient,
})

go func() {
ticker := time.NewTicker(time.Second * 10)
for range ticker.C {
log.Println("new log", time.Now().UnixNano())
}
}()

log.Println("Starting agent server...")
log.Println(server.Serve(lis))
}
Expand Down Expand Up @@ -150,4 +158,4 @@ func fetchJWTToken() (string, error) {
}

return s.Token, nil
}
}
7 changes: 7 additions & 0 deletions tests/e2e/e2e_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ func (s *Suite) TestLinkUnlink() {

s.expectUpIn(largeTimeout, agentContainerID)

s.expectIn(
smallTimeout, func() (ok bool) {
b := s.alertServer.GetLogs()
return len(b) > 0
},
)

tx, err = s.mockRegistryContract.UnlinkTestAgent(s.deployer)
s.r.NoError(err)
s.ensureTx("unlink agent", tx)
Expand Down
8 changes: 7 additions & 1 deletion tests/e2e/e2e_forta_local_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,11 @@ ens:
telemetry:
disable: true
# this should not be helpful in sending logs
# the feature is disabled by default in local mode
agentLogs:
disable: true
url: http://localhost:9090/logs/agents
sendIntervalSeconds: 1
publicApiProxy:
url: http://localhost:%d
Expand Down Expand Up @@ -213,6 +216,9 @@ func (s *Suite) runLocalMode(webhookURL, logFileName string, readAlertsFunc func
s.r.NotNil(exploiterAlert)
s.r.NotNil(tokenAlert)

// bot logs are disabled by default in local mode
s.r.Nil(s.alertServer.GetLogs())

_, err = security.VerifyScannerJWT(s.getTokenFromAlert(tokenAlert))
s.r.NoError(err)

Expand Down
18 changes: 18 additions & 0 deletions testutils/alertserver/alertserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type AlertServer struct {
router *mux.Router

knownBatches map[string][]byte
knownLogs []byte
mu sync.RWMutex
}

Expand All @@ -34,6 +35,7 @@ func New(ctx context.Context, port int) *AlertServer {

r := mux.NewRouter()
r.HandleFunc("/batch/{ref}", alertServer.AddAlert).Methods("POST")
r.HandleFunc("/logs/agents", alertServer.AddLogs).Methods("POST")
alertServer.router = r

return alertServer
Expand Down Expand Up @@ -62,6 +64,12 @@ func (as *AlertServer) GetAlert(ref string) ([]byte, bool) {
return b, ok
}

func (as *AlertServer) GetLogs() []byte {
as.mu.RLock()
defer as.mu.RUnlock()
return as.knownLogs
}

func (as *AlertServer) AddAlert(w http.ResponseWriter, r *http.Request) {
as.mu.Lock()
defer as.mu.Unlock()
Expand All @@ -73,3 +81,13 @@ func (as *AlertServer) AddAlert(w http.ResponseWriter, r *http.Request) {
as.knownBatches[ref] = b
return
}

func (as *AlertServer) AddLogs(w http.ResponseWriter, r *http.Request) {
as.mu.Lock()
defer as.mu.Unlock()

b, _ := ioutil.ReadAll(r.Body)
logrus.Info("received bot logs")
as.knownLogs = b
return
}

0 comments on commit de4627b

Please sign in to comment.