|
| 1 | +// Copyright 2017 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package utils |
| 6 | + |
| 7 | +import ( |
| 8 | + "errors" |
| 9 | + "io" |
| 10 | + "io/ioutil" |
| 11 | + "log" |
| 12 | + "os" |
| 13 | + "os/exec" |
| 14 | + "path/filepath" |
| 15 | + "syscall" |
| 16 | + "testing" |
| 17 | +) |
| 18 | + |
| 19 | +// T wraps testing.T and the configurations of the testing instance. |
| 20 | +type T struct { |
| 21 | + *testing.T |
| 22 | + Config *Config |
| 23 | +} |
| 24 | + |
| 25 | +// New create an instance of T |
| 26 | +func New(t *testing.T, c *Config) *T { |
| 27 | + return &T{T: t, Config: c} |
| 28 | +} |
| 29 | + |
| 30 | +// Config Settings of the testing program |
| 31 | +type Config struct { |
| 32 | + // The executable path of the tested program. |
| 33 | + Program string |
| 34 | + // Working directory prepared for the tested program. |
| 35 | + // If empty, a directory named with random suffixes is picked, and created under the platform-dependent default temporary directory. |
| 36 | + // The directory will be removed when the test finishes. |
| 37 | + WorkDir string |
| 38 | + // Command-line arguments passed to the tested program. |
| 39 | + Args []string |
| 40 | + |
| 41 | + // Where to redirect the stdout/stderr to. For debugging purposes. |
| 42 | + LogFile *os.File |
| 43 | +} |
| 44 | + |
| 45 | +func redirect(cmd *exec.Cmd, f *os.File) error { |
| 46 | + stdout, err := cmd.StdoutPipe() |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + stderr, err := cmd.StderrPipe() |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + go io.Copy(f, stdout) |
| 57 | + go io.Copy(f, stderr) |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +// RunTest Helper function for setting up a running Gitea server for functional testing and then gracefully terminating it. |
| 62 | +func (t *T) RunTest(tests ...func(*T) error) (err error) { |
| 63 | + if t.Config.Program == "" { |
| 64 | + return errors.New("Need input file") |
| 65 | + } |
| 66 | + |
| 67 | + path, err := filepath.Abs(t.Config.Program) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + workdir := t.Config.WorkDir |
| 73 | + if workdir == "" { |
| 74 | + workdir, err = ioutil.TempDir(os.TempDir(), "gitea_tests-") |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + defer os.RemoveAll(workdir) |
| 79 | + } |
| 80 | + |
| 81 | + newpath := filepath.Join(workdir, filepath.Base(path)) |
| 82 | + if err := os.Symlink(path, newpath); err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + log.Printf("Starting the server: %s args:%s workdir:%s", newpath, t.Config.Args, workdir) |
| 87 | + |
| 88 | + cmd := exec.Command(newpath, t.Config.Args...) |
| 89 | + cmd.Dir = workdir |
| 90 | + |
| 91 | + if t.Config.LogFile != nil && testing.Verbose() { |
| 92 | + if err := redirect(cmd, t.Config.LogFile); err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if err := cmd.Start(); err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + log.Println("Server started.") |
| 102 | + |
| 103 | + defer func() { |
| 104 | + // Do not early return. We have to call Wait anyway. |
| 105 | + _ = cmd.Process.Signal(syscall.SIGTERM) |
| 106 | + |
| 107 | + if _err := cmd.Wait(); _err != nil { |
| 108 | + if _err.Error() != "signal: terminated" { |
| 109 | + err = _err |
| 110 | + return |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + log.Println("Server exited") |
| 115 | + }() |
| 116 | + |
| 117 | + for _, fn := range tests { |
| 118 | + if err := fn(t); err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Note that the return value 'err' may be updated by the 'defer' statement before despite it's returning nil here. |
| 124 | + return nil |
| 125 | +} |
0 commit comments