add test and improve config error messages

This commit is contained in:
André Roth
2024-12-04 12:14:00 +01:00
parent eb6dd4d69e
commit a7d6782176
4 changed files with 27 additions and 10 deletions

View File

@@ -84,6 +84,6 @@ func (s *AptlyContextSuite) TestGetPublishedStorageBadFS(c *C) {
// storage never exists.
c.Assert(func() { s.context.GetPublishedStorage("filesystem:fuji") },
FatalErrorPanicMatches,
&FatalError{ReturnCode: 1, Message: fmt.Sprintf("error loading config file %s/.aptly.conf: not valid yaml or json",
&FatalError{ReturnCode: 1, Message: fmt.Sprintf("error loading config file %s/.aptly.conf: invalid yaml (EOF) or json (EOF)",
os.Getenv("HOME"))})
}

View File

@@ -1 +1 @@
ERROR: error loading config file ${HOME}/.aptly.conf: not valid yaml or json
ERROR: error loading config file ${HOME}/.aptly.conf: invalid yaml (yaml: line 1: did not find expected ',' or '}') or json (invalid character 's' looking for beginning of object key string)

View File

@@ -2,7 +2,6 @@ package utils
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
@@ -258,9 +257,11 @@ func LoadConfig(filename string, config *ConfigStructure) error {
if err = decJSON.Decode(&config); err != nil {
f.Seek(0, 0)
decYAML := yaml.NewDecoder(f)
if err = decYAML.Decode(&config); err != nil {
err = errors.New("not valid yaml or json")
}
if err2 := decYAML.Decode(&config); err2 != nil {
err = fmt.Errorf("invalid yaml (%s) or json (%s)", err2, err)
} else {
err = nil
}
}
return err
}

View File

@@ -175,9 +175,22 @@ func (s *ConfigSuite) TestLoadYAMLConfig(c *C) {
c.Check(s.config.DatabaseOpenAttempts, Equals, 10)
}
func (s *ConfigSuite) TestSaveYAMLConfig(c *C) {
func (s *ConfigSuite) TestLoadYAMLErrorConfig(c *C) {
configname := filepath.Join(c.MkDir(), "aptly.yaml2")
f, _ := os.Create(configname)
f.WriteString(configFileYAMLError)
f.Close()
// start with empty config
s.config = ConfigStructure{}
err := LoadConfig(configname, &s.config)
c.Assert(err.Error(), Equals, "invalid yaml (unknown pool storage type: invalid) or json (invalid character 'p' looking for beginning of value)")
}
func (s *ConfigSuite) TestSaveYAMLConfig(c *C) {
configname := filepath.Join(c.MkDir(), "aptly.yaml3")
f, _ := os.Create(configname)
f.WriteString(configFileYAML)
f.Close()
@@ -207,7 +220,7 @@ func (s *ConfigSuite) TestSaveYAML2Config(c *C) {
s.config.PackagePoolStorage.Local = &LocalPoolStorage{"/tmp/aptly-pool"}
s.config.PackagePoolStorage.Azure = nil
configname := filepath.Join(c.MkDir(), "aptly.yaml3")
configname := filepath.Join(c.MkDir(), "aptly.yaml4")
err := SaveConfigYAML(configname, &s.config)
c.Assert(err, IsNil)
@@ -260,7 +273,7 @@ func (s *ConfigSuite) TestSaveYAML2Config(c *C) {
}
func (s *ConfigSuite) TestLoadEmptyConfig(c *C) {
configname := filepath.Join(c.MkDir(), "aptly.yaml4")
configname := filepath.Join(c.MkDir(), "aptly.yaml5")
f, _ := os.Create(configname)
f.Close()
@@ -268,7 +281,7 @@ func (s *ConfigSuite) TestLoadEmptyConfig(c *C) {
s.config = ConfigStructure{}
err := LoadConfig(configname, &s.config)
c.Assert(err.Error(), Equals, "not valid yaml or json")
c.Assert(err.Error(), Equals, "invalid yaml (EOF) or json (EOF)")
}
const configFile = `{"rootDir": "/opt/aptly/", "downloadConcurrency": 33, "databaseOpenAttempts": 33}`
@@ -355,3 +368,6 @@ packagepool_storage:
account_key: a key
endpoint: ""
`
const configFileYAMLError = `packagepool_storage:
type: invalid
`