From 44f5507649db8536a07c4c21c8ad6e4a60ba3f43 Mon Sep 17 00:00:00 2001 From: Eugene Burkov Date: Tue, 13 Sep 2022 16:40:26 +0300 Subject: [PATCH] dhcpd: add mock --- internal/dhcpd/dhcpd.go | 68 ++++++++++++++++++++------ internal/dnsforward/dns_test.go | 4 +- internal/dnsforward/dnsforward_test.go | 50 ++++++++++--------- internal/dnsforward/filter_test.go | 2 +- 4 files changed, 82 insertions(+), 42 deletions(-) diff --git a/internal/dhcpd/dhcpd.go b/internal/dhcpd/dhcpd.go index bf4235d62e9..74a463a473d 100644 --- a/internal/dhcpd/dhcpd.go +++ b/internal/dhcpd/dhcpd.go @@ -141,22 +141,6 @@ const ( LeaseChangedDBStore ) -// server - the current state of the DHCP server -type server struct { - srv4 DHCPServer - srv6 DHCPServer - - // TODO(a.garipov): Either create a separate type for the internal config or - // just put the config values into Server. - conf *ServerConfig - - // Called when the leases DB is modified - onLeaseChanged []OnLeaseChangedT -} - -// type check -var _ Interface = (*server)(nil) - // GetLeasesFlags are the flags for GetLeases. type GetLeasesFlags uint8 @@ -181,6 +165,58 @@ type Interface interface { WriteDiskConfig(c *ServerConfig) } +// MockInterface is a mock Interface implementation. +// +// TODO(e.burkov): Move to aghtest when the API stabilized. +type MockInterface struct { + OnStart func() (err error) + OnStop func() (err error) + OnEnabled func() (ok bool) + OnLeases func(flags GetLeasesFlags) (leases []*Lease) + OnSetOnLeaseChanged func(f OnLeaseChangedT) + OnFindMACbyIP func(ip net.IP) (mac net.HardwareAddr) + OnWriteDiskConfig func(c *ServerConfig) +} + +var _ Interface = (*MockInterface)(nil) + +// Start implements the Interface for *MockInterface. +func (s *MockInterface) Start() (err error) { return s.OnStart() } + +// Stop implements the Interface for *MockInterface. +func (s *MockInterface) Stop() (err error) { return s.OnStop() } + +// Enabled implements the Interface for *MockInterface. +func (s *MockInterface) Enabled() (ok bool) { return s.OnEnabled() } + +// Leases implements the Interface for *MockInterface. +func (s *MockInterface) Leases(flags GetLeasesFlags) (ls []*Lease) { return s.OnLeases(flags) } + +// SetOnLeaseChanged implements the Interface for *MockInterface. +func (s *MockInterface) SetOnLeaseChanged(f OnLeaseChangedT) { s.OnSetOnLeaseChanged(f) } + +// FindMACbyIP implements the Interface for *MockInterface. +func (s *MockInterface) FindMACbyIP(ip net.IP) (mac net.HardwareAddr) { return s.OnFindMACbyIP(ip) } + +// WriteDiskConfig implements the Interface for *MockInterface. +func (s *MockInterface) WriteDiskConfig(c *ServerConfig) { s.OnWriteDiskConfig(c) } + +// server - the current state of the DHCP server +type server struct { + srv4 DHCPServer + srv6 DHCPServer + + // TODO(a.garipov): Either create a separate type for the internal config or + // just put the config values into Server. + conf *ServerConfig + + // Called when the leases DB is modified + onLeaseChanged []OnLeaseChangedT +} + +// type check +var _ Interface = (*server)(nil) + // Create - create object func Create(conf *ServerConfig) (s *server, err error) { s = &server{ diff --git a/internal/dnsforward/dns_test.go b/internal/dnsforward/dns_test.go index 218edb1a9f5..ebdc716c01a 100644 --- a/internal/dnsforward/dns_test.go +++ b/internal/dnsforward/dns_test.go @@ -267,7 +267,7 @@ func TestServer_ProcessDHCPHosts_localRestriction(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { s := &Server{ - dhcpServer: &testDHCP{}, + dhcpServer: testDHCP, localDomainSuffix: defaultLocalDomainSuffix, tableHostToIP: hostToIPTable{ "example." + defaultLocalDomainSuffix: knownIP, @@ -378,7 +378,7 @@ func TestServer_ProcessDHCPHosts(t *testing.T) { for _, tc := range testCases { s := &Server{ - dhcpServer: &testDHCP{}, + dhcpServer: testDHCP, localDomainSuffix: tc.suffix, tableHostToIP: hostToIPTable{ "example." + tc.suffix: knownIP, diff --git a/internal/dnsforward/dnsforward_test.go b/internal/dnsforward/dnsforward_test.go index 11a46ed75c4..b11d1f657ba 100644 --- a/internal/dnsforward/dnsforward_test.go +++ b/internal/dnsforward/dnsforward_test.go @@ -72,7 +72,7 @@ func createTestServer( var err error s, err = NewServer(DNSCreateParams{ - DHCPServer: &testDHCP{}, + DHCPServer: testDHCP, DNSFilter: f, PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed), }) @@ -776,7 +776,7 @@ func TestBlockedCustomIP(t *testing.T) { f := filtering.New(&filtering.Config{}, filters) s, err := NewServer(DNSCreateParams{ - DHCPServer: &testDHCP{}, + DHCPServer: testDHCP, DNSFilter: f, PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed), }) @@ -910,7 +910,7 @@ func TestRewrite(t *testing.T) { f.SetEnabled(true) s, err := NewServer(DNSCreateParams{ - DHCPServer: &testDHCP{}, + DHCPServer: testDHCP, DNSFilter: f, PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed), }) @@ -1005,32 +1005,36 @@ func publicKey(priv any) any { } } -// testDHCP is a mock dhcpd.Interface implementation. -// -// TODO(e.burkov): Create aghtest.DHCP when the API stabilized. -type testDHCP struct{} - -func (*testDHCP) Start() (err error) { return nil } -func (*testDHCP) Stop() (err error) { return nil } -func (*testDHCP) FindMACbyIP(net.IP) (mac net.HardwareAddr) { return nil } -func (*testDHCP) Enabled() (ok bool) { return true } -func (*testDHCP) SetOnLeaseChanged(dhcpd.OnLeaseChangedT) {} -func (*testDHCP) WriteDiskConfig(*dhcpd.ServerConfig) {} - -func (*testDHCP) Leases(flags dhcpd.GetLeasesFlags) (leases []*dhcpd.Lease) { - return []*dhcpd.Lease{{ - IP: net.IP{192, 168, 12, 34}, - HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}, - Hostname: "myhost", - }} +var testDHCP = &dhcpd.MockInterface{ + OnStart: func() (err error) { panic("not implemented") }, + OnStop: func() (err error) { panic("not implemented") }, + OnEnabled: func() (ok bool) { return true }, + OnLeases: func(flags dhcpd.GetLeasesFlags) (leases []*dhcpd.Lease) { + return []*dhcpd.Lease{{ + IP: net.IP{192, 168, 12, 34}, + HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}, + Hostname: "myhost", + }} + }, + OnSetOnLeaseChanged: func(olct dhcpd.OnLeaseChangedT) {}, + OnFindMACbyIP: func(ip net.IP) (mac net.HardwareAddr) { panic("not implemented") }, + OnWriteDiskConfig: func(c *dhcpd.ServerConfig) { panic("not implemented") }, } +// func (*testDHCP) Leases(flags dhcpd.GetLeasesFlags) (leases []*dhcpd.Lease) { +// return []*dhcpd.Lease{{ +// IP: net.IP{192, 168, 12, 34}, +// HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}, +// Hostname: "myhost", +// }} +// } + func TestPTRResponseFromDHCPLeases(t *testing.T) { const localDomain = "lan" s, err := NewServer(DNSCreateParams{ DNSFilter: filtering.New(&filtering.Config{}, nil), - DHCPServer: &testDHCP{}, + DHCPServer: testDHCP, PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed), LocalDomain: localDomain, }) @@ -1103,7 +1107,7 @@ func TestPTRResponseFromHosts(t *testing.T) { var s *Server s, err = NewServer(DNSCreateParams{ - DHCPServer: &testDHCP{}, + DHCPServer: testDHCP, DNSFilter: flt, PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed), }) diff --git a/internal/dnsforward/filter_test.go b/internal/dnsforward/filter_test.go index e25d40372cf..00c04252a0d 100644 --- a/internal/dnsforward/filter_test.go +++ b/internal/dnsforward/filter_test.go @@ -39,7 +39,7 @@ func TestHandleDNSRequest_filterDNSResponse(t *testing.T) { f.SetEnabled(true) s, err := NewServer(DNSCreateParams{ - DHCPServer: &testDHCP{}, + DHCPServer: testDHCP, DNSFilter: f, PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed), })