run_with_timeout_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2021 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "bytes"
  17. "io"
  18. "regexp"
  19. "testing"
  20. "time"
  21. )
  22. func Test_runWithTimeout(t *testing.T) {
  23. type args struct {
  24. command string
  25. args []string
  26. timeout time.Duration
  27. onTimeoutCmd string
  28. stdin io.Reader
  29. }
  30. tests := []struct {
  31. name string
  32. args args
  33. wantStdout string
  34. wantStderr string
  35. wantErr bool
  36. }{
  37. {
  38. name: "no timeout",
  39. args: args{
  40. command: "echo",
  41. args: []string{"foo"},
  42. },
  43. wantStdout: "foo\n",
  44. },
  45. {
  46. name: "timeout not reached",
  47. args: args{
  48. command: "echo",
  49. args: []string{"foo"},
  50. timeout: 10 * time.Second,
  51. },
  52. wantStdout: "foo\n",
  53. },
  54. {
  55. name: "timed out",
  56. args: args{
  57. command: "sh",
  58. args: []string{"-c", "sleep 10 && echo foo"},
  59. timeout: 1 * time.Millisecond,
  60. },
  61. wantStderr: ".*: process timed out after .*\n",
  62. wantErr: true,
  63. },
  64. {
  65. name: "on_timeout command",
  66. args: args{
  67. command: "sh",
  68. args: []string{"-c", "sleep 10 && echo foo"},
  69. timeout: 1 * time.Millisecond,
  70. onTimeoutCmd: "echo bar",
  71. },
  72. wantStdout: "bar\n",
  73. wantStderr: ".*: process timed out after .*\n.*: running on_timeout command `echo bar`\n",
  74. wantErr: true,
  75. },
  76. }
  77. for _, tt := range tests {
  78. t.Run(tt.name, func(t *testing.T) {
  79. stdout := &bytes.Buffer{}
  80. stderr := &bytes.Buffer{}
  81. err := runWithTimeout(tt.args.command, tt.args.args, tt.args.timeout, tt.args.onTimeoutCmd, tt.args.stdin, stdout, stderr)
  82. if (err != nil) != tt.wantErr {
  83. t.Errorf("runWithTimeout() error = %v, wantErr %v", err, tt.wantErr)
  84. return
  85. }
  86. if gotStdout := stdout.String(); gotStdout != tt.wantStdout {
  87. t.Errorf("runWithTimeout() gotStdout = %v, want %v", gotStdout, tt.wantStdout)
  88. }
  89. if gotStderr := stderr.String(); !regexp.MustCompile(tt.wantStderr).MatchString(gotStderr) {
  90. t.Errorf("runWithTimeout() gotStderr = %v, want %v", gotStderr, tt.wantStderr)
  91. }
  92. })
  93. }
  94. }