Skip to content

Commit

Permalink
test(config): make sure docs/config.yml doesn't use deprecated options
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinkChaos committed Jan 8, 2024
1 parent dece894 commit 22442da
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
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

0 comments on commit 22442da

Please sign in to comment.