Only sync dhcp config if it is valid (#184)

* handle new install page redirect location

* only sync dhcp config if valid
This commit is contained in:
Marc Brugger 2023-04-12 20:02:55 +02:00 committed by GitHub
parent 009715ccea
commit c93084e623
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 2 deletions

1
.gitignore vendored
View file

@ -7,3 +7,4 @@ main
tmp
bin
config.yaml
*.log

View file

@ -490,7 +490,7 @@ func (w *worker) syncDHCPServer(osc *types.DHCPServerConfig, rc client.Client, r
return nil
}
sc, err := rc.DHCPServerConfig()
if w.cfg.Features.DHCP.ServerConfig {
if w.cfg.Features.DHCP.ServerConfig && osc.HasConfig() {
if err != nil {
return err
}

View file

@ -2,6 +2,7 @@ package sync
import (
"errors"
"net"
"github.com/bakito/adguardhome-sync/pkg/client"
clientmock "github.com/bakito/adguardhome-sync/pkg/mocks/client"
@ -433,11 +434,17 @@ var _ = Describe("Sync", func() {
rsc *types.DHCPServerConfig
)
BeforeEach(func() {
osc = &types.DHCPServerConfig{}
osc = &types.DHCPServerConfig{V4: &types.V4ServerConfJSON{
GatewayIP: net.IPv4(1, 2, 3, 4),
RangeStart: net.IPv4(1, 2, 3, 5),
RangeEnd: net.IPv4(1, 2, 3, 6),
SubnetMask: net.IPv4(255, 255, 255, 0),
}}
rsc = &types.DHCPServerConfig{}
w.cfg.Features.DHCP.StaticLeases = false
})
It("should have no changes", func() {
rsc.V4 = osc.V4
cl.EXPECT().DHCPServerConfig().Return(rsc, nil)
err := w.syncDHCPServer(osc, cl, types.AdGuardInstance{})
Ω(err).ShouldNot(HaveOccurred())

View file

@ -33,6 +33,10 @@ func (c *DHCPServerConfig) Equals(o *DHCPServerConfig) bool {
return string(a) == string(b)
}
func (c *DHCPServerConfig) HasConfig() bool {
return (c.V4 != nil && c.V4.isValid()) || (c.V6 != nil && c.V6.isValid())
}
// V4ServerConfJSON v4 server conf
type V4ServerConfJSON struct {
GatewayIP net.IP `json:"gateway_ip"`
@ -42,6 +46,10 @@ type V4ServerConfJSON struct {
LeaseDuration uint32 `json:"lease_duration"`
}
func (j V4ServerConfJSON) isValid() bool {
return j.GatewayIP != nil && j.SubnetMask != nil && j.RangeStart != nil && j.RangeEnd != nil
}
// V6ServerConfJSON v6 server conf
type V6ServerConfJSON struct {
RangeStart net.IP `json:"range_start"`
@ -49,6 +57,10 @@ type V6ServerConfJSON struct {
LeaseDuration uint32 `json:"lease_duration"`
}
func (j V6ServerConfJSON) isValid() bool {
return j.RangeStart != nil && j.RangeEnd != nil
}
// Leases slice of leases type
type Leases []Lease

View file

@ -420,5 +420,36 @@ var _ = Describe("Types", func() {
Ω(dc1.Clone().Equals(dc1)).Should(BeTrue())
})
})
Context("HasConfig", func() {
It("should not have a config", func() {
dc1 := &types.DHCPServerConfig{
V4: &types.V4ServerConfJSON{},
V6: &types.V6ServerConfJSON{},
}
Ω(dc1.HasConfig()).Should(BeFalse())
})
It("should not have a v4 config", func() {
dc1 := &types.DHCPServerConfig{
V4: &types.V4ServerConfJSON{
GatewayIP: net.IPv4(1, 2, 3, 4),
RangeStart: net.IPv4(1, 2, 3, 5),
RangeEnd: net.IPv4(1, 2, 3, 6),
SubnetMask: net.IPv4(255, 255, 255, 0),
},
V6: &types.V6ServerConfJSON{},
}
Ω(dc1.HasConfig()).Should(BeTrue())
})
It("should not have a v6 config", func() {
dc1 := &types.DHCPServerConfig{
V4: &types.V4ServerConfJSON{},
V6: &types.V6ServerConfJSON{
RangeStart: net.IPv4(1, 2, 3, 5),
RangeEnd: net.IPv4(1, 2, 3, 6),
},
}
Ω(dc1.HasConfig()).Should(BeTrue())
})
})
})
})