| 1 | from argparse import Namespace |
| 2 | from unittest.mock import MagicMock, patch |
| 3 | |
| 4 | import pytest |
| 5 | from llama_stack.distribution.datatypes import BuildConfig |
| 6 | from llama_stack.cli.stack.build import StackBuild |
| 7 | |
| 8 | |
| 9 | # temporary while we make the tests work |
| 10 | pytest.skip(allow_module_level=True) |
| 11 | |
| 12 | |
| 13 | @pytest.fixture |
| 14 | def stack_build(): |
| 15 | parser = MagicMock() |
| 16 | subparsers = MagicMock() |
| 17 | return StackBuild(subparsers) |
| 18 | |
| 19 | |
| 20 | def test_stack_build_initialization(stack_build): |
| 21 | assert stack_build.parser is not None |
| 22 | assert stack_build.parser.set_defaults.called_once_with( |
| 23 | func=stack_build._run_stack_build_command |
| 24 | ) |
| 25 | |
| 26 | |
| 27 | @patch("llama_stack.distribution.build.build_image") |
| 28 | def test_run_stack_build_command_with_config( |
| 29 | mock_build_image, mock_build_config, stack_build |
| 30 | ): |
| 31 | args = Namespace( |
| 32 | config="test_config.yaml", |
| 33 | template=None, |
| 34 | list_templates=False, |
| 35 | name=None, |
| 36 | image_type="conda", |
| 37 | ) |
| 38 | |
| 39 | with patch("builtins.open", MagicMock()): |
| 40 | with patch("yaml.safe_load") as mock_yaml_load: |
| 41 | mock_yaml_load.return_value = {"name": "test_build", "image_type": "conda"} |
| 42 | mock_build_config.return_value = MagicMock() |
| 43 | |
| 44 | stack_build._run_stack_build_command(args) |
| 45 | |
| 46 | mock_build_config.assert_called_once() |
| 47 | mock_build_image.assert_called_once() |
| 48 | |
| 49 | |
| 50 | @patch("llama_stack.cli.table.print_table") |
| 51 | def test_run_stack_build_command_list_templates(mock_print_table, stack_build): |
| 52 | args = Namespace(list_templates=True) |
| 53 | |
| 54 | stack_build._run_stack_build_command(args) |
| 55 | |
| 56 | mock_print_table.assert_called_once() |
| 57 | |
| 58 | |
| 59 | @patch("prompt_toolkit.prompt") |
| 60 | @patch("llama_stack.distribution.datatypes.BuildConfig") |
| 61 | @patch("llama_stack.distribution.build.build_image") |
| 62 | def test_run_stack_build_command_interactive( |
| 63 | mock_build_image, mock_build_config, mock_prompt, stack_build |
| 64 | ): |
| 65 | args = Namespace( |
| 66 | config=None, template=None, list_templates=False, name=None, image_type=None |
| 67 | ) |
| 68 | |
| 69 | mock_prompt.side_effect = [ |
| 70 | "test_name", |
| 71 | "conda", |
| 72 | "meta-reference", |
| 73 | "test description", |
| 74 | ] |
| 75 | mock_build_config.return_value = MagicMock() |
| 76 | |
| 77 | stack_build._run_stack_build_command(args) |
| 78 | |
| 79 | assert mock_prompt.call_count == 4 |
| 80 | mock_build_config.assert_called_once() |
| 81 | mock_build_image.assert_called_once() |
| 82 | |
| 83 | |
| 84 | @patch("llama_stack.distribution.datatypes.BuildConfig") |
| 85 | @patch("llama_stack.distribution.build.build_image") |
| 86 | def test_run_stack_build_command_with_template( |
| 87 | mock_build_image, mock_build_config, stack_build |
| 88 | ): |
| 89 | args = Namespace( |
| 90 | config=None, |
| 91 | template="test_template", |
| 92 | list_templates=False, |
| 93 | name="test_name", |
| 94 | image_type="docker", |
| 95 | ) |
| 96 | |
| 97 | with patch("builtins.open", MagicMock()): |
| 98 | with patch("yaml.safe_load") as mock_yaml_load: |
| 99 | mock_yaml_load.return_value = {"name": "test_build", "image_type": "conda"} |
| 100 | mock_build_config.return_value = MagicMock() |
| 101 | |
| 102 | stack_build._run_stack_build_command(args) |
| 103 | |
| 104 | mock_build_config.assert_called_once() |
| 105 | mock_build_image.assert_called_once() |