summaryrefslogtreecommitdiffstats
path: root/integrations/version_test.go
blob: beda5c3ab78704dbc52af235ef6b226e4f3f56b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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)
	}
}