github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/test/e2e/pkg/infra/docker/docker.go (about)

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