Merge branch 'tests' into 'master'

Adding tests for instances

See merge request khanzf/fedilogue!6
This commit is contained in:
Fikrān Mutasā'il 2021-09-29 14:42:18 +00:00
commit a6e7e134e0
7 changed files with 116 additions and 1 deletions

View File

@ -45,6 +45,7 @@ type Settings struct {
var settings Settings var settings Settings
/* Test: TestStringexists */
func stringexists(needle string, haystack []string) bool { func stringexists(needle string, haystack []string) bool {
for _, check := range haystack { for _, check := range haystack {
if check == needle { if check == needle {

14
fedilogue/config_test.go Normal file
View File

@ -0,0 +1,14 @@
package main
import (
"testing"
)
func TestStringexists(t *testing.T) {
var empty_strings = []string {}
var three_strings = []string {"first", "second", "third"}
AssertEqual(t, stringexists("amything", empty_strings), false)
AssertEqual(t, stringexists("second", three_strings), true)
AssertEqual(t, stringexists("fourth", three_strings), false)
}

View File

@ -10,7 +10,8 @@ var pool *pgxpool.Pool
func getDbPool() *pgxpool.Pool { func getDbPool() *pgxpool.Pool {
// Setup Database // Setup Database
pool, err := pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL")) dburl := os.Getenv("DATABASE_URL")
pool, err := pgxpool.Connect(context.Background(), dburl)
if err != nil { if err != nil {
logFatal.Fatal("Unable to connect to database:", err) logFatal.Fatal("Unable to connect to database:", err)
} }

View File

@ -27,6 +27,10 @@ func statusReportHandler() {
} }
} }
/* Tests:
- TestStatusReport_empty_run
- TestStatusReport_full_content
*/
func StatusReport() { func StatusReport() {
running := 0 running := 0
keepalive := 0 keepalive := 0

View File

@ -31,6 +31,8 @@ func DoTries(o *RunningInstance, req *http.Request) (*http.Response, error) {
} }
func BuildClient(endpoint string) http.Client { func BuildClient(endpoint string) http.Client {
// Test: TestBuildClient, TestBuildClientProxy
/* The seemingly unused 'endpoint' variable is for proxying based on endpoint, ie for Tor */
tr := &http.Transport{ tr := &http.Transport{
MaxIdleConns: 2, MaxIdleConns: 2,
IdleConnTimeout: 3600 * time.Second, IdleConnTimeout: 3600 * time.Second,
@ -57,6 +59,7 @@ func BuildClient(endpoint string) http.Client {
} }
func GetRunner(endpoint string) (RunningInstance, bool) { func GetRunner(endpoint string) (RunningInstance, bool) {
// Tests: TestGetRunnerNonExist, TestGetRunnerExists
ri_mutex.Lock() ri_mutex.Lock()
o, exists := runninginstances[endpoint] o, exists := runninginstances[endpoint]
@ -74,6 +77,7 @@ func GetRunner(endpoint string) (RunningInstance, bool) {
} }
func UpdateRunner(endpoint string, o RunningInstance) { func UpdateRunner(endpoint string, o RunningInstance) {
// Tests: None necessary
ri_mutex.Lock() ri_mutex.Lock()
runninginstances[endpoint] = o runninginstances[endpoint] = o
ri_mutex.Unlock() ri_mutex.Unlock()

View File

@ -0,0 +1,76 @@
package main
import (
"gitlab.com/khanzf/fedilogue/shared"
"reflect"
"net/http"
"testing"
"time"
"net"
)
func TestBuildClient(t *testing.T) {
tr := &http.Transport{
MaxIdleConns: 2,
IdleConnTimeout: 3600 * time.Second,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
}
want := http.Client{Transport: tr}
have := BuildClient("testdomain.com")
if reflect.DeepEqual(want, have) {
t.Fatalf("TestBuildClient client different from expected.")
}
}
func TestBuildClientProxy(t *testing.T) {
// Currently not implemented
}
func TestGetRunnerNonExist(t *testing.T) {
defer func() {
runninginstances = map[string]RunningInstance{}
}()
want_o := RunningInstance{}
want_o.client = BuildClient("some-non-existent-domain.tld")
want_o.Status = KEEPALIVE
want_o.recentactivities = shared.NewUniqueFifo(10)
want_o.recentactors = shared.NewUniqueFifo(10)
want_exists := false
have_o, have_exists := GetRunner("some-non-existent-domain.tld")
if reflect.DeepEqual(want_o, have_o) {
t.Fatalf("TestGetRunnerBlank expected asfasfsf")
}
if have_exists != false {
t.Fatalf("TestGetRunnerBlank expected %v, got %v", want_exists, have_exists)
}
}
func TestGetRunnerExists(t *testing.T) {
defer func() {
runninginstances = map[string]RunningInstance{}
}()
want_o := RunningInstance{}
want_o.client = BuildClient("some-non-existent-domain.tld")
want_o.Status = KEEPALIVE
want_o.recentactivities = shared.NewUniqueFifo(10)
want_o.recentactors = shared.NewUniqueFifo(10)
runninginstances["some-non-existent-domain.tld"] = want_o
want_exists := true
_, have_exists := GetRunner("some-non-existent-domain.tld")
if have_exists != want_exists {
t.Fatalf("TestGetRunnerBlank expected %v, got %v", want_exists, have_exists)
}
// if reflect.DeepEqual(want_o, have_o) {
// t.Fatalf("TestGetRunnerExists failed, should have the same value")
// }
}

15
fedilogue/testhelper.go Normal file
View File

@ -0,0 +1,15 @@
package main
import (
"reflect"
"testing"
)
// AssertEqual checks if values are equal
func AssertEqual(t *testing.T, a interface{}, b interface{}) {
if a == b {
return
}
// debug.PrintStack()
t.Errorf("Received %v (type %v), expected %v (type %v)", a, reflect.TypeOf(a), b, reflect.TypeOf(b))
}