github.com/badrootd/nibiru-cometbft@v0.37.5-0.20240307173500-2a75559eee9b/test/e2e/pkg/infra/docker/docker.go (about)

     1  package docker
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"path/filepath"
     7  	"text/template"
     8  
     9  	e2e "github.com/badrootd/nibiru-cometbft/test/e2e/pkg"
    10  	"github.com/badrootd/nibiru-cometbft/test/e2e/pkg/infra"
    11  )
    12  
    13  var _ infra.Provider = &Provider{}
    14  
    15  // Provider implements a docker-compose backed infrastructure provider.
    16  type Provider struct {
    17  	Testnet *e2e.Testnet
    18  }
    19  
    20  // Setup generates the docker-compose file and write it to disk, erroring if
    21  // any of these operations fail.
    22  func (p *Provider) Setup() error {
    23  	compose, err := dockerComposeBytes(p.Testnet)
    24  	if err != nil {
    25  		return err
    26  	}
    27  	//nolint: gosec
    28  	// G306: Expect WriteFile permissions to be 0600 or less
    29  	err = os.WriteFile(filepath.Join(p.Testnet.Dir, "docker-compose.yml"), compose, 0644)
    30  	if err != nil {
    31  		return err
    32  	}
    33  	return nil
    34  }
    35  
    36  // dockerComposeBytes generates a Docker Compose config file for a testnet and returns the
    37  // file as bytes to be written out to disk.
    38  func dockerComposeBytes(testnet *e2e.Testnet) ([]byte, error) {
    39  	// Must use version 2 Docker Compose format, to support IPv6.
    40  	tmpl, err := template.New("docker-compose").Parse(`version: '2.4'
    41  networks:
    42    {{ .Name }}:
    43      labels:
    44        e2e: true
    45      driver: bridge
    46  {{- if .IPv6 }}
    47      enable_ipv6: true
    48  {{- end }}
    49      ipam:
    50        driver: default
    51        config:
    52        - subnet: {{ .IP }}
    53  
    54  services:
    55  {{- range .Nodes }}
    56    {{ .Name }}:
    57      labels:
    58        e2e: true
    59      container_name: {{ .Name }}
    60      image: {{ .Version }}
    61  {{- if or (eq .ABCIProtocol "builtin") (eq .ABCIProtocol "builtin_unsync") }}
    62      entrypoint: /usr/bin/entrypoint-builtin
    63  {{- end }}
    64      init: true
    65      ports:
    66      - 26656
    67      - {{ if .ProxyPort }}{{ .ProxyPort }}:{{ end }}26657
    68  {{- if .PrometheusProxyPort }}
    69      - {{ .PrometheusProxyPort }}:26660
    70  {{- end }}
    71      - 6060
    72      volumes:
    73      - ./{{ .Name }}:/cometbft
    74      - ./{{ .Name }}:/tendermint
    75      networks:
    76        {{ $.Name }}:
    77          ipv{{ if $.IPv6 }}6{{ else }}4{{ end}}_address: {{ .IP }}
    78  {{- if ne .Version $.UpgradeVersion}}
    79  
    80    {{ .Name }}_u:
    81      labels:
    82        e2e: true
    83      container_name: {{ .Name }}_u
    84      image: {{ $.UpgradeVersion }}
    85  {{- if or (eq .ABCIProtocol "builtin") (eq .ABCIProtocol "builtin_unsync") }}
    86      entrypoint: /usr/bin/entrypoint-builtin
    87  {{- end }}
    88      init: true
    89      ports:
    90      - 26656
    91      - {{ if .ProxyPort }}{{ .ProxyPort }}:{{ end }}26657
    92  {{- if .PrometheusProxyPort }}
    93      - {{ .PrometheusProxyPort }}:26660
    94  {{- end }}
    95      - 6060
    96      volumes:
    97      - ./{{ .Name }}:/cometbft
    98      - ./{{ .Name }}:/tendermint
    99      networks:
   100        {{ $.Name }}:
   101          ipv{{ if $.IPv6 }}6{{ else }}4{{ end}}_address: {{ .IP }}
   102  {{- end }}
   103  
   104  {{end}}`)
   105  	if err != nil {
   106  		return nil, err
   107  	}
   108  	var buf bytes.Buffer
   109  	err = tmpl.Execute(&buf, testnet)
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  	return buf.Bytes(), nil
   114  }