xref: /freebsd/contrib/kyua/cli/cmd_config_test.cpp (revision 87b759f0fa1f7554d50ce640c40138512bbded44)
1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 //   notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 //   notice, this list of conditions and the following disclaimer in the
12 //   documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 //   may be used to endorse or promote products derived from this software
15 //   without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #include "cli/cmd_config.hpp"
30 
31 #include <cstdlib>
32 
33 #include <atf-c++.hpp>
34 
35 #include "cli/common.ipp"
36 #include "engine/config.hpp"
37 #include "utils/cmdline/globals.hpp"
38 #include "utils/cmdline/parser.hpp"
39 #include "utils/cmdline/ui_mock.hpp"
40 #include "utils/config/tree.ipp"
41 #include "utils/optional.ipp"
42 
43 namespace cmdline = utils::cmdline;
44 namespace config = utils::config;
45 
46 using cli::cmd_config;
47 using utils::none;
48 
49 
50 namespace {
51 
52 
53 /// Instantiates a fake user configuration for testing purposes.
54 ///
55 /// The user configuration is populated with a collection of test-suite
56 /// properties and some hardcoded values for the generic configuration options.
57 ///
58 /// \return A new user configuration object.
59 static config::tree
60 fake_config(void)
61 {
62     config::tree user_config = engine::default_config();
63     user_config.set_string("architecture", "the-architecture");
64     user_config.set_string("execenvs", "the-env");
65     user_config.set_string("parallelism", "128");
66     user_config.set_string("platform", "the-platform");
67     //user_config.set_string("unprivileged_user", "");
68     user_config.set_string("test_suites.foo.bar", "first");
69     user_config.set_string("test_suites.foo.baz", "second");
70     return user_config;
71 }
72 
73 
74 }  // anonymous namespace
75 
76 
77 ATF_TEST_CASE_WITHOUT_HEAD(all);
78 ATF_TEST_CASE_BODY(all)
79 {
80     cmdline::args_vector args;
81     args.push_back("config");
82 
83     cmd_config cmd;
84     cmdline::ui_mock ui;
85     ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, fake_config()));
86 
87     ATF_REQUIRE_EQ(6, ui.out_log().size());
88     ATF_REQUIRE_EQ("architecture = the-architecture", ui.out_log()[0]);
89     ATF_REQUIRE_EQ("execenvs = the-env", ui.out_log()[1]);
90     ATF_REQUIRE_EQ("parallelism = 128", ui.out_log()[2]);
91     ATF_REQUIRE_EQ("platform = the-platform", ui.out_log()[3]);
92     ATF_REQUIRE_EQ("test_suites.foo.bar = first", ui.out_log()[4]);
93     ATF_REQUIRE_EQ("test_suites.foo.baz = second", ui.out_log()[5]);
94     ATF_REQUIRE(ui.err_log().empty());
95 }
96 
97 
98 ATF_TEST_CASE_WITHOUT_HEAD(some__ok);
99 ATF_TEST_CASE_BODY(some__ok)
100 {
101     cmdline::args_vector args;
102     args.push_back("config");
103     args.push_back("platform");
104     args.push_back("test_suites.foo.baz");
105 
106     cmd_config cmd;
107     cmdline::ui_mock ui;
108     ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, fake_config()));
109 
110     ATF_REQUIRE_EQ(2, ui.out_log().size());
111     ATF_REQUIRE_EQ("platform = the-platform", ui.out_log()[0]);
112     ATF_REQUIRE_EQ("test_suites.foo.baz = second", ui.out_log()[1]);
113     ATF_REQUIRE(ui.err_log().empty());
114 }
115 
116 
117 ATF_TEST_CASE_WITHOUT_HEAD(some__fail);
118 ATF_TEST_CASE_BODY(some__fail)
119 {
120     cmdline::args_vector args;
121     args.push_back("config");
122     args.push_back("platform");
123     args.push_back("unknown");
124     args.push_back("test_suites.foo.baz");
125 
126     cmdline::init("progname");
127 
128     cmd_config cmd;
129     cmdline::ui_mock ui;
130     ATF_REQUIRE_EQ(EXIT_FAILURE, cmd.main(&ui, args, fake_config()));
131 
132     ATF_REQUIRE_EQ(2, ui.out_log().size());
133     ATF_REQUIRE_EQ("platform = the-platform", ui.out_log()[0]);
134     ATF_REQUIRE_EQ("test_suites.foo.baz = second", ui.out_log()[1]);
135     ATF_REQUIRE_EQ(1, ui.err_log().size());
136     ATF_REQUIRE(atf::utils::grep_string("unknown.*not defined",
137                                         ui.err_log()[0]));
138 }
139 
140 
141 ATF_INIT_TEST_CASES(tcs)
142 {
143     ATF_ADD_TEST_CASE(tcs, all);
144     ATF_ADD_TEST_CASE(tcs, some__ok);
145     ATF_ADD_TEST_CASE(tcs, some__fail);
146 }
147