|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +"""Smoke tests for the `kci-dev storage` subcommand. |
| 5 | +
|
| 6 | +These exercise the Click layer with mocks to ensure CLI arguments are |
| 7 | +forwarded to the underlying library functions exactly as expected. |
| 8 | +""" |
| 9 | + |
| 10 | +from unittest.mock import patch |
| 11 | + |
| 12 | +from click.testing import CliRunner |
| 13 | + |
| 14 | +from kcidev.subcommands.storage import storage |
| 15 | + |
| 16 | + |
| 17 | +def _invoke(args, obj=None): |
| 18 | + runner = CliRunner() |
| 19 | + return runner.invoke(storage, args, obj=obj or {}) |
| 20 | + |
| 21 | + |
| 22 | +class TestStorageUpload: |
| 23 | + def test_cli_flags_forwarded_to_resolve_and_upload(self, tmp_path): |
| 24 | + local_file = tmp_path / "log.txt" |
| 25 | + local_file.write_text("hello") |
| 26 | + |
| 27 | + with patch( |
| 28 | + "kcidev.subcommands.storage.resolve_storage_config", |
| 29 | + return_value=("https://storage.example.com", "tok-abc"), |
| 30 | + ) as mock_resolve, patch( |
| 31 | + "kcidev.subcommands.storage.upload_file", |
| 32 | + return_value="ok", |
| 33 | + ) as mock_upload: |
| 34 | + result = _invoke( |
| 35 | + [ |
| 36 | + "upload", |
| 37 | + "--storage-url", |
| 38 | + "https://storage.example.com", |
| 39 | + "--storage-token", |
| 40 | + "tok-abc", |
| 41 | + "--path", |
| 42 | + "myci/build-123", |
| 43 | + str(local_file), |
| 44 | + ], |
| 45 | + ) |
| 46 | + |
| 47 | + assert result.exit_code == 0, result.output |
| 48 | + mock_resolve.assert_called_once_with( |
| 49 | + None, None, "https://storage.example.com", "tok-abc" |
| 50 | + ) |
| 51 | + mock_upload.assert_called_once_with( |
| 52 | + "https://storage.example.com", |
| 53 | + "tok-abc", |
| 54 | + "myci/build-123", |
| 55 | + str(local_file), |
| 56 | + ) |
| 57 | + |
| 58 | + def test_multiple_files_each_uploaded(self, tmp_path): |
| 59 | + f1 = tmp_path / "a.txt" |
| 60 | + f2 = tmp_path / "b.txt" |
| 61 | + f1.write_text("a") |
| 62 | + f2.write_text("b") |
| 63 | + |
| 64 | + with patch( |
| 65 | + "kcidev.subcommands.storage.resolve_storage_config", |
| 66 | + return_value=("https://s", "t"), |
| 67 | + ), patch( |
| 68 | + "kcidev.subcommands.storage.upload_file", |
| 69 | + return_value="ok", |
| 70 | + ) as mock_upload: |
| 71 | + result = _invoke( |
| 72 | + [ |
| 73 | + "upload", |
| 74 | + "--storage-url", |
| 75 | + "https://s", |
| 76 | + "--storage-token", |
| 77 | + "t", |
| 78 | + "--path", |
| 79 | + "myci/build-1", |
| 80 | + str(f1), |
| 81 | + str(f2), |
| 82 | + ], |
| 83 | + ) |
| 84 | + |
| 85 | + assert result.exit_code == 0, result.output |
| 86 | + assert mock_upload.call_count == 2 |
| 87 | + mock_upload.assert_any_call("https://s", "t", "myci/build-1", str(f1)) |
| 88 | + mock_upload.assert_any_call("https://s", "t", "myci/build-1", str(f2)) |
| 89 | + |
| 90 | + def test_context_cfg_and_instance_forwarded_to_resolve(self, tmp_path): |
| 91 | + local_file = tmp_path / "log.txt" |
| 92 | + local_file.write_text("hello") |
| 93 | + |
| 94 | + cfg = {"staging": {"storage_url": "https://s", "storage_token": "t"}} |
| 95 | + |
| 96 | + with patch( |
| 97 | + "kcidev.subcommands.storage.resolve_storage_config", |
| 98 | + return_value=("https://s", "t"), |
| 99 | + ) as mock_resolve, patch( |
| 100 | + "kcidev.subcommands.storage.upload_file", |
| 101 | + return_value="ok", |
| 102 | + ): |
| 103 | + result = _invoke( |
| 104 | + [ |
| 105 | + "upload", |
| 106 | + "--path", |
| 107 | + "myci/build-9", |
| 108 | + str(local_file), |
| 109 | + ], |
| 110 | + obj={"CFG": cfg, "INSTANCE": "staging"}, |
| 111 | + ) |
| 112 | + |
| 113 | + assert result.exit_code == 0, result.output |
| 114 | + mock_resolve.assert_called_once_with(cfg, "staging", None, None) |
| 115 | + |
| 116 | + def test_path_is_required(self, tmp_path): |
| 117 | + local_file = tmp_path / "log.txt" |
| 118 | + local_file.write_text("hello") |
| 119 | + |
| 120 | + result = _invoke(["upload", str(local_file)]) |
| 121 | + assert result.exit_code != 0 |
| 122 | + assert "--path" in result.output |
| 123 | + |
| 124 | + def test_files_argument_required(self): |
| 125 | + result = _invoke(["upload", "--path", "myci/build-1"]) |
| 126 | + assert result.exit_code != 0 |
| 127 | + |
| 128 | + |
| 129 | +class TestStorageCheckauth: |
| 130 | + def test_cli_flags_forwarded_to_resolve_and_check_auth(self): |
| 131 | + with patch( |
| 132 | + "kcidev.subcommands.storage.resolve_storage_config", |
| 133 | + return_value=("https://storage.example.com", "tok-abc"), |
| 134 | + ) as mock_resolve, patch( |
| 135 | + "kcidev.subcommands.storage.check_auth", |
| 136 | + return_value="valid", |
| 137 | + ) as mock_check: |
| 138 | + result = _invoke( |
| 139 | + [ |
| 140 | + "checkauth", |
| 141 | + "--storage-url", |
| 142 | + "https://storage.example.com", |
| 143 | + "--storage-token", |
| 144 | + "tok-abc", |
| 145 | + ], |
| 146 | + ) |
| 147 | + |
| 148 | + assert result.exit_code == 0, result.output |
| 149 | + mock_resolve.assert_called_once_with( |
| 150 | + None, None, "https://storage.example.com", "tok-abc" |
| 151 | + ) |
| 152 | + mock_check.assert_called_once_with("https://storage.example.com", "tok-abc") |
| 153 | + |
| 154 | + def test_no_flags_passes_none_to_resolve(self): |
| 155 | + cfg = {"staging": {"storage_url": "https://s", "storage_token": "t"}} |
| 156 | + |
| 157 | + with patch( |
| 158 | + "kcidev.subcommands.storage.resolve_storage_config", |
| 159 | + return_value=("https://s", "t"), |
| 160 | + ) as mock_resolve, patch( |
| 161 | + "kcidev.subcommands.storage.check_auth", |
| 162 | + return_value="valid", |
| 163 | + ) as mock_check: |
| 164 | + result = _invoke( |
| 165 | + ["checkauth"], |
| 166 | + obj={"CFG": cfg, "INSTANCE": "staging"}, |
| 167 | + ) |
| 168 | + |
| 169 | + assert result.exit_code == 0, result.output |
| 170 | + mock_resolve.assert_called_once_with(cfg, "staging", None, None) |
| 171 | + mock_check.assert_called_once_with("https://s", "t") |
0 commit comments