forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
op-deployer: Test for existing OPCM (ethereum-optimism#12257)
* op-deployer: Test for existing OPCM Adds a test for deployments against an existing OPCM. The test works by spinning up an Anvil instance and forking Sepolia. To run this test, you'll need to specify two env vars: - `SEPOLIA_RPC_URL`: RPC URL for a Sepolia node. - `ENABLE_ANVIL`: Set to `true` to enable the test. In CI, the test uses our internal CI RPC nodes. * goimports * ensure streams close * lint * run anvil as part of unit not integration tests * simplify * remove foundry from kurtosis * use auto mine * mount artifacts * redeploy OPCM * comment
- Loading branch information
Showing
4 changed files
with
244 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package anvil | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/log" | ||
) | ||
|
||
func Test(t *testing.T) { | ||
if os.Getenv("ENABLE_ANVIL") == "" { | ||
t.Skip("skipping Anvil test") | ||
} | ||
} | ||
|
||
const AnvilPort = 31967 | ||
|
||
type Runner struct { | ||
proc *exec.Cmd | ||
stdout io.ReadCloser | ||
stderr io.ReadCloser | ||
logger log.Logger | ||
startedCh chan struct{} | ||
wg sync.WaitGroup | ||
} | ||
|
||
func New(l1RPCURL string, logger log.Logger) (*Runner, error) { | ||
proc := exec.Command( | ||
"anvil", | ||
"--fork-url", l1RPCURL, | ||
"--port", | ||
strconv.Itoa(AnvilPort), | ||
) | ||
stdout, err := proc.StdoutPipe() | ||
if err != nil { | ||
return nil, err | ||
} | ||
stderr, err := proc.StderrPipe() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Runner{ | ||
proc: proc, | ||
stdout: stdout, | ||
stderr: stderr, | ||
logger: logger, | ||
startedCh: make(chan struct{}, 1), | ||
}, nil | ||
} | ||
|
||
func (r *Runner) Start(ctx context.Context) error { | ||
if err := r.proc.Start(); err != nil { | ||
return err | ||
} | ||
|
||
r.wg.Add(2) | ||
go r.outputStream(r.stdout) | ||
go r.outputStream(r.stderr) | ||
|
||
select { | ||
case <-r.startedCh: | ||
return nil | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
} | ||
} | ||
|
||
func (r *Runner) Stop() error { | ||
err := r.proc.Process.Signal(os.Interrupt) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// make sure the output streams close | ||
defer r.wg.Wait() | ||
return r.proc.Wait() | ||
} | ||
|
||
func (r *Runner) outputStream(stream io.ReadCloser) { | ||
defer r.wg.Done() | ||
scanner := bufio.NewScanner(stream) | ||
listenLine := fmt.Sprintf("Listening on 127.0.0.1:%d", AnvilPort) | ||
started := sync.OnceFunc(func() { | ||
r.startedCh <- struct{}{} | ||
}) | ||
|
||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if strings.Contains(line, listenLine) { | ||
started() | ||
} | ||
|
||
r.logger.Debug("[ANVIL] " + scanner.Text()) | ||
} | ||
} | ||
|
||
func (r *Runner) RPCUrl() string { | ||
return fmt.Sprintf("http://localhost:%d", AnvilPort) | ||
} |