From a78f3f00dcc1f700076b52967703506b1b356905 Mon Sep 17 00:00:00 2001 From: bakito Date: Tue, 6 Apr 2021 21:27:33 +0200 Subject: [PATCH] start writing tests --- README.md | 5 +-- go.mod | 1 + go.sum | 2 ++ pkg/types/types.go | 8 +++-- pkg/types/types_test.go | 76 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b71b839..7b41b4c 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ -[![Go](https://github.com/bakito/adguardhome-sync/actions/workflows/go.yml/badge.svg)](https://github.com/bakito/adguardhome-sync/actions/workflows/go.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/bakito/adguardhome-sync)](https://goreportcard.com/report/github.com/bakito/adguardhome-sync) - +[![Go](https://github.com/bakito/adguardhome-sync/actions/workflows/go.yml/badge.svg)](https://github.com/bakito/adguardhome-sync/actions/workflows/go.yml) +[![Go Report Card](https://goreportcard.com/badge/github.com/bakito/adguardhome-sync)](https://goreportcard.com/report/github.com/bakito/adguardhome-sync) +[![Coverage Status](https://coveralls.io/repos/github/bakito/adguardhome-sync/badge.svg?branch=main)](https://coveralls.io/github/bakito/adguardhome-sync?branch=main) # AdGuardHome sync Synchronize [AdGuardHome](https://github.com/AdguardTeam/AdGuardHome) config to a replica instance. diff --git a/go.mod b/go.mod index 88f4252..9570727 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.16 require ( github.com/go-resty/resty/v2 v2.4.0 + github.com/google/uuid v1.2.0 github.com/mitchellh/go-homedir v1.1.0 github.com/onsi/ginkgo v1.16.0 github.com/onsi/gomega v1.11.0 diff --git a/go.sum b/go.sum index 28b12d6..14e4cb0 100644 --- a/go.sum +++ b/go.sum @@ -78,6 +78,8 @@ github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXi github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs= +github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= diff --git a/pkg/types/types.go b/pkg/types/types.go index 5c092ec..8820c1d 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -7,6 +7,10 @@ import ( "strings" ) +const ( + DefaultAPIPath = "/control" +) + // Config application configuration struct type Config struct { Origin AdGuardInstance `json:"origin" yaml:"origin"` @@ -38,7 +42,7 @@ func (cfg *Config) UniqueReplicas() []AdGuardInstance { var r []AdGuardInstance for _, replica := range dedup { if replica.APIPath == "" { - replica.APIPath = "/control" + replica.APIPath = DefaultAPIPath } r = append(r, replica) } @@ -56,7 +60,7 @@ type AdGuardInstance struct { // Key AdGuardInstance key func (i *AdGuardInstance) Key() string { - return fmt.Sprintf("%s%s", i.URL, i.APIPath) + return fmt.Sprintf("%s#%s", i.URL, i.APIPath) } // Protection API struct diff --git a/pkg/types/types_test.go b/pkg/types/types_test.go index bdf0751..69870b7 100644 --- a/pkg/types/types_test.go +++ b/pkg/types/types_test.go @@ -8,9 +8,19 @@ import ( . "github.com/onsi/gomega" "github.com/bakito/adguardhome-sync/pkg/types" + "github.com/google/uuid" ) var _ = Describe("Types", func() { + var ( + url string + apiPath string + ) + BeforeEach(func() { + url = "http://" + uuid.NewString() + apiPath = "/" + uuid.NewString() + }) + Context("FilteringStatus", func() { It("should correctly parse json", func() { b, err := ioutil.ReadFile("../..//testdata/filtering-status.json") @@ -20,4 +30,70 @@ var _ = Describe("Types", func() { Ω(err).ShouldNot(HaveOccurred()) }) }) + Context("AdGuardInstance", func() { + It("should build a key with url and api apiPath", func() { + i := &types.AdGuardInstance{URL: url, APIPath: apiPath} + Ω(i.Key()).Should(Equal(url + "#" + apiPath)) + }) + }) + Context("RewriteEntry", func() { + It("should build a key with url and api apiPath", func() { + domain := uuid.NewString() + answer := uuid.NewString() + re := &types.RewriteEntry{Domain: domain, Answer: answer} + Ω(re.Key()).Should(Equal(domain + "#" + answer)) + }) + }) + Context("UserRules", func() { + It("should join the rules correctly", func() { + r1 := uuid.NewString() + r2 := uuid.NewString() + ur := types.UserRules([]string{r1, r2}) + Ω(ur.String()).Should(Equal(r1 + "\n" + r2)) + }) + }) + Context("Config", func() { + var ( + cfg *types.Config + ) + BeforeEach(func() { + cfg = &types.Config{} + }) + Context("UniqueReplicas", func() { + It("should be empty if noting defined", func() { + r := cfg.UniqueReplicas() + Ω(r).Should(BeEmpty()) + }) + It("should be empty if replica url is not set", func() { + cfg.Replica = types.AdGuardInstance{URL: ""} + r := cfg.UniqueReplicas() + Ω(r).Should(BeEmpty()) + }) + It("should be empty if replicas url is not set", func() { + cfg.Replicas = []types.AdGuardInstance{{URL: ""}} + r := cfg.UniqueReplicas() + Ω(r).Should(BeEmpty()) + }) + It("should return only one replica if same url and apiPath", func() { + cfg.Replica = types.AdGuardInstance{URL: url, APIPath: apiPath} + cfg.Replicas = []types.AdGuardInstance{{URL: url, APIPath: apiPath}, {URL: url, APIPath: apiPath}} + r := cfg.UniqueReplicas() + Ω(r).Should(HaveLen(1)) + }) + It("should return 3 one replicas if urls are different", func() { + cfg.Replica = types.AdGuardInstance{URL: url, APIPath: apiPath} + cfg.Replicas = []types.AdGuardInstance{{URL: url + "1", APIPath: apiPath}, {URL: url, APIPath: apiPath + "1"}} + r := cfg.UniqueReplicas() + Ω(r).Should(HaveLen(3)) + }) + It("should set default api apiPath if not set", func() { + cfg.Replica = types.AdGuardInstance{URL: url} + cfg.Replicas = []types.AdGuardInstance{{URL: url + "1"}} + r := cfg.UniqueReplicas() + Ω(r).Should(HaveLen(2)) + Ω(r[0].APIPath).Should(Equal(types.DefaultAPIPath)) + Ω(r[1].APIPath).Should(Equal(types.DefaultAPIPath)) + }) + }) + }) })