github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/cli/interactive_tests/test_temp_dir.tcl (about)

     1  #! /usr/bin/env expect -f
     2  #
     3  source [file join [file dirname $argv0] common.tcl]
     4  
     5  set recordfile "temp-dirs-record.txt"
     6  set tempprefix "cockroach-temp"
     7  set storedir "mystore"
     8  set tempdir "mystore/temp"
     9  set cwd ""
    10  # Ensure that pwd will only return absolute paths.
    11  cd [file normalize [pwd]]
    12  set ::env(PWD) [pwd]
    13  
    14  # We want cwd to be "" if pwd is root since it will mess up our pattern matching.
    15  if {! [string match "/" [pwd]]} {
    16    set cwd [pwd]
    17  }
    18  
    19  proc file_exists {filepath} {
    20    if {! [ file exist $filepath]} {
    21      report "MISSING EXPECTED FILE: $filepath"
    22      exit 1
    23    }
    24  }
    25  
    26  proc file_not_exists {filepath} {
    27    if {[ file exist $filepath]} {
    28      report "FILE STILL EXISTS: $filepath"
    29      exit 1
    30    }
    31  }
    32  
    33  proc glob_exists {pattern} {
    34    if {[llength [glob -nocomplain -type d $pattern]] == 0} {
    35      report "MISSING EXPECTED GLOB FILES: $pattern"
    36      exit 1
    37    }
    38  }
    39  
    40  proc glob_not_exists {pattern} {
    41    if {[llength [glob -nocomplain -type d $pattern]] > 0} {
    42      report "GLOB FILES STILL EXIST: $pattern"
    43      exit 1
    44    }
    45  }
    46  
    47  spawn /bin/bash
    48  send "PS1=':''/# '\r"
    49  eexpect ":/# "
    50  
    51  start_test "Check that on node startup a temporary subdirectory is created under --temp-dir and recorded to a record file, and on node shutdown the directory is removed."
    52  send "mkdir -p $tempdir\r"
    53  send "$argv start-single-node --insecure --store=$storedir --temp-dir=$tempdir\r"
    54  eexpect "node starting"
    55  eexpect "temp dir:*$tempdir/$tempprefix"
    56  # Verify the temp directory under first store is created.
    57  glob_exists "$tempdir/$tempprefix*"
    58  set rfile [open "$storedir/$recordfile"]
    59  # Verify temp directory path recorded to record file.
    60  if {! [string match "$cwd/$tempdir/$tempprefix*" [gets $rfile]]} {
    61    report "MISSING DIRECTORY PATH FROM RECORD FILE"
    62    exit 1
    63  }
    64  close $rfile
    65  interrupt
    66  eexpect "shutdown completed"
    67  # Verify the temp directory is removed.
    68  glob_not_exists "$tempdir/$tempprefix*"
    69  # Verify temp directory path is removed from record file.
    70  if {[file size "$storedir/$recordfile"] > 0} {
    71    report "RECORD FILE NOT EMPTY"
    72    exit 1
    73  }
    74  end_test
    75  
    76  start_test "Check that on node startup a temporary subdirectory is created under --temp-dir even if store is in-memory and removed on shutdown."
    77  send "mkdir -p $tempdir\r"
    78  send "$argv start-single-node --insecure --store=type=mem,size=1GB --temp-dir=$tempdir\r"
    79  eexpect "node starting"
    80  eexpect "temp dir:*$tempdir/$tempprefix"
    81  # Verify the temp directory under first store is created.
    82  glob_exists "$tempdir/$tempprefix*"
    83  interrupt
    84  eexpect "shutdown completed"
    85  # Verify the temp directory is removed.
    86  glob_not_exists "$tempdir/$tempprefix*"
    87  end_test
    88  
    89  start_test "Check that temporary directories in record file are cleaned up during cli startup."
    90  send "mkdir -p $tempdir $storedir/temp1 $storedir/temp2\r"
    91  send "echo foobartext >  $storedir/temp1/foo.txt\r"
    92  # We add the temp directories to the record file.
    93  send "cat > $storedir/$recordfile <<EOF\r$cwd/$storedir/temp1\r$cwd/$storedir/temp2\rEOF\r"
    94  send "$argv start-single-node --insecure --store=$storedir --temp-dir=$tempdir\r"
    95  eexpect "node starting"
    96  eexpect "temp dir:*$cwd/$tempdir/$tempprefix"
    97  # Verify temp1 and temp2 are removed shortly after startup.
    98  file_not_exists "$storedir/temp1"
    99  file_not_exists "$storedir/temp2"
   100  interrupt
   101  eexpect "shutdown completed"
   102  # Verify the temp directory is removed.
   103  glob_not_exists "$tempdir/$tempprefix*"
   104  # Verify store directory still exists.
   105  file_exists $storedir
   106  end_test
   107  
   108  start_test "Check that if --temp-dir is unspecified, a temporary directory is created under --store"
   109  send "$argv start-single-node --insecure --store=$storedir\r"
   110  eexpect "node starting"
   111  eexpect "temp dir:*$cwd/$storedir/$tempprefix"
   112  # Verify the temp directory under first store is created.
   113  glob_exists "$storedir/$tempprefix*"
   114  interrupt
   115  eexpect "shutdown completed"
   116  # Verify the temp directory is removed.
   117  glob_not_exists "$storedir/$tempprefix*"
   118  # Verify the store file still exists.
   119  file_exists $storedir
   120  end_test
   121  
   122  start_test "Check that temp directory does not get wiped upon subsequent failed cockroach start attempt and that a cockroach instance can be subsequently started up after a shutdown"
   123  send "$argv start-single-node --insecure --store=$storedir --background\r"
   124  eexpect ":/# "
   125  # Try to start up a second cockroach instance with the same store path.
   126  send "$argv start-single-node --insecure --store=$storedir\r"
   127  eexpect "ERROR: could not cleanup temporary directories from record file: could not lock temporary directory $cwd/$storedir/$tempprefix*"
   128  # Verify the temp directory still exists.
   129  glob_exists "$storedir/$tempprefix*"
   130  send "pkill -9 cockroach\r"
   131  # We should be able to start the cockroach instance again.
   132  send "$argv start-single-node --insecure --store=$storedir\r"
   133  eexpect "node starting"
   134  interrupt
   135  eexpect "shutdown completed"
   136  end_test