forked from opensearch-project/opensearch-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_run_build.py
161 lines (140 loc) · 8.22 KB
/
test_run_build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
import os
import tempfile
import unittest
from typing import Any
from unittest.mock import MagicMock, Mock, patch
import pytest
from manifests.input_manifest import InputManifest
from run_build import main
class TestRunBuild(unittest.TestCase):
@pytest.fixture(autouse=True)
def _capfd(self, capfd: Any) -> None:
self.capfd = capfd
@patch("argparse._sys.argv", ["run_build.py", "--help"])
def test_usage(self) -> None:
with self.assertRaises(SystemExit):
main()
out, _ = self.capfd.readouterr()
self.assertTrue(out.startswith("usage:"))
MANIFESTS = os.path.join(
os.path.dirname(__file__),
"..",
"manifests",
)
OPENSEARCH_MANIFEST = os.path.realpath(os.path.join(MANIFESTS, "templates", "opensearch", "1.x", "os-template-1.1.0.yml"))
OPENSEARCH_MANIFEST_1_2 = os.path.realpath(os.path.join(MANIFESTS, "templates", "opensearch", "1.x", "os-template-1.2.0.yml"))
@patch("argparse._sys.argv", ["run_build.py", OPENSEARCH_MANIFEST, "-p", "linux"])
@patch("run_build.Builders.builder_from", return_value=MagicMock())
@patch("run_build.BuildRecorder", return_value=MagicMock())
@patch("run_build.TemporaryDirectory")
def test_main_platform_linux(self, mock_temp: Mock, mock_recorder: Mock, mock_builder: Mock, *mocks: Any) -> None:
mock_temp.return_value.__enter__.return_value.name = tempfile.gettempdir()
main()
self.assertNotEqual(mock_builder.return_value.build.call_count, 0)
self.assertEqual(mock_builder.return_value.build.call_count, mock_builder.return_value.export_artifacts.call_count)
mock_recorder.return_value.write_manifest.assert_called()
@patch("argparse._sys.argv", ["run_build.py", OPENSEARCH_MANIFEST, "-p", "darwin"])
@patch("run_build.Builders.builder_from", return_value=MagicMock())
@patch("run_build.BuildRecorder", return_value=MagicMock())
@patch("run_build.TemporaryDirectory")
def test_main_platform_darwin(self, mock_temp: Mock, mock_recorder: Mock, mock_builder: Mock, *mocks: Any) -> None:
mock_temp.return_value.__enter__.return_value.name = tempfile.gettempdir()
main()
self.assertNotEqual(mock_builder.return_value.build.call_count, 0)
self.assertEqual(mock_builder.return_value.build.call_count, mock_builder.return_value.export_artifacts.call_count)
mock_recorder.return_value.write_manifest.assert_called()
@patch("argparse._sys.argv", ["run_build.py", OPENSEARCH_MANIFEST, "-p", "windows"])
@patch("run_build.Builders.builder_from", return_value=MagicMock())
@patch("run_build.BuildRecorder", return_value=MagicMock())
@patch("run_build.TemporaryDirectory")
def test_main_platform_windows(self, mock_temp: Mock, mock_recorder: Mock, mock_builder: Mock, *mocks: Any) -> None:
mock_temp.return_value.__enter__.return_value.name = tempfile.gettempdir()
main()
# excludes performance analyzer and k-nn
for call_args in mock_builder.call_args:
if len(call_args) > 1:
component = call_args[0]
self.assertNotIn("k-nn", component.name.lower())
self.assertNotIn("analyzer", component.name.lower())
self.assertNotEqual(mock_builder.call_count, 0)
self.assertEqual(mock_builder.return_value.build.call_count, mock_builder.call_count)
self.assertEqual(mock_builder.return_value.export_artifacts.call_count, mock_builder.call_count)
mock_recorder.return_value.write_manifest.assert_called()
OPENSEARCH_DASHBOARDS_MANIFEST = os.path.realpath(
os.path.join(
os.path.dirname(__file__),
"..",
"manifests",
"templates",
"opensearch-dashboards",
"1.x",
"osd-template-1.1.0.yml",
)
)
@patch("argparse._sys.argv", ["run_build.py", OPENSEARCH_DASHBOARDS_MANIFEST, "-a", "x64"])
@patch("run_build.Builders.builder_from", return_value=MagicMock())
@patch("run_build.BuildRecorder", return_value=MagicMock())
@patch("run_build.TemporaryDirectory")
def test_main_with_architecture(self, mock_temp: Mock, mock_recorder: Mock, mock_builder: Mock, *mocks: Any) -> None:
mock_temp.return_value.__enter__.return_value.name = tempfile.gettempdir()
main()
self.assertEqual(mock_builder.return_value.build.call_count, 11)
self.assertEqual(mock_builder.return_value.export_artifacts.call_count, 11)
mock_recorder.return_value.write_manifest.assert_called()
@patch("argparse._sys.argv", ["run_build.py", OPENSEARCH_DASHBOARDS_MANIFEST, "-p", "invalidplatform", "-a", "x64"])
def test_main_with_invalid_platform(self, *mocks: Any) -> None:
with self.assertRaises(SystemExit):
main()
@patch("argparse._sys.argv", ["run_build.py", OPENSEARCH_DASHBOARDS_MANIFEST, "-p", "linux", "-a", "invalidarchitecture"])
def test_main_with_invalid_architecture(self, *mocks: Any) -> None:
with self.assertRaises(SystemExit):
main()
@patch("os.path.exists", return_value=True)
@patch("argparse._sys.argv", ["run_build.py", OPENSEARCH_MANIFEST, "--lock"])
@patch("run_build.InputManifest.from_path", return_value=InputManifest.from_path(OPENSEARCH_MANIFEST))
@patch("run_build.InputManifest.stable", return_value=InputManifest.from_path(OPENSEARCH_MANIFEST))
@patch("run_build.InputManifest.to_file")
@patch("logging.info")
def test_main_manifest_lock_without_changes(self, mock_logging: Mock, mock_to_file: Mock, mock_stable: Mock, *mocks: Any) -> None:
main()
mock_stable.assert_called_with()
mock_to_file.assert_not_called()
mock_logging.assert_called_with(f"No changes since {self.OPENSEARCH_MANIFEST}.lock")
@patch("os.path.exists", return_value=True)
@patch("argparse._sys.argv", ["run_build.py", OPENSEARCH_MANIFEST, "--lock"])
@patch("run_build.InputManifest.from_path", return_value=InputManifest.from_path(OPENSEARCH_MANIFEST))
@patch("run_build.InputManifest.stable", return_value=InputManifest.from_path(OPENSEARCH_MANIFEST_1_2))
@patch("run_build.InputManifest.to_file")
@patch("logging.info")
def test_main_manifest_lock_with_changes(self, mock_logging: Mock, mock_to_file: Mock, mock_stable: Mock, *mocks: Any) -> None:
main()
mock_stable.assert_called_with()
mock_to_file.assert_called_with(self.OPENSEARCH_MANIFEST + ".lock")
mock_logging.assert_called_with(f"Updating {self.OPENSEARCH_MANIFEST}.lock")
@patch("os.path.exists", return_value=False)
@patch("argparse._sys.argv", ["run_build.py", OPENSEARCH_MANIFEST, "--lock"])
@patch("run_build.InputManifest.from_path", return_value=InputManifest.from_path(OPENSEARCH_MANIFEST))
@patch("run_build.InputManifest.stable", return_value=InputManifest.from_path(OPENSEARCH_MANIFEST_1_2))
@patch("run_build.InputManifest.to_file")
@patch("logging.info")
def test_main_manifest_new_lock(self, mock_logging: Mock, mock_to_file: Mock, mock_stable: Mock, *mocks: Any) -> None:
main()
mock_stable.assert_called_with()
mock_to_file.assert_called_with(self.OPENSEARCH_MANIFEST + ".lock")
mock_logging.assert_called_with(f"Creating {self.OPENSEARCH_MANIFEST}.lock")
@patch("os.path.exists", return_value=False)
@patch("argparse._sys.argv", ["run_build.py", OPENSEARCH_MANIFEST, "--lock", "--architecture", "arm64", "--platform", "windows", "--snapshot"])
@patch("run_build.InputManifest.from_path", return_value=InputManifest.from_path(OPENSEARCH_MANIFEST))
@patch("run_build.InputManifest.stable", return_value=InputManifest.from_path(OPENSEARCH_MANIFEST_1_2))
@patch("run_build.InputManifest.to_file")
@patch("logging.info")
def test_main_manifest_new_lock_with_overrides(self, mock_logging: Mock, mock_to_file: Mock, mock_stable: Mock, *mocks: Any) -> None:
main()
mock_stable.assert_called_with()
mock_to_file.assert_called_with(self.OPENSEARCH_MANIFEST + ".lock")
mock_logging.assert_called_with(f"Creating {self.OPENSEARCH_MANIFEST}.lock")