Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes done while investigating outdated docs #1332

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,12 @@ func mustDefault[T any]() T {

// LoadConfig creates new config from YAML file or a directory containing YAML files
func LoadConfig(path string, mandatory bool) (rCfg *Config, rerr error) {
logger := logrus.NewEntry(log.Log())

return loadConfig(logger, path, mandatory)
}

func loadConfig(logger *logrus.Entry, path string, mandatory bool) (rCfg *Config, rerr error) {
cfg, err := WithDefaults[Config]()
if err != nil {
return nil, err
Expand Down Expand Up @@ -464,7 +470,7 @@ func LoadConfig(path string, mandatory bool) (rCfg *Config, rerr error) {
}
}

err = unmarshalConfig(data, &cfg)
err = unmarshalConfig(logger, data, &cfg)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -523,14 +529,12 @@ func isRegularFile(path string) (bool, error) {
return isRegular, nil
}

func unmarshalConfig(data []byte, cfg *Config) error {
func unmarshalConfig(logger *logrus.Entry, data []byte, cfg *Config) error {
err := yaml.UnmarshalStrict(data, cfg)
if err != nil {
return fmt.Errorf("wrong file structure: %w", err)
}

logger := logrus.NewEntry(log.Log())

usesDepredOpts := cfg.migrate(logger)
if usesDepredOpts {
logger.Error("configuration uses deprecated options, see warning logs for details")
Expand Down
28 changes: 19 additions & 9 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ var _ = Describe("Config", func() {
blocking:
loading:
refreshPeriod: wrongduration`
err := unmarshalConfig([]byte(data), &cfg)
err := unmarshalConfig(logger, []byte(data), &cfg)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("invalid duration \"wrongduration\""))
})
Expand All @@ -230,7 +230,7 @@ blocking:
data := `customDNS:
mapping:
someDomain: 192.168.178.WRONG`
err := unmarshalConfig([]byte(data), &cfg)
err := unmarshalConfig(logger, []byte(data), &cfg)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("invalid IP address '192.168.178.WRONG'"))
})
Expand All @@ -241,7 +241,7 @@ blocking:
data := `conditional:
mapping:
multiple.resolvers: 192.168.178.1,wrongprotocol:4.4.4.4:53`
err := unmarshalConfig([]byte(data), &cfg)
err := unmarshalConfig(logger, []byte(data), &cfg)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("wrong host name 'wrongprotocol:4.4.4.4:53'"))
})
Expand All @@ -254,7 +254,7 @@ blocking:
- 8.8.8.8
- wrongprotocol:8.8.4.4
- 1.1.1.1`
err := unmarshalConfig([]byte(data), &cfg)
err := unmarshalConfig(logger, []byte(data), &cfg)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("can't convert upstream 'wrongprotocol:8.8.4.4'"))
})
Expand All @@ -266,7 +266,7 @@ blocking:
queryTypes:
- invalidqtype
`
err := unmarshalConfig([]byte(data), &cfg)
err := unmarshalConfig(logger, []byte(data), &cfg)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("unknown DNS query type: 'invalidqtype'"))
})
Expand All @@ -277,7 +277,7 @@ blocking:
cfg := Config{}
data := "bootstrapDns: 0.0.0.0"

err := unmarshalConfig([]byte(data), &cfg)
err := unmarshalConfig(logger, []byte(data), &cfg)
Expect(err).Should(Succeed())
Expect(cfg.BootstrapDNS[0].Upstream.Host).Should(Equal("0.0.0.0"))
})
Expand All @@ -289,7 +289,7 @@ bootstrapDns:
ips:
- 0.0.0.0
`
err := unmarshalConfig([]byte(data), &cfg)
err := unmarshalConfig(logger, []byte(data), &cfg)
Expect(err).Should(Succeed())
Expect(cfg.BootstrapDNS[0].Upstream.Host).Should(Equal("dns.example.com"))
Expect(cfg.BootstrapDNS[0].IPs).Should(HaveLen(1))
Expand All @@ -303,7 +303,7 @@ bootstrapDns:
- 0.0.0.0
- upstream: 1.2.3.4
`
err := unmarshalConfig([]byte(data), &cfg)
err := unmarshalConfig(logger, []byte(data), &cfg)
Expect(err).Should(Succeed())
Expect(cfg.BootstrapDNS).Should(HaveLen(2))
Expect(cfg.BootstrapDNS[0].Upstream.Host).Should(Equal("dns.example.com"))
Expand All @@ -318,7 +318,7 @@ bootstrapDns:
It("should return error", func() {
cfg := Config{}
data := `///`
err := unmarshalConfig([]byte(data), &cfg)
err := unmarshalConfig(logger, []byte(data), &cfg)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("cannot unmarshal !!str `///`"))
})
Expand Down Expand Up @@ -846,6 +846,16 @@ bootstrapDns:
Expect(err).Should(HaveOccurred())
})
})

Describe("Documentation config", func() {
It("should not use deprecated options", func() {
logger, hook := log.NewMockEntry()

_, err := loadConfig(logger, "../docs/config.yml", true)
Expect(err).Should(Succeed())
Expect(hook.Messages).ShouldNot(ContainElement(ContainSubstring("deprecated")))
})
})
})

func defaultTestFileConfig(config *Config) {
Expand Down
4 changes: 2 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ markdown_extensions:
- abbr
- pymdownx.snippets
- pymdownx.emoji:
emoji_index: !!python/name:materialx.emoji.twemoji
emoji_generator: !!python/name:materialx.emoji.to_svg
emoji_index: !!python/name:material.extensions.emoji.twemoji
emoji_generator: !!python/name:material.extensions.emoji.to_svg
- pymdownx.highlight
- pymdownx.superfences
- admonition
Expand Down
Loading