File: test_vars.cpp

package info (click to toggle)
dnf5 5.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,960 kB
  • sloc: cpp: 94,312; python: 3,370; xml: 1,073; ruby: 600; sql: 250; ansic: 232; sh: 104; perl: 62; makefile: 30
file content (241 lines) | stat: -rw-r--r-- 9,862 bytes parent folder | download
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: GPL-2.0-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf 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 of the License, or
// (at your option) any later version.
//
// Libdnf 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 libdnf.  If not, see <https://www.gnu.org/licenses/>.


#include "test_vars.hpp"


CPPUNIT_TEST_SUITE_REGISTRATION(VarsTest);

using namespace std::literals::string_literals;

// TODO possibly test the automatically detected vars (they depend on the host)

void VarsTest::setUp() {
    TestCaseFixture::setUp();
    base = get_preconfigured_base();
}


void VarsTest::test_vars() {
    base->get_config().get_varsdir_option().set(
        std::vector<std::string>{PROJECT_SOURCE_DIR "/test/libdnf5/conf/data/vars"});
    // Load all variables.
    base->setup();

    CPPUNIT_ASSERT_EQUAL("foovalue123-bar"s, base->get_vars()->substitute("foo$var1-bar"));
    CPPUNIT_ASSERT_EQUAL("$$$value123456-$nn-${nnn}"s, base->get_vars()->substitute("$$$${var1}$var2-$nn-${nnn}"));
    CPPUNIT_ASSERT_EQUAL(
        "alternate-default-${nn:+n${nn:-${nnn:}"s,
        base->get_vars()->substitute("${var1:+alternate}-${unset:-default}-${nn:+n${nn:-${nnn:}"));
    CPPUNIT_ASSERT_EQUAL("456"s, base->get_vars()->substitute("${unset:-${var1:+${var2:+$var2}}}"));
}


void VarsTest::test_vars_multiple_dirs() {
    base->get_config().get_varsdir_option().set(std::vector<std::string>{
        PROJECT_SOURCE_DIR "/test/libdnf5/conf/data/vars",
        PROJECT_SOURCE_DIR "/test/libdnf5/conf/data/vars2",
    });
    // Load all variables.
    base->setup();

    CPPUNIT_ASSERT_EQUAL("av333bthe answer is here"s, base->get_vars()->substitute("a${var1}b${var42}"));
}


void VarsTest::test_vars_env() {
    base->get_config().get_varsdir_option().set(
        std::vector<std::string>{PROJECT_SOURCE_DIR "/test/libdnf5/conf/data/vars"});
    // Setting environment variables.
    // Environment variables have higher priority than variables from files.
    setenv("DNF0", "foo0", 1);
    setenv("DNF1", "foo1", 1);
    setenv("DNF9", "foo9", 1);
    setenv("DNF_VAR_var1", "testvar1", 1);
    setenv("DNF_VAR_var41", "testvar2", 1);

    // Load all variables.
    base->setup();

    // The variables var1 and var2 are defined in the files.
    // However, var1 was also an environment variable. The environment has a higher priority.
    CPPUNIT_ASSERT_EQUAL(
        "foo0-foo1-foo9-testvar1-testvar2-456"s,
        base->get_vars()->substitute("${DNF0}-${DNF1}-${DNF9}-${var1}-${var41}-${var2}"));
}


void VarsTest::test_vars_api() {
    base->setup();
    auto vars = base->get_vars();

    CPPUNIT_ASSERT(!vars->contains("test_var1"));

    vars->set("test_var1", "123", libdnf5::Vars::Priority::PLUGIN);
    CPPUNIT_ASSERT(vars->contains("test_var1"));

    {
        auto & [value, prioriry] = vars->get("test_var1");
        CPPUNIT_ASSERT_EQUAL_MESSAGE("value returned by vars->get()", "123"s, value);
        CPPUNIT_ASSERT_EQUAL_MESSAGE("priority returned by vars->get()", libdnf5::Vars::Priority::PLUGIN, prioriry);
    }

    CPPUNIT_ASSERT_EQUAL_MESSAGE("value returned by vars->get_value()", "123"s, vars->get_value("test_var1"));

    CPPUNIT_ASSERT(vars->unset("test_var1"));
    CPPUNIT_ASSERT_MESSAGE("after vars->unset(\"test_var1\")", !vars->contains("test_var1"));
}


void VarsTest::test_vars_api_set_prio() {
    base->setup();
    auto vars = base->get_vars();

    CPPUNIT_ASSERT(!vars->contains("test_var1"));

    // set a new variable
    vars->set("test_var1", "123", libdnf5::Vars::Priority::PLUGIN);
    {
        auto & [value, prioriry] = vars->get("test_var1");
        CPPUNIT_ASSERT_EQUAL_MESSAGE("value returned by vars->get()", "123"s, value);
        CPPUNIT_ASSERT_EQUAL_MESSAGE("priority returned by vars->get()", libdnf5::Vars::Priority::PLUGIN, prioriry);
    }

    // change the value using the same priority
    vars->set("test_var1", "345", libdnf5::Vars::Priority::PLUGIN);
    {
        auto & [value, prioriry] = vars->get("test_var1");
        CPPUNIT_ASSERT_EQUAL_MESSAGE("value returned by vars->get()", "345"s, value);
        CPPUNIT_ASSERT_EQUAL_MESSAGE("priority returned by vars->get()", libdnf5::Vars::Priority::PLUGIN, prioriry);
    }

    // change the value using a higher priority
    vars->set("test_var1", "678", libdnf5::Vars::Priority::COMMANDLINE);
    {
        auto & [value, prioriry] = vars->get("test_var1");
        CPPUNIT_ASSERT_EQUAL_MESSAGE("value returned by vars->get()", "678"s, value);
        CPPUNIT_ASSERT_EQUAL_MESSAGE(
            "priority returned by vars->get()", libdnf5::Vars::Priority::COMMANDLINE, prioriry);
    }

    // changing the value using a lower priority is ignored
    vars->set("test_var1", "ignored_value", libdnf5::Vars::Priority::PLUGIN);
    {
        auto & [value, prioriry] = vars->get("test_var1");
        CPPUNIT_ASSERT_EQUAL_MESSAGE("value returned by vars->get()", "678"s, value);
        CPPUNIT_ASSERT_EQUAL_MESSAGE(
            "priority returned by vars->get()", libdnf5::Vars::Priority::COMMANDLINE, prioriry);
    }

    // change the value using default priority - default is RUNTIME, the highest priority
    vars->set("test_var1", "999");
    {
        auto & [value, prioriry] = vars->get("test_var1");
        CPPUNIT_ASSERT_EQUAL_MESSAGE("value returned by vars->get()", "999"s, value);
        CPPUNIT_ASSERT_EQUAL_MESSAGE("priority returned by vars->get()", libdnf5::Vars::Priority::RUNTIME, prioriry);
    }
}


void VarsTest::test_vars_api_unset_prio() {
    base->setup();
    auto vars = base->get_vars();

    // set a new variable
    vars->set("test_var1", "123", libdnf5::Vars::Priority::COMMANDLINE);
    {
        auto & [value, prioriry] = vars->get("test_var1");
        CPPUNIT_ASSERT_EQUAL_MESSAGE("value returned by vars->get()", "123"s, value);
        CPPUNIT_ASSERT_EQUAL_MESSAGE(
            "priority returned by vars->get()", libdnf5::Vars::Priority::COMMANDLINE, prioriry);
    }

    // removing a variable using a lower priority is ignored
    CPPUNIT_ASSERT(!vars->unset("test_var1", libdnf5::Vars::Priority::PLUGIN));
    CPPUNIT_ASSERT_MESSAGE(
        "after vars->unset(\"test_var1\", libdnf5::Vars::Priority::PLUGIN)", vars->contains("test_var1"));
    {
        auto & [value, prioriry] = vars->get("test_var1");
        CPPUNIT_ASSERT_EQUAL_MESSAGE("value returned by vars->get()", "123"s, value);
        CPPUNIT_ASSERT_EQUAL_MESSAGE(
            "priority returned by vars->get()", libdnf5::Vars::Priority::COMMANDLINE, prioriry);
    }

    // removing a variable using the same priority
    CPPUNIT_ASSERT(vars->unset("test_var1", libdnf5::Vars::Priority::COMMANDLINE));
    CPPUNIT_ASSERT_MESSAGE(
        "after vars->unset(\"test_var1\", libdnf5::Vars::Priority::COMMANDLINE)", !vars->contains("test_var1"));


    // set a new variable
    vars->set("test_var2", "345", libdnf5::Vars::Priority::PLUGIN);
    CPPUNIT_ASSERT(vars->contains("test_var2"));

    // removing a variable using a higher priority
    CPPUNIT_ASSERT(vars->unset("test_var2", libdnf5::Vars::Priority::COMMANDLINE));
    CPPUNIT_ASSERT_MESSAGE(
        "after vars->unset(\"test_var2\", libdnf5::Vars::Priority::COMMANDLINE)", !vars->contains("test_var2"));


    // set a new variable
    vars->set("test_var3", "678", libdnf5::Vars::Priority::PLUGIN);
    CPPUNIT_ASSERT(vars->contains("test_var3"));

    // removing a variable using default priority - default is RUNTIME, the highest priority
    CPPUNIT_ASSERT(vars->unset("test_var3"));
    CPPUNIT_ASSERT_MESSAGE("after vars->unset(\"test_var3\")", !vars->contains("test_var3"));
}


void VarsTest::test_vars_api_releasever() {
    base->setup();
    auto vars = base->get_vars();

    // set the "releasever" variable
    vars->set("releasever", "40.12", libdnf5::Vars::Priority::PLUGIN);

    // check the value the of "releasever" variable
    {
        auto & [value, prioriry] = vars->get("releasever");
        CPPUNIT_ASSERT_EQUAL_MESSAGE("value returned by vars->get()", "40.12"s, value);
        CPPUNIT_ASSERT_EQUAL_MESSAGE("priority returned by vars->get()", libdnf5::Vars::Priority::PLUGIN, prioriry);
    }

    // check the value the of auto-created read-only variables
    {
        auto & [value, prioriry] = vars->get("releasever_major");
        CPPUNIT_ASSERT_EQUAL_MESSAGE("value returned by vars->get()", "40"s, value);
        CPPUNIT_ASSERT_EQUAL_MESSAGE("priority returned by vars->get()", libdnf5::Vars::Priority::PLUGIN, prioriry);
    }
    {
        auto & [value, prioriry] = vars->get("releasever_minor");
        CPPUNIT_ASSERT_EQUAL_MESSAGE("value returned by vars->get()", "12"s, value);
        CPPUNIT_ASSERT_EQUAL_MESSAGE("priority returned by vars->get()", libdnf5::Vars::Priority::PLUGIN, prioriry);
    }

    // the "releasever", "releasever_major", and "releasever_minor" variables are read-write
    CPPUNIT_ASSERT(!vars->is_read_only("releasever"));
    CPPUNIT_ASSERT(!vars->is_read_only("releasever_major"));
    CPPUNIT_ASSERT(!vars->is_read_only("releasever_minor"));

    // because the variable "releasever" is read-write, it can be removed
    CPPUNIT_ASSERT(vars->unset("releasever", libdnf5::Vars::Priority::PLUGIN));
    CPPUNIT_ASSERT_MESSAGE("after vars->unset(\"test_var3\")", !vars->contains("releasever"));
}