Skip to content

Commit 13ebe18

Browse files
[Nexthop] Add CMake test infrastructure for FBOSS Image Builder
Integrate distro_cli Python unit tests into the FBOSS CMake build system to enable automated testing in CI/CD pipelines. - Add FbossImageDistroCliTests.cmake to discover and register Python unit tests - Configure test discovery for distro_cli modules (builder, cmds, lib, tools) - Update root CMakeLists.txt to include distro_cli test suite - Enable distro_cli tests in GitHub Actions workflow - Update distro_cli README with build and test instructions The CMake configuration automatically discovers all *_test.py files and registers them as CTest targets, allowing tests to run via 'ctest' or 'make test'.
1 parent 7a99879 commit 13ebe18

File tree

3 files changed

+103
-4
lines changed

3 files changed

+103
-4
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,3 +1154,6 @@ if (GITHUB_ACTIONS_BUILD)
11541154
fsdb_all_services
11551155
)
11561156
endif()
1157+
1158+
# FBOSS Image Builder distro_cli tests
1159+
include(cmake/FbossImageDistroCliTests.cmake)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# CMake to build and test fboss-image/distro_cli
2+
3+
# In general, libraries and binaries in fboss/foo/bar are built by
4+
# cmake/FooBar.cmake
5+
6+
include(FBPythonBinary)
7+
8+
file(GLOB DISTRO_CLI_TEST_SOURCES
9+
"fboss-image/distro_cli/tests/*_test.py"
10+
)
11+
12+
file(GLOB DISTRO_CLI_TEST_HELPERS
13+
"fboss-image/distro_cli/tests/test_helpers.py"
14+
)
15+
16+
file(GLOB_RECURSE DISTRO_CLI_LIB_SOURCES
17+
"fboss-image/distro_cli/builder/*.py"
18+
"fboss-image/distro_cli/cmds/*.py"
19+
"fboss-image/distro_cli/lib/*.py"
20+
"fboss-image/distro_cli/tools/*.py"
21+
)
22+
23+
file(COPY "fboss-image/distro_cli/tests/data" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/fboss-image/distro_cli/tests")
24+
25+
add_fb_python_unittest(
26+
distro_cli_tests
27+
BASE_DIR "fboss-image"
28+
SOURCES
29+
${DISTRO_CLI_TEST_SOURCES}
30+
${DISTRO_CLI_TEST_HELPERS}
31+
${DISTRO_CLI_LIB_SOURCES}
32+
ENV
33+
"PYTHONPATH=${CMAKE_CURRENT_SOURCE_DIR}/fboss-image/distro_cli"
34+
)
35+
36+
install_fb_python_executable(distro_cli_tests)

fboss-image/distro_cli/README.md

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,56 @@ Components are built in the following order:
110110

111111
### Running Tests
112112

113+
#### With Bazel (Recommended)
114+
113115
```bash
114116
# Run all tests
115-
cd fboss-image/distro_cli
116-
python3 -m unittest discover -s tests -p '*_test.py'
117+
bazel test //fboss/fboss-image/distro_cli:all
117118

118119
# Run specific test
119-
python3 -m unittest tests.cli_test
120+
bazel test //fboss/fboss-image/distro_cli:cli_test
121+
122+
# Run with detailed output
123+
bazel test //fboss/fboss-image/distro_cli:all --test_output=all
124+
```
125+
126+
#### With pytest
127+
128+
```bash
129+
# Run all tests (from fboss-image directory)
130+
cd fboss-image
131+
PYTHONPATH=. python3 -m pytest distro_cli/tests/ -v
132+
133+
# Run specific test file
134+
PYTHONPATH=. python3 -m pytest distro_cli/tests/cli_test.py -v
135+
136+
# Run specific test class or method
137+
PYTHONPATH=. python3 -m pytest distro_cli/tests/cli_test.py::CLITest::test_cli_creation -v
138+
```
139+
140+
#### With unittest
141+
142+
```bash
143+
# Run all tests (from fboss-image directory)
144+
cd fboss-image
145+
PYTHONPATH=. python3 -m unittest discover -s distro_cli/tests -p '*_test.py'
146+
147+
# Run specific test module
148+
PYTHONPATH=. python3 -m unittest distro_cli.tests.cli_test
149+
150+
# Run specific test class
151+
PYTHONPATH=. python3 -m unittest distro_cli.tests.cli_test.CLITest
120152
```
121153

122154
### Linting
123155

124156
```bash
157+
# With ruff (from distro_cli directory)
125158
cd fboss-image/distro_cli
126159
python3 -m ruff check .
160+
161+
# With bazel (from repository root)
162+
bazel test //private-fboss/fboss-image/distro_cli:lint_check
127163
```
128164

129165
### Project Structure
@@ -152,7 +188,7 @@ fboss-image/distro_cli/
152188
The CLI uses a custom OOP wrapper around argparse (stdlib only, no external dependencies):
153189

154190
```python
155-
from lib.cli import CLI
191+
from distro_cli.lib.cli import CLI
156192

157193
# Create CLI
158194
cli = CLI(description='My CLI')
@@ -171,3 +207,27 @@ device.add_command('ssh', ssh_func, help_text='SSH to device')
171207
# Run
172208
cli.run(setup_logging_func=setup_logging)
173209
```
210+
211+
## Running Tests
212+
213+
### Quick Tests (Default)
214+
Run all fast unit tests (excludes long-running E2E tests):
215+
```bash
216+
python3 -m pytest distro_cli/tests/ -v
217+
# or explicitly exclude e2e tests:
218+
python3 -m pytest distro_cli/tests/ -m "not e2e" -v
219+
```
220+
221+
### E2E Tests Only
222+
Run only the end-to-end tests (kernel build, SAI build):
223+
```bash
224+
python3 -m pytest distro_cli/tests/ -m e2e -v -s
225+
```
226+
227+
### All Tests (Including E2E)
228+
Run everything:
229+
```bash
230+
python3 -m pytest distro_cli/tests/ -v -s
231+
```
232+
233+
**Note:** E2E tests can take 10-60 minutes and require actual source files (kernel sources, SAI SDK, etc.).

0 commit comments

Comments
 (0)