From d1b36d704c237dca738066b1a9cf4960262892d5 Mon Sep 17 00:00:00 2001 From: Naoki Kosaka Date: Mon, 11 Feb 2019 23:32:52 +0900 Subject: [PATCH] Add worker test. --- worker/worker.go | 5 +- worker/worker_test.go | 122 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 worker/worker_test.go diff --git a/worker/worker.go b/worker/worker.go index a33e3a3..22797eb 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -64,6 +64,8 @@ func initConfig() { ResultsExpireIn: 5, } machineryServer, _ = machinery.NewServer(machineryConfig) + newNullLogger := NewNullLogger() + log.DEBUG = newNullLogger uaString = viper.GetString("relay_servicename") + " (golang net/http; Activity-Relay v0.2.1; " + hostURL.Host + ")" Actor.GenerateSelfKey(hostURL, &hostPrivatekey.PublicKey) @@ -84,9 +86,6 @@ func main() { panic(err.Error()) } - newNullLogger := NewNullLogger() - log.DEBUG = newNullLogger - workerID := uuid.NewV4() worker := machineryServer.NewWorker(workerID.String(), 200) err = worker.Launch() diff --git a/worker/worker_test.go b/worker/worker_test.go new file mode 100644 index 0000000..dd9d16b --- /dev/null +++ b/worker/worker_test.go @@ -0,0 +1,122 @@ +package main + +import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "net/url" + "os" + "testing" + + "github.com/spf13/viper" +) + +func TestMain(m *testing.M) { + viper.Set("Actor_pem", "../misc/testKey.pem") + viper.Set("Relay_domain", "relay.yukimochi.example.org") + initConfig() + redisClient.FlushAll().Result() + + // Load Config + code := m.Run() + os.Exit(code) + redisClient.FlushAll().Result() +} + +func TestRelayActivity(t *testing.T) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + data, _ := ioutil.ReadAll(r.Body) + if string(data) != "data" { + w.WriteHeader(500) + w.Write(nil) + } else { + w.WriteHeader(202) + w.Write(nil) + } + })) + defer s.Close() + + err := relayActivity(s.URL, "data") + if err != nil { + t.Fatal("Failed - Data transfar not collect") + } +} + +func TestRelayActivityNoHost(t *testing.T) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + + })) + defer s.Close() + + err := relayActivity("http://nohost.example.jp", "data") + if err == nil { + t.Fatal("Failed - Error not reported.") + } + domain, _ := url.Parse("http://nohost.example.jp") + data, err := redisClient.HGet("relay:statistics:"+domain.Host, "last_error").Result() + if data == "" { + t.Fatal("Failed - Error not cached.") + } +} + +func TestRelayActivityResp500(t *testing.T) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(500) + w.Write(nil) + })) + defer s.Close() + + err := relayActivity(s.URL, "data") + if err == nil { + t.Fatal("Failed - Error not reported.") + } + domain, _ := url.Parse(s.URL) + data, err := redisClient.HGet("relay:statistics:"+domain.Host, "last_error").Result() + if data == "" { + t.Fatal("Failed - Error not cached.") + } +} + +func TestRegistorActivity(t *testing.T) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + data, _ := ioutil.ReadAll(r.Body) + if string(data) != "data" { + w.WriteHeader(500) + w.Write(nil) + } else { + w.WriteHeader(202) + w.Write(nil) + } + })) + defer s.Close() + + err := registorActivity(s.URL, "data") + if err != nil { + t.Fatal("Failed - Data transfar not collect") + } +} + +func TestRegistorActivityNoHost(t *testing.T) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + + })) + defer s.Close() + + err := registorActivity("http://nohost.example.jp", "data") + if err == nil { + t.Fatal("Failed - Error not reported.") + } +} + +func TestRegistorActivityResp500(t *testing.T) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(500) + w.Write(nil) + })) + defer s.Close() + + err := registorActivity(s.URL, "data") + if err == nil { + t.Fatal("Failed - Error not reported.") + } +}