From b018e150e79bd7d7ce84645ae8e13534e7815caa Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Wed, 31 Aug 2022 19:11:00 +0300 Subject: [PATCH] websvc: add tests; imp names --- internal/v1/cmd/cmd.go | 13 +++++---- internal/v1/websvc/dns.go | 16 ++++++++-- internal/v1/websvc/http.go | 10 ++++++- internal/v1/websvc/settings.go | 29 +++---------------- .../v1/websvc/waitlistener_internal_test.go | 17 +++++++++++ internal/v1/websvc/websvc.go | 2 -- internal/v1/websvc/websvc_internal_test.go | 6 ++++ internal/v1/websvc/websvc_test.go | 1 + 8 files changed, 58 insertions(+), 36 deletions(-) create mode 100644 internal/v1/websvc/websvc_internal_test.go diff --git a/internal/v1/cmd/cmd.go b/internal/v1/cmd/cmd.go index 2f61509b..d6f35251 100644 --- a/internal/v1/cmd/cmd.go +++ b/internal/v1/cmd/cmd.go @@ -17,23 +17,26 @@ import ( // Main is the entry point of application. func Main(clientBuildFS fs.FS) { - // # Initial Configuration + // Initial Configuration start := time.Now() rand.Seed(start.UnixNano()) // TODO(a.garipov): Set up logging. - // # Web Service + // Web Service // TODO(a.garipov): Use in the Web service. _ = clientBuildFS // TODO(a.garipov): Make configurable. web := websvc.New(&websvc.Config{ - Addresses: []netip.AddrPort{netip.MustParseAddrPort("127.0.0.1:3001")}, - Start: start, - Timeout: 60 * time.Second, + // TODO(a.garipov): Use an actual implementation. + ConfigManager: nil, + Addresses: []netip.AddrPort{netip.MustParseAddrPort("127.0.0.1:3001")}, + Start: start, + Timeout: 60 * time.Second, + ForceHTTPS: false, }) err := web.Start() diff --git a/internal/v1/websvc/dns.go b/internal/v1/websvc/dns.go index 419167c8..670a5b31 100644 --- a/internal/v1/websvc/dns.go +++ b/internal/v1/websvc/dns.go @@ -25,8 +25,18 @@ type ReqPatchSettingsDNS struct { UpstreamTimeout timeutil.Duration `json:"upstream_timeout"` } -// handlePatchSettingsDNS is the handler for the PATCH /api/v1/settings/http -// HTTP API. +// httpAPIDNSSettings are the DNS settings as used by the HTTP API. +type httpAPIDNSSettings struct { + // TODO(a.garipov): Add more as we go. + + Addresses []netip.AddrPort `json:"addresses"` + BootstrapServers []string `json:"bootstrap_servers"` + UpstreamServers []string `json:"upstream_servers"` + UpstreamTimeout timeutil.Duration `json:"upstream_timeout"` +} + +// handlePatchSettingsDNS is the handler for the PATCH /api/v1/settings/dns HTTP +// API. func (svc *Service) handlePatchSettingsDNS(w http.ResponseWriter, r *http.Request) { req := &ReqPatchSettingsDNS{ Addresses: []netip.AddrPort{}, @@ -66,7 +76,7 @@ func (svc *Service) handlePatchSettingsDNS(w http.ResponseWriter, r *http.Reques return } - writeJSONResponse(w, r, &respGetV1SettingsAllDNS{ + writeJSONResponse(w, r, &httpAPIDNSSettings{ Addresses: newConf.Addresses, BootstrapServers: newConf.BootstrapServers, UpstreamServers: newConf.UpstreamServers, diff --git a/internal/v1/websvc/http.go b/internal/v1/websvc/http.go index ec881d4d..279043e4 100644 --- a/internal/v1/websvc/http.go +++ b/internal/v1/websvc/http.go @@ -26,6 +26,14 @@ type ReqPatchSettingsHTTP struct { SecureAddresses []netip.AddrPort `json:"secure_addresses"` } +// httpAPIDNSSettings are the HTTP settings as used by the HTTP API. +type httpAPIHTTPSettings struct { + // TODO(a.garipov): Add more as we go. + + Addresses []netip.AddrPort `json:"addresses"` + SecureAddresses []netip.AddrPort `json:"secure_addresses"` +} + // handlePatchSettingsHTTP is the handler for the PATCH /api/v1/settings/http // HTTP API. func (svc *Service) handlePatchSettingsHTTP(w http.ResponseWriter, r *http.Request) { @@ -52,7 +60,7 @@ func (svc *Service) handlePatchSettingsHTTP(w http.ResponseWriter, r *http.Reque ForceHTTPS: svc.forceHTTPS, } - writeJSONResponse(w, r, &respGetV1SettingsAllHTTP{ + writeJSONResponse(w, r, &httpAPIHTTPSettings{ Addresses: newConf.Addresses, SecureAddresses: newConf.SecureAddresses, }) diff --git a/internal/v1/websvc/settings.go b/internal/v1/websvc/settings.go index e3c133c8..c9bd922c 100644 --- a/internal/v1/websvc/settings.go +++ b/internal/v1/websvc/settings.go @@ -2,7 +2,6 @@ package websvc import ( "net/http" - "net/netip" "github.com/AdguardTeam/golibs/timeutil" ) @@ -16,28 +15,8 @@ import ( type RespGetV1SettingsAll struct { // TODO(a.garipov): Add more as we go. - DNS *respGetV1SettingsAllDNS `json:"dns"` - HTTP *respGetV1SettingsAllHTTP `json:"http"` -} - -// respGetV1SettingsAllDNS describes the DNS part of the response of the GET -// /api/v1/settings/all HTTP API. -type respGetV1SettingsAllDNS struct { - // TODO(a.garipov): Add more as we go. - - Addresses []netip.AddrPort `json:"addresses"` - BootstrapServers []string `json:"bootstrap_servers"` - UpstreamServers []string `json:"upstream_servers"` - UpstreamTimeout timeutil.Duration `json:"upstream_timeout"` -} - -// respGetV1SettingsAllHTTP describes the HTTP part of the response of the GET -// /api/v1/settings/all HTTP API. -type respGetV1SettingsAllHTTP struct { - // TODO(a.garipov): Add more as we go. - - Addresses []netip.AddrPort `json:"addresses"` - SecureAddresses []netip.AddrPort `json:"secure_addresses"` + DNS *httpAPIDNSSettings `json:"dns"` + HTTP *httpAPIHTTPSettings `json:"http"` } // handleGetSettingsAll is the handler for the GET /api/v1/settings/all HTTP @@ -49,13 +28,13 @@ func (svc *Service) handleGetSettingsAll(w http.ResponseWriter, r *http.Request) httpConf := svc.Config() writeJSONResponse(w, r, &RespGetV1SettingsAll{ - DNS: &respGetV1SettingsAllDNS{ + DNS: &httpAPIDNSSettings{ Addresses: dnsConf.Addresses, BootstrapServers: dnsConf.BootstrapServers, UpstreamServers: dnsConf.UpstreamServers, UpstreamTimeout: timeutil.Duration{Duration: dnsConf.UpstreamTimeout}, }, - HTTP: &respGetV1SettingsAllHTTP{ + HTTP: &httpAPIHTTPSettings{ Addresses: httpConf.Addresses, SecureAddresses: httpConf.SecureAddresses, }, diff --git a/internal/v1/websvc/waitlistener_internal_test.go b/internal/v1/websvc/waitlistener_internal_test.go index ad710481..74c0bf80 100644 --- a/internal/v1/websvc/waitlistener_internal_test.go +++ b/internal/v1/websvc/waitlistener_internal_test.go @@ -1,12 +1,15 @@ package websvc import ( + "fmt" "net" "sync" "sync/atomic" "testing" + "time" "github.com/AdguardTeam/AdGuardHome/internal/aghtest" + "github.com/stretchr/testify/assert" ) func TestWaitListener_Accept(t *testing.T) { @@ -29,6 +32,17 @@ func TestWaitListener_Accept(t *testing.T) { wg := &sync.WaitGroup{} wg.Add(1) + done := make(chan struct{}) + a := time.After(testTimeout) + go func() { + select { + case <-a: + panic(fmt.Errorf("did not finish after %s", testTimeout)) + case <-done: + // Success. + } + }() + go func() { var wrapper net.Listener = &waitListener{ Listener: l, @@ -39,4 +53,7 @@ func TestWaitListener_Accept(t *testing.T) { }() wg.Wait() + close(done) + + assert.Equal(t, uint32(1), atomic.LoadUint32(&numAcceptCalls)) } diff --git a/internal/v1/websvc/websvc.go b/internal/v1/websvc/websvc.go index 8f385950..8183d92c 100644 --- a/internal/v1/websvc/websvc.go +++ b/internal/v1/websvc/websvc.go @@ -22,8 +22,6 @@ import ( ) // ConfigManager is the configuration manager interface. -// -// See internal/v1/svc/ for the main implementation. type ConfigManager interface { DNS() (svc *dnssvc.Service) Web() (svc *Service) diff --git a/internal/v1/websvc/websvc_internal_test.go b/internal/v1/websvc/websvc_internal_test.go new file mode 100644 index 00000000..3509b193 --- /dev/null +++ b/internal/v1/websvc/websvc_internal_test.go @@ -0,0 +1,6 @@ +package websvc + +import "time" + +// testTimeout is the common timeout for tests. +const testTimeout = 1 * time.Second diff --git a/internal/v1/websvc/websvc_test.go b/internal/v1/websvc/websvc_test.go index a1961348..8b73a015 100644 --- a/internal/v1/websvc/websvc_test.go +++ b/internal/v1/websvc/websvc_test.go @@ -15,6 +15,7 @@ import ( "github.com/stretchr/testify/require" ) +// testTimeout is the common timeout for tests. const testTimeout = 1 * time.Second // testStart is the server start value for tests.