github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/acceptance/util_docker.go (about)

     1  // Copyright 2015 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package acceptance
    12  
    13  import (
    14  	"context"
    15  	"fmt"
    16  	"os"
    17  	"path/filepath"
    18  	"testing"
    19  
    20  	"github.com/cockroachdb/cockroach/pkg/acceptance/cluster"
    21  	"github.com/cockroachdb/cockroach/pkg/base"
    22  	"github.com/cockroachdb/cockroach/pkg/security"
    23  	"github.com/docker/docker/api/types"
    24  	"github.com/docker/docker/api/types/container"
    25  )
    26  
    27  func defaultContainerConfig() container.Config {
    28  	return container.Config{
    29  		Image: acceptanceImage,
    30  		Env: []string{
    31  			fmt.Sprintf("PGUSER=%s", security.RootUser),
    32  			fmt.Sprintf("PGPORT=%s", base.DefaultPort),
    33  			"PGSSLCERT=/certs/client.root.crt",
    34  			"PGSSLKEY=/certs/client.root.key",
    35  		},
    36  		Entrypoint: []string{"autouseradd", "-u", "roach", "-C", "/home/roach", "--"},
    37  	}
    38  }
    39  
    40  // testDockerFail ensures the specified docker cmd fails.
    41  func testDockerFail(ctx context.Context, t *testing.T, name string, cmd []string) {
    42  	containerConfig := defaultContainerConfig()
    43  	containerConfig.Cmd = cmd
    44  	if err := testDockerSingleNode(ctx, t, name, containerConfig); err == nil {
    45  		t.Error("expected failure")
    46  	}
    47  }
    48  
    49  // testDockerSuccess ensures the specified docker cmd succeeds.
    50  func testDockerSuccess(ctx context.Context, t *testing.T, name string, cmd []string) {
    51  	containerConfig := defaultContainerConfig()
    52  	containerConfig.Cmd = cmd
    53  	if err := testDockerSingleNode(ctx, t, name, containerConfig); err != nil {
    54  		t.Error(err)
    55  	}
    56  }
    57  
    58  const (
    59  	// Iterating against a locally built version of the docker image can be done
    60  	// by changing acceptanceImage to the hash of the container.
    61  	acceptanceImage = "docker.io/cockroachdb/acceptance:20200303-091324"
    62  )
    63  
    64  func testDocker(
    65  	ctx context.Context, t *testing.T, num int, name string, containerConfig container.Config,
    66  ) error {
    67  	var err error
    68  	RunDocker(t, func(t *testing.T) {
    69  		cfg := cluster.TestConfig{
    70  			Name:     name,
    71  			Duration: *flagDuration,
    72  		}
    73  		for i := 0; i < num; i++ {
    74  			cfg.Nodes = append(cfg.Nodes, cluster.NodeConfig{Stores: []cluster.StoreConfig{{}}})
    75  		}
    76  		l := StartCluster(ctx, t, cfg).(*cluster.DockerCluster)
    77  		defer l.AssertAndStop(ctx, t)
    78  
    79  		if len(l.Nodes) > 0 {
    80  			containerConfig.Env = append(containerConfig.Env, "PGHOST="+l.Hostname(0))
    81  		}
    82  		var pwd string
    83  		pwd, err = os.Getwd()
    84  		if err != nil {
    85  			return
    86  		}
    87  		hostConfig := container.HostConfig{
    88  			NetworkMode: "host",
    89  			Binds:       []string{filepath.Join(pwd, "testdata") + ":/mnt/data"},
    90  		}
    91  		err = l.OneShot(
    92  			ctx, acceptanceImage, types.ImagePullOptions{}, containerConfig, hostConfig, "docker-"+name,
    93  		)
    94  		preserveLogs := err != nil
    95  		l.Cleanup(ctx, preserveLogs)
    96  	})
    97  	return err
    98  }
    99  
   100  func testDockerSingleNode(
   101  	ctx context.Context, t *testing.T, name string, containerConfig container.Config,
   102  ) error {
   103  	return testDocker(ctx, t, 1, name, containerConfig)
   104  }
   105  
   106  func testDockerOneShot(
   107  	ctx context.Context, t *testing.T, name string, containerConfig container.Config,
   108  ) error {
   109  	return testDocker(ctx, t, 0, name, containerConfig)
   110  }