mirror of
https://github.com/ddworken/hishtory.git
synced 2025-08-16 18:01:46 +02:00
add code to auto-update the version tag in the server + tests
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@ -335,3 +336,40 @@ func TestAdvancedQuery(t *testing.T) {
|
|||||||
|
|
||||||
// TODO: test the username,hostname atoms
|
// TODO: test the username,hostname atoms
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGithubRedirects(t *testing.T) {
|
||||||
|
// Set up
|
||||||
|
defer shared.BackupAndRestore(t)()
|
||||||
|
defer shared.RunTestServer(t)()
|
||||||
|
|
||||||
|
// Check the redirects
|
||||||
|
client := &http.Client{
|
||||||
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||||
|
return http.ErrUseLastResponse
|
||||||
|
},
|
||||||
|
}
|
||||||
|
resp, err := client.Get("http://localhost:8080/download/hishtory-linux-amd64")
|
||||||
|
shared.Check(t, err)
|
||||||
|
if resp.StatusCode != 302 {
|
||||||
|
t.Fatalf("expected endpoint to return redirect")
|
||||||
|
}
|
||||||
|
locationHeader := resp.Header.Get("location")
|
||||||
|
if !strings.Contains(locationHeader, "https://github.com/ddworken/hishtory/releases/download/v") {
|
||||||
|
t.Fatalf("expected location header to point to github")
|
||||||
|
}
|
||||||
|
if !strings.HasSuffix(locationHeader, "/hishtory-linux-amd64") {
|
||||||
|
t.Fatalf("expected location header to point to binary")
|
||||||
|
}
|
||||||
|
|
||||||
|
// And retrieve it and check we can do that
|
||||||
|
resp, err = http.Get("http://localhost:8080/download/hishtory-linux-amd64")
|
||||||
|
shared.Check(t, err)
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf("didn't return a 200 status code, status_code=%d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
respBody, err := ioutil.ReadAll(resp.Body)
|
||||||
|
shared.Check(t, err)
|
||||||
|
if len(respBody) < 5_000_000 {
|
||||||
|
t.Fatalf("response is too short to be a binary, resp=%d", len(respBody))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/ddworken/hishtory/shared"
|
"github.com/ddworken/hishtory/shared"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
@ -133,12 +134,47 @@ func OpenDB() (*gorm.DB, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if ReleaseVersion == "UNKNOWN" {
|
if ReleaseVersion == "UNKNOWN" && !isTestEnvironment() {
|
||||||
panic("server.go was built without a ReleaseVersion!")
|
panic("server.go was built without a ReleaseVersion!")
|
||||||
}
|
}
|
||||||
|
go keepReleaseVersionUpToDate()
|
||||||
InitDB()
|
InitDB()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func keepReleaseVersionUpToDate() {
|
||||||
|
for {
|
||||||
|
updateReleaseVersion()
|
||||||
|
time.Sleep(10 * time.Minute)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type releaseInfo struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateReleaseVersion() {
|
||||||
|
resp, err := http.Get("https://api.github.com/repos/ddworken/hishtory/releases/latest")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("failed to get latest release version: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
fmt.Printf("failed to call github API, status_code=%d\n", resp.StatusCode)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
respBody, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("failed to read github API response body: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var info releaseInfo
|
||||||
|
err = json.Unmarshal(respBody, &info)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("failed to parse github API response: %v", err)
|
||||||
|
}
|
||||||
|
ReleaseVersion = info.Name
|
||||||
|
}
|
||||||
|
|
||||||
func InitDB() {
|
func InitDB() {
|
||||||
var err error
|
var err error
|
||||||
GLOBAL_DB, err = OpenDB()
|
GLOBAL_DB, err = OpenDB()
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -75,6 +76,9 @@ func RunTestServer(t *testing.T) func() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to kill process: %v", err)
|
t.Fatalf("failed to kill process: %v", err)
|
||||||
}
|
}
|
||||||
|
if strings.Contains(stderr.String()+stdout.String(), "failed to") {
|
||||||
|
t.Fatalf("server failed to do something: stderr=%#v, stdout=%#v", stderr.String(), stdout.String())
|
||||||
|
}
|
||||||
fmt.Printf("stderr=%#v, stdout=%#v\n", stderr.String(), stdout.String())
|
fmt.Printf("stderr=%#v, stdout=%#v\n", stderr.String(), stdout.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user