Skip to content

Commit

Permalink
Fix shape.stable_json to handle special characters better
Browse files Browse the repository at this point in the history
Test Plan:
- `buck2 run global_services_index/administrator:tw.global_services_index.logview.cm=publish` doesn't error and produces `tw.global_services_index.logview.cm:MC0xTGdmNGV1V2JqcFJMVDVDMTR0MVp2N0xDZV9GOThnX3UtakNVNDdaTUEvNDQ0YTc0NGExYmViYmJlNTZiNzY1ZmQ4MjZkNzdhYTE`

- `cm print tw.global_services_index.logview.cm:MC0xTGdmNGV1V2JqcFJMVDVDMTR0MVp2N0xDZV9GOThnX3UtakNVNDdaTUEvNDQ0YTc0NGExYmViYmJlNTZiNzY1ZmQ4MjZkNzdhYTE` and observe sane `pre_run_commands`
```
TaskContainerManifest {
    handle: ContainerManifestHandle {
        name: "tw.global_services_index.logview.cm",
        id: "MC0xTGdmNGV1V2JqcFJMVDVDMTR0MVp2N0xDZV9GOThnX3UtakNVNDdaTUEvNDQ0YTc0NGExYmViYmJlNTZiNzY1ZmQ4MjZkNzdhYTE",
        variant: None,
        labels: None,
    },
    flavorToManifest: {
        "centos8": ContainerManifest {
            pkgs: [],
            directories: [],
            image: ContainerManifestImage {
                imageConfig: ImageConfig {
                    fbpkg: Fbpkg {
                        name: "tupperware.image.sendstream.base.c8",
                        version: "1e4db2c093574fce7f52edb8b0abb7b5",
                        preCompressedSizeBytes: 0,
                    },
                    minRootfsSize: 0,
                    filename: "",
                    usesTwManagedImage: false,
                },
                beingPushed: false,
            },
            metadata: ContainerManifestMetadata {
                buildTarget: "fbcode//global_services_index/administrator:tw.global_services_index.logview.cm",
                revision: "6bf34acfcc0bf3b080590e83a80b61ca11a9b29c",
            },
            rootfsOps: UnknownField(
                -1,
            ),
            runtimeConfig: Some(
                ContainerManifestRuntimeConfig {
                    pre_run_commands: [
                        Command {
                            command: "mkdir -p /var/tmp/logconsolidator/\ncat >/var/tmp/logconsolidator/config.py << EOF\ndef get_log_tuples(h, s, c):\n    base_tuples = [('/logs/stderr', 'errorlog_global_services_index', 1, ('^((I|V)[0-9]{4} )',), 'ads_delivery.py', None), ('/logs/stderr', 'errorlog_global_services_index', 1, ('^(?!(I|V)[0-9]{4} )',), 'ads_delivery.py', None)]\n    tier_tuples = {}\n    env_var_tuples = {}\n    import os\n    return base_tuples + [\n        tuple\n        for tier in s if tier in tier_tuples\n        for tuple in tier_tuples[tier]\n    ] + [\n        tuple\n        for env_var, tuples in env_var_tuples.items()\n        if os.environ.get(env_var)\n        for tuple in tuples\n    ]\nhostname_prefix_whitelist = []\nhostname_prefix_to_pools = {}\n\nEOF\n\n/packages/scribeclient2/logconsolidator_ctrl.par start",
                            arguments: [],
                            unix_user: Some(
                                "nobody",
                            ),
                            capabilities: None,
                        },
                    ],
                    environment_variables: {
                        "LOGCONSOLIDATOR_CONFIG": "/var/tmp/logconsolidator/config.py",
                    },
                    command: Command {
                        command: "",
                        arguments: [],
                        unix_user: Some(
                            "nobody",
                        ),
                        capabilities: None,
                    },
                    kill_command: None,
                    kill_timeout: None,
                    ports: Some(
                        [],
                    ),
                    user_attributes: {},
                    log_policy: Some(
                        LogPolicy {
                            retentionDays: 30,
                            retentionSize: 0,
                            rotationSize: 0,
                            rotationRate: 24,
                            uploadPolicy: None,
                        },
                    ),
                    resource_limit: None,
                },
            ),
        },
    },
    defaultFlavor: "centos8",
    mixins: [],
    oncall: "global_services_index",

Reviewed By: sergeyfd

Differential Revision: D47441722

fbshipit-source-id: 0faff10b0d3dc6251db59e42d7c27bcd4a8fc8d9
  • Loading branch information
Naveed Golafshani authored and facebook-github-bot committed Jul 13, 2023
1 parent 17b8b6f commit 9615692
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion antlir/bzl/shape.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ def _stable_json(instance):
elif instance == None:
return "null"
elif types.is_string(instance):
return '"{}"'.format(instance)
return repr(instance)
elif types.is_bool(instance):
return str(instance).lower()
else:
Expand Down
4 changes: 2 additions & 2 deletions antlir/bzl/tests/shapes/test_shape_bzl.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,12 +480,12 @@ def test_stable_json(self) -> None:
v2=123,
v3=False,
v1=None,
items=["two", "one"],
items=["two", "one", "item_with_new_line\n"],
)
instance_json = shape.stable_json(instance)
self.assertEqual(
instance_json,
'{"items":["two","one"],"v1":null,"v2":123,"v3":false}',
"{'items':['two','one','item_with_new_line\\n'],'v1':null,'v2':123,'v3':false}",
)

def test_hash(self) -> None:
Expand Down

0 comments on commit 9615692

Please sign in to comment.