Skip to content

Commit 8377147

Browse files
committed
add test
1 parent 5498e84 commit 8377147

File tree

4 files changed

+194
-2
lines changed

4 files changed

+194
-2
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
root_dir: ${HOME}/.aptly
2+
log_level: debug
3+
log_format: default
4+
database_open_attempts: 10
5+
architectures: []
6+
skip_legacy_pool: false
7+
dep_follow_suggests: false
8+
dep_follow_recommends: false
9+
dep_follow_all_variants: false
10+
dep_follow_source: false
11+
dep_verboseresolve: false
12+
ppa_distributor_id: ubuntu
13+
ppa_codename: ""
14+
serve_in_api_mode: true
15+
enable_metrics_endpoint: true
16+
enable_swagger_endpoint: false
17+
async_api: false
18+
database_backend:
19+
type: ""
20+
db_path: ""
21+
url: ""
22+
downloader: default
23+
download_concurrency: 4
24+
download_limit: 0
25+
download_retries: 5
26+
download_sourcepackages: false
27+
gpg_provider: gpg
28+
gpg_disable_sign: false
29+
gpg_disable_verify: false
30+
skip_contents_publishing: false
31+
skip_bz2_publishing: false
32+
filesystem_publish_endpoints: {}
33+
s3_publish_endpoints: {}
34+
swift_publish_endpoints: {}
35+
azure_publish_endpoints: {}
36+
packagepool_storage: {}
37+

system/t02_config/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,11 @@ class ConfigShowTest(BaseTest):
6464
"""
6565
runCmd = ["aptly", "config", "show"]
6666
gold_processor = BaseTest.expand_environ
67+
68+
69+
class ConfigShowYAMLTest(BaseTest):
70+
"""
71+
config showing
72+
"""
73+
runCmd = ["aptly", "config", "show", "-yaml"]
74+
gold_processor = BaseTest.expand_environ

utils/config.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,23 @@ func SaveConfigRaw(filename string, conf []byte) error {
294294
return err
295295
}
296296

297+
// SaveConfigYAML write configuration to yaml file
298+
func SaveConfigYAML(filename string, config *ConfigStructure) error {
299+
f, err := os.Create(filename)
300+
if err != nil {
301+
return err
302+
}
303+
defer f.Close()
304+
305+
yamlData, err := yaml.Marshal(&config)
306+
if err != nil {
307+
return fmt.Errorf("error marshaling to YAML: %s", err)
308+
}
309+
310+
_, err = f.Write(yamlData)
311+
return err
312+
}
313+
297314
// GetRootDir returns the RootDir with expanded ~ as home directory
298315
func (conf *ConfigStructure) GetRootDir() string {
299316
return strings.Replace(conf.RootDir, "~", os.Getenv("HOME"), 1)

utils/config_test.go

Lines changed: 132 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ type ConfigSuite struct {
1414
var _ = Suite(&ConfigSuite{})
1515

1616
func (s *ConfigSuite) TestLoadConfig(c *C) {
17-
configname := filepath.Join(c.MkDir(), "aptly.json")
17+
configname := filepath.Join(c.MkDir(), "aptly.json1")
1818
f, _ := os.Create(configname)
1919
f.WriteString(configFile)
2020
f.Close()
2121

22+
// start with empty config
23+
s.config = ConfigStructure{}
24+
2225
err := LoadConfig(configname, &s.config)
2326
c.Assert(err, IsNil)
2427
c.Check(s.config.GetRootDir(), Equals, "/opt/aptly/")
@@ -27,7 +30,10 @@ func (s *ConfigSuite) TestLoadConfig(c *C) {
2730
}
2831

2932
func (s *ConfigSuite) TestSaveConfig(c *C) {
30-
configname := filepath.Join(c.MkDir(), "aptly.json")
33+
configname := filepath.Join(c.MkDir(), "aptly.json2")
34+
35+
// start with empty config
36+
s.config = ConfigStructure{}
3137

3238
s.config.RootDir = "/tmp/aptly"
3339
s.config.DownloadConcurrency = 5
@@ -153,4 +159,128 @@ func (s *ConfigSuite) TestSaveConfig(c *C) {
153159
"}")
154160
}
155161

162+
func (s *ConfigSuite) TestLoadYAMLConfig(c *C) {
163+
configname := filepath.Join(c.MkDir(), "aptly.yaml1")
164+
f, _ := os.Create(configname)
165+
f.WriteString(configFileYAML)
166+
f.Close()
167+
168+
// start with empty config
169+
s.config = ConfigStructure{}
170+
171+
err := LoadConfig(configname, &s.config)
172+
c.Assert(err, IsNil)
173+
c.Check(s.config.GetRootDir(), Equals, "/opt/aptly/")
174+
c.Check(s.config.DownloadConcurrency, Equals, 40)
175+
c.Check(s.config.DatabaseOpenAttempts, Equals, 10)
176+
}
177+
178+
func (s *ConfigSuite) TestSaveYAMLConfig(c *C) {
179+
configname := filepath.Join(c.MkDir(), "aptly.yaml2")
180+
f, _ := os.Create(configname)
181+
f.WriteString(configFileYAML)
182+
f.Close()
183+
184+
// start with empty config
185+
s.config = ConfigStructure{}
186+
187+
err := LoadConfig(configname, &s.config)
188+
c.Assert(err, IsNil)
189+
190+
err = SaveConfigYAML(configname, &s.config)
191+
c.Assert(err, IsNil)
192+
193+
f, _ = os.Open(configname)
194+
defer f.Close()
195+
196+
st, _ := f.Stat()
197+
buf := make([]byte, st.Size())
198+
f.Read(buf)
199+
200+
c.Check(string(buf), Equals, configFileYAML)
201+
}
202+
156203
const configFile = `{"rootDir": "/opt/aptly/", "downloadConcurrency": 33, "databaseOpenAttempts": 33}`
204+
const configFileYAML = `root_dir: /opt/aptly/
205+
log_level: error
206+
log_format: json
207+
database_open_attempts: 10
208+
architectures:
209+
- amd64
210+
- arm64
211+
skip_legacy_pool: true
212+
dep_follow_suggests: false
213+
dep_follow_recommends: true
214+
dep_follow_all_variants: false
215+
dep_follow_source: true
216+
dep_verboseresolve: false
217+
ppa_distributor_id: Ubuntu
218+
ppa_codename: short
219+
serve_in_api_mode: true
220+
enable_metrics_endpoint: false
221+
enable_swagger_endpoint: true
222+
async_api: false
223+
database_backend:
224+
type: etcd
225+
db_path: ""
226+
url: 127.0.0.1:2379
227+
downloader: grab
228+
download_concurrency: 40
229+
download_limit: 100
230+
download_retries: 10
231+
download_sourcepackages: true
232+
gpg_provider: gpg
233+
gpg_disable_sign: true
234+
gpg_disable_verify: false
235+
skip_contents_publishing: true
236+
skip_bz2_publishing: false
237+
filesystem_publish_endpoints:
238+
test1:
239+
root_dir: /opt/srv/aptly_public
240+
link_method: hardlink
241+
verify_method: md5
242+
s3_publish_endpoints:
243+
test:
244+
region: us-east-1
245+
bucket: test-bucket
246+
prefix: ""
247+
acl: public-read
248+
access_key_id: "2"
249+
secret_access_key: secret
250+
session_token: none
251+
endpoint: endpoint
252+
storage_class: STANDARD
253+
encryption_method: AES256
254+
plus_workaround: true
255+
disable_multidel: false
256+
force_sigv2: true
257+
force_virtualhosted_style: false
258+
debug: true
259+
swift_publish_endpoints:
260+
test:
261+
container: c1
262+
prefix: pre
263+
username: user
264+
password: pass
265+
tenant: t
266+
tenant_id: "2"
267+
domain: pop
268+
domain_id: "1"
269+
tenant_domain: td
270+
tenant_domain_id: "3"
271+
auth_url: http://auth.url
272+
azure_publish_endpoints:
273+
test:
274+
container: container1
275+
prefix: pre2
276+
account_name: aname
277+
account_key: akey
278+
endpoint: https://end.point
279+
packagepool_storage:
280+
type: azure
281+
container: test-pool1
282+
prefix: pre3
283+
account_name: a name
284+
account_key: a key
285+
endpoint: ""
286+
`

0 commit comments

Comments
 (0)