File: action-unit-util-systemd.c

package info (click to toggle)
snoopy 2.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,016 kB
  • sloc: ansic: 7,169; sh: 4,514; makefile: 1,095
file content (144 lines) | stat: -rw-r--r-- 4,737 bytes parent folder | download | duplicates (3)
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
/*
 * SNOOPY COMMAND LOGGER
 *
 * Copyright (c) 2015 Bostjan Skufca Jese <bostjan@a2o.si>
 *
 * 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 2, 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, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */



/*
 * Includes order: from local to global
 */
#include "action-unit-util-systemd.h"
#include "action-common.h"

#include "util/systemd-snoopy.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>



/*
 * Local helper functions
 */
static void mockCgroupEntryConversion(char const * const cgroupEntry, char const * const expectedUnitName);



void snoopyTestCli_action_unit_util_systemd_showHelp ()
{
    char * helpContent =
        "Snoopy TEST SUITE CLI utility :: Action `unit` :: Unit `util` :: Subunit 'systemd'\n"
        "\n"
        "Description:\n"
        "    Mocks src/action/systemd.c implementation code paths (mainly for the coverage of code parts/paths not covered by the test suite).\n"
        "\n"
        "Usage:\n"
        "    snoopy-test unit util systemd\n"
        "    snoopy-test unit util systemd --help\n"
        "\n";
    printf("%s", helpContent);
}



int snoopyTestCli_action_unit_util_systemd (int argc, char ** argv)
{
    const char *arg1;

    if (argc > 0) {
        arg1 = argv[0];
    } else {
        arg1 = "";
    }

    if (0 == strcmp(arg1, "--help")) {
        snoopyTestCli_action_unit_util_systemd_showHelp();
        return 0;
    }


    char         cgroupEntry1[]    = "1:name=systemd:/init.scope";
    const char * expectedUnitName1 = "init";
    mockCgroupEntryConversion(cgroupEntry1, expectedUnitName1);

    char         cgroupEntry2[]    = "1:name=systemd:/system.slice/dbus.service";
    const char * expectedUnitName2 = "dbus";
    mockCgroupEntryConversion(cgroupEntry2, expectedUnitName2);

    char         cgroupEntry3[]    = "1:name=systemd:/user.slice/user-0.slice/session-3177.scope";
    const char * expectedUnitName3 = "root"; // Expected to exist
    mockCgroupEntryConversion(cgroupEntry3, expectedUnitName3);

    char         cgroupEntry4[]    = "1:name=systemd:/user.slice/user-57123.slice/session-3177.scope";
    const char * expectedUnitName4 = "user-57123"; // Expected to not exist on the system
    mockCgroupEntryConversion(cgroupEntry4, expectedUnitName4);

    char         cgroupEntry5[]    = "1:name=systemd:/";
    const char * expectedUnitName5 = "-";
    mockCgroupEntryConversion(cgroupEntry5, expectedUnitName5);

    char         cgroupEntry6[]    = "0::/whatever";
    const char * expectedUnitName6 = NULL;
    mockCgroupEntryConversion(cgroupEntry6, expectedUnitName6);

    char         cgroupEntry7[]    = "1:name=systemd:/actions_job/44e2bfe9d6f3ece192a9f762d91c9514131b8cfbbd0469b1722fd8637ef6feac";
    const char * expectedUnitName7 = NULL;
    mockCgroupEntryConversion(cgroupEntry7, expectedUnitName7);


    printSuccess("Mocking src/unit/systemd.c complete.");
    return 0;
}



static void mockCgroupEntryConversion(char const * const cgroupEntry, char const * const expectedUnitName)
{
    char * unitName = snoopy_util_systemd_convertCgroupEntryToUnitName(cgroupEntry);

    if (!unitName && !expectedUnitName) {
        return; // Both are NULL, so it's expected
    }

    if (!unitName && expectedUnitName) {
        printDiagValue("cgroupEntry", cgroupEntry);
        printDiagValue("Expected unit name", expectedUnitName);
        fatalError("Unable to convert cgroup entry to Systemd unit name");
        return; // Fake for Sonar CI, fatalError() exits
    }

    if (unitName && !expectedUnitName) {
        printDiagValue("cgroupEntry", cgroupEntry);
        printDiagValue("Converted unit name", unitName);
        fatalError("Cgroup entry not converted to NULL");
        return; // Fake for Sonar CI, fatalError() exits
    }

    if (0 != strcmp(expectedUnitName, unitName)) {
        printDiagValue("cgroupEntry", cgroupEntry);
        printDiagValue("Expected  unit name", expectedUnitName);
        printDiagValue("Converted unit name", unitName);
        fatalError("Unexpected unit name conversion");
        return; // Fake for Sonar CI, fatalError() exits
    }

    free(unitName);
    return;
}