예제 #1
0
    def test_aaz_dict_arg(self):
        from azure.cli.core.aaz._arg import AAZDictArg, AAZStrArg, AAZArgumentsSchema
        from azure.cli.core.aaz._arg_action import AAZArgActionOperations
        schema = AAZArgumentsSchema()
        v = schema()

        schema.tags = AAZDictArg(options=["--tags", "-t"], )
        schema.tags.Element = AAZStrArg(nullable=True, blank="a blank value")

        arg = schema.tags.to_cmd_arg("tags")
        action = arg.type.settings["action"]

        dest_ops = AAZArgActionOperations()
        assert len(dest_ops._ops) == 0

        # null value
        action.setup_operations(dest_ops, ["a=null", "b=None"])
        assert len(dest_ops._ops) == 2
        dest_ops.apply(v, "tags")
        assert v.tags == {"a": None, "b": 'None'}

        action.setup_operations(dest_ops, ["b=6", "c="])
        assert len(dest_ops._ops) == 4
        dest_ops.apply(v, "tags")
        assert v.tags == {"a": None, "b": "6", "c": "a blank value"}

        action.setup_operations(dest_ops, ["{ab:1,bc:2,cd:'null'}"])
        assert len(dest_ops._ops) == 5
        dest_ops.apply(v, "tags")
        assert v.tags == {"ab": '1', "bc": '2', 'cd': 'null'}

        action.setup_operations(dest_ops, ["{}"])
        assert len(dest_ops._ops) == 6
        dest_ops.apply(v, "tags")
        assert v.tags == {}

        with self.assertRaises(aazerror.AAZInvalidValueError):
            action.setup_operations(dest_ops, ["=1"])

        with self.assertRaises(aazerror.AAZInvalidValueError):
            action.setup_operations(dest_ops, ["null"])

        with self.assertRaises(aazerror.AAZInvalidValueError):
            action.setup_operations(dest_ops, ["null"])

        with self.assertRaises(aazerror.AAZInvalidShorthandSyntaxError):
            action.setup_operations(dest_ops, ["{c:}"])
예제 #2
0
    def test_aaz_object_arg(self):
        from azure.cli.core.aaz._arg import AAZDictArg, AAZListArg, AAZObjectArg, AAZIntArg, AAZBoolArg, AAZFloatArg, \
            AAZStrArg, AAZArgumentsSchema
        from azure.cli.core.aaz._arg_action import AAZArgActionOperations
        schema = AAZArgumentsSchema()
        v = schema()

        schema.properties = AAZObjectArg(options=["--prop", "-p"],
                                         nullable=True)

        schema.properties.enable = AAZBoolArg(
            options=["enable"],
            nullable=True,
        )
        schema.properties.tags = AAZDictArg(options=["tags"], nullable=True)
        schema.properties.tags.Element = AAZIntArg()
        schema.properties.vnets = AAZListArg(options=["vnets"], nullable=True)
        schema.properties.vnets.Element = AAZObjectArg()
        schema.properties.vnets.Element.id = AAZStrArg(options=["id"], )

        schema.properties.pt = AAZFloatArg(options=["pt"],
                                           nullable=True,
                                           blank="0.1")

        arg = schema.properties.to_cmd_arg("properties")
        action = arg.type.settings["action"]

        dest_ops = AAZArgActionOperations()
        assert len(dest_ops._ops) == 0

        action.setup_operations(
            dest_ops,
            ["{enable:false,tags:{a:1,3:2},vnets:[{id:/123}],pt:12.123}"])
        assert len(dest_ops._ops) == 1
        dest_ops.apply(v, "properties")
        assert v.properties == {
            "enable": False,
            "tags": {
                "a": 1,
                "3": 2,
            },
            "vnets": [
                {
                    "id": "/123"
                },
            ],
            "pt": 12.123
        }

        action.setup_operations(dest_ops, ["pt=", "enable=null", "vnets=[]"])
        assert len(dest_ops._ops) == 4
        dest_ops.apply(v, "properties")
        assert v.properties == {
            "enable": None,
            "tags": {
                "a": 1,
                "3": 2,
            },
            "vnets": [],
            "pt": 0.1
        }

        action.setup_operations(dest_ops, ["{}"])
        assert len(dest_ops._ops) == 5
        dest_ops.apply(v, "properties")
        assert v.properties == {}

        action.setup_operations(dest_ops, ["null"])
        assert len(dest_ops._ops) == 6
        dest_ops.apply(v, "properties")
        assert v.properties == None

        action.setup_operations(
            dest_ops, ["{enable:True,tags:null,vnets:null,pt:12.123}"])
        assert len(dest_ops._ops) == 7
        dest_ops.apply(v, "properties")
        assert v.properties == {
            "enable": True,
            "tags": None,
            "vnets": None,
            "pt": 12.123
        }
예제 #3
0
    def test_aaz_list_arg(self):
        from azure.cli.core.aaz._arg import AAZListArg, AAZStrArg, AAZArgumentsSchema
        from azure.cli.core.aaz._arg_action import AAZArgActionOperations, _ELEMENT_APPEND_KEY
        schema = AAZArgumentsSchema()
        v = schema()

        schema.names = AAZListArg(options=["--names", "--ns"],
                                  singular_options=["--name", "-n"])
        schema.names.Element = AAZStrArg(nullable=True, blank="a blank value")

        arg = schema.names.to_cmd_arg("names")
        action = arg.type.settings["action"]

        dest_ops = AAZArgActionOperations()
        assert len(dest_ops._ops) == 0

        # null value
        action.setup_operations(dest_ops, ["null"])
        assert len(dest_ops._ops) == 1
        dest_ops.apply(v, "names")
        assert v.names == [
            None,
        ]

        action.setup_operations(dest_ops, ["[a,b,'c',' ']"])
        assert len(dest_ops._ops) == 2
        dest_ops.apply(v, "names")
        assert v.names == ['a', 'b', 'c', ' ']

        action.setup_operations(dest_ops, ["[2]=efg", "[-1]='null'", "[0]="])
        assert len(dest_ops._ops) == 5
        dest_ops.apply(v, "names")
        assert v.names == ['a blank value', 'b', 'efg', 'null']

        action.setup_operations(dest_ops, ["c", "d"])
        assert len(dest_ops._ops) == 6
        dest_ops.apply(v, "names")
        assert v.names == ['c', 'd']

        action.setup_operations(dest_ops, ["[]"])
        assert len(dest_ops._ops) == 7
        dest_ops.apply(v, "names")
        assert v.names == []

        action.setup_operations(dest_ops, ["a"])
        assert len(dest_ops._ops) == 8
        dest_ops.apply(v, "names")
        assert v.names == ["a"]

        action.setup_operations(dest_ops, ["", "''"])
        assert len(dest_ops._ops) == 9
        dest_ops.apply(v, "names")
        assert v.names == ["", ""]

        action.setup_operations(dest_ops, ["a", 'null', 'None', "b", ""])
        assert len(dest_ops._ops) == 10
        dest_ops.apply(v, "names")
        assert v.names == ["a", None, 'None', "b", ""]

        # blank value
        with self.assertRaises(aazerror.AAZInvalidValueError):
            action.setup_operations(dest_ops, None)

        action.setup_operations(dest_ops, ["[]"])
        assert len(dest_ops._ops) == 11
        dest_ops.apply(v, "names")
        assert v.names == []

        # test singular action
        singular_action = schema.names.Element._build_cmd_action()

        singular_action.setup_operations(dest_ops, ["a"],
                                         prefix_keys=[_ELEMENT_APPEND_KEY])
        assert len(dest_ops._ops) == 12
        dest_ops.apply(v, "names")
        assert v.names == ["a"]

        singular_action.setup_operations(dest_ops, ["b"],
                                         prefix_keys=[_ELEMENT_APPEND_KEY])
        assert len(dest_ops._ops) == 13
        dest_ops.apply(v, "names")
        assert v.names == ["a", "b"]

        singular_action.setup_operations(dest_ops,
                                         None,
                                         prefix_keys=[_ELEMENT_APPEND_KEY])
        assert len(dest_ops._ops) == 14
        dest_ops.apply(v, "names")
        assert v.names == ["a", "b", "a blank value"]

        singular_action.setup_operations(dest_ops, [""],
                                         prefix_keys=[_ELEMENT_APPEND_KEY])
        assert len(dest_ops._ops) == 15
        dest_ops.apply(v, "names")
        assert v.names == ["a", "b", "a blank value", ""]
예제 #4
0
    def test_aaz_bool_arg(self):
        from azure.cli.core.aaz._arg import AAZBoolArg, AAZArgumentsSchema
        from azure.cli.core.aaz._arg_action import AAZArgActionOperations
        schema = AAZArgumentsSchema()
        v = schema()

        schema.enable = AAZBoolArg(options=["--enable", "-e"])
        arg = schema.enable.to_cmd_arg("enable")
        assert len(arg.choices) == 10
        action = arg.type.settings["action"]

        dest_ops = AAZArgActionOperations()
        assert len(dest_ops._ops) == 0

        action.setup_operations(dest_ops, None)
        assert len(dest_ops._ops) == 1
        dest_ops.apply(v, "enable")
        assert v.enable == True

        action.setup_operations(dest_ops, "false")
        assert len(dest_ops._ops) == 2
        dest_ops.apply(v, "enable")
        assert v.enable == False

        action.setup_operations(dest_ops, "true")
        assert len(dest_ops._ops) == 3
        dest_ops.apply(v, "enable")
        assert v.enable == True

        action.setup_operations(dest_ops, "f")
        assert len(dest_ops._ops) == 4
        dest_ops.apply(v, "enable")
        assert v.enable == False

        action.setup_operations(dest_ops, "t")
        assert len(dest_ops._ops) == 5
        dest_ops.apply(v, "enable")
        assert v.enable == True

        action.setup_operations(dest_ops, "no")
        assert len(dest_ops._ops) == 6
        dest_ops.apply(v, "enable")
        assert v.enable == False

        action.setup_operations(dest_ops, "yes")
        assert len(dest_ops._ops) == 7
        dest_ops.apply(v, "enable")
        assert v.enable == True

        action.setup_operations(dest_ops, "n")
        assert len(dest_ops._ops) == 8
        dest_ops.apply(v, "enable")
        assert v.enable == False

        action.setup_operations(dest_ops, "y")
        assert len(dest_ops._ops) == 9
        dest_ops.apply(v, "enable")
        assert v.enable == True

        action.setup_operations(dest_ops, "0")
        assert len(dest_ops._ops) == 10
        dest_ops.apply(v, "enable")
        assert v.enable == False

        action.setup_operations(dest_ops, "1")
        assert len(dest_ops._ops) == 11
        dest_ops.apply(v, "enable")
        assert v.enable == True

        # null value
        with self.assertRaises(aazerror.AAZInvalidValueError):
            action.setup_operations(dest_ops, "null")

        # null value
        with self.assertRaises(aazerror.AAZInvalidValueError):
            action.setup_operations(dest_ops, "null")

        schema.started = AAZBoolArg(
            options=["--started", "-s"],
            nullable=True,
            blank=False,
        )
        arg = schema.started.to_cmd_arg("started")
        assert len(arg.choices) == 10
        action = arg.type.settings["action"]

        dest_ops = AAZArgActionOperations()
        assert len(dest_ops._ops) == 0

        # null value
        action.setup_operations(dest_ops, "null")
        assert len(dest_ops._ops) == 1
        dest_ops.apply(v, "started")
        assert v.started == None

        # blank value
        action.setup_operations(dest_ops, None)
        assert len(dest_ops._ops) == 2
        dest_ops.apply(v, "started")
        assert v.started == False

        action.setup_operations(dest_ops, "TRUE")
        assert len(dest_ops._ops) == 3
        dest_ops.apply(v, "started")
        assert v.started == True
예제 #5
0
    def test_aaz_float_arg(self):
        from azure.cli.core.aaz._arg import AAZFloatArg, AAZArgumentsSchema
        from azure.cli.core.aaz._arg_action import AAZArgActionOperations
        schema = AAZArgumentsSchema()
        v = schema()

        schema.score = AAZFloatArg(options=["--score", "-s"],
                                   enum={
                                       "A": 100.0,
                                       "B": 90.0,
                                       "C": 80.0,
                                       "D": 0.0,
                                   },
                                   nullable=True,
                                   blank=0.0)
        arg = schema.score.to_cmd_arg("score")
        assert len(arg.choices) == 4
        action = arg.type.settings["action"]

        dest_ops = AAZArgActionOperations()
        assert len(dest_ops._ops) == 0

        action.setup_operations(dest_ops, "A")
        assert len(dest_ops._ops) == 1
        dest_ops.apply(v, "score")
        assert v.score == 100.0

        # null value
        action.setup_operations(dest_ops, "null")
        assert len(dest_ops._ops) == 2
        dest_ops.apply(v, "score")
        assert v.score == None

        # blank value
        action.setup_operations(dest_ops, None)
        assert len(dest_ops._ops) == 3
        dest_ops.apply(v, "score")
        assert v.score == 0.0

        # null value
        action.setup_operations(dest_ops, "null")
        assert len(dest_ops._ops) == 4
        dest_ops.apply(v, "score")
        assert v.score == None

        # credit argument
        schema.credit = AAZFloatArg(options=["--credit", "-c"])
        arg = schema.credit.to_cmd_arg("credit")
        action = arg.type.settings["action"]

        dest_ops = AAZArgActionOperations()
        assert len(dest_ops._ops) == 0

        action.setup_operations(dest_ops, "-100")
        assert len(dest_ops._ops) == 1
        dest_ops.apply(v, "credit")
        assert v.credit == -100.0

        action.setup_operations(dest_ops, "0.23")
        assert len(dest_ops._ops) == 2
        dest_ops.apply(v, "credit")
        assert v.credit == 0.23

        action.setup_operations(dest_ops, "100.1")
        assert len(dest_ops._ops) == 3
        dest_ops.apply(v, "credit")
        assert v.credit == 100.1

        action.setup_operations(dest_ops, "'10.123'")
        assert len(dest_ops._ops) == 4
        dest_ops.apply(v, "credit")
        assert v.credit == 10.123

        # test blank
        with self.assertRaises(aazerror.AAZInvalidValueError):
            action.setup_operations(dest_ops, None)

        # test null
        with self.assertRaises(aazerror.AAZInvalidValueError):
            action.setup_operations(dest_ops, "null")

        with self.assertRaises(ValueError):
            action.setup_operations(dest_ops, " ")
예제 #6
0
    def test_aaz_str_arg(self):
        from azure.cli.core.aaz._arg import AAZStrArg, AAZArgumentsSchema
        from azure.cli.core.aaz._arg_action import AAZArgActionOperations
        schema = AAZArgumentsSchema()
        v = schema()

        schema.work_day = AAZStrArg(options=["--work-day", "-d"],
                                    enum={
                                        "1": "Monday",
                                        "2": "Tuesday",
                                        "3": "Wednesday",
                                        "4": "Thursday",
                                        "5": "Friday",
                                        "6": "Saturday",
                                        "7": "Sunday",
                                        "Mon": "Monday",
                                        "Tue": "Tuesday",
                                        "Wed": "Wednesday",
                                        "Thu": "Thursday",
                                        "Fri": "Friday",
                                        "Sat": "Saturday",
                                        "Sun": "Sunday",
                                    },
                                    nullable=True,
                                    blank="Sunday")
        arg = schema.work_day.to_cmd_arg("work_day")
        assert len(arg.choices) == 14
        action = arg.type.settings["action"]

        dest_ops = AAZArgActionOperations()
        assert len(dest_ops._ops) == 0

        action.setup_operations(dest_ops, "1")
        assert len(dest_ops._ops) == 1
        dest_ops.apply(v, "work_day")
        assert v.work_day == "Monday"

        action.setup_operations(dest_ops, "2")
        assert len(dest_ops._ops) == 2
        dest_ops.apply(v, "work_day")
        assert v.work_day == "Tuesday"

        action.setup_operations(dest_ops, "Thu")
        assert len(dest_ops._ops) == 3
        dest_ops.apply(v, "work_day")
        assert v.work_day == "Thursday"

        action.setup_operations(dest_ops, "fri")
        assert len(dest_ops._ops) == 4
        dest_ops.apply(v, "work_day")
        assert v.work_day == "Friday"

        # null value
        action.setup_operations(dest_ops, 'null')
        assert len(dest_ops._ops) == 5
        dest_ops.apply(v, "work_day")
        assert v.work_day == None  # must use '== None', because 'is None' will not work

        # blank value
        action.setup_operations(dest_ops, None)
        assert len(dest_ops._ops) == 6
        dest_ops.apply(v, "work_day")
        assert v.work_day == "Sunday"

        # null value
        action.setup_operations(dest_ops, 'null')
        assert len(dest_ops._ops) == 7
        dest_ops.apply(v, "work_day")
        assert v.work_day == None

        # test invalid operations
        with self.assertRaises(azclierror.InvalidArgumentValueError):
            action.setup_operations(dest_ops, '1234')
        assert len(dest_ops._ops) == 7
        dest_ops.apply(v, "work_day")
        assert v.work_day == None

        # New argument
        schema.name = AAZStrArg(options=["--name", "-n"])
        arg = schema.name.to_cmd_arg("work_day")
        action = arg.type.settings["action"]
        dest_ops = AAZArgActionOperations()
        assert len(dest_ops._ops) == 0

        action.setup_operations(dest_ops, "test name")
        assert len(dest_ops._ops) == 1
        dest_ops.apply(v, "name")
        assert v.name == "test name"

        action.setup_operations(dest_ops, "")
        assert len(dest_ops._ops) == 2
        dest_ops.apply(v, "name")
        assert v.name == ""

        with self.assertRaises(aazerror.AAZInvalidValueError):
            action.setup_operations(dest_ops, "null")
        with self.assertRaises(aazerror.AAZInvalidShorthandSyntaxError):
            action.setup_operations(dest_ops, "'l")
        with self.assertRaises(aazerror.AAZInvalidValueError):
            action.setup_operations(dest_ops, None)

        assert len(dest_ops._ops) == 2
        dest_ops.apply(v, "name")
        assert v.name == ""

        action.setup_operations(dest_ops, " aa' l_;{]'")
        assert len(dest_ops._ops) == 3
        dest_ops.apply(v, "name")
        assert v.name == " aa' l_;{]'"