|
| 1 | +# Copyright 2026 The Crossplane Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import dataclasses |
| 16 | +import datetime |
| 17 | +import unittest |
| 18 | +from unittest import mock |
| 19 | + |
| 20 | +import click |
| 21 | +from click.testing import CliRunner |
| 22 | +from google.protobuf import duration_pb2 as durationpb |
| 23 | +from google.protobuf import json_format |
| 24 | + |
| 25 | +from crossplane.function import cli, response |
| 26 | +from crossplane.function.proto.v1 import run_function_pb2 as fnv1 |
| 27 | + |
| 28 | + |
| 29 | +class TestParseDuration(unittest.TestCase): |
| 30 | + def test_parse_duration(self) -> None: |
| 31 | + @dataclasses.dataclass |
| 32 | + class TestCase: |
| 33 | + reason: str |
| 34 | + value: str |
| 35 | + want: datetime.timedelta |
| 36 | + |
| 37 | + cases = [ |
| 38 | + TestCase( |
| 39 | + reason="Bare integers are interpreted as seconds.", |
| 40 | + value="60", |
| 41 | + want=datetime.timedelta(seconds=60), |
| 42 | + ), |
| 43 | + TestCase( |
| 44 | + reason="Seconds suffix should work.", |
| 45 | + value="60s", |
| 46 | + want=datetime.timedelta(seconds=60), |
| 47 | + ), |
| 48 | + TestCase( |
| 49 | + reason="Minutes suffix should work.", |
| 50 | + value="1m", |
| 51 | + want=datetime.timedelta(minutes=1), |
| 52 | + ), |
| 53 | + TestCase( |
| 54 | + reason="Hours suffix should work.", |
| 55 | + value="1h", |
| 56 | + want=datetime.timedelta(hours=1), |
| 57 | + ), |
| 58 | + TestCase( |
| 59 | + reason="Days suffix should work.", |
| 60 | + value="1d", |
| 61 | + want=datetime.timedelta(days=1), |
| 62 | + ), |
| 63 | + TestCase( |
| 64 | + reason="Combined durations should be summed.", |
| 65 | + value="1h30m", |
| 66 | + want=datetime.timedelta(hours=1, minutes=30), |
| 67 | + ), |
| 68 | + TestCase( |
| 69 | + reason="Zero seconds should work.", |
| 70 | + value="0s", |
| 71 | + want=datetime.timedelta(seconds=0), |
| 72 | + ), |
| 73 | + TestCase( |
| 74 | + reason="Zero as a bare integer should work.", |
| 75 | + value="0", |
| 76 | + want=datetime.timedelta(seconds=0), |
| 77 | + ), |
| 78 | + TestCase( |
| 79 | + reason="Fractional seconds should work.", |
| 80 | + value="1.5s", |
| 81 | + want=datetime.timedelta(seconds=1.5), |
| 82 | + ), |
| 83 | + ] |
| 84 | + |
| 85 | + for case in cases: |
| 86 | + got = cli.parse_duration(case.value) |
| 87 | + self.assertEqual(case.want, got, case.reason) |
| 88 | + |
| 89 | + def test_parse_duration_invalid(self) -> None: |
| 90 | + for value in ("", "1x", "-5m", "1m2"): |
| 91 | + with self.assertRaises(ValueError, msg=value): |
| 92 | + cli.parse_duration(value) |
| 93 | + |
| 94 | + |
| 95 | +class TestStandardOptions(unittest.TestCase): |
| 96 | + def setUp(self) -> None: |
| 97 | + self._saved_default_ttl = response.get_default_ttl() |
| 98 | + |
| 99 | + def tearDown(self) -> None: |
| 100 | + response.set_default_ttl(self._saved_default_ttl) |
| 101 | + |
| 102 | + def test_run_sets_default_ttl_from_flag(self) -> None: |
| 103 | + @click.command() |
| 104 | + @cli.standard_options |
| 105 | + def main(**kwargs): |
| 106 | + cli.run(mock.Mock(), **kwargs) |
| 107 | + |
| 108 | + runner = CliRunner() |
| 109 | + with mock.patch("crossplane.function.cli.runtime.serve"): |
| 110 | + result = runner.invoke(main, ["--ttl", "5m", "--insecure"]) |
| 111 | + self.assertEqual(0, result.exit_code, result.output) |
| 112 | + self.assertEqual(datetime.timedelta(minutes=5), response.get_default_ttl()) |
| 113 | + |
| 114 | + def test_run_sets_default_ttl_from_env(self) -> None: |
| 115 | + @click.command() |
| 116 | + @cli.standard_options |
| 117 | + def main(**kwargs): |
| 118 | + cli.run(mock.Mock(), **kwargs) |
| 119 | + |
| 120 | + runner = CliRunner() |
| 121 | + with mock.patch("crossplane.function.cli.runtime.serve"): |
| 122 | + result = runner.invoke( |
| 123 | + main, |
| 124 | + ["--insecure"], |
| 125 | + env={"TTL": "10m"}, |
| 126 | + ) |
| 127 | + self.assertEqual(0, result.exit_code, result.output) |
| 128 | + self.assertEqual(datetime.timedelta(minutes=10), response.get_default_ttl()) |
| 129 | + |
| 130 | + def test_run_leaves_default_ttl_when_flag_omitted(self) -> None: |
| 131 | + response.set_default_ttl(response.DEFAULT_TTL) |
| 132 | + |
| 133 | + @click.command() |
| 134 | + @cli.standard_options |
| 135 | + def main(**kwargs): |
| 136 | + cli.run(mock.Mock(), **kwargs) |
| 137 | + |
| 138 | + runner = CliRunner() |
| 139 | + with mock.patch("crossplane.function.cli.runtime.serve"): |
| 140 | + result = runner.invoke(main, ["--insecure"]) |
| 141 | + self.assertEqual(0, result.exit_code, result.output) |
| 142 | + self.assertEqual(response.DEFAULT_TTL, response.get_default_ttl()) |
| 143 | + |
| 144 | + def test_invalid_ttl_flag(self) -> None: |
| 145 | + @click.command() |
| 146 | + @cli.standard_options |
| 147 | + def main(**kwargs): |
| 148 | + cli.run(mock.Mock(), **kwargs) |
| 149 | + |
| 150 | + runner = CliRunner() |
| 151 | + result = runner.invoke(main, ["--ttl", "1x", "--insecure"]) |
| 152 | + self.assertNotEqual(0, result.exit_code) |
| 153 | + |
| 154 | + def test_ttl_flag_affects_response_to(self) -> None: |
| 155 | + @click.command() |
| 156 | + @cli.standard_options |
| 157 | + def main(**kwargs): |
| 158 | + cli.run(mock.Mock(), **kwargs) |
| 159 | + |
| 160 | + runner = CliRunner() |
| 161 | + with mock.patch("crossplane.function.cli.runtime.serve"): |
| 162 | + result = runner.invoke(main, ["--ttl", "5m", "--insecure"]) |
| 163 | + self.assertEqual(0, result.exit_code, result.output) |
| 164 | + |
| 165 | + req = fnv1.RunFunctionRequest(meta=fnv1.RequestMeta(tag="hi")) |
| 166 | + got = response.to(req) |
| 167 | + want = fnv1.RunFunctionResponse( |
| 168 | + meta=fnv1.ResponseMeta(tag="hi", ttl=durationpb.Duration(seconds=60 * 5)), |
| 169 | + desired=req.desired, |
| 170 | + context=req.context, |
| 171 | + ) |
| 172 | + self.assertEqual( |
| 173 | + json_format.MessageToJson(want, sort_keys=True), |
| 174 | + json_format.MessageToJson(got, sort_keys=True), |
| 175 | + ) |
| 176 | + |
| 177 | + |
| 178 | +class TestDurationParamType(unittest.TestCase): |
| 179 | + def test_convert(self) -> None: |
| 180 | + param_type = cli.DurationParamType() |
| 181 | + got = param_type.convert("5m", None, None) |
| 182 | + self.assertEqual(datetime.timedelta(minutes=5), got) |
| 183 | + |
| 184 | + def test_convert_invalid(self) -> None: |
| 185 | + param_type = cli.DurationParamType() |
| 186 | + with self.assertRaises(click.BadParameter): |
| 187 | + param_type.convert("1x", None, None) |
| 188 | + |
| 189 | + |
| 190 | +if __name__ == "__main__": |
| 191 | + unittest.main() |
0 commit comments