Files
percona-toolkit/src/go/lib/versioncheck/version_check_test.go
Carlos Salguero 92b1109804 Fixed PT-69
2017-02-13 13:08:54 -03:00

62 lines
1.2 KiB
Go

package versioncheck
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
func TestCheckUpdates(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
m := strings.Split(string(body), ";")
advices := []Advice{
Advice{
Hash: m[0],
ToolName: m[1],
Advice: "There is a new version",
},
}
buf, _ := json.Marshal(advices)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(buf))
}))
defer ts.Close()
os.Setenv("PERCONA_VERSION_CHECK_URL", ts.URL)
msg, err := CheckUpdates("pt-test", "2.2.18")
if err != nil {
t.Errorf("error while checking %s", err)
}
if msg == "" {
t.Error("got empty response")
}
}
func TestEmptyResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "")
}))
defer ts.Close()
os.Setenv("PERCONA_VERSION_CHECK_URL", ts.URL)
msg, err := CheckUpdates("pt-test", "2.2.18")
if err == nil {
t.Error("response should return error due to empty body")
}
if msg != "" {
t.Error("response should return error due to empty body")
}
}