You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.go 2.8 kB

Add basic integration test infrastructure (and new endpoint `/api/v1/version` for testing it) (#741) * Implement '/api/v1/version' * Cleanup and various fixes * Enhance run.sh * Add install_test.go * Add parameter utils.Config for testing handlers * Re-organize TestVersion.go * Rename functions * handling process cleanup properly * Fix missing function renaming * Cleanup the 'retry' logic * Cleanup * Remove unneeded logging code * Logging messages tweaking * Logging message tweaking * Fix logging messages * Use 'const' instead of hardwired numbers * We don't really need retries anymore * Move constant ServerHttpPort to install_test.go * Restore mistakenly removed constant * Add required comments to make the linter happy. * Fix comments and naming to address linter's complaints * Detect Gitea executale version automatically * Remove tests/run.sh, `go test` suffices. * Make `make build` a prerequisite of `make test` * Do not sleep before trying * Speedup the server pinging loop * Use defined const instead of hardwired numbers * Remove redundant error handling * Use a dedicated target for running code.gitea.io/tests * Do not make 'test' depend on 'build' target * Rectify the excluded package list * Remove redundant 'exit 1' * Change the API to allow passing test.T to test handlers * Make testing.T an embedded field * Use assert.Equal to comparing results * Add copyright info * Parametrized logging output * Use tmpdir instead * Eliminate redundant casting * Remove unneeded variable * Fix last commit * Add missing copyright info * Replace fmt.Fprintf with fmt.Fprint * rename the xtest to integration-test * Use Symlink instead of hard-link for cross-device linking * Turn debugging logs on * Follow the existing framework for APIs * Output logs only if test.v is true * Re-order import statements * Enhance the error message * Fix comment which breaks the linter's rule * Rename 'integration-test' to 'e2e-test' for saving keystrokes * Add comment to avoid possible confusion * Rename tests -> integration-tests Also change back the Makefile to use `make integration-test`. * Use tests/integration for now * tests/integration -> integrations Slightly flattened directory hierarchy is better. * Update Makefile accordingly * Fix a missing change in Makefile * govendor update code.gitea.io/sdk/gitea * Fix comment of struct fields * Fix conditional nonsense * Fix missing updates regarding version string changes * Make variable naming more consistent * Check http status code * Rectify error messages
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. package utils
  5. import (
  6. "errors"
  7. "io"
  8. "io/ioutil"
  9. "log"
  10. "os"
  11. "os/exec"
  12. "path/filepath"
  13. "syscall"
  14. "testing"
  15. )
  16. // T wraps testing.T and the configurations of the testing instance.
  17. type T struct {
  18. *testing.T
  19. Config *Config
  20. }
  21. // New create an instance of T
  22. func New(t *testing.T, c *Config) *T {
  23. return &T{T: t, Config: c}
  24. }
  25. // Config Settings of the testing program
  26. type Config struct {
  27. // The executable path of the tested program.
  28. Program string
  29. // Working directory prepared for the tested program.
  30. // If empty, a directory named with random suffixes is picked, and created under the platform-dependent default temporary directory.
  31. // The directory will be removed when the test finishes.
  32. WorkDir string
  33. // Command-line arguments passed to the tested program.
  34. Args []string
  35. // Where to redirect the stdout/stderr to. For debugging purposes.
  36. LogFile *os.File
  37. }
  38. func redirect(cmd *exec.Cmd, f *os.File) error {
  39. stdout, err := cmd.StdoutPipe()
  40. if err != nil {
  41. return err
  42. }
  43. stderr, err := cmd.StderrPipe()
  44. if err != nil {
  45. return err
  46. }
  47. go io.Copy(f, stdout)
  48. go io.Copy(f, stderr)
  49. return nil
  50. }
  51. // RunTest Helper function for setting up a running Gitea server for functional testing and then gracefully terminating it.
  52. func (t *T) RunTest(tests ...func(*T) error) (err error) {
  53. if t.Config.Program == "" {
  54. return errors.New("Need input file")
  55. }
  56. path, err := filepath.Abs(t.Config.Program)
  57. if err != nil {
  58. return err
  59. }
  60. workdir := t.Config.WorkDir
  61. if workdir == "" {
  62. workdir, err = ioutil.TempDir(os.TempDir(), "gitea_tests-")
  63. if err != nil {
  64. return err
  65. }
  66. defer os.RemoveAll(workdir)
  67. }
  68. newpath := filepath.Join(workdir, filepath.Base(path))
  69. if err := os.Symlink(path, newpath); err != nil {
  70. return err
  71. }
  72. log.Printf("Starting the server: %s args:%s workdir:%s", newpath, t.Config.Args, workdir)
  73. cmd := exec.Command(newpath, t.Config.Args...)
  74. cmd.Dir = workdir
  75. if t.Config.LogFile != nil && testing.Verbose() {
  76. if err := redirect(cmd, t.Config.LogFile); err != nil {
  77. return err
  78. }
  79. }
  80. if err := cmd.Start(); err != nil {
  81. return err
  82. }
  83. log.Println("Server started.")
  84. defer func() {
  85. // Do not early return. We have to call Wait anyway.
  86. _ = cmd.Process.Signal(syscall.SIGTERM)
  87. if _err := cmd.Wait(); _err != nil {
  88. if _err.Error() != "signal: terminated" {
  89. err = _err
  90. return
  91. }
  92. }
  93. log.Println("Server exited")
  94. }()
  95. for _, fn := range tests {
  96. if err := fn(t); err != nil {
  97. return err
  98. }
  99. }
  100. // Note that the return value 'err' may be updated by the 'defer' statement before despite it's returning nil here.
  101. return nil
  102. }