From dbee18df0de60c939840bca72f014ad041d5e0a0 Mon Sep 17 00:00:00 2001 From: Matthew Slipper Date: Tue, 31 Dec 2024 13:42:30 -0700 Subject: [PATCH] Add back unit tests to CI --- .circleci/config.yml | 88 ++++++++++++++++++++++++ justfile | 6 +- ops/cmd/sync_staging/main.go | 12 ++-- ops/go.mod | 10 +-- ops/go.sum | 3 + ops/internal/fs/utils_test.go | 4 +- ops/internal/manage/validation.go | 1 + ops/internal/report/comment.go | 3 +- ops/internal/testutil/mockrpc/mockrpc.go | 20 +++++- superchain/extra/README.MD | 1 - validation/go.mod | 6 +- validation/go.sum | 1 + validation/params.go | 6 +- validation/roles.go | 6 +- validation/types.go | 2 +- validation/versions.go | 6 +- 16 files changed, 149 insertions(+), 26 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index e69de29bb..92eb1a468 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -0,0 +1,88 @@ +version: 2.1 + +orbs: + codecov: codecov/codecov@5.0.3 + +jobs: + go-lint-test: + parameters: + package: + type: string + docker: + - image: cimg/go:1.23.1 + steps: + - checkout + - run: + name: Tidy mod + command: | + go mod tidy + git diff --exit-code + working_directory: << parameters.package >> + - run: + name: Lint + command: golangci-lint run ./... + working_directory: << parameters.package >> + - run: + name: Run tests + command: gotestsum --format testname --junitfile test-results.xml --packages=./... -- --coverprofile coverage.out + working_directory: << parameters.package >> + - store_test_results: + path: << parameters.package >>/test-results.xml + - codecov/upload: + disable_search: true + files: ./<>/coverage.out + + run-tool: + parameters: + tool: + type: string + args: + type: string + default: "" + check_diff: + type: boolean + default: false + docker: + - image: cimg/go:1.23.1 + steps: + - checkout + - run: + name: Build tool + command: go build -o ./bin/<> ./cmd/<< parameters.tool >>/main.go + working_directory: ./ops + - run: + name: Run tool + command: ./ops/bin/<< parameters.tool >> << parameters.args >> + - when: + condition: << parameters.check_diff >> + steps: + - run: + name: Check diff + command: git diff --exit-code + + +workflows: + main: + jobs: + - go-lint-test: + name: go-lint-test-ops + package: ops + - go-lint-test: + name: go-lint-test-validation + package: validation + - run-tool: + name: check-genesis-integrity + tool: check_genesis_integrity + - run-tool: + name: check-codegen + tool: codegen + check_diff: true + - run-tool: + name: check-staging + tool: sync_staging + check_diff: true + - run-tool: + name: check-apply-hardforks + tool: apply_hardforks + check_diff: true + diff --git a/justfile b/justfile index 9bb40c5c4..b9b2d0b87 100644 --- a/justfile +++ b/justfile @@ -4,9 +4,11 @@ set dotenv-load := true alias t := test-all alias l := lint-all +_lint DIRECTORY FLAGS='': + cd {{ DIRECTORY }} && golangci-lint run {{FLAGS}} + # Lint all Go code -lint-all: - golangci-lint run validation/... ops/... --fix +lint-all: (_lint 'ops' '--fix') (_lint 'validation' '--fix') _go-test DIRECTORY: cd {{ DIRECTORY }} && go run gotest.tools/gotestsum@v1.12.0 --format testname ./... diff --git a/ops/cmd/sync_staging/main.go b/ops/cmd/sync_staging/main.go index 22e7710c8..f995451f0 100644 --- a/ops/cmd/sync_staging/main.go +++ b/ops/cmd/sync_staging/main.go @@ -49,12 +49,14 @@ func action(cliCtx *cli.Context) error { } stagingDir := path.Join(wd, ".staging") - exists, err := fs.DirExists(stagingDir) + + intentExists, err := fs.FileExists(path.Join(stagingDir, "intent.toml")) if err != nil { - return fmt.Errorf("failed to check if staging dir exists: %w", err) + return fmt.Errorf("failed to check for intent file: %w", err) } - if !exists { - return fmt.Errorf("staging dir does not exist - check working directory") + if !intentExists { + output.WriteOK("no staged intent file found, exiting") + return nil } stagedChain, err := manage.NewStagedChain(stagingDir) @@ -83,7 +85,7 @@ func action(cliCtx *cli.Context) error { return fmt.Errorf("failed to fetch global chain IDs: %w", err) } if globalChainData.ChainIDs[chainCfg.ChainID] { - return fmt.Errorf("chain ID %s is already in use", chainCfg.ChainID) + return fmt.Errorf("chain ID %d is already in use", chainCfg.ChainID) } if globalChainData.ShortNames[stagedChain.Meta.ShortName] { return fmt.Errorf("short name %s is already in use", stagedChain.Meta.ShortName) diff --git a/ops/go.mod b/ops/go.mod index 6879f7048..fc3f4ed71 100644 --- a/ops/go.mod +++ b/ops/go.mod @@ -11,7 +11,11 @@ require ( github.com/urfave/cli/v2 v2.27.5 ) -require github.com/ethereum-optimism/superchain-registry/validation v0.0.0-20241225221610-a64f6a26aa8e +require ( + github.com/ethereum-optimism/superchain-registry/validation v0.0.0-20241225221610-a64f6a26aa8e + github.com/google/go-github/v68 v68.0.0 + github.com/klauspost/compress v1.17.11 +) require ( github.com/DataDog/zstd v1.5.6-0.20230824185856-869dae002e5e // indirect @@ -49,7 +53,7 @@ require ( github.com/gogo/protobuf v1.3.2 // indirect github.com/golang-jwt/jwt/v4 v4.5.1 // indirect github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect - github.com/google/go-github/v68 v68.0.0 // indirect + github.com/google/go-querystring v1.1.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/gorilla/websocket v1.5.3 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect @@ -60,7 +64,6 @@ require ( github.com/holiman/uint256 v1.3.2 // indirect github.com/huin/goupnp v1.3.0 // indirect github.com/jackpal/go-nat-pmp v1.0.2 // indirect - github.com/klauspost/compress v1.17.11 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/kr/text v0.2.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect @@ -82,7 +85,6 @@ require ( github.com/rs/cors v1.11.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/shirou/gopsutil v3.21.11+incompatible // indirect - github.com/stretchr/objx v0.5.2 // indirect github.com/supranational/blst v0.3.13 // indirect github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect diff --git a/ops/go.sum b/ops/go.sum index 5a97c9731..50cac5999 100644 --- a/ops/go.sum +++ b/ops/go.sum @@ -137,11 +137,14 @@ github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb/go.mod h1:/XxbfmMg github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-github/v68 v68.0.0 h1:ZW57zeNZiXTdQ16qrDiZ0k6XucrxZ2CGmoTvcCyQG6s= github.com/google/go-github/v68 v68.0.0/go.mod h1:K9HAUBovM2sLwM408A18h+wd9vqdLOEqTUCbnRIcx68= +github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/gofuzz v1.2.1-0.20220503160820-4a35382e8fc8 h1:Ep/joEub9YwcjRY6ND3+Y/w0ncE540RtGatVhtZL0/Q= github.com/google/gofuzz v1.2.1-0.20220503160820-4a35382e8fc8/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= diff --git a/ops/internal/fs/utils_test.go b/ops/internal/fs/utils_test.go index 201f5fe70..8cf7ff2c7 100644 --- a/ops/internal/fs/utils_test.go +++ b/ops/internal/fs/utils_test.go @@ -15,7 +15,7 @@ func TestDirExists(t *testing.T) { require.NoError(t, err) require.False(t, exists) - exists, err = DirExists("testdata/file.txt") + _, err = DirExists("testdata/file.txt") require.Error(t, err) require.ErrorContains(t, err, "is not a directory") } @@ -25,7 +25,7 @@ func TestFileExists(t *testing.T) { require.NoError(t, err) require.True(t, exists) - exists, err = FileExists("testdata") + _, err = FileExists("testdata") require.Error(t, err) require.ErrorContains(t, err, "is a directory") diff --git a/ops/internal/manage/validation.go b/ops/internal/manage/validation.go index 8adf80ee3..f00828c6e 100644 --- a/ops/internal/manage/validation.go +++ b/ops/internal/manage/validation.go @@ -93,6 +93,7 @@ func FetchGlobalChainIDs() (*GlobalChainIDs, error) { if err != nil { return nil, err } + defer res.Body.Close() type chainEntry struct { ChainId uint64 `json:"chainId"` diff --git a/ops/internal/report/comment.go b/ops/internal/report/comment.go index e94e94877..0e7835321 100644 --- a/ops/internal/report/comment.go +++ b/ops/internal/report/comment.go @@ -4,11 +4,10 @@ import ( "bytes" _ "embed" "fmt" + "math/big" "text/template" "time" - "math/big" - "github.com/ethereum-optimism/superchain-registry/validation" "github.com/ethereum/go-ethereum/common" ) diff --git a/ops/internal/testutil/mockrpc/mockrpc.go b/ops/internal/testutil/mockrpc/mockrpc.go index 619886d54..e4a48d384 100644 --- a/ops/internal/testutil/mockrpc/mockrpc.go +++ b/ops/internal/testutil/mockrpc/mockrpc.go @@ -10,6 +10,7 @@ import ( "net/http" "os" "testing" + "time" "github.com/ethereum/go-ethereum/log" "github.com/stretchr/testify/require" @@ -112,9 +113,26 @@ func NewMockRPC(t *testing.T, lgr log.Logger, opts ...Option) *MockRPC { srv := &http.Server{ Handler: m, } - go srv.Serve(m.lis) + + errCh := make(chan error, 1) + go func() { + err := srv.Serve(m.lis) + if errors.Is(err, http.ErrServerClosed) { + err = nil + } + errCh <- err + }() + + timer := time.NewTimer(100 * time.Millisecond) + select { + case err := <-errCh: + require.NoError(t, err) + case <-timer.C: + } + t.Cleanup(func() { require.NoError(t, srv.Shutdown(context.Background())) + require.NoError(t, <-errCh) }) return m diff --git a/superchain/extra/README.MD b/superchain/extra/README.MD index 1950b29d4..118973851 100644 --- a/superchain/extra/README.MD +++ b/superchain/extra/README.MD @@ -3,5 +3,4 @@ This is a collection of extra configuration data because not all address and system-config information is available through L1 for some chains. - [addresses.json](./addresses/addresses.json): L1 Smart Contract addresses for each network. -- [bytecodes](./bytecodes/): The full bytecode that is factored out of the compressed genesis system configuration file. Not designed for human consumption. - [genesis](./genesis/): Compressed genesis system configuration data. Not designed for human consumption. diff --git a/validation/go.mod b/validation/go.mod index aadc1773d..79f53b2ef 100644 --- a/validation/go.mod +++ b/validation/go.mod @@ -2,11 +2,13 @@ module github.com/ethereum-optimism/superchain-registry/validation go 1.23.1 -require github.com/BurntSushi/toml v1.4.0 +require ( + github.com/BurntSushi/toml v1.4.0 + github.com/stretchr/testify v1.10.0 +) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/testify v1.10.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/validation/go.sum b/validation/go.sum index 5f46fe759..e06781f54 100644 --- a/validation/go.sum +++ b/validation/go.sum @@ -6,6 +6,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/validation/params.go b/validation/params.go index 06f0a749f..62fe3b760 100644 --- a/validation/params.go +++ b/validation/params.go @@ -82,8 +82,10 @@ var standardConfigParamsMainnetToml []byte //go:embed standard/standard-config-params-sepolia.toml var standardConfigParamsSepoliaToml []byte -var StandardConfigParamsMainnet ConfigParams -var StandardConfigParamsSepolia ConfigParams +var ( + StandardConfigParamsMainnet ConfigParams + StandardConfigParamsSepolia ConfigParams +) func init() { if err := toml.Unmarshal(standardConfigParamsMainnetToml, &StandardConfigParamsMainnet); err != nil { diff --git a/validation/roles.go b/validation/roles.go index 902523a0e..428953fb1 100644 --- a/validation/roles.go +++ b/validation/roles.go @@ -20,8 +20,10 @@ var standardConfigRolesMainnetToml []byte //go:embed standard/standard-config-roles-sepolia.toml var standardConfigRolesSepoliaToml []byte -var StandardConfigRolesMainnet RolesConfig -var StandardConfigRolesSepolia RolesConfig +var ( + StandardConfigRolesMainnet RolesConfig + StandardConfigRolesSepolia RolesConfig +) func init() { if err := toml.Unmarshal(standardConfigRolesMainnetToml, &StandardConfigRolesMainnet); err != nil { diff --git a/validation/types.go b/validation/types.go index b8d966288..742744069 100644 --- a/validation/types.go +++ b/validation/types.go @@ -53,7 +53,7 @@ func (h *Hash) UnmarshalText(text []byte) error { b, err := hex.DecodeString(string(text[2:])) if err != nil { - return fmt.Errorf("invalid hex string: %v", err) + return fmt.Errorf("invalid hex string: %w", err) } if len(b) != len(h) { diff --git a/validation/versions.go b/validation/versions.go index 8da64d32f..60adbabf0 100644 --- a/validation/versions.go +++ b/validation/versions.go @@ -50,8 +50,10 @@ var standardVersionsMainnetToml []byte //go:embed standard/standard-versions-sepolia.toml var standardVersionsSepoliaToml []byte -var StandardVersionsMainnet Versions -var StandardVersionsSepolia Versions +var ( + StandardVersionsMainnet Versions + StandardVersionsSepolia Versions +) func init() { if err := toml.Unmarshal(standardVersionsMainnetToml, &StandardVersionsMainnet); err != nil {