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"
|
2021-02-25 09:49:57 +00:00
|
|
|
"math"
|
2021-02-24 02:34:33 +00:00
|
|
|
"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()
|
|
|
|
}
|
|
|
|
|
2021-02-25 09:49:57 +00:00
|
|
|
func ToUnixSeconds(t time.Time) float64 {
|
|
|
|
// 1614236182.651912345
|
|
|
|
secs := float64(t.Unix()) // 1614236182
|
|
|
|
nanos := float64(t.Nanosecond()) / 1_000_000_000.0 // 0.651912345
|
|
|
|
//nanos := (float64(t.UnixNano()) - secs) / 1_000_000_000.0 // 0.651912345
|
|
|
|
|
|
|
|
// in my case I want to truncate the precision to milliseconds
|
|
|
|
nanos = math.Round((10000 * nanos) / 10000) // 0.6519
|
|
|
|
|
|
|
|
s := secs + nanos // 1614236182.651912345
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-02-24 02:34:33 +00:00
|
|
|
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)
|
2021-02-26 02:12:12 +00:00
|
|
|
t.Log("sleep so job can finish")
|
|
|
|
time.Sleep(jobDelay)
|
2021-02-24 02:34:33 +00:00
|
|
|
time.Sleep(jobDelay)
|
|
|
|
|
|
|
|
// TODO test that the API gives this back to us
|
|
|
|
urlRevID := hook.GetURLSafeRevID()
|
|
|
|
|
2021-02-25 09:49:57 +00:00
|
|
|
s := ToUnixSeconds(t7.Add(-1 * time.Second))
|
|
|
|
|
2021-02-24 02:34:33 +00:00
|
|
|
// TODO needs auth
|
2021-02-25 09:49:57 +00:00
|
|
|
reqURL := fmt.Sprintf(
|
|
|
|
"http://%s/api/admin/logs/%s?since=%f",
|
|
|
|
runOpts.Addr, string(urlRevID), s,
|
2021-02-24 02:34:33 +00:00
|
|
|
)
|
|
|
|
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{}
|
2021-02-26 01:41:35 +00:00
|
|
|
b, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
if err := json.Unmarshal(b, job); nil != err {
|
2021-02-24 08:13:36 +00:00
|
|
|
t.Errorf(
|
|
|
|
"response decode error: %d %s\n%#v\n%#v",
|
|
|
|
resp.StatusCode, reqURL, resp.Header, err,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(job.Logs) < 3 {
|
2021-02-26 01:41:35 +00:00
|
|
|
t.Errorf("too few logs: %s\n%s\n%#v", reqURL, string(b), job)
|
2021-02-24 08:13:36 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|