| 1 | package reexec |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "reflect" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | ) |
| 15 | |
| 16 | const ( |
| 17 | testReExec = "test-reexec" |
| 18 | testReExec2 = "test-reexec2" |
| 19 | testReExec3 = "test-reexec3" |
| 20 | ) |
| 21 | |
| 22 | func init() { |
| 23 | Register(testReExec, func() { |
| 24 | panic("Return Error") |
| 25 | }) |
| 26 | Register(testReExec2, func() { |
| 27 | var args string |
| 28 | if len(os.Args) > 1 { |
| 29 | args = fmt.Sprintf("(args: %#v)", os.Args[1:]) |
| 30 | } |
| 31 | fmt.Println("Hello", testReExec2, args) |
| 32 | os.Exit(0) |
| 33 | }) |
| 34 | Register(testReExec3, func() { |
| 35 | fmt.Println("Hello " + testReExec3) |
| 36 | time.Sleep(1 * time.Second) |
| 37 | os.Exit(0) |
| 38 | }) |
| 39 | Init() |
| 40 | } |
| 41 | |
| 42 | func TestRegister(t *testing.T) { |
| 43 | tests := []struct { |
| 44 | doc string |
| 45 | name string |
| 46 | expectedErr string |
| 47 | }{ |
| 48 | { |
| 49 | doc: "duplicate name", |
| 50 | name: testReExec, |
| 51 | expectedErr: `reexec func already registered under name "test-reexec"`, |
| 52 | }, |
| 53 | { |
| 54 | doc: "invalid name", |
| 55 | name: filepath.Join("something", testReExec), |
| 56 | expectedErr: fmt.Sprintf("reexec func does not expect a path component: %q", filepath.Join("something", testReExec)), |
| 57 | }, |
| 58 | } |
| 59 | |
| 60 | for _, tc := range tests { |
| 61 | t.Run(tc.doc, func(t *testing.T) { |
| 62 | defer func() { |
| 63 | r := recover() |
| 64 | if r == nil { |
| 65 | t.Errorf("Register() did not panic") |
| 66 | return |
| 67 | } |
| 68 | if r != tc.expectedErr { |
| 69 | t.Errorf("got %q, want %q", r, tc.expectedErr) |
| 70 | } |
| 71 | }() |
| 72 | Register(tc.name, func() {}) |
| 73 | }) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestCommand(t *testing.T) { |
| 78 | tests := []struct { |
| 79 | doc string |
| 80 | cmdAndArgs []string |
| 81 | expOut string |
| 82 | }{ |
| 83 | { |
| 84 | doc: "basename", |
| 85 | cmdAndArgs: []string{testReExec2}, |
| 86 | expOut: "Hello test-reexec2", |
| 87 | }, |
| 88 | { |
| 89 | doc: "full path", |
| 90 | cmdAndArgs: []string{filepath.Join("something", testReExec2)}, |
| 91 | expOut: `Hello test-reexec2`, |
| 92 | }, |
| 93 | { |
| 94 | doc: "command with args", |
| 95 | cmdAndArgs: []string{testReExec2, "--some-flag", "some-value", "arg1", "arg2"}, |
| 96 | expOut: `Hello test-reexec2 (args: []string{"--some-flag", "some-value", "arg1", "arg2"})`, |
| 97 | }, |
| 98 | } |
| 99 | for _, tc := range tests { |
| 100 | t.Run(tc.doc, func(t *testing.T) { |
| 101 | cmd := Command(tc.cmdAndArgs...) |
| 102 | if !reflect.DeepEqual(cmd.Args, tc.cmdAndArgs) { |
| 103 | t.Fatalf("got %+v, want %+v", cmd.Args, tc.cmdAndArgs) |
| 104 | } |
| 105 | w, err := cmd.StdinPipe() |
| 106 | if err != nil { |
| 107 | t.Fatalf("Error on pipe creation: %v", err) |
| 108 | } |
| 109 | defer func() { _ = w.Close() }() |
| 110 | |
| 111 | out, err := cmd.CombinedOutput() |
| 112 | if err != nil { |
| 113 | t.Errorf("Error on re-exec cmd: %v, out: %v", err, string(out)) |
| 114 | } |
| 115 | |
| 116 | actual := strings.TrimSpace(string(out)) |
| 117 | if actual != tc.expOut { |
| 118 | t.Errorf("got %v, want %v", actual, tc.expOut) |
| 119 | } |
| 120 | }) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | func TestCommandContext(t *testing.T) { |
| 125 | testError := errors.New("test-error: the command was canceled") |
| 126 | |
| 127 | tests := []struct { |
| 128 | doc string |
| 129 | cmdAndArgs []string |
| 130 | cancel bool |
| 131 | expOut string |
| 132 | expError error |
| 133 | }{ |
| 134 | { |
| 135 | doc: "basename", |
| 136 | cmdAndArgs: []string{testReExec2}, |
| 137 | expOut: "Hello test-reexec2", |
| 138 | }, |
| 139 | { |
| 140 | doc: "full path", |
| 141 | cmdAndArgs: []string{filepath.Join("something", testReExec2)}, |
| 142 | expOut: "Hello test-reexec2", |
| 143 | }, |
| 144 | { |
| 145 | doc: "command with args", |
| 146 | cmdAndArgs: []string{testReExec2, "--some-flag", "some-value", "arg1", "arg2"}, |
| 147 | expOut: `Hello test-reexec2 (args: []string{"--some-flag", "some-value", "arg1", "arg2"})`, |
| 148 | }, |
| 149 | { |
| 150 | doc: "context canceled", |
| 151 | cancel: true, |
| 152 | cmdAndArgs: []string{testReExec2}, |
| 153 | expError: context.Canceled, |
| 154 | }, |
| 155 | { |
| 156 | doc: "context timeout", |
| 157 | cmdAndArgs: []string{testReExec3}, |
| 158 | expOut: "Hello test-reexec3", |
| 159 | expError: testError, |
| 160 | }, |
| 161 | } |
| 162 | |
| 163 | for _, tc := range tests { |
| 164 | t.Run(tc.doc, func(t *testing.T) { |
| 165 | ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 166 | defer cancel() |
| 167 | |
| 168 | cmd := CommandContext(ctx, tc.cmdAndArgs...) |
| 169 | if !reflect.DeepEqual(cmd.Args, tc.cmdAndArgs) { |
| 170 | t.Fatalf("got %+v, want %+v", cmd.Args, tc.cmdAndArgs) |
| 171 | } |
| 172 | cmd.Cancel = func() error { |
| 173 | return testError |
| 174 | } |
| 175 | |
| 176 | w, err := cmd.StdinPipe() |
| 177 | if err != nil { |
| 178 | t.Fatalf("Error on pipe creation: %v", err) |
| 179 | } |
| 180 | defer func() { _ = w.Close() }() |
| 181 | if tc.cancel { |
| 182 | cancel() |
| 183 | } |
| 184 | out, err := cmd.CombinedOutput() |
| 185 | if !errors.Is(err, tc.expError) { |
| 186 | t.Errorf("expected %v, got: %v", tc.expError, err) |
| 187 | } |
| 188 | |
| 189 | actual := strings.TrimSpace(string(out)) |
| 190 | if actual != tc.expOut { |
| 191 | t.Errorf("got %v, want %v", actual, tc.expOut) |
| 192 | } |
| 193 | }) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | func TestNaiveSelf(t *testing.T) { |
| 198 | if os.Getenv("TEST_CHECK") == "1" { |
| 199 | os.Exit(2) |
| 200 | } |
| 201 | cmd := exec.Command(naiveSelf(), "-test.run=TestNaiveSelf") |
| 202 | cmd.Env = append(os.Environ(), "TEST_CHECK=1") |
| 203 | err := cmd.Start() |
| 204 | if err != nil { |
| 205 | t.Fatalf("Unable to start command: %v", err) |
| 206 | } |
| 207 | err = cmd.Wait() |
| 208 | |
| 209 | var expError *exec.ExitError |
| 210 | if !errors.As(err, &expError) { |
| 211 | t.Fatalf("got %T, want %T", err, expError) |
| 212 | } |
| 213 | |
| 214 | const expected = "exit status 2" |
| 215 | if err.Error() != expected { |
| 216 | t.Fatalf("got %v, want %v", err, expected) |
| 217 | } |
| 218 | |
| 219 | os.Args[0] = "mkdir" |
| 220 | if naiveSelf() == os.Args[0] { |
| 221 | t.Fatalf("Expected naiveSelf to resolve the location of mkdir") |
| 222 | } |
| 223 | } |