summaryrefslogtreecommitdiff
path: root/python/samba/tests/rust.py
blob: 99f66a26cd2cf1efd7d04e10476ab3084228b943 (plain)
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
# Unix SMB/CIFS implementation.
#
# Tests for Rust
#
# Copyright (C) David Mulder <dmulder@samba.org> 2024
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

"""Cargo tests for Rust sources"""

from samba.tests import TestCase, BlackboxProcessError
import os
from subprocess import Popen, PIPE
from samba import is_rust_built


class RustCargoTests(TestCase):
    def setUp(self):
        super().setUp()

        # Locate the rust source directory
        self.rust_dir = os.path.abspath(
                os.path.join(
                    os.path.realpath(
                        os.path.dirname(__file__)
                    ),
                    '../../../../rust'
                )
            )

        # Locate the bin directory
        self.target_dir = os.path.abspath(
                os.path.join(
                    os.path.realpath(
                        os.path.dirname(__file__)
                    ),
                    '../../..',
                    'default/rust',
                )
            )

    def check_cargo_test(self, crate_toml):
        # Execute the cargo test command
        cmd = 'cargo test --target-dir=%s --manifest-path=%s' % (self.target_dir, crate_toml)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
        stdoutdata, stderrdata = p.communicate()
        retcode = p.returncode
        if retcode != 0:
            msg = "cargo test failed; return code %s" % retcode
            raise BlackboxProcessError(retcode,
                                       cmd,
                                       stdoutdata.decode('utf-8'),
                                       stderrdata.decode('utf-8'),
                                       msg)

    def test_rust(self):
        if not is_rust_built():
            self.skipTest('Cannot test Samba Rust if not built')

        crates = []
        for root, dirs, files in os.walk(self.rust_dir):
            for file in files:
                if os.path.basename(file) == 'Cargo.toml':
                    if root != self.rust_dir:
                        crates.append(os.path.join(root, file))

        for crate_toml in crates:
            with self.subTest(crate_toml):
                self.check_cargo_test(crate_toml)