From ad8b7d12a72f31b71581987d705a7a0fabcf0b4c Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Sun, 8 Aug 2021 23:46:29 +0000 Subject: [PATCH 1/4] Adding tests for instances Minor modification to db code --- fedilogue/db.go | 3 +- fedilogue/instance.go | 4 ++ fedilogue/instance_test.go | 75 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 fedilogue/instance_test.go diff --git a/fedilogue/db.go b/fedilogue/db.go index eca817b..b8e8b38 100644 --- a/fedilogue/db.go +++ b/fedilogue/db.go @@ -10,7 +10,8 @@ var pool *pgxpool.Pool func getDbPool() *pgxpool.Pool { // 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 { logFatal.Fatal("Unable to connect to database:", err) } diff --git a/fedilogue/instance.go b/fedilogue/instance.go index b279d49..2bb34ab 100644 --- a/fedilogue/instance.go +++ b/fedilogue/instance.go @@ -30,6 +30,8 @@ func DoTries(o *RunningInstance, req *http.Request) (*http.Response, error) { } 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{ MaxIdleConns: 2, IdleConnTimeout: 3600 * time.Second, @@ -56,6 +58,7 @@ func BuildClient(endpoint string) http.Client { } func GetRunner(endpoint string) (RunningInstance, bool) { + // Tests: TestGetRunnerNonExist, TestGetRunnerExists ri_mutex.Lock() o, exists := runninginstances[endpoint] @@ -73,6 +76,7 @@ func GetRunner(endpoint string) (RunningInstance, bool) { } func UpdateRunner(endpoint string, o RunningInstance) { + // Tests: None necessary ri_mutex.Lock() runninginstances[endpoint] = o ri_mutex.Unlock() diff --git a/fedilogue/instance_test.go b/fedilogue/instance_test.go new file mode 100644 index 0000000..474441c --- /dev/null +++ b/fedilogue/instance_test.go @@ -0,0 +1,75 @@ +package main + +import ( + "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 = newUniqueFifo(10) + want_o.recentactors = 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 = newUniqueFifo(10) + want_o.recentactors = 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") +// } +} From b99ad6da99e981d38fdcecaec4dd69b093e90c21 Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Wed, 29 Sep 2021 06:43:06 +0000 Subject: [PATCH 2/4] adding unit tests for config.go --- fedilogue/config.go | 1 + fedilogue/config_test.go | 14 ++++++++++++++ fedilogue/fedilogue.go | 4 ++++ fedilogue/testhelper.go | 15 +++++++++++++++ 4 files changed, 34 insertions(+) create mode 100644 fedilogue/config_test.go create mode 100644 fedilogue/testhelper.go diff --git a/fedilogue/config.go b/fedilogue/config.go index d799f21..611250e 100644 --- a/fedilogue/config.go +++ b/fedilogue/config.go @@ -45,6 +45,7 @@ type Settings struct { var settings Settings +/* Test: TestStringexists */ func stringexists(needle string, haystack []string) bool { for _, check := range haystack { if check == needle { diff --git a/fedilogue/config_test.go b/fedilogue/config_test.go new file mode 100644 index 0000000..40369a8 --- /dev/null +++ b/fedilogue/config_test.go @@ -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) +} diff --git a/fedilogue/fedilogue.go b/fedilogue/fedilogue.go index 44e66bf..863f11c 100644 --- a/fedilogue/fedilogue.go +++ b/fedilogue/fedilogue.go @@ -27,6 +27,10 @@ func statusReportHandler() { } } +/* Tests: + - TestStatusReport_empty_run + - TestStatusReport_full_content +*/ func StatusReport() { running := 0 keepalive := 0 diff --git a/fedilogue/testhelper.go b/fedilogue/testhelper.go new file mode 100644 index 0000000..9baca3a --- /dev/null +++ b/fedilogue/testhelper.go @@ -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)) +} From 6776cd28e549302537bcf62dc55f1b7e993481c3 Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Sun, 8 Aug 2021 23:46:29 +0000 Subject: [PATCH 3/4] Adding tests for instances Minor modification to db code --- fedilogue/db.go | 3 +- fedilogue/instance.go | 4 ++ fedilogue/instance_test.go | 75 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 fedilogue/instance_test.go diff --git a/fedilogue/db.go b/fedilogue/db.go index eca817b..b8e8b38 100644 --- a/fedilogue/db.go +++ b/fedilogue/db.go @@ -10,7 +10,8 @@ var pool *pgxpool.Pool func getDbPool() *pgxpool.Pool { // 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 { logFatal.Fatal("Unable to connect to database:", err) } diff --git a/fedilogue/instance.go b/fedilogue/instance.go index fe32fea..83dec17 100644 --- a/fedilogue/instance.go +++ b/fedilogue/instance.go @@ -31,6 +31,8 @@ func DoTries(o *RunningInstance, req *http.Request) (*http.Response, error) { } 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{ MaxIdleConns: 2, IdleConnTimeout: 3600 * time.Second, @@ -57,6 +59,7 @@ func BuildClient(endpoint string) http.Client { } func GetRunner(endpoint string) (RunningInstance, bool) { + // Tests: TestGetRunnerNonExist, TestGetRunnerExists ri_mutex.Lock() o, exists := runninginstances[endpoint] @@ -74,6 +77,7 @@ func GetRunner(endpoint string) (RunningInstance, bool) { } func UpdateRunner(endpoint string, o RunningInstance) { + // Tests: None necessary ri_mutex.Lock() runninginstances[endpoint] = o ri_mutex.Unlock() diff --git a/fedilogue/instance_test.go b/fedilogue/instance_test.go new file mode 100644 index 0000000..474441c --- /dev/null +++ b/fedilogue/instance_test.go @@ -0,0 +1,75 @@ +package main + +import ( + "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 = newUniqueFifo(10) + want_o.recentactors = 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 = newUniqueFifo(10) + want_o.recentactors = 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") +// } +} From 58ad6ca2763edc5989582db2f2307d9b4753857a Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Wed, 29 Sep 2021 14:39:39 +0000 Subject: [PATCH 4/4] Updating instance_test.go shared library reference --- fedilogue/instance_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/fedilogue/instance_test.go b/fedilogue/instance_test.go index 474441c..69c0961 100644 --- a/fedilogue/instance_test.go +++ b/fedilogue/instance_test.go @@ -1,6 +1,7 @@ package main import ( + "gitlab.com/khanzf/fedilogue/shared" "reflect" "net/http" "testing" @@ -37,8 +38,8 @@ func TestGetRunnerNonExist(t *testing.T) { want_o := RunningInstance{} want_o.client = BuildClient("some-non-existent-domain.tld") want_o.Status = KEEPALIVE - want_o.recentactivities = newUniqueFifo(10) - want_o.recentactors = newUniqueFifo(10) + 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") @@ -59,8 +60,8 @@ func TestGetRunnerExists(t *testing.T) { want_o := RunningInstance{} want_o.client = BuildClient("some-non-existent-domain.tld") want_o.Status = KEEPALIVE - want_o.recentactivities = newUniqueFifo(10) - want_o.recentactors = newUniqueFifo(10) + want_o.recentactivities = shared.NewUniqueFifo(10) + want_o.recentactors = shared.NewUniqueFifo(10) runninginstances["some-non-existent-domain.tld"] = want_o want_exists := true