github.com/pachyderm/pachyderm@v1.13.4/src/server/config/cmds_test.go (about)

     1  package cmds
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/pachyderm/pachyderm/src/client/pkg/require"
     9  	tu "github.com/pachyderm/pachyderm/src/server/pkg/testutil"
    10  )
    11  
    12  func run(t *testing.T, cmd string) error {
    13  	t.Helper()
    14  
    15  	tmpfile, err := ioutil.TempFile("", "test-pach-config-*.json")
    16  	require.NoError(t, err)
    17  
    18  	// remove the empty file so that a config can be generated
    19  	require.NoError(t, os.Remove(tmpfile.Name()))
    20  
    21  	// remove the config file when done
    22  	defer os.Remove(tmpfile.Name())
    23  
    24  	return tu.BashCmd(`
    25  		export PACH_CONFIG={{.config}}
    26  		{{.cmd}}
    27  		`,
    28  		"config", tmpfile.Name(),
    29  		"cmd", cmd,
    30  	).Run()
    31  }
    32  
    33  func TestInvalidEnvValue(t *testing.T) {
    34  	if testing.Short() {
    35  		t.Skip("Skipping integration tests in short mode")
    36  	}
    37  
    38  	require.YesError(t, run(t, `
    39  		export PACH_CONTEXT=foobar
    40  		pachctl config get active-context
    41  	`))
    42  }
    43  
    44  func TestEnvValue(t *testing.T) {
    45  	if testing.Short() {
    46  		t.Skip("Skipping integration tests in short mode")
    47  	}
    48  
    49  	require.NoError(t, run(t, `
    50  		echo '{}' | pachctl config set context foo --overwrite
    51  		echo '{}' | pachctl config set context bar --overwrite
    52  		pachctl config set active-context bar
    53  		export PACH_CONTEXT=foo
    54  		pachctl config get active-context | match foo
    55  	`))
    56  }
    57  
    58  func TestMetrics(t *testing.T) {
    59  	if testing.Short() {
    60  		t.Skip("Skipping integration tests in short mode")
    61  	}
    62  
    63  	require.NoError(t, run(t, `
    64  		pachctl config get metrics | match true
    65  		pachctl config set metrics false
    66  		pachctl config get metrics | match false
    67  		pachctl config set metrics true
    68  		pachctl config get metrics | match true
    69  	`))
    70  }
    71  
    72  func TestActiveContext(t *testing.T) {
    73  	if testing.Short() {
    74  		t.Skip("Skipping integration tests in short mode")
    75  	}
    76  
    77  	require.YesError(t, run(t, `
    78  		pachctl config set active-context foo 2>&1 | match "context does not exist: foo"
    79  	`))
    80  
    81  	require.NoError(t, run(t, `
    82  		echo '{}' | pachctl config set context foo --overwrite
    83  		pachctl config set active-context foo
    84  		pachctl config get active-context | match "foo"
    85  	`))
    86  }
    87  
    88  func TestSetContext(t *testing.T) {
    89  	if testing.Short() {
    90  		t.Skip("Skipping integration tests in short mode")
    91  	}
    92  
    93  	require.YesError(t, run(t, `
    94  		echo 'malformed_json' | pachctl config set context foo
    95  	`))
    96  
    97  	require.YesError(t, run(t, `
    98  		echo '{}' | pachctl config set context foo
    99  		echo '{}' | pachctl config set context foo
   100  	`))
   101  
   102  	require.NoError(t, run(t, `
   103  		echo '{}' | pachctl config set context foo
   104  		echo '{"pachd_address": "foobar:9000"}' | pachctl config set context foo --overwrite
   105  		pachctl config get context foo | match '"pachd_address": "grpc://foobar:9000"'
   106  	`))
   107  }
   108  
   109  func TestUpdateContext(t *testing.T) {
   110  	if testing.Short() {
   111  		t.Skip("Skipping integration tests in short mode")
   112  	}
   113  
   114  	require.YesError(t, run(t, `
   115  		pachctl config update context foo --pachd-address=bar
   116  	`))
   117  
   118  	require.NoError(t, run(t, `
   119  		echo '{}' | pachctl config set context foo
   120  		pachctl config update context foo --pachd-address="foobar:9000"
   121  		pachctl config get context foo | match '"pachd_address": "grpc://foobar:9000"'
   122  		pachctl config update context foo --pachd-address=""
   123  		pachctl config get context foo | match -v pachd_address
   124  	`))
   125  }
   126  
   127  func TestDeleteContext(t *testing.T) {
   128  	if testing.Short() {
   129  		t.Skip("Skipping integration tests in short mode")
   130  	}
   131  
   132  	require.YesError(t, run(t, `
   133  		pachctl config delete context foo
   134  	`))
   135  
   136  	require.NoError(t, run(t, `
   137  		echo '{}' | pachctl config set context foo
   138  		pachctl config delete context foo
   139  	`))
   140  }
   141  
   142  func TestConfigListContext(t *testing.T) {
   143  	if testing.Short() {
   144  		t.Skip("Skipping integration tests in short mode")
   145  	}
   146  
   147  	require.NoError(t, run(t, `
   148  		echo '{}' | pachctl config set context foo
   149  		echo '{}' | pachctl config set context bar
   150  		pachctl config list context | match -v "\*	bar"
   151  		pachctl config list context | match "	bar"
   152  		pachctl config list context | match "	foo"
   153  
   154  		pachctl config set active-context bar
   155  		pachctl config list context | match "\*	bar"
   156  		pachctl config list context | match "	foo"
   157  	`))
   158  }