Add test framework (#227)
This commit is contained in:
parent
4f17bb0fbc
commit
543e31634f
6 changed files with 236 additions and 13 deletions
36
.drone.yml
36
.drone.yml
|
@ -6,32 +6,46 @@ platform:
|
|||
os: linux
|
||||
arch: amd64
|
||||
|
||||
clone:
|
||||
disable: true
|
||||
|
||||
workspace:
|
||||
base: /go
|
||||
path: src/code.gitea.io/sdk
|
||||
|
||||
steps:
|
||||
- name: git
|
||||
pull: default
|
||||
image: plugins/git:next
|
||||
settings:
|
||||
depth: 50
|
||||
tags: true
|
||||
- name: gitea
|
||||
image: gitea/gitea:latest
|
||||
detach: true
|
||||
commands:
|
||||
- mkdir -p /tmp/conf/
|
||||
- mkdir -p /tmp/data/
|
||||
- echo "[security]" > /tmp/conf/app.ini
|
||||
- echo "INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE1NTg4MzY4ODB9.LoKQyK5TN_0kMJFVHWUW0uDAyoGjDP6Mkup4ps2VJN4" >> /tmp/conf/app.ini
|
||||
- echo "INSTALL_LOCK = true" >> /tmp/conf/app.ini
|
||||
- echo "SECRET_KEY = 2crAW4UANgvLipDS6U5obRcFosjSJHQANll6MNfX7P0G3se3fKcCwwK3szPyGcbo" >> /tmp/conf/app.ini
|
||||
- echo "[database]" >> /tmp/conf/app.ini
|
||||
- echo "DB_TYPE = sqlite3" >> /tmp/conf/app.ini
|
||||
- echo "[repository]" >> /tmp/conf/app.ini
|
||||
- echo "ROOT = /tmp/data/" >> /tmp/conf/app.ini
|
||||
- gitea migrate -c /tmp/conf/app.ini
|
||||
- gitea admin create-user --username=test01 --password=test01 --email=test01@gitea.io --admin=true --must-change-password=false --access-token -c /tmp/conf/app.ini
|
||||
- gitea web -c /tmp/conf/app.ini
|
||||
|
||||
- name: testing
|
||||
pull: always
|
||||
image: golang:1.13
|
||||
environment:
|
||||
GOPROXY: https://goproxy.cn
|
||||
GITEA_SDK_TEST_URL: "http://gitea:3000"
|
||||
GITEA_SDK_TEST_USERNAME: "test01"
|
||||
GITEA_SDK_TEST_PASSWORD: "test01"
|
||||
#GITEA_SDK_TEST_RUN_GITEA: "true"
|
||||
commands:
|
||||
- make clean
|
||||
- make vet
|
||||
- make lint
|
||||
- make test
|
||||
#- make lint
|
||||
- make build
|
||||
- export HTTP_PROXY=""
|
||||
- curl --noproxy "*" http://gitea:3000/api/v1/version # verify connection to instance
|
||||
- HTTP_PROXY="" http_proxy="" make test
|
||||
|
||||
- name: discord
|
||||
pull: always
|
||||
|
|
|
@ -25,6 +25,8 @@ func Version() string {
|
|||
type Client struct {
|
||||
url string
|
||||
accessToken string
|
||||
username string
|
||||
password string
|
||||
sudo string
|
||||
client *http.Client
|
||||
}
|
||||
|
@ -45,6 +47,11 @@ func NewClientWithHTTP(url string, httpClient *http.Client) *Client {
|
|||
return client
|
||||
}
|
||||
|
||||
// SetBasicAuth sets basicauth
|
||||
func (c *Client) SetBasicAuth(username, password string) {
|
||||
c.username, c.password = username, password
|
||||
}
|
||||
|
||||
// SetHTTPClient replaces default http.Client with user given one.
|
||||
func (c *Client) SetHTTPClient(client *http.Client) {
|
||||
c.client = client
|
||||
|
@ -63,6 +70,9 @@ func (c *Client) doRequest(method, path string, header http.Header, body io.Read
|
|||
if len(c.accessToken) != 0 {
|
||||
req.Header.Set("Authorization", "token "+c.accessToken)
|
||||
}
|
||||
if len(c.username) != 0 {
|
||||
req.SetBasicAuth(c.username, c.password)
|
||||
}
|
||||
if c.sudo != "" {
|
||||
req.Header.Set("Sudo", c.sudo)
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
module code.gitea.io/sdk/gitea
|
||||
|
||||
go 1.12
|
||||
|
||||
require github.com/stretchr/testify v1.4.0
|
||||
|
|
11
gitea/go.sum
11
gitea/go.sum
|
@ -0,0 +1,11 @@
|
|||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
156
gitea/main_test.go
Normal file
156
gitea/main_test.go
Normal file
|
@ -0,0 +1,156 @@
|
|||
// Copyright 2020 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 gitea
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func getGiteaURL() string {
|
||||
return os.Getenv("GITEA_SDK_TEST_URL")
|
||||
}
|
||||
|
||||
func getGiteaToken() string {
|
||||
return os.Getenv("GITEA_SDK_TEST_TOKEN")
|
||||
}
|
||||
|
||||
func getGiteaUsername() string {
|
||||
return os.Getenv("GITEA_SDK_TEST_USERNAME")
|
||||
}
|
||||
|
||||
func getGiteaPassword() string {
|
||||
return os.Getenv("GITEA_SDK_TEST_PASSWORD")
|
||||
}
|
||||
|
||||
func enableRunGitea() bool {
|
||||
r, _ := strconv.ParseBool(os.Getenv("GITEA_SDK_TEST_RUN_GITEA"))
|
||||
return r
|
||||
}
|
||||
|
||||
func newTestClient() *Client {
|
||||
token := getGiteaToken()
|
||||
if token == "" {
|
||||
client := NewClientWithHTTP(getGiteaURL(), &http.Client{})
|
||||
log.Printf("testing with %v, %v, %v\n", getGiteaURL(), getGiteaUsername(), getGiteaPassword())
|
||||
client.SetBasicAuth(getGiteaUsername(), getGiteaPassword())
|
||||
return client
|
||||
}
|
||||
return NewClient(getGiteaURL(), getGiteaToken())
|
||||
}
|
||||
|
||||
func giteaMasterPath() string {
|
||||
switch runtime.GOOS {
|
||||
case "darwin":
|
||||
return fmt.Sprintf("https://dl.gitea.io/gitea/master/gitea-master-darwin-10.6-%s", runtime.GOARCH)
|
||||
case "linux":
|
||||
return fmt.Sprintf("https://dl.gitea.io/gitea/master/gitea-master-linux-%s", runtime.GOARCH)
|
||||
case "windows":
|
||||
return fmt.Sprintf("https://dl.gitea.io/gitea/master/gitea-master-windows-4.0-%s.exe", runtime.GOARCH)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func downGitea() (string, error) {
|
||||
for i := 3; i > 0; i-- {
|
||||
resp, err := http.Get(giteaMasterPath())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
f, err := ioutil.TempFile(os.TempDir(), "gitea")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
_, err = io.Copy(f, resp.Body)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if err = os.Chmod(f.Name(), 700); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return f.Name(), nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("Download gitea from %v failed", giteaMasterPath())
|
||||
}
|
||||
|
||||
func runGitea() (*os.Process, error) {
|
||||
log.Println("Downloading Gitea from", giteaMasterPath())
|
||||
p, err := downGitea()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
giteaDir := filepath.Dir(p)
|
||||
cfgDir := filepath.Join(giteaDir, "custom", "conf")
|
||||
os.MkdirAll(cfgDir, os.ModePerm)
|
||||
cfg, err := os.Create(filepath.Join(cfgDir, "app.ini"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = cfg.WriteString(`[security]
|
||||
INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE1NTg4MzY4ODB9.LoKQyK5TN_0kMJFVHWUW0uDAyoGjDP6Mkup4ps2VJN4
|
||||
INSTALL_LOCK = true
|
||||
SECRET_KEY = 2crAW4UANgvLipDS6U5obRcFosjSJHQANll6MNfX7P0G3se3fKcCwwK3szPyGcbo
|
||||
[database]
|
||||
DB_TYPE = sqlite3
|
||||
[log]
|
||||
MODE = console
|
||||
LEVEL = Trace
|
||||
REDIRECT_MACARON_LOG = true
|
||||
MACARON = ,
|
||||
ROUTER = ,`)
|
||||
cfg.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println("Run gitea migrate", p)
|
||||
err = exec.Command(p, "migrate").Run()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println("Run gitea admin", p)
|
||||
err = exec.Command(p, "admin", "create-user", "--username=test01", "--password=test01", "--email=test01@gitea.io", "--admin=true", "--must-change-password=false", "--access-token").Run()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println("Start Gitea", p)
|
||||
return os.StartProcess(filepath.Base(p), []string{}, &os.ProcAttr{
|
||||
Dir: giteaDir,
|
||||
})
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
if enableRunGitea() {
|
||||
p, err := runGitea()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
p.Kill()
|
||||
}()
|
||||
}
|
||||
exitCode := m.Run()
|
||||
os.Exit(exitCode)
|
||||
}
|
30
gitea/repo_test.go
Normal file
30
gitea/repo_test.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
// Copyright 2020 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 gitea
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCreateRepo(t *testing.T) {
|
||||
c := newTestClient()
|
||||
user, err := c.GetMyUserInfo()
|
||||
assert.NoError(t, err)
|
||||
|
||||
var repoName = "test1"
|
||||
_, err = c.GetRepo(user.UserName, repoName)
|
||||
if err != nil {
|
||||
repo, err := c.CreateRepo(CreateRepoOption{
|
||||
Name: repoName,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, repo)
|
||||
}
|
||||
|
||||
err = c.DeleteRepo(user.UserName, repoName)
|
||||
assert.NoError(t, err)
|
||||
}
|
Loading…
Reference in a new issue