diff options
Diffstat (limited to 'integrations')
-rw-r--r-- | integrations/install_test.go | 97 | ||||
-rw-r--r-- | integrations/internal/utils/utils.go | 125 | ||||
-rw-r--r-- | integrations/version_test.go | 82 |
3 files changed, 304 insertions, 0 deletions
diff --git a/integrations/install_test.go b/integrations/install_test.go new file mode 100644 index 0000000000..96d0ce1780 --- /dev/null +++ b/integrations/install_test.go @@ -0,0 +1,97 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package integration + +import ( + "fmt" + "net/http" + "os" + "os/user" + "path/filepath" + "testing" + "time" + + "code.gitea.io/gitea/integrations/internal/utils" +) + +// The HTTP port listened by the Gitea server. +const ServerHTTPPort = "3001" + +const _RetryLimit = 10 + +func makeSimpleSettings(user, workdir, port string) map[string][]string { + return map[string][]string{ + "db_type": {"SQLite3"}, + "db_host": {"localhost"}, + "db_path": {workdir + "data/gitea.db"}, + "app_name": {"Gitea: Git with a cup of tea"}, + "repo_root_path": {workdir + "repositories"}, + "run_user": {user}, + "domain": {"localhost"}, + "ssh_port": {"22"}, + "http_port": {port}, + "app_url": {"http://localhost:" + port}, + "log_root_path": {workdir + "log"}, + } +} + +func install(t *utils.T) error { + var r *http.Response + var err error + + for i := 1; i <= _RetryLimit; i++ { + + r, err = http.Get("http://:" + ServerHTTPPort + "/") + if err == nil { + fmt.Fprintln(os.Stderr) + break + } + + // Give the server some amount of time to warm up. + time.Sleep(100 * time.Millisecond) + fmt.Fprint(os.Stderr, ".") + } + + if err != nil { + return err + } + + defer r.Body.Close() + + _user, err := user.Current() + if err != nil { + return err + } + + path, err := filepath.Abs(t.Config.WorkDir) + if err != nil { + return err + } + + settings := makeSimpleSettings(_user.Username, path, ServerHTTPPort) + r, err = http.PostForm("http://:"+ServerHTTPPort+"/install", settings) + if err != nil { + return err + } + defer r.Body.Close() + + if r.StatusCode != http.StatusOK { + return fmt.Errorf("'/install': %s", r.Status) + } + return nil +} + +func TestInstall(t *testing.T) { + conf := utils.Config{ + Program: "../gitea", + WorkDir: "", + Args: []string{"web", "--port", ServerHTTPPort}, + LogFile: os.Stderr, + } + + if err := utils.New(t, &conf).RunTest(install); err != nil { + t.Fatal(err) + } +} diff --git a/integrations/internal/utils/utils.go b/integrations/internal/utils/utils.go new file mode 100644 index 0000000000..511fa478b9 --- /dev/null +++ b/integrations/internal/utils/utils.go @@ -0,0 +1,125 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package utils + +import ( + "errors" + "io" + "io/ioutil" + "log" + "os" + "os/exec" + "path/filepath" + "syscall" + "testing" +) + +// T wraps testing.T and the configurations of the testing instance. +type T struct { + *testing.T + Config *Config +} + +// New create an instance of T +func New(t *testing.T, c *Config) *T { + return &T{T: t, Config: c} +} + +// Config Settings of the testing program +type Config struct { + // The executable path of the tested program. + Program string + // Working directory prepared for the tested program. + // If empty, a directory named with random suffixes is picked, and created under the platform-dependent default temporary directory. + // The directory will be removed when the test finishes. + WorkDir string + // Command-line arguments passed to the tested program. + Args []string + + // Where to redirect the stdout/stderr to. For debugging purposes. + LogFile *os.File +} + +func redirect(cmd *exec.Cmd, f *os.File) error { + stdout, err := cmd.StdoutPipe() + if err != nil { + return err + } + + stderr, err := cmd.StderrPipe() + if err != nil { + return err + } + + go io.Copy(f, stdout) + go io.Copy(f, stderr) + return nil +} + +// RunTest Helper function for setting up a running Gitea server for functional testing and then gracefully terminating it. +func (t *T) RunTest(tests ...func(*T) error) (err error) { + if t.Config.Program == "" { + return errors.New("Need input file") + } + + path, err := filepath.Abs(t.Config.Program) + if err != nil { + return err + } + + workdir := t.Config.WorkDir + if workdir == "" { + workdir, err = ioutil.TempDir(os.TempDir(), "gitea_tests-") + if err != nil { + return err + } + defer os.RemoveAll(workdir) + } + + newpath := filepath.Join(workdir, filepath.Base(path)) + if err := os.Symlink(path, newpath); err != nil { + return err + } + + log.Printf("Starting the server: %s args:%s workdir:%s", newpath, t.Config.Args, workdir) + + cmd := exec.Command(newpath, t.Config.Args...) + cmd.Dir = workdir + + if t.Config.LogFile != nil && testing.Verbose() { + if err := redirect(cmd, t.Config.LogFile); err != nil { + return err + } + } + + if err := cmd.Start(); err != nil { + return err + } + + log.Println("Server started.") + + defer func() { + // Do not early return. We have to call Wait anyway. + _ = cmd.Process.Signal(syscall.SIGTERM) + + if _err := cmd.Wait(); _err != nil { + if _err.Error() != "signal: terminated" { + err = _err + return + } + } + + log.Println("Server exited") + }() + + for _, fn := range tests { + if err := fn(t); err != nil { + return err + } + } + + // Note that the return value 'err' may be updated by the 'defer' statement before despite it's returning nil here. + return nil +} diff --git a/integrations/version_test.go b/integrations/version_test.go new file mode 100644 index 0000000000..beda5c3ab7 --- /dev/null +++ b/integrations/version_test.go @@ -0,0 +1,82 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package integration + +import ( + "encoding/json" + "fmt" + "log" + "net/http" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + + "code.gitea.io/gitea/integrations/internal/utils" + "code.gitea.io/sdk/gitea" + + "github.com/stretchr/testify/assert" +) + +func version(t *utils.T) error { + var err error + + path, err := filepath.Abs(t.Config.Program) + if err != nil { + return err + } + + cmd := exec.Command(path, "--version") + out, err := cmd.Output() + if err != nil { + return err + } + + fields := strings.Fields(string(out)) + if !strings.HasPrefix(string(out), "Gitea version") { + return fmt.Errorf("unexpected version string '%s' of the gitea executable", out) + } + + expected := fields[2] + + var r *http.Response + r, err = http.Get("http://:" + ServerHTTPPort + "/api/v1/version") + if err != nil { + return err + } + defer r.Body.Close() + + if r.StatusCode != http.StatusOK { + return fmt.Errorf("'/api/v1/version': %s\n", r.Status) + } + + var v gitea.ServerVersion + + dec := json.NewDecoder(r.Body) + if err := dec.Decode(&v); err != nil { + return err + } + + actual := v.Version + + log.Printf("Actual: \"%s\" Expected: \"%s\"\n", actual, expected) + assert.Equal(t, expected, actual) + + return nil +} + +func TestVersion(t *testing.T) { + conf := utils.Config{ + Program: "../gitea", + WorkDir: "", + Args: []string{"web", "--port", ServerHTTPPort}, + LogFile: os.Stderr, + } + + if err := utils.New(t, &conf).RunTest(install, version); err != nil { + t.Fatal(err) + } +} |