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

     1  // Copyright 2016 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  	"path/filepath"
    17  	"strings"
    18  	"testing"
    19  
    20  	"github.com/cockroachdb/cockroach/pkg/acceptance/cluster"
    21  	"github.com/cockroachdb/cockroach/pkg/security"
    22  	"github.com/cockroachdb/cockroach/pkg/util/log"
    23  )
    24  
    25  const testGlob = "../cli/interactive_tests/test*.tcl"
    26  const containerPath = "/go/src/github.com/cockroachdb/cockroach/cli/interactive_tests"
    27  
    28  var cmdBase = []string{
    29  	"/usr/bin/env",
    30  	"COCKROACH_SKIP_UPDATE_CHECK=1",
    31  	"COCKROACH_CRASH_REPORTS=",
    32  	"/bin/bash",
    33  	"-c",
    34  }
    35  
    36  func TestDockerCLI(t *testing.T) {
    37  	s := log.Scope(t)
    38  	defer s.Close(t)
    39  
    40  	containerConfig := defaultContainerConfig()
    41  	containerConfig.Cmd = []string{"stat", cluster.CockroachBinaryInContainer}
    42  	containerConfig.Env = []string{fmt.Sprintf("PGUSER=%s", security.RootUser)}
    43  	ctx := context.Background()
    44  	if err := testDockerOneShot(ctx, t, "cli_test", containerConfig); err != nil {
    45  		t.Skipf(`TODO(dt): No binary in one-shot container, see #6086: %s`, err)
    46  	}
    47  
    48  	paths, err := filepath.Glob(testGlob)
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  	if len(paths) == 0 {
    53  		t.Fatalf("no testfiles found (%v)", testGlob)
    54  	}
    55  
    56  	for _, p := range paths {
    57  		testFile := filepath.Base(p)
    58  		testPath := filepath.Join(containerPath, testFile)
    59  		if strings.Contains(testPath, "disabled") {
    60  			t.Logf("Skipping explicitly disabled test %s", testFile)
    61  			continue
    62  		}
    63  		t.Run(testFile, func(t *testing.T) {
    64  			log.Infof(ctx, "-- starting tests from: %s", testFile)
    65  
    66  			// Symlink the logs directory to /logs, which is visible outside of the
    67  			// container and preserved if the test fails. (They don't write to /logs
    68  			// directly because they are often run manually outside of Docker, where
    69  			// /logs is unlikely to exist.)
    70  			cmd := "ln -s /logs logs"
    71  
    72  			// We run the expect command using `bash -c "(expect ...)"`.
    73  			//
    74  			// We cannot run `expect` directly, nor `bash -c "expect ..."`,
    75  			// because both cause Expect to become the PID 1 process inside
    76  			// the container. On Unix, orphan processes need to be wait()ed
    77  			// upon by the PID 1 process when they terminate, lest they
    78  			// remain forever in the zombie state. Unfortunately, Expect
    79  			// does not contain code to do this. Bash does.
    80  			cmd += "; (expect"
    81  			if log.V(2) {
    82  				cmd = cmd + " -d"
    83  			}
    84  			cmd = cmd + " -f " + testPath + " " + cluster.CockroachBinaryInContainer + ")"
    85  			containerConfig.Cmd = append(cmdBase, cmd)
    86  
    87  			if err := testDockerOneShot(ctx, t, "cli_test", containerConfig); err != nil {
    88  				t.Error(err)
    89  			}
    90  		})
    91  	}
    92  }
    93  
    94  // TestDockerUnixSocket verifies that CockroachDB initializes a unix
    95  // socket useable by 'psql', even when the server runs insecurely.
    96  func TestDockerUnixSocket(t *testing.T) {
    97  	s := log.Scope(t)
    98  	defer s.Close(t)
    99  
   100  	containerConfig := defaultContainerConfig()
   101  	containerConfig.Cmd = []string{"stat", cluster.CockroachBinaryInContainer}
   102  	ctx := context.Background()
   103  
   104  	if err := testDockerOneShot(ctx, t, "cli_test", containerConfig); err != nil {
   105  		t.Skipf(`TODO(dt): No binary in one-shot container, see #6086: %s`, err)
   106  	}
   107  
   108  	containerConfig.Env = []string{fmt.Sprintf("PGUSER=%s", security.RootUser)}
   109  	containerConfig.Cmd = append(cmdBase,
   110  		"/mnt/data/psql/test-psql-unix.sh "+cluster.CockroachBinaryInContainer)
   111  	if err := testDockerOneShot(ctx, t, "unix_socket_test", containerConfig); err != nil {
   112  		t.Error(err)
   113  	}
   114  }