Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
neolynx committed Dec 3, 2024
1 parent 5498e84 commit 8377147
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 2 deletions.
37 changes: 37 additions & 0 deletions system/t02_config/ConfigShowYAMLTest_gold
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
root_dir: ${HOME}/.aptly
log_level: debug
log_format: default
database_open_attempts: 10
architectures: []
skip_legacy_pool: false
dep_follow_suggests: false
dep_follow_recommends: false
dep_follow_all_variants: false
dep_follow_source: false
dep_verboseresolve: false
ppa_distributor_id: ubuntu
ppa_codename: ""
serve_in_api_mode: true
enable_metrics_endpoint: true
enable_swagger_endpoint: false
async_api: false
database_backend:
type: ""
db_path: ""
url: ""
downloader: default
download_concurrency: 4
download_limit: 0
download_retries: 5
download_sourcepackages: false
gpg_provider: gpg
gpg_disable_sign: false
gpg_disable_verify: false
skip_contents_publishing: false
skip_bz2_publishing: false
filesystem_publish_endpoints: {}
s3_publish_endpoints: {}
swift_publish_endpoints: {}
azure_publish_endpoints: {}
packagepool_storage: {}

8 changes: 8 additions & 0 deletions system/t02_config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ class ConfigShowTest(BaseTest):
"""
runCmd = ["aptly", "config", "show"]
gold_processor = BaseTest.expand_environ


class ConfigShowYAMLTest(BaseTest):
"""
config showing
"""
runCmd = ["aptly", "config", "show", "-yaml"]
gold_processor = BaseTest.expand_environ
17 changes: 17 additions & 0 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,23 @@ func SaveConfigRaw(filename string, conf []byte) error {
return err
}

// SaveConfigYAML write configuration to yaml file
func SaveConfigYAML(filename string, config *ConfigStructure) error {
f, err := os.Create(filename)
if err != nil {
return err
}

Check warning on line 302 in utils/config.go

View check run for this annotation

Codecov / codecov/patch

utils/config.go#L301-L302

Added lines #L301 - L302 were not covered by tests
defer f.Close()

yamlData, err := yaml.Marshal(&config)
if err != nil {
return fmt.Errorf("error marshaling to YAML: %s", err)
}

Check warning on line 308 in utils/config.go

View check run for this annotation

Codecov / codecov/patch

utils/config.go#L307-L308

Added lines #L307 - L308 were not covered by tests

_, err = f.Write(yamlData)
return err
}

// GetRootDir returns the RootDir with expanded ~ as home directory
func (conf *ConfigStructure) GetRootDir() string {
return strings.Replace(conf.RootDir, "~", os.Getenv("HOME"), 1)
Expand Down
134 changes: 132 additions & 2 deletions utils/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ type ConfigSuite struct {
var _ = Suite(&ConfigSuite{})

func (s *ConfigSuite) TestLoadConfig(c *C) {
configname := filepath.Join(c.MkDir(), "aptly.json")
configname := filepath.Join(c.MkDir(), "aptly.json1")
f, _ := os.Create(configname)
f.WriteString(configFile)
f.Close()

// start with empty config
s.config = ConfigStructure{}

err := LoadConfig(configname, &s.config)
c.Assert(err, IsNil)
c.Check(s.config.GetRootDir(), Equals, "/opt/aptly/")
Expand All @@ -27,7 +30,10 @@ func (s *ConfigSuite) TestLoadConfig(c *C) {
}

func (s *ConfigSuite) TestSaveConfig(c *C) {
configname := filepath.Join(c.MkDir(), "aptly.json")
configname := filepath.Join(c.MkDir(), "aptly.json2")

// start with empty config
s.config = ConfigStructure{}

s.config.RootDir = "/tmp/aptly"
s.config.DownloadConcurrency = 5
Expand Down Expand Up @@ -153,4 +159,128 @@ func (s *ConfigSuite) TestSaveConfig(c *C) {
"}")
}

func (s *ConfigSuite) TestLoadYAMLConfig(c *C) {
configname := filepath.Join(c.MkDir(), "aptly.yaml1")
f, _ := os.Create(configname)
f.WriteString(configFileYAML)
f.Close()

// start with empty config
s.config = ConfigStructure{}

err := LoadConfig(configname, &s.config)
c.Assert(err, IsNil)
c.Check(s.config.GetRootDir(), Equals, "/opt/aptly/")
c.Check(s.config.DownloadConcurrency, Equals, 40)
c.Check(s.config.DatabaseOpenAttempts, Equals, 10)
}

func (s *ConfigSuite) TestSaveYAMLConfig(c *C) {
configname := filepath.Join(c.MkDir(), "aptly.yaml2")
f, _ := os.Create(configname)
f.WriteString(configFileYAML)
f.Close()

// start with empty config
s.config = ConfigStructure{}

err := LoadConfig(configname, &s.config)
c.Assert(err, IsNil)

err = SaveConfigYAML(configname, &s.config)
c.Assert(err, IsNil)

f, _ = os.Open(configname)
defer f.Close()

st, _ := f.Stat()
buf := make([]byte, st.Size())
f.Read(buf)

c.Check(string(buf), Equals, configFileYAML)
}

const configFile = `{"rootDir": "/opt/aptly/", "downloadConcurrency": 33, "databaseOpenAttempts": 33}`
const configFileYAML = `root_dir: /opt/aptly/
log_level: error
log_format: json
database_open_attempts: 10
architectures:
- amd64
- arm64
skip_legacy_pool: true
dep_follow_suggests: false
dep_follow_recommends: true
dep_follow_all_variants: false
dep_follow_source: true
dep_verboseresolve: false
ppa_distributor_id: Ubuntu
ppa_codename: short
serve_in_api_mode: true
enable_metrics_endpoint: false
enable_swagger_endpoint: true
async_api: false
database_backend:
type: etcd
db_path: ""
url: 127.0.0.1:2379
downloader: grab
download_concurrency: 40
download_limit: 100
download_retries: 10
download_sourcepackages: true
gpg_provider: gpg
gpg_disable_sign: true
gpg_disable_verify: false
skip_contents_publishing: true
skip_bz2_publishing: false
filesystem_publish_endpoints:
test1:
root_dir: /opt/srv/aptly_public
link_method: hardlink
verify_method: md5
s3_publish_endpoints:
test:
region: us-east-1
bucket: test-bucket
prefix: ""
acl: public-read
access_key_id: "2"
secret_access_key: secret
session_token: none
endpoint: endpoint
storage_class: STANDARD
encryption_method: AES256
plus_workaround: true
disable_multidel: false
force_sigv2: true
force_virtualhosted_style: false
debug: true
swift_publish_endpoints:
test:
container: c1
prefix: pre
username: user
password: pass
tenant: t
tenant_id: "2"
domain: pop
domain_id: "1"
tenant_domain: td
tenant_domain_id: "3"
auth_url: http://auth.url
azure_publish_endpoints:
test:
container: container1
prefix: pre2
account_name: aname
account_key: akey
endpoint: https://end.point
packagepool_storage:
type: azure
container: test-pool1
prefix: pre3
account_name: a name
account_key: a key
endpoint: ""
`

0 comments on commit 8377147

Please sign in to comment.