Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Dec 11, 2024
1 parent c3fab05 commit 04c5e68
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 64 deletions.
34 changes: 0 additions & 34 deletions internal/config/config_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,40 +453,6 @@ func TestNormalize(t *testing.T) {
)
},
},
"dectioctn-time-too-short": {
input: &config.Config{ //nolint:exhaustruct
UpdateOnStart: true,
Provider: map[ipnet.Type]provider.Provider{
ipnet.IP6: provider.NewCloudflareTrace(),
},
Domains: map[ipnet.Type][]domain.Domain{
ipnet.IP6: {domain.FQDN("a.b.c")},
},
ProxiedTemplate: "true",
DetectionTimeout: time.Nanosecond,
},
ok: true,
expected: &config.Config{ //nolint:exhaustruct
UpdateOnStart: true,
Provider: map[ipnet.Type]provider.Provider{
ipnet.IP6: provider.NewCloudflareTrace(),
},
Domains: map[ipnet.Type][]domain.Domain{
ipnet.IP6: {domain.FQDN("a.b.c")},
},
ProxiedTemplate: "true",
Proxied: map[domain.Domain]bool{domain.FQDN("a.b.c"): true},
DetectionTimeout: time.Nanosecond,
},
prepareMockPP: func(m *mocks.MockPP) {
gomock.InOrder(
m.EXPECT().IsShowing(pp.Info).Return(true),
m.EXPECT().Infof(pp.EmojiEnvVars, "Checking settings . . ."),
m.EXPECT().Indent().Return(m),
m.EXPECT().Noticef(pp.EmojiUserWarning, "DETECTION_TIMEOUT=%s may be too short for trying 1.0.0.1 when 1.1.1.1 does not work", time.Nanosecond),
)
},
},
} {
t.Run(name, func(t *testing.T) {
t.Parallel()
Expand Down
3 changes: 1 addition & 2 deletions internal/provider/protocol/const_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ func TestConstGetIP(t *testing.T) {
ProviderName: "",
IP: tc.savedIP,
}
ip, method, ok := provider.GetIP(context.Background(), mockPP, tc.ipNet)
ip, ok := provider.GetIP(context.Background(), mockPP, tc.ipNet)
require.Equal(t, tc.ok, ok)
require.NotEqual(t, protocol.MethodAlternative, method)
require.Equal(t, tc.expected, ip)
})
}
Expand Down
3 changes: 1 addition & 2 deletions internal/provider/protocol/local_auto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ func TestLocalAuteGetIP(t *testing.T) {
ProviderName: "",
RemoteUDPAddr: tc.addr,
}
ip, method, ok := provider.GetIP(context.Background(), mockPP, tc.ipNet)
ip, ok := provider.GetIP(context.Background(), mockPP, tc.ipNet)
require.Equal(t, tc.expected, ip)
require.NotEqual(t, protocol.MethodAlternative, method)
require.Equal(t, tc.ok, ok)
})
}
Expand Down
51 changes: 25 additions & 26 deletions internal/updater/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/favonia/cloudflare-ddns/internal/monitor"
"github.com/favonia/cloudflare-ddns/internal/notifier"
"github.com/favonia/cloudflare-ddns/internal/pp"
"github.com/favonia/cloudflare-ddns/internal/provider"
"github.com/favonia/cloudflare-ddns/internal/setter"
"github.com/favonia/cloudflare-ddns/internal/updater"
)
Expand Down Expand Up @@ -102,7 +101,7 @@ func TestUpdateIPsMultiple(t *testing.T) {
providerEnablers{ipnet.IP4: true},
func(p *mocks.MockPP, pv mockProviders, s *mocks.MockSetter) {
gomock.InOrder(
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, provider.MethodPrimary, true),
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v", "IPv4", ip4),
p.EXPECT().SuppressHint(pp.HintIP4DetectionFails),
s.EXPECT().Set(gomock.Any(), p, ipnet.IP4, domain.FQDN("ip4.hello1"), ip4, api.TTLAuto, false, recordComment).Return(setter.ResponseUpdating),
Expand Down Expand Up @@ -130,7 +129,7 @@ func TestUpdateIPsMultiple(t *testing.T) {
providerEnablers{ipnet.IP4: true},
func(p *mocks.MockPP, pv mockProviders, s *mocks.MockSetter) {
gomock.InOrder(
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, provider.MethodPrimary, true),
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v", "IPv4", ip4),
p.EXPECT().SuppressHint(pp.HintIP4DetectionFails),
s.EXPECT().Set(gomock.Any(), p, ipnet.IP4, domain.FQDN("ip4.hello1"), ip4, api.TTLAuto, false, recordComment).Return(setter.ResponseUpdated),
Expand Down Expand Up @@ -300,7 +299,7 @@ func TestUpdateIPs(t *testing.T) {
providerEnablers{ipnet.IP4: true},
func(p *mocks.MockPP, pv mockProviders, s *mocks.MockSetter) {
gomock.InOrder(
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, provider.MethodPrimary, true),
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v", "IPv4", ip4),
p.EXPECT().SuppressHint(pp.HintIP4DetectionFails),
s.EXPECT().Set(gomock.Any(), p, ipnet.IP4, domain.FQDN("ip4.hello"), ip4, api.TTLAuto, false, recordComment).Return(setter.ResponseNoop),
Expand All @@ -315,7 +314,7 @@ func TestUpdateIPs(t *testing.T) {
providerEnablers{ipnet.IP4: true},
func(p *mocks.MockPP, pv mockProviders, s *mocks.MockSetter) {
gomock.InOrder(
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, provider.MethodPrimary, true),
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v", "IPv4", ip4),
p.EXPECT().SuppressHint(pp.HintIP4DetectionFails),
s.EXPECT().Set(gomock.Any(), p, ipnet.IP4, domain.FQDN("ip4.hello"), ip4, api.TTLAuto, false, recordComment).Return(setter.ResponseFailed),
Expand All @@ -330,7 +329,7 @@ func TestUpdateIPs(t *testing.T) {
providerEnablers{ipnet.IP4: true},
func(p *mocks.MockPP, pv mockProviders, s *mocks.MockSetter) {
gomock.InOrder(
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, provider.MethodPrimary, true),
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v", "IPv4", ip4),
p.EXPECT().SuppressHint(pp.HintIP4DetectionFails),
s.EXPECT().Set(gomock.Any(), p, ipnet.IP4, domain.FQDN("ip4.hello"), ip4, api.TTLAuto, false, recordComment).Return(setter.ResponseUpdating),
Expand All @@ -345,7 +344,7 @@ func TestUpdateIPs(t *testing.T) {
providerEnablers{ipnet.IP6: true},
func(p *mocks.MockPP, pv mockProviders, s *mocks.MockSetter) {
gomock.InOrder(
pv[ipnet.IP6].EXPECT().GetIP(gomock.Any(), p, ipnet.IP6).Return(ip6, provider.MethodPrimary, true),
pv[ipnet.IP6].EXPECT().GetIP(gomock.Any(), p, ipnet.IP6).Return(ip6, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v", "IPv6", ip6),
p.EXPECT().SuppressHint(pp.HintIP6DetectionFails),
s.EXPECT().Set(gomock.Any(), p, ipnet.IP6, domain.FQDN("ip6.hello"), ip6, api.TTLAuto, false, recordComment).Return(setter.ResponseUpdated),
Expand All @@ -360,7 +359,7 @@ func TestUpdateIPs(t *testing.T) {
providerEnablers{ipnet.IP6: true},
func(p *mocks.MockPP, pv mockProviders, s *mocks.MockSetter) {
gomock.InOrder(
pv[ipnet.IP6].EXPECT().GetIP(gomock.Any(), p, ipnet.IP6).Return(ip6, provider.MethodPrimary, true),
pv[ipnet.IP6].EXPECT().GetIP(gomock.Any(), p, ipnet.IP6).Return(ip6, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v", "IPv6", ip6),
p.EXPECT().SuppressHint(pp.HintIP6DetectionFails),
s.EXPECT().Set(gomock.Any(), p, ipnet.IP6, domain.FQDN("ip6.hello"), ip6, api.TTLAuto, false, recordComment).Return(setter.ResponseFailed),
Expand All @@ -373,11 +372,11 @@ func TestUpdateIPs(t *testing.T) {
providerEnablers{ipnet.IP4: true, ipnet.IP6: true},
func(p *mocks.MockPP, pv mockProviders, s *mocks.MockSetter) {
gomock.InOrder(
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, provider.MethodAlternative, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v (using 1.0.0.1)", "IPv4", ip4),
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v", "IPv4", ip4),
p.EXPECT().SuppressHint(pp.HintIP4DetectionFails),
s.EXPECT().Set(gomock.Any(), p, ipnet.IP4, domain.FQDN("ip4.hello"), ip4, api.TTLAuto, false, recordComment).Return(setter.ResponseNoop),
pv[ipnet.IP6].EXPECT().GetIP(gomock.Any(), p, ipnet.IP6).Return(ip6, provider.MethodPrimary, true),
pv[ipnet.IP6].EXPECT().GetIP(gomock.Any(), p, ipnet.IP6).Return(ip6, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v", "IPv6", ip6),
p.EXPECT().SuppressHint(pp.HintIP6DetectionFails),
s.EXPECT().Set(gomock.Any(), p, ipnet.IP6, domain.FQDN("ip6.hello"), ip6, api.TTLAuto, false, recordComment).Return(setter.ResponseNoop),
Expand All @@ -392,11 +391,11 @@ func TestUpdateIPs(t *testing.T) {
providerEnablers{ipnet.IP4: true, ipnet.IP6: true},
func(p *mocks.MockPP, pv mockProviders, s *mocks.MockSetter) {
gomock.InOrder(
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, provider.MethodAlternative, true),
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v", "IPv4", ip4),
p.EXPECT().SuppressHint(pp.HintIP4DetectionFails),
s.EXPECT().Set(gomock.Any(), p, ipnet.IP4, domain.FQDN("ip4.hello"), ip4, api.TTLAuto, false, recordComment).Return(setter.ResponseFailed),
pv[ipnet.IP6].EXPECT().GetIP(gomock.Any(), p, ipnet.IP6).Return(ip6, provider.MethodPrimary, true),
pv[ipnet.IP6].EXPECT().GetIP(gomock.Any(), p, ipnet.IP6).Return(ip6, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v", "IPv6", ip6),
p.EXPECT().SuppressHint(pp.HintIP6DetectionFails),
s.EXPECT().Set(gomock.Any(), p, ipnet.IP6, domain.FQDN("ip6.hello"), ip6, api.TTLAuto, false, recordComment).Return(setter.ResponseNoop),
Expand All @@ -411,11 +410,11 @@ func TestUpdateIPs(t *testing.T) {
providerEnablers{ipnet.IP4: true, ipnet.IP6: true},
func(p *mocks.MockPP, pv mockProviders, s *mocks.MockSetter) {
gomock.InOrder(
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, provider.MethodAlternative, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v (using 1.0.0.1)", "IPv4", ip4),
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v", "IPv4", ip4),
p.EXPECT().SuppressHint(pp.HintIP4DetectionFails),
s.EXPECT().Set(gomock.Any(), p, ipnet.IP4, domain.FQDN("ip4.hello"), ip4, api.TTLAuto, false, recordComment).Return(setter.ResponseNoop),
pv[ipnet.IP6].EXPECT().GetIP(gomock.Any(), p, ipnet.IP6).Return(ip6, provider.MethodPrimary, true),
pv[ipnet.IP6].EXPECT().GetIP(gomock.Any(), p, ipnet.IP6).Return(ip6, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v", "IPv6", ip6),
p.EXPECT().SuppressHint(pp.HintIP6DetectionFails),
s.EXPECT().Set(gomock.Any(), p, ipnet.IP6, domain.FQDN("ip6.hello"), ip6, api.TTLAuto, false, recordComment).Return(setter.ResponseFailed),
Expand Down Expand Up @@ -449,10 +448,10 @@ func TestUpdateIPs(t *testing.T) {
providerEnablers{ipnet.IP4: true, ipnet.IP6: true},
func(p *mocks.MockPP, pv mockProviders, s *mocks.MockSetter) {
gomock.InOrder(
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(netip.Addr{}, provider.MethodUnspecified, false),
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(netip.Addr{}, false),
p.EXPECT().Noticef(pp.EmojiError, "Failed to detect the %s address", "IPv4"),
p.EXPECT().Hintf(pp.HintIP4DetectionFails, "If your network does not support IPv4, you can disable it with IP4_PROVIDER=none"),
pv[ipnet.IP6].EXPECT().GetIP(gomock.Any(), p, ipnet.IP6).Return(ip6, provider.MethodPrimary, true),
pv[ipnet.IP6].EXPECT().GetIP(gomock.Any(), p, ipnet.IP6).Return(ip6, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v", "IPv6", ip6),
p.EXPECT().SuppressHint(pp.HintIP6DetectionFails),
s.EXPECT().Set(gomock.Any(), p, ipnet.IP6, domain.FQDN("ip6.hello"), ip6, api.TTLAuto, false, recordComment).Return(setter.ResponseNoop),
Expand All @@ -467,11 +466,11 @@ func TestUpdateIPs(t *testing.T) {
providerEnablers{ipnet.IP4: true, ipnet.IP6: true},
func(p *mocks.MockPP, pv mockProviders, s *mocks.MockSetter) {
gomock.InOrder(
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, provider.MethodAlternative, true),
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v", "IPv4", ip4),
p.EXPECT().SuppressHint(pp.HintIP4DetectionFails),
s.EXPECT().Set(gomock.Any(), p, ipnet.IP4, domain.FQDN("ip4.hello"), ip4, api.TTLAuto, false, recordComment).Return(setter.ResponseNoop),
pv[ipnet.IP6].EXPECT().GetIP(gomock.Any(), p, ipnet.IP6).Return(netip.Addr{}, provider.MethodUnspecified, false),
pv[ipnet.IP6].EXPECT().GetIP(gomock.Any(), p, ipnet.IP6).Return(netip.Addr{}, false),
p.EXPECT().Noticef(pp.EmojiError, "Failed to detect the %s address", "IPv6"),
hintIP6DetectionFails(p),
s.EXPECT().SetWAFList(gomock.Any(), p, list, wafListDescription, detectedIPs{ipnet.IP4: ip4, ipnet.IP6: netip.Addr{}}, ""),
Expand All @@ -485,10 +484,10 @@ func TestUpdateIPs(t *testing.T) {
providerEnablers{ipnet.IP4: true, ipnet.IP6: true},
func(p *mocks.MockPP, pv mockProviders, _ *mocks.MockSetter) {
gomock.InOrder(
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(netip.Addr{}, provider.MethodUnspecified, false),
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(netip.Addr{}, false),
p.EXPECT().Noticef(pp.EmojiError, "Failed to detect the %s address", "IPv4"),
p.EXPECT().Hintf(pp.HintIP4DetectionFails, "If your network does not support IPv4, you can disable it with IP4_PROVIDER=none"),
pv[ipnet.IP6].EXPECT().GetIP(gomock.Any(), p, ipnet.IP6).Return(netip.Addr{}, provider.MethodUnspecified, false),
pv[ipnet.IP6].EXPECT().GetIP(gomock.Any(), p, ipnet.IP6).Return(netip.Addr{}, false),
p.EXPECT().Noticef(pp.EmojiError, "Failed to detect the %s address", "IPv6"),
hintIP6DetectionFails(p),
)
Expand All @@ -502,9 +501,9 @@ func TestUpdateIPs(t *testing.T) {
func(p *mocks.MockPP, pv mockProviders, s *mocks.MockSetter) {
gomock.InOrder(
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).DoAndReturn(
func(context.Context, pp.PP, ipnet.Type) (netip.Addr, provider.Method, bool) {
func(context.Context, pp.PP, ipnet.Type) (netip.Addr, bool) {
time.Sleep(2 * time.Second)
return netip.Addr{}, provider.MethodUnspecified, false
return netip.Addr{}, false
},
),
p.EXPECT().Noticef(pp.EmojiError, "Failed to detect the %s address", "IPv4"),
Expand All @@ -521,7 +520,7 @@ func TestUpdateIPs(t *testing.T) {
providerEnablers{ipnet.IP4: true},
func(p *mocks.MockPP, pv mockProviders, s *mocks.MockSetter) {
gomock.InOrder(
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, provider.MethodPrimary, true),
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v", "IPv4", ip4),
p.EXPECT().SuppressHint(pp.HintIP4DetectionFails),
s.EXPECT().Set(gomock.Any(), p, ipnet.IP4, domain.FQDN("ip4.hello"), ip4, api.TTLAuto, false, recordComment).DoAndReturn(
Expand All @@ -541,7 +540,7 @@ func TestUpdateIPs(t *testing.T) {
providerEnablers{ipnet.IP4: true},
func(p *mocks.MockPP, pv mockProviders, s *mocks.MockSetter) {
gomock.InOrder(
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, provider.MethodPrimary, true),
pv[ipnet.IP4].EXPECT().GetIP(gomock.Any(), p, ipnet.IP4).Return(ip4, true),
p.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address %v", "IPv4", ip4),
p.EXPECT().SuppressHint(pp.HintIP4DetectionFails),
s.EXPECT().Set(gomock.Any(), p, ipnet.IP4, domain.FQDN("ip4.hello"), ip4, api.TTLAuto, false, recordComment).Return(setter.ResponseNoop),
Expand Down

0 comments on commit 04c5e68

Please sign in to comment.