github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/cli/interactive_tests/common.tcl (about) 1 # We are sending vt100 terminal sequences below, so inform readline 2 # accordingly. 3 set env(TERM) vt100 4 5 system "mkdir -p logs" 6 7 # Keep the history in a test location, so as to not override the 8 # developer's own history file when running out of Docker. 9 set histfile "cockroach_sql_history" 10 11 set ::env(COCKROACH_SKIP_ENABLING_DIAGNOSTIC_REPORTING) "true" 12 set ::env(COCKROACH_CONNECT_TIMEOUT) 15 13 set ::env(COCKROACH_SQL_CLI_HISTORY) $histfile 14 # Set client commands as insecure. The server uses --insecure. 15 set ::env(COCKROACH_INSECURE) "true" 16 system "rm -f $histfile" 17 18 # Everything in this test should be fast. Don't be tolerant for long 19 # waits. 20 set timeout 30 21 22 # When run via Docker the enclosing terminal has 0 columns and 0 rows, 23 # and this confuses readline. Ensure sane defaults here. 24 set stty_init "cols 80 rows 25" 25 26 # Convenience function to tag what's going on in log files. 27 proc report {text} { 28 system "echo; echo \$(date '+.%y%m%d %H:%M:%S.%N') EXPECT TEST: '$text' | tee -a logs/expect-cmd.log" 29 # We really want to have all files erasable outside of the container 30 # even though the commands here run with uid 0. 31 # Docker is obnoxious in that it doesn't support setting `umask`. 32 # Also CockroachDB doesn't honor umask anyway. 33 # So we simply come after the fact and adjust the permissions. 34 # 35 # The find may race with a cockroach process shutting down in the 36 # background; cockroach might be deleting files as they are being 37 # found, causing chmod to not find its target file. We ignore 38 # these errors. 39 system "find logs -exec chmod a+rw '{}' \\; || true" 40 } 41 42 # Catch signals 43 proc mysig {} { 44 report "EXPECT KILLED BY SIGNAL" 45 exit 130 46 } 47 trap mysig SIGINT 48 trap mysig SIGTERM 49 50 # Convenience functions to tag a test 51 proc start_test {text} { 52 report "START TEST: $text" 53 } 54 proc end_test {} { 55 report "END TEST" 56 } 57 58 # Convenience wrapper function, which ensures that all expects are 59 # mandatory (i.e. with a mandatory fail if the expected output doesn't 60 # show up fast). 61 proc handle_timeout {text} { 62 report "TIMEOUT WAITING FOR \"$text\"" 63 exit 1 64 } 65 proc eexpect {text} { 66 expect { 67 $text {} 68 timeout { handle_timeout $text } 69 } 70 } 71 72 # Convenience function that sends Ctrl+C to the monitored process. 73 proc interrupt {} { 74 report "INTERRUPT TO FOREGROUND PROCESS" 75 send "\003" 76 sleep 0.4 77 } 78 79 # Convenience function that sends Ctrl+D to the monitored process. 80 # Leaves some upfront delay to let the readline process the time 81 # to initialize the key binding. 82 proc send_eof {} { 83 report "EOF TO FOREGROUND PROCESS" 84 sleep 0.4 85 send "\004" 86 } 87 88 # Convenience functions to start/shutdown the server. 89 # Preserves the invariant that the server's PID is saved 90 # in `server_pid`. 91 proc start_server {argv} { 92 report "BEGIN START SERVER" 93 system "$argv start-single-node --insecure --max-sql-memory=128MB --pid-file=server_pid --listening-url-file=server_url --background -s=path=logs/db >>logs/expect-cmd.log 2>&1; 94 $argv sql --insecure -e 'select 1'" 95 report "START SERVER DONE" 96 } 97 proc stop_server {argv} { 98 report "BEGIN STOP SERVER" 99 # Trigger a normal shutdown. 100 # If after 30 seconds the server hasn't shut down, kill the process and trigger an error. 101 # Note: kill -CONT tests whether the PID exists (SIGCONT is a no-op for the process). 102 system "kill -TERM `cat server_pid` 2>/dev/null; 103 for i in `seq 1 30`; do 104 kill -CONT `cat server_pid` 2>/dev/null || exit 0 105 echo still waiting 106 sleep 1 107 done 108 echo 'server still running?' 109 # Send an unclean shutdown signal to trigger a stack trace dump. 110 kill -ABRT `cat server_pid` 2>/dev/null 111 # Sleep to increase the probability that the stack trace actually 112 # makes it to disk before we force-kill the process. 113 sleep 1 114 kill -KILL `cat server_pid` 2>/dev/null 115 exit 1" 116 117 report "END STOP SERVER" 118 } 119 120 proc flush_server_logs {} { 121 report "BEGIN FLUSH LOGS" 122 system "kill -HUP `cat server_pid` 2>/dev/null" 123 # Wait for flush to occur. 124 system "for i in `seq 1 3`; do 125 grep 'hangup received, flushing logs' logs/db/logs/cockroach.log && exit 0; 126 echo still waiting 127 sleep 1 128 done 129 echo 'server failed to flush logs?' 130 exit 1" 131 report "END FLUSH LOGS" 132 } 133 134 proc force_stop_server {argv} { 135 report "BEGIN FORCE STOP SERVER" 136 system "kill -KILL `cat server_pid`" 137 report "END FORCE STOP SERVER" 138 }