Skip to content

Commit e679487

Browse files
cognifloydroot
authored andcommitted
Add tests for pack config patternProperties and additionalItems
1 parent af936f2 commit e679487

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed

st2common/tests/unit/test_config_loader.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,116 @@ def test_get_config_dynamic_config_item_under_additional_properties(self):
575575

576576
config_db.delete()
577577

578+
def test_get_config_dynamic_config_item_under_pattern_properties(self):
579+
pack_name = "dummy_pack_schema_with_pattern_properties_1"
580+
loader = ContentPackConfigLoader(pack_name=pack_name)
581+
582+
encrypted_value = crypto.symmetric_encrypt(
583+
KeyValuePairAPI.crypto_key, "v1_encrypted"
584+
)
585+
KeyValuePair.add_or_update(
586+
KeyValuePairDB(name="k1_encrypted", value=encrypted_value, secret=True)
587+
)
588+
589+
####################
590+
# values in objects under an object with additionalProperties
591+
values = {
592+
"profiles": {
593+
"dev": {
594+
# no host or port to test default value
595+
"token": "hard-coded-secret",
596+
},
597+
"prod": {
598+
"host": "127.1.2.7",
599+
"port": 8282,
600+
# encrypted in datastore
601+
"token": "{{st2kv.system.k1_encrypted}}",
602+
# schema declares `secret: true` which triggers auto-decryption.
603+
# If this were not encrypted, it would try to decrypt it and fail.
604+
},
605+
}
606+
}
607+
config_db = ConfigDB(pack=pack_name, values=values)
608+
config_db = Config.add_or_update(config_db)
609+
610+
config_rendered = loader.get_config()
611+
612+
self.assertEqual(
613+
config_rendered,
614+
{
615+
"region": "us-east-1",
616+
"profiles": {
617+
"dev": {
618+
"host": "127.0.0.3",
619+
"port": 8080,
620+
"token": "hard-coded-secret",
621+
},
622+
"prod": {
623+
"host": "127.1.2.7",
624+
"port": 8282,
625+
"token": "v1_encrypted",
626+
},
627+
},
628+
},
629+
)
630+
631+
config_db.delete()
632+
633+
def test_get_config_dynamic_config_item_under_additional_items(self):
634+
pack_name = "dummy_pack_schema_with_additional_items_1"
635+
loader = ContentPackConfigLoader(pack_name=pack_name)
636+
637+
encrypted_value = crypto.symmetric_encrypt(
638+
KeyValuePairAPI.crypto_key, "v1_encrypted"
639+
)
640+
KeyValuePair.add_or_update(
641+
KeyValuePairDB(name="k1_encrypted", value=encrypted_value, secret=True)
642+
)
643+
644+
####################
645+
# values in objects under an object with additionalProperties
646+
values = {
647+
"profiles": [
648+
{
649+
# no host or port to test default value
650+
"token": "hard-coded-secret",
651+
},
652+
{
653+
"host": "127.1.2.7",
654+
"port": 8282,
655+
# encrypted in datastore
656+
"token": "{{st2kv.system.k1_encrypted}}",
657+
# schema declares `secret: true` which triggers auto-decryption.
658+
# If this were not encrypted, it would try to decrypt it and fail.
659+
},
660+
]
661+
}
662+
config_db = ConfigDB(pack=pack_name, values=values)
663+
config_db = Config.add_or_update(config_db)
664+
665+
config_rendered = loader.get_config()
666+
667+
self.assertEqual(
668+
config_rendered,
669+
{
670+
"region": "us-east-1",
671+
"profiles": [
672+
{
673+
"host": "127.0.0.3",
674+
"port": 8080,
675+
"token": "hard-coded-secret",
676+
},
677+
{
678+
"host": "127.1.2.7",
679+
"port": 8282,
680+
"token": "v1_encrypted",
681+
},
682+
],
683+
},
684+
)
685+
686+
config_db.delete()
687+
578688
def test_empty_config_object_in_the_database(self):
579689
pack_name = "dummy_pack_empty_config"
580690

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
region:
3+
type: "string"
4+
required: false
5+
default: "us-east-1"
6+
profiles:
7+
type: "array"
8+
required: false
9+
additionalItems:
10+
type: object
11+
additionalProperties: false
12+
properties:
13+
host:
14+
type: "string"
15+
required: false
16+
default: "127.0.0.3"
17+
port:
18+
type: "integer"
19+
required: false
20+
default: 8080
21+
token:
22+
type: "string"
23+
required: true
24+
secret: true
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
name : dummy_pack_schema_with_additional_items_1
3+
description : dummy pack with nested objects under additionalItems
4+
version : 0.1.0
5+
author : st2-dev
6+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
region:
3+
type: "string"
4+
required: false
5+
default: "us-east-1"
6+
profiles:
7+
type: "object"
8+
required: false
9+
patternProperties:
10+
"^\\w+$":
11+
type: object
12+
additionalProperties: false
13+
properties:
14+
host:
15+
type: "string"
16+
required: false
17+
default: "127.0.0.3"
18+
port:
19+
type: "integer"
20+
required: false
21+
default: 8080
22+
token:
23+
type: "string"
24+
required: true
25+
secret: true
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
name : dummy_pack_schema_with_pattern_properties_1
3+
description : dummy pack with nested objects under patternProperties
4+
version : 0.1.0
5+
author : st2-dev
6+

0 commit comments

Comments
 (0)