Skip to content

Commit

Permalink
refactor: implement Dockerized chain initialization in e2e tests (#1330)
Browse files Browse the repository at this point in the history
Closes: #XXX

## What is the purpose of the change

This is a follow-up PR to #1293 and builds upon its work. It is part of the e2e test chain upgrade epic #1235 

This PR introduces the ability to run chain initialization in a Docker container by running the following:
```
docker run -v < path >:/tmp/osmo-test osmosis-e2e-chain-init:debug --data-dir=/tmp/osmo-test
```
All chain data is placed at the given `< path >` that is mounted as a volume on the container. In addition, this PR introduces documentation about the current state of the e2e tests. 

## Brief change log

- [pull chain temp folder creation our of chain to e2e package](c175289)
- [create image and makefile steps to initialize chain state](de89119)
- [allow for running the upgrade initialization in Docker by providing a data dir](dabb683)
- [improve abstractions for chain initialization and add README](cf0d8a3)
- improve README

## Testing and Verifying

- ran e2e tests locally a few times
- `make build-e2e-chain-init`
- `make docker-build-e2e-chain-init`
- `docker run -v /home/roman/cosmos/osmosis/tmp:/tmp/osmo-test osmosis-e2e-chain-init:debug --data-dir=/tmp/osmo-test`

All steps worked as desired

## Documentation and Release Note

  - Does this pull request introduce a new feature or user-facing behavior changes? no
  - Is a relevant changelog entry added to the `Unreleased` section in `CHANGELOG.md`? no
  - How is the feature or change documented? improved `tests/e2e/README.md`

## Next Steps

The next step is to switch our chain initialization logic in `func (s *IntegrationTestSuite) configureChain(chainId string)` to use Dockertest and the newly introduced container. Then, mount the genesis and configs on the Osmosis containers, against which the e2e tests are executed
  • Loading branch information
p0mvn committed Apr 24, 2022
1 parent 6b6d5b5 commit c71f077
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 13 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ build-contract-tests-hooks:
mkdir -p $(BUILDDIR)
go build -mod=readonly $(BUILD_FLAGS) -o $(BUILDDIR)/ ./cmd/contract_tests

build-e2e-chain-init:
mkdir -p $(BUILDDIR)
go build -mod=readonly $(BUILD_FLAGS) -o $(BUILDDIR)/ ./tests/e2e/chain_init

go-mod-cache: go.sum
@echo "--> Download go modules to local cache"
@go mod download
Expand Down Expand Up @@ -239,6 +243,9 @@ benchmark:
docker-build-debug:
@docker build -t osmosis:debug --build-arg BASE_IMG_TAG=debug -f Dockerfile .

docker-build-e2e-chain-init:
@docker build -t osmosis-e2e-chain-init:debug -f tests/e2e/chain_init/chain-init.Dockerfile .

###############################################################################
### Linting ###
###############################################################################
Expand Down
62 changes: 60 additions & 2 deletions tests/e2e/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# End-to-end Tests

The package e2e defines an integration testing suite used for full end-to-end
testing functionality.
# Structure

## `e2e` Package

The `e2e` package defines an integration testing suite used for full end-to-end
testing functionality. This package is decoupled from depending on the Osmosis codebase.
It initializes the chains for testing via Docker files. As a result, the test suite may
provide the desired Osmosis version to Docker containers during the initialization.
This design allows for the opportunity of testing chain upgrades in the future by providing
an older Osmosis version to the container, performing the chain upgrade, and running the latest test suite.

The file e2e_suite_test.go defines the testing suite and contains the core
bootstrapping logic that creates a testing environment via Docker containers.
Expand All @@ -11,3 +19,53 @@ The file e2e_test.go contains the actual end-to-end integration tests that
utilize the testing suite.

Currently, there is a single test in `e2e_test.go` to query the balances of a validator.

## `chain` Package

The `chain` package introduces the logic necessary for initializing a chain by creating a genesis
file and all required configuration files such as the `app.toml`. This package directly depends on the Osmosis codebase.

## `upgrade` Package

The `upgrade` package starts chain initialization. In addition, there is a Dockerfile `init-e2e.Dockerfile`.
When executed, its container produces all files necessary for starting up a new chain.
These resulting files can be mounted on a volume and propagated to our production osmosis container to start the `osmosisd` service.

The decoupling between chain initialization and start-up allows to minimize the differences between our test suite and the production environment.

# Running Locally

##### To build the binary that initializes the chain:

```
make build-e2e-chain-init
```
- The produced binary is an entrypoint to the `osmosis-e2e-chain-init:debug` image.

##### To build the image for initializing the chain (`osmosis-e2e-chain-init:debug`):

```
make docker-build-e2e-chain-init
```

##### To run the chain initialization container locally:

```
mkdir < path >
docker run -v < path >:/tmp/osmo-test osmosis-e2e-chain-init:debug --data-dir=/tmp/osmo-test
sudo rm -r < path > # must be root to clean up
```
- runs a container with a volume mounted at < path > where all chain initialization files are placed.
- < path > must be absolute.
- `--data-dir` flag is needed for outputting the files into a directory inside the container

Example:
```
docker run -v /home/roman/cosmos/osmosis/tmp:/tmp/osmo-test osmosis-e2e-chain-init:debug --data-dir=/tmp/osmo-test
```

##### To build the debug Osmosis image:

```
make docker-build-e2e-debug
```
10 changes: 2 additions & 8 deletions tests/e2e/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package chain

import (
"fmt"
"io/ioutil"
)

const (
Expand All @@ -16,15 +15,10 @@ type Chain struct {
Validators []*Validator
}

func new(id string) (*Chain, error) {
tmpDir, err := ioutil.TempDir("", "osmosis-e2e-testnet-")
if err != nil {
return nil, err
}

func new(id, dataDir string) (*Chain, error) {
return &Chain{
Id: id,
DataDir: tmpDir,
DataDir: dataDir,
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/chain/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package chain

func Init(id string) (*Chain, error) {
chain, err := new(id)
func Init(id, dataDir string) (*Chain, error) {
chain, err := new(id, dataDir)
if err != nil {
return nil, err
}
Expand Down
23 changes: 23 additions & 0 deletions tests/e2e/chain_init/chain-init.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# syntax=docker/dockerfile:1

## Build Image
FROM golang:1.18-bullseye as build

WORKDIR /osmosis
COPY . /osmosis

# From https://github.com/CosmWasm/wasmd/blob/master/Dockerfile
# For more details see https://github.com/CosmWasm/wasmvm#builds-of-libwasmvm
ADD https://github.com/CosmWasm/wasmvm/releases/download/v1.0.0-beta7/libwasmvm_muslc.a /lib/libwasmvm_muslc.a
RUN sha256sum /lib/libwasmvm_muslc.a | grep d0152067a5609bfdfb3f0d5d6c0f2760f79d5f2cd7fd8513cafa9932d22eb350
RUN BUILD_TAGS=muslc make build-e2e-chain-init

## Deploy image
FROM ubuntu

COPY --from=build /osmosis/build/chain_init /bin/chain_init

ENV HOME /osmosis
WORKDIR $HOME

ENTRYPOINT [ "chain_init" ]
29 changes: 29 additions & 0 deletions tests/e2e/chain_init/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/osmosis-labs/osmosis/v7/tests/e2e/chain"
)

func main() {
var dataDir string
flag.StringVar(&dataDir, "data-dir", "", "chain data directory")
flag.Parse()

if len(dataDir) == 0 {
panic("data-dir is required")
}

if err := os.MkdirAll(dataDir, 0o755); err != nil {
panic(err)
}

chain, err := chain.Init(chain.ChainAID, dataDir)
if err != nil {
panic(err)
}
fmt.Println(chain)
}
4 changes: 3 additions & 1 deletion tests/e2e/e2e_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ func (s *IntegrationTestSuite) runIBCRelayer() {

func (s *IntegrationTestSuite) configureChain(chainId string) {
s.T().Logf("starting e2e infrastructure for chain-id: %s", chainId)
newChain, err := chain.Init(chainId)
tmpDir, err := ioutil.TempDir("", "osmosis-e2e-testnet-")
s.Require().NoError(err)
newChain, err := chain.Init(chainId, tmpDir)
s.chains = append(s.chains, newChain)
s.Require().NoError(err)
}
Expand Down

0 comments on commit c71f077

Please sign in to comment.