xpk 1.0.0__py3-none-any.whl → 1.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. xpk/commands/cluster.py +29 -30
  2. xpk/commands/cluster_gcluster.py +19 -14
  3. xpk/commands/cluster_test.py +1 -21
  4. xpk/commands/common.py +39 -6
  5. xpk/commands/common_test.py +170 -0
  6. xpk/commands/info.py +9 -5
  7. xpk/commands/inspector.py +33 -4
  8. xpk/commands/inspector_test.py +142 -0
  9. xpk/commands/workload.py +22 -8
  10. xpk/commands/workload_test.py +70 -3
  11. xpk/core/blueprint/blueprint_generator.py +19 -8
  12. xpk/core/blueprint/testing/data/a3_ultra.yaml +3 -1
  13. xpk/core/blueprint/testing/data/a4.yaml +3 -1
  14. xpk/core/capacity.py +37 -17
  15. xpk/core/capacity_test.py +66 -1
  16. xpk/core/cluster.py +10 -10
  17. xpk/core/cluster_private.py +3 -3
  18. xpk/core/cluster_test.py +29 -2
  19. xpk/core/docker_container.py +31 -24
  20. xpk/core/docker_manager.py +4 -4
  21. xpk/core/docker_resources.py +4 -1
  22. xpk/core/kueue_manager.py +6 -8
  23. xpk/core/kueue_manager_test.py +4 -5
  24. xpk/core/nap.py +14 -3
  25. xpk/core/nodepool.py +46 -13
  26. xpk/core/nodepool_test.py +143 -8
  27. xpk/core/remote_state/fuse_remote_state.py +1 -1
  28. xpk/core/scheduling.py +4 -1
  29. xpk/core/scheduling_test.py +1 -1
  30. xpk/core/system_characteristics.py +6 -0
  31. xpk/core/telemetry.py +11 -1
  32. xpk/core/telemetry_test.py +39 -0
  33. xpk/core/testing/commands_tester.py +26 -0
  34. xpk/core/testing/commands_tester_test.py +20 -1
  35. xpk/core/workload_decorators/rdma_decorator.py +9 -0
  36. xpk/parser/cluster.py +11 -1
  37. xpk/parser/cluster_test.py +59 -1
  38. xpk/parser/common.py +11 -0
  39. xpk/parser/storage.py +3 -3
  40. xpk/utils/console.py +1 -1
  41. xpk/utils/feature_flags.py +7 -3
  42. {xpk-1.0.0.dist-info → xpk-1.1.0.dist-info}/METADATA +37 -21
  43. {xpk-1.0.0.dist-info → xpk-1.1.0.dist-info}/RECORD +47 -54
  44. xpk-1.1.0.dist-info/top_level.txt +1 -0
  45. integration/README.md +0 -19
  46. integration/__init__.py +0 -15
  47. integration/docker_manager_test.py +0 -102
  48. integration/gcluster_a3mega_test.py +0 -215
  49. integration/gcluster_a3ultra_test.py +0 -187
  50. integration/gcluster_a4_test.py +0 -187
  51. integration/gcluster_test.py +0 -107
  52. xpk/utils/user_input.py +0 -48
  53. xpk/utils/user_input_test.py +0 -92
  54. xpk-1.0.0.dist-info/top_level.txt +0 -2
  55. {xpk-1.0.0.dist-info → xpk-1.1.0.dist-info}/WHEEL +0 -0
  56. {xpk-1.0.0.dist-info → xpk-1.1.0.dist-info}/entry_points.txt +0 -0
  57. {xpk-1.0.0.dist-info → xpk-1.1.0.dist-info}/licenses/LICENSE +0 -0
@@ -1,187 +0,0 @@
1
- """
2
- Copyright 2024 Google LLC
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- https://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- """
16
-
17
- import os
18
- import shutil
19
-
20
- import pytest
21
-
22
- from xpk.commands.cluster_gcluster import get_unique_name
23
- from xpk.core.blueprint.blueprint_generator import BlueprintGenerator
24
- from xpk.core.capacity import CapacityType
25
- from xpk.core.docker_manager import DockerManager
26
- from xpk.core.gcluster_manager import GclusterManager
27
- from xpk.utils.versions import ReleaseChannel
28
-
29
- ctk_gcloud_cfg = os.getenv("GCLOUD_CFG_PATH")
30
- project_id = os.getenv("PROJECT_ID")
31
- region = os.getenv("REGION")
32
- zone = os.getenv("ZONE")
33
- auth_cidr = os.getenv("AUTH_CIDR")
34
- cluster_name = os.getenv("A3_ULTRA_TEST_CLUSTER_NAME")
35
- release_channel = os.getenv("RELEASE_CHANNEL")
36
- cluster_version = os.getenv("CLUSTER_VERSION")
37
-
38
-
39
- @pytest.fixture(name="setup_tests")
40
- def prepare_test():
41
- pwd = os.getcwd()
42
- docker_path = os.path.join(pwd, "xpk_test_docker_dir")
43
- bp_path = os.path.join(pwd, "xpk_test_bp_dir")
44
- if not os.path.exists(docker_path):
45
- os.makedirs(docker_path)
46
- if not os.path.exists(bp_path):
47
- os.makedirs(bp_path)
48
- yield (docker_path, bp_path)
49
- shutil.rmtree(docker_path)
50
- shutil.rmtree(bp_path)
51
-
52
-
53
- @pytest.mark.skip(
54
- reason=(
55
- "This test requires A3 capacity, therefore it should not be run on each"
56
- " build. Please invoke it manually if needed. "
57
- )
58
- )
59
- def test_create_a3_ultra_deployment_files(setup_tests):
60
- assert project_id is not None
61
- assert region is not None
62
- assert zone is not None
63
- assert auth_cidr is not None
64
- assert ctk_gcloud_cfg is not None
65
- assert cluster_name is not None
66
- assert release_channel is not None
67
- assert cluster_version is not None
68
- docker_path, bp_path = setup_tests[0], setup_tests[1]
69
- blueprint_name = f"{cluster_name}-a3-ultra-xpk"
70
-
71
- docker_manager = DockerManager(
72
- gcloud_cfg_path=ctk_gcloud_cfg, working_dir=docker_path
73
- )
74
- docker_manager.initialize()
75
- prefix = f"{project_id}-{region}".lower()
76
- bpm = BlueprintGenerator(storage_path=bp_path)
77
- a3_mega_blueprint = bpm.generate_a3_ultra_blueprint(
78
- cluster_name=cluster_name,
79
- blueprint_name=blueprint_name,
80
- region=region,
81
- project_id=project_id,
82
- auth_cidr=auth_cidr,
83
- zone=zone,
84
- reservation="foo",
85
- num_nodes=1,
86
- system_node_pool_machine_type="e2-standard-16",
87
- prefix=prefix,
88
- release_channel=ReleaseChannel(release_channel),
89
- cluster_version=cluster_version,
90
- )
91
- blueprint_test_path = os.path.join(bp_path, prefix, f"{blueprint_name}.yaml")
92
- blueprint_deps_test_path = os.path.join(bp_path, blueprint_name)
93
- assert a3_mega_blueprint.blueprint_file == blueprint_test_path
94
- assert a3_mega_blueprint.blueprint_dependencies == blueprint_deps_test_path
95
-
96
- assert os.path.isfile(blueprint_test_path)
97
- assert os.path.isdir(blueprint_deps_test_path)
98
- assert os.path.isfile(
99
- os.path.join(blueprint_deps_test_path, "mlgru-disable.yaml")
100
- )
101
- assert os.path.isfile(
102
- os.path.join(blueprint_deps_test_path, "nccl-installer.yaml")
103
- )
104
- gcluster_manager = GclusterManager(
105
- gcluster_command_runner=docker_manager, remote_state_client=None
106
- )
107
-
108
- staged_bp_path = gcluster_manager.stage_files(
109
- blueprint_file=a3_mega_blueprint.blueprint_file,
110
- blueprint_dependencies=a3_mega_blueprint.blueprint_dependencies,
111
- prefix=prefix,
112
- )
113
- assert staged_bp_path == os.path.join(
114
- "/out/uploads", prefix, f"{blueprint_name}.yaml"
115
- )
116
- unique_name = get_unique_name(project_id, region, zone)
117
- gcluster_manager.deploy(
118
- blueprint_path=staged_bp_path, deployment_name=unique_name, dry_run=True
119
- )
120
-
121
-
122
- @pytest.mark.skip(
123
- reason=(
124
- "This test requires A3 capacity, therefore it should not be run on each"
125
- " build. Please invoke it manually if needed. "
126
- )
127
- )
128
- def test_create_a3_ultra_deployment(setup_tests):
129
- assert project_id is not None
130
- assert region is not None
131
- assert zone is not None
132
- assert auth_cidr is not None
133
- assert ctk_gcloud_cfg is not None
134
- assert cluster_name is not None
135
- assert release_channel is not None
136
- assert cluster_version is not None
137
- docker_path, bp_path = setup_tests[0], setup_tests[1]
138
- blueprint_name = f"{cluster_name}-a3-ultra-xpk"
139
-
140
- docker_manager = DockerManager(
141
- gcloud_cfg_path=ctk_gcloud_cfg, working_dir=docker_path
142
- )
143
- docker_manager.initialize()
144
-
145
- bpm = BlueprintGenerator(storage_path=bp_path)
146
- a3_mega_blueprint = bpm.generate_a3_ultra_blueprint(
147
- cluster_name=cluster_name,
148
- blueprint_name=blueprint_name,
149
- region=region,
150
- project_id=project_id,
151
- auth_cidr=auth_cidr,
152
- zone=zone,
153
- capacity_type=CapacityType.SPOT,
154
- num_nodes=1,
155
- system_node_pool_machine_type="e2-standard-16",
156
- release_channel=ReleaseChannel(release_channel),
157
- cluster_version=cluster_version,
158
- )
159
- blueprint_test_path = os.path.join(bp_path, f"{blueprint_name}.yaml")
160
- blueprint_deps_test_path = os.path.join(bp_path, blueprint_name)
161
-
162
- assert a3_mega_blueprint.blueprint_file == blueprint_test_path
163
- assert a3_mega_blueprint.blueprint_dependencies == blueprint_deps_test_path
164
-
165
- assert os.path.isfile(blueprint_test_path)
166
- assert os.path.isdir(blueprint_deps_test_path)
167
- assert os.path.isfile(
168
- os.path.join(blueprint_deps_test_path, "mlgru-disable.yaml")
169
- )
170
- assert os.path.isfile(
171
- os.path.join(blueprint_deps_test_path, "nccl-installer.yaml")
172
- )
173
- gcluster_manager = GclusterManager(
174
- gcluster_command_runner=docker_manager, remote_state_client=None
175
- )
176
-
177
- staged_bp_path = gcluster_manager.stage_files(
178
- blueprint_file=a3_mega_blueprint.blueprint_file,
179
- blueprint_dependencies=a3_mega_blueprint.blueprint_dependencies,
180
- )
181
-
182
- gcluster_manager.deploy(
183
- blueprint_path=staged_bp_path, deployment_name=blueprint_name
184
- )
185
-
186
- # cleanup part
187
- gcluster_manager.destroy_deployment(deployment_name=blueprint_name)
@@ -1,187 +0,0 @@
1
- """
2
- Copyright 2024 Google LLC
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- https://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- """
16
-
17
- import os
18
- import shutil
19
-
20
- import pytest
21
-
22
- from xpk.commands.cluster_gcluster import get_unique_name
23
- from xpk.core.blueprint.blueprint_generator import BlueprintGenerator
24
- from xpk.core.capacity import CapacityType
25
- from xpk.core.docker_manager import DockerManager
26
- from xpk.core.gcluster_manager import GclusterManager
27
- from xpk.utils.versions import ReleaseChannel
28
-
29
- ctk_gcloud_cfg = os.getenv("GCLOUD_CFG_PATH")
30
- project_id = os.getenv("PROJECT_ID")
31
- region = os.getenv("REGION")
32
- zone = os.getenv("ZONE")
33
- auth_cidr = os.getenv("AUTH_CIDR")
34
- cluster_name = os.getenv("A4_TEST_CLUSTER_NAME")
35
- release_channel = os.getenv("RELEASE_CHANNEL")
36
- cluster_version = os.getenv("CLUSTER_VERSION")
37
-
38
-
39
- @pytest.fixture(name="setup_tests")
40
- def prepare_test():
41
- pwd = os.getcwd()
42
- docker_path = os.path.join(pwd, "xpk_test_docker_dir")
43
- bp_path = os.path.join(pwd, "xpk_test_bp_dir")
44
- if not os.path.exists(docker_path):
45
- os.makedirs(docker_path)
46
- if not os.path.exists(bp_path):
47
- os.makedirs(bp_path)
48
- yield (docker_path, bp_path)
49
- shutil.rmtree(docker_path)
50
- shutil.rmtree(bp_path)
51
-
52
-
53
- @pytest.mark.skip(
54
- reason=(
55
- "This test requires A4 capacity, therefore it should not be run on each"
56
- " build. Please invoke it manually if needed. "
57
- )
58
- )
59
- def test_create_a4_deployment_files(setup_tests):
60
- assert project_id is not None
61
- assert region is not None
62
- assert zone is not None
63
- assert auth_cidr is not None
64
- assert ctk_gcloud_cfg is not None
65
- assert cluster_name is not None
66
- assert release_channel is not None
67
- assert cluster_version is not None
68
- docker_path, bp_path = setup_tests[0], setup_tests[1]
69
- blueprint_name = f"{cluster_name}-a4-xpk"
70
-
71
- docker_manager = DockerManager(
72
- gcloud_cfg_path=ctk_gcloud_cfg, working_dir=docker_path
73
- )
74
- docker_manager.initialize()
75
- prefix = f"{project_id}-{region}".lower()
76
- bpm = BlueprintGenerator(storage_path=bp_path)
77
- a4_blueprint = bpm.generate_a4_blueprint(
78
- cluster_name=cluster_name,
79
- blueprint_name=blueprint_name,
80
- region=region,
81
- project_id=project_id,
82
- auth_cidr=auth_cidr,
83
- zone=zone,
84
- reservation="foo",
85
- num_nodes=1,
86
- system_node_pool_machine_type="e2-standard-16",
87
- prefix=prefix,
88
- release_channel=ReleaseChannel(release_channel),
89
- cluster_version=cluster_version,
90
- )
91
- blueprint_test_path = os.path.join(bp_path, prefix, f"{blueprint_name}.yaml")
92
- blueprint_deps_test_path = os.path.join(bp_path, blueprint_name)
93
- assert a4_blueprint.blueprint_file == blueprint_test_path
94
- assert a4_blueprint.blueprint_dependencies == blueprint_deps_test_path
95
-
96
- assert os.path.isfile(blueprint_test_path)
97
- assert os.path.isdir(blueprint_deps_test_path)
98
- assert os.path.isfile(
99
- os.path.join(blueprint_deps_test_path, "mlgru-disable.yaml")
100
- )
101
- assert os.path.isfile(
102
- os.path.join(blueprint_deps_test_path, "nccl-installer.yaml")
103
- )
104
- gcluster_manager = GclusterManager(
105
- gcluster_command_runner=docker_manager, remote_state_client=None
106
- )
107
-
108
- staged_bp_path = gcluster_manager.stage_files(
109
- blueprint_file=a4_blueprint.blueprint_file,
110
- blueprint_dependencies=a4_blueprint.blueprint_dependencies,
111
- prefix=prefix,
112
- )
113
- assert staged_bp_path == os.path.join(
114
- "/out/uploads", prefix, f"{blueprint_name}.yaml"
115
- )
116
- unique_name = get_unique_name(project_id, region, zone)
117
- gcluster_manager.deploy(
118
- blueprint_path=staged_bp_path, deployment_name=unique_name, dry_run=True
119
- )
120
-
121
-
122
- @pytest.mark.skip(
123
- reason=(
124
- "This test requires A4 capacity, therefore it should not be run on each"
125
- " build. Please invoke it manually if needed. "
126
- )
127
- )
128
- def test_create_a4_deployment(setup_tests):
129
- assert project_id is not None
130
- assert region is not None
131
- assert zone is not None
132
- assert auth_cidr is not None
133
- assert ctk_gcloud_cfg is not None
134
- assert cluster_name is not None
135
- assert release_channel is not None
136
- assert cluster_version is not None
137
- docker_path, bp_path = setup_tests[0], setup_tests[1]
138
- blueprint_name = f"{cluster_name}-a4-xpk"
139
-
140
- docker_manager = DockerManager(
141
- gcloud_cfg_path=ctk_gcloud_cfg, working_dir=docker_path
142
- )
143
- docker_manager.initialize()
144
-
145
- bpm = BlueprintGenerator(storage_path=bp_path)
146
- a4_blueprint = bpm.generate_a4_blueprint(
147
- cluster_name=cluster_name,
148
- blueprint_name=blueprint_name,
149
- region=region,
150
- project_id=project_id,
151
- auth_cidr=auth_cidr,
152
- zone=zone,
153
- capacity_type=CapacityType.SPOT,
154
- num_nodes=1,
155
- system_node_pool_machine_type="e2-standard-16",
156
- release_channel=ReleaseChannel(release_channel),
157
- cluster_version=cluster_version,
158
- )
159
- blueprint_test_path = os.path.join(bp_path, f"{blueprint_name}.yaml")
160
- blueprint_deps_test_path = os.path.join(bp_path, blueprint_name)
161
-
162
- assert a4_blueprint.blueprint_file == blueprint_test_path
163
- assert a4_blueprint.blueprint_dependencies == blueprint_deps_test_path
164
-
165
- assert os.path.isfile(blueprint_test_path)
166
- assert os.path.isdir(blueprint_deps_test_path)
167
- assert os.path.isfile(
168
- os.path.join(blueprint_deps_test_path, "mlgru-disable.yaml")
169
- )
170
- assert os.path.isfile(
171
- os.path.join(blueprint_deps_test_path, "nccl-installer.yaml")
172
- )
173
- gcluster_manager = GclusterManager(
174
- gcluster_command_runner=docker_manager, remote_state_client=None
175
- )
176
-
177
- staged_bp_path = gcluster_manager.stage_files(
178
- blueprint_file=a4_blueprint.blueprint_file,
179
- blueprint_dependencies=a4_blueprint.blueprint_dependencies,
180
- )
181
-
182
- gcluster_manager.deploy(
183
- blueprint_path=staged_bp_path, deployment_name=blueprint_name
184
- )
185
-
186
- # cleanup part
187
- gcluster_manager.destroy_deployment(deployment_name=blueprint_name)
@@ -1,107 +0,0 @@
1
- """
2
- Copyright 2024 Google LLC
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- https://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- """
16
-
17
- from xpk.core.docker_manager import DockerManager
18
- from xpk.core.gcluster_manager import GclusterManager
19
- from xpk.core.blueprint.blueprint_generator import BlueprintGenerator
20
- import os
21
- import pytest
22
- import shutil
23
-
24
- ctk_gcloud_cfg = os.getenv("GCLOUD_CFG_PATH")
25
- project_id = os.getenv("PROJECT_ID")
26
- region = os.getenv("REGION")
27
- zone = os.getenv("ZONE")
28
- auth_cidr = os.getenv("AUTH_CIDR")
29
- cluster_name = os.getenv("GKE_ML_TEST_CLUSTER_NAME")
30
-
31
- uploads_dir = "uploads"
32
-
33
-
34
- def prepare_test(docker_path: str, bp_path: str) -> None:
35
- if not os.path.exists(docker_path):
36
- os.makedirs(docker_path)
37
- if not os.path.exists(bp_path):
38
- os.makedirs(bp_path)
39
-
40
-
41
- @pytest.mark.skip(reason="Credentails not working. Skipping for now")
42
- def test_create_deployment():
43
- assert project_id is not None
44
- assert region is not None
45
- assert zone is not None
46
- assert auth_cidr is not None
47
- assert ctk_gcloud_cfg is not None
48
- assert cluster_name is not None
49
-
50
- pwd = os.getcwd()
51
- test_docker_working_dir = os.path.join(
52
- pwd, "xpkclusters/tests/xpk_test_docker_dir"
53
- )
54
- test_bp_dir = os.path.join(pwd, "xpkclusters/tests/xpk_test_bp_dir")
55
- prepare_test(test_docker_working_dir, test_bp_dir)
56
- blueprint_name = "my-test-blueprint"
57
- prefix = "prefix"
58
-
59
- docker_manager = DockerManager(
60
- gcloud_cfg_path=ctk_gcloud_cfg, working_dir=test_docker_working_dir
61
- )
62
- docker_manager.initialize()
63
-
64
- bpm = BlueprintGenerator(storage_path=test_bp_dir)
65
- ml_gke_blueprint = bpm.generate_gke_ml_blueprint(
66
- cluster_name=cluster_name,
67
- blueprint_name=blueprint_name,
68
- prefix=prefix,
69
- region=region,
70
- project_id=project_id,
71
- auth_cidr=auth_cidr,
72
- )
73
- blueprint_test_path = os.path.join(
74
- test_bp_dir, prefix, f"{blueprint_name}.yaml"
75
- )
76
- # there are no files in ghcp stage for this blueprint
77
- blueprint_deps_test_path = ""
78
-
79
- assert ml_gke_blueprint.blueprint_file == blueprint_test_path
80
- assert ml_gke_blueprint.blueprint_dependencies == blueprint_deps_test_path
81
-
82
- assert os.path.exists(blueprint_test_path)
83
-
84
- gcluster_manager = GclusterManager(
85
- gcluster_command_runner=docker_manager, remote_state_client=None
86
- )
87
-
88
- staged_bp_path = gcluster_manager.stage_files(
89
- blueprint_file=ml_gke_blueprint.blueprint_file,
90
- blueprint_dependencies=ml_gke_blueprint.blueprint_dependencies,
91
- prefix=prefix,
92
- )
93
-
94
- assert staged_bp_path == os.path.join(
95
- "/out", uploads_dir, prefix, f"{blueprint_name}.yaml"
96
- )
97
-
98
- gcluster_manager.deploy(
99
- blueprint_path=staged_bp_path,
100
- deployment_name=blueprint_name,
101
- prefix=prefix,
102
- )
103
- gcluster_manager.destroy_deployment(
104
- deployment_name=blueprint_name, prefix=prefix
105
- )
106
- shutil.rmtree(test_docker_working_dir)
107
- shutil.rmtree(test_bp_dir)
xpk/utils/user_input.py DELETED
@@ -1,48 +0,0 @@
1
- """
2
- Copyright 2025 Google LLC
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- https://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- """
16
-
17
- from typing import Literal
18
-
19
- from .console import xpk_print
20
- from .execution_context import is_quiet
21
-
22
-
23
- def ask_for_user_consent(
24
- question: str, default_option: Literal["Y", "N"] = "N"
25
- ) -> bool:
26
- """Prompts user with the given question, asking for a yes/no answer and returns a relevant boolean.
27
- Important: immediatelly returns `True` in quiet mode!
28
-
29
- Example prompt for `question='Continue?'`: `[XPK] Continue? (y/N): `.
30
-
31
- Args:
32
- question: The question to ask the user.
33
- default_option: Option to use when user response is empty.
34
- """
35
- if is_quiet():
36
- return True
37
-
38
- options = "y/N" if default_option == "N" else "Y/n"
39
- prompt = f"[XPK] {question} ({options}): "
40
-
41
- while True:
42
- user_input = input(prompt) or default_option
43
- if user_input.lower() in ["yes", "y"]:
44
- return True
45
- elif user_input.lower() in ["no", "n"]:
46
- return False
47
- else:
48
- xpk_print("Invalid input. Please enter: yes/no/y/n.")
@@ -1,92 +0,0 @@
1
- """
2
- Copyright 2025 Google LLC
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- https://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- """
16
-
17
- from unittest.mock import MagicMock, patch
18
- import pytest
19
- from pytest_mock import MockerFixture
20
-
21
- from xpk.utils.user_input import ask_for_user_consent
22
-
23
-
24
- @pytest.fixture(autouse=True)
25
- def mock_is_quiet(mocker: MockerFixture):
26
- return mocker.patch("xpk.utils.user_input.is_quiet", return_value=False)
27
-
28
-
29
- @pytest.mark.parametrize(
30
- "user_input,expected",
31
- [
32
- ("yes", True),
33
- ("y", True),
34
- ("Y", True),
35
- ("Yes", True),
36
- ("YES", True),
37
- ("no", False),
38
- ("n", False),
39
- ("N", False),
40
- ("No", False),
41
- ("NO", False),
42
- ],
43
- )
44
- @patch("xpk.utils.user_input.input")
45
- def test_ask_for_user_consent(mock_input: MagicMock, user_input, expected):
46
- mock_input.return_value = user_input
47
-
48
- assert ask_for_user_consent("Test question?") is expected
49
-
50
-
51
- def fake_input_factory(user_inputs: list[str]):
52
- def fake_input(prompt: str) -> str:
53
- return user_inputs.pop(0)
54
-
55
- return fake_input
56
-
57
-
58
- @patch("xpk.utils.user_input.input", wraps=fake_input_factory(["invalid", "y"]))
59
- def test_ask_for_user_consent_invalid_input(mock_input: MagicMock):
60
- agreed = ask_for_user_consent("Test question?")
61
-
62
- assert agreed is True
63
- assert mock_input.call_count == 2
64
-
65
-
66
- @patch("xpk.utils.user_input.input", return_value="")
67
- def test_ask_for_user_consent_default_No(mock_input: MagicMock):
68
- agreed = ask_for_user_consent("Test question?", default_option="N")
69
-
70
- assert agreed is False
71
- mock_input.assert_called_once_with("[XPK] Test question? (y/N): ")
72
-
73
-
74
- @patch("xpk.utils.user_input.input", return_value="")
75
- def test_ask_for_user_consent_default_Yes(mock_input: MagicMock):
76
- agreed = ask_for_user_consent("Test question?", default_option="Y")
77
-
78
- assert agreed is True
79
- mock_input.assert_called_once_with("[XPK] Test question? (Y/n): ")
80
-
81
-
82
- @patch("xpk.utils.user_input.input")
83
- def test_ask_for_user_consent_with_quiet_mode_always_agrees(
84
- mock_input: MagicMock,
85
- mock_is_quiet: MagicMock,
86
- ):
87
- mock_is_quiet.return_value = True
88
-
89
- agreed = ask_for_user_consent("Test question?", default_option="N")
90
-
91
- assert agreed is True
92
- mock_input.assert_not_called()
@@ -1,2 +0,0 @@
1
- integration
2
- xpk
File without changes