Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
[epociask/issue-160-slack-fix] Cleanup E2E tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethen Pociask committed Oct 19, 2023
1 parent a5e381b commit 0c6e468
Show file tree
Hide file tree
Showing 7 changed files with 9,459 additions and 64 deletions.
4 changes: 0 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,3 @@ issues:
- wrapcheck
- lll
- whitespace

- path: mock_servers.go
linters:
- gomnd
3 changes: 1 addition & 2 deletions e2e/alerting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ func TestMultiDirectiveRouting(t *testing.T) {
tx, err := sysCfg.SetGasConfig(opts, overhead, scalar)
require.NoError(t, err, "Error setting gas config")

txTimeoutDuration := 10 * time.Duration(ts.Cfg.DeployConfig.L1BlockTime) * time.Second
receipt, err := e2e.WaitForTransaction(tx.Hash(), ts.L1Client, txTimeoutDuration)
receipt, err := wait.ForReceipt(context.Background(), ts.L1Client, tx.Hash(), types.ReceiptStatusSuccessful)

require.NoError(t, err, "Error waiting for transaction")
require.Equal(t, receipt.Status, types.ReceiptStatusSuccessful, "transaction failed")
Expand Down
11 changes: 4 additions & 7 deletions e2e/heuristic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,8 @@ func TestContractEvent(t *testing.T) {
tx, err := sysCfg.SetGasConfig(opts, overhead, scalar)
require.NoError(t, err, "Error setting gas config")

// Wait for the transaction to be canonicalized.
txTimeoutDuration := 10 * time.Duration(ts.Cfg.DeployConfig.L1BlockTime) * time.Second
receipt, err := e2e.WaitForTransaction(tx.Hash(), ts.L1Client, txTimeoutDuration)
// Wait for the L1 transaction to be executed.
receipt, err := wait.ForReceipt(context.Background(), ts.L1Client, tx.Hash(), types.ReceiptStatusSuccessful)

require.NoError(t, err, "Error waiting for transaction")
require.Equal(t, receipt.Status, types.ReceiptStatusSuccessful, "transaction failed")
Expand Down Expand Up @@ -216,7 +215,7 @@ func TestWithdrawalEnforcement(t *testing.T) {
fakeAddr, tx, _, err := bindings.DeployL2ToL1MessagePasser(opts, ts.L2Client)
require.NoError(t, err, "error deploying dummy message passer on L2")

_, err = e2e.WaitForTransaction(tx.Hash(), ts.L2Client, 10*time.Second)
_, err = wait.ForReceipt(context.Background(), ts.L2Client, tx.Hash(), types.ReceiptStatusSuccessful)
require.NoError(t, err, "error waiting for transaction")

// Setup Pessimism to listen for fraudulent withdrawals
Expand Down Expand Up @@ -301,8 +300,6 @@ func TestFaultDetector(t *testing.T) {
ts := e2e.CreateSysTestSuite(t)
defer ts.Close()

txTimeoutDuration := 10 * time.Duration(ts.Cfg.DeployConfig.L1BlockTime) * time.Second

// Generate transactor opts
l1Opts, err := bind.NewKeyedTransactorWithChainID(ts.Cfg.Secrets.Proposer, ts.Cfg.L1ChainIDBig())
require.Nil(t, err)
Expand Down Expand Up @@ -347,7 +344,7 @@ func TestFaultDetector(t *testing.T) {
tx, err := outputOracle.ProposeL2Output(l1Opts, dummyRoot, latestNum, l1Hash, big.NewInt(0))
require.Nil(t, err)

receipt, err := e2e.WaitForTransaction(tx.Hash(), ts.L1Client, txTimeoutDuration)
receipt, err := wait.ForReceipt(context.Background(), ts.L1Client, tx.Hash(), types.ReceiptStatusSuccessful)
require.Nil(t, err)

require.NoError(t, wait.For(context.Background(), 500*time.Millisecond, func() (bool, error) {
Expand Down
35 changes: 14 additions & 21 deletions e2e/mock_servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package e2e
import (
"encoding/json"
"fmt"
"math/rand"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -62,11 +61,6 @@ func (svr *TestPagerDutyServer) Close() {
svr.Server.Close()
}

// RandBool ... Returns a random boolean based on the current time
func RandBool() bool {
return rand.Intn(2) == 1 //#nosec G404: This is not a cryptographic use case
}

// mockPagerDutyPost ... Mocks a pagerduty post request
func (svr *TestPagerDutyServer) mockPagerDutyPost(w http.ResponseWriter, r *http.Request) {
var alert *client.PagerDutyRequest
Expand All @@ -79,16 +73,7 @@ func (svr *TestPagerDutyServer) mockPagerDutyPost(w http.ResponseWriter, r *http

svr.Payloads = append(svr.Payloads, alert)

// Randomly return different API payload responses
// This ensures that the client implementation can handle different
// slack workspace types
publicAPI := RandBool()
w.WriteHeader(http.StatusOK)
if publicAPI {
_, _ = w.Write([]byte(`ok`))
} else {
_, _ = w.Write([]byte(`{"status":"success", "message":""}`))
}
_, _ = w.Write([]byte(`{"status":"success", "message":""}`))
}

// PagerDutyAlerts ... Returns the pagerduty alerts
Expand All @@ -105,9 +90,10 @@ func (svr *TestPagerDutyServer) ClearAlerts() {

// TestSlackServer ... Mock server for testing slack alerts
type TestSlackServer struct {
Server *httptest.Server
Payloads []*client.SlackPayload
Port int
Server *httptest.Server
Payloads []*client.SlackPayload
Port int
Unstructured bool
}

// NewTestSlackServer ... Creates a new mock slack server
Expand Down Expand Up @@ -161,9 +147,16 @@ func (svr *TestSlackServer) mockSlackPost(w http.ResponseWriter, r *http.Request
}

svr.Payloads = append(svr.Payloads, alert)

// Randomly return different API payload responses
// This ensures that the client implementation can handle different
// slack workspace types
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"message":"ok", "error":""}`))
if svr.Unstructured {
_, _ = w.Write([]byte(`ok`))
} else {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"message":"ok", "error":""}`))
}
}

// SlackAlerts ... Returns the slack alerts
Expand Down
32 changes: 2 additions & 30 deletions e2e/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package e2e

import (
"context"
"errors"
"fmt"
"os"
"testing"
"time"

"github.com/base-org/pessimism/internal/alert"
"github.com/base-org/pessimism/internal/api/server"
Expand All @@ -21,9 +19,6 @@ import (
"github.com/base-org/pessimism/internal/subsystem"

op_e2e "github.com/ethereum-optimism/optimism/op-e2e"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
)
Expand Down Expand Up @@ -165,7 +160,9 @@ func CreateSysTestSuite(t *testing.T) *SysTestSuite {

appCfg := DefaultTestConfig()

// Use unstructured slack server responses for testing E2E system flows
slackServer := NewTestSlackServer("127.0.0.1", 0)
slackServer.Unstructured = true

pagerdutyServer := NewTestPagerDutyServer("127.0.0.1", 0)

Expand Down Expand Up @@ -289,28 +286,3 @@ func DefaultRoutingParams(slackURL core.StringFromEnv) *core.AlertRoutingParams
},
}
}

// WaitForTransaction ... Waits for a transaction receipt to be generated or times out
func WaitForTransaction(hash common.Hash, client *ethclient.Client, timeout time.Duration) (*types.Receipt, error) {
timeoutCh := time.After(timeout)
ms100 := 100

ticker := time.NewTicker(time.Duration(ms100) * time.Millisecond)
defer ticker.Stop()
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
for {
receipt, err := client.TransactionReceipt(ctx, hash)
if receipt != nil && err == nil {
return receipt, nil
} else if err != nil && !errors.Is(err, ethereum.NotFound) {
return nil, err
}

select {
case <-timeoutCh:
return nil, errors.New("timeout")
case <-ticker.C:
}
}
}
Loading

0 comments on commit 0c6e468

Please sign in to comment.