gitdeploy/internal/api/api_test.go

121 lines
2.7 KiB
Go
Raw Normal View History

2021-02-24 02:34:33 +00:00
package api
import (
2021-02-24 08:13:36 +00:00
"encoding/json"
2021-02-24 02:34:33 +00:00
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"git.rootprojects.org/root/gitdeploy/internal/jobs"
"git.rootprojects.org/root/gitdeploy/internal/options"
"git.rootprojects.org/root/gitdeploy/internal/webhooks"
"github.com/go-chi/chi"
)
var server *httptest.Server
var runOpts *options.ServerConfig
var debounceDelay time.Duration
var jobDelay time.Duration
var logDir string
func init() {
tmpDir, _ := ioutil.TempDir("", "gitdeploy-*")
runOpts = &options.ServerConfig{
//Addr: "localhost:4483",
ScriptsPath: "./testdata",
2021-02-24 08:13:36 +00:00
LogDir: "./test-logs/api",
2021-02-24 02:34:33 +00:00
TmpDir: tmpDir,
DebounceDelay: 25 * time.Millisecond,
StaleJobAge: 5 * time.Minute,
StaleLogAge: 5 * time.Minute,
ExpiredLogAge: 10 * time.Minute,
}
logDir, _ = filepath.Abs(runOpts.LogDir)
r := chi.NewRouter()
server = httptest.NewServer(r)
runOpts.Addr = server.Listener.Addr().String()
RouteStopped(r, runOpts)
os.Setenv("GIT_DEPLOY_TEST_WAIT", "0.1")
debounceDelay = 50 * time.Millisecond
jobDelay = 250 * time.Millisecond
jobs.Start(runOpts)
//server.Close()
}
func TestCallback(t *testing.T) {
// TODO use full API request with local webhook
t7 := time.Now().Add(-40 * time.Second)
r7 := "ef1234abcd"
// skip debounce
hook := webhooks.Ref{
Timestamp: t7,
RepoID: "git.example.com/owner/repo",
HTTPSURL: "https://git.example.com/owner/repo.git",
Rev: r7,
RefName: "master",
RefType: "branch",
Owner: "owner",
Repo: "repo",
}
// , _ := json.MarshallIndent(&hook , "", " ")
jobs.Debounce(hook)
/*
body := bytes.NewReader(hook)
r := httptest.NewRequest("POST", "/api/local/webhook", body)
*/
2021-02-24 08:13:36 +00:00
// TODO use callback or chan chan to avoid sleeps?
2021-02-24 02:34:33 +00:00
time.Sleep(debounceDelay)
time.Sleep(jobDelay)
// TODO test that the API gives this back to us
urlRevID := hook.GetURLSafeRevID()
// TODO needs auth
reqURL := fmt.Sprintf("http://%s/api/admin/logs/%s",
runOpts.Addr,
string(urlRevID),
)
resp, err := http.Get(reqURL)
if nil != err {
2021-02-24 08:13:36 +00:00
t.Errorf("HTTP response error: %s\n%#v", reqURL, err)
2021-02-24 02:34:33 +00:00
return
}
2021-02-24 08:13:36 +00:00
job := &jobs.Job{}
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(job); nil != err {
t.Errorf(
"response decode error: %d %s\n%#v\n%#v",
resp.StatusCode, reqURL, resp.Header, err,
)
return
}
if len(job.Logs) < 3 {
t.Errorf("too few logs: %s\n%#v", reqURL, job)
return
}
if nil == job.Report || len(job.Report.Results) < 1 {
t.Errorf("too few results: %s\n%#v", reqURL, job)
return
}
if nil == job.ExitCode || 0 != *job.ExitCode {
t.Errorf("non-zero exit code: %s\n%#v", reqURL, job)
2021-02-24 02:34:33 +00:00
return
}
}