add code to auto-update the version tag in the server + tests

This commit is contained in:
David Dworken 2022-04-08 23:47:13 -07:00
parent 3c6cc1e771
commit 2a083b7d06
3 changed files with 79 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
@ -335,3 +336,40 @@ func TestAdvancedQuery(t *testing.T) {
// 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))
}
}

View File

@ -7,6 +7,7 @@ import (
"log"
"net/http"
"os"
"time"
"github.com/ddworken/hishtory/shared"
_ "github.com/lib/pq"
@ -133,12 +134,47 @@ func OpenDB() (*gorm.DB, error) {
}
func init() {
if ReleaseVersion == "UNKNOWN" {
if ReleaseVersion == "UNKNOWN" && !isTestEnvironment() {
panic("server.go was built without a ReleaseVersion!")
}
go keepReleaseVersionUpToDate()
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() {
var err error
GLOBAL_DB, err = OpenDB()

View File

@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path"
"strings"
"testing"
"time"
)
@ -75,6 +76,9 @@ func RunTestServer(t *testing.T) func() {
if err != nil {
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())
}
}