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
43namespace cmdline = utils::cmdline;
44namespace config = utils::config;
45
46using cli::cmd_config;
47using utils::none;
48
49
50namespace {
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.
59static config::tree
60fake_config(void)
61{
62    config::tree user_config = engine::default_config();
63    user_config.set_string("architecture", "the-architecture");
64    user_config.set_string("parallelism", "128");
65    user_config.set_string("platform", "the-platform");
66    //user_config.set_string("unprivileged_user", "");
67    user_config.set_string("test_suites.foo.bar", "first");
68    user_config.set_string("test_suites.foo.baz", "second");
69    return user_config;
70}
71
72
73}  // anonymous namespace
74
75
76ATF_TEST_CASE_WITHOUT_HEAD(all);
77ATF_TEST_CASE_BODY(all)
78{
79    cmdline::args_vector args;
80    args.push_back("config");
81
82    cmd_config cmd;
83    cmdline::ui_mock ui;
84    ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, fake_config()));
85
86    ATF_REQUIRE_EQ(5, ui.out_log().size());
87    ATF_REQUIRE_EQ("architecture = the-architecture", ui.out_log()[0]);
88    ATF_REQUIRE_EQ("parallelism = 128", ui.out_log()[1]);
89    ATF_REQUIRE_EQ("platform = the-platform", ui.out_log()[2]);
90    ATF_REQUIRE_EQ("test_suites.foo.bar = first", ui.out_log()[3]);
91    ATF_REQUIRE_EQ("test_suites.foo.baz = second", ui.out_log()[4]);
92    ATF_REQUIRE(ui.err_log().empty());
93}
94
95
96ATF_TEST_CASE_WITHOUT_HEAD(some__ok);
97ATF_TEST_CASE_BODY(some__ok)
98{
99    cmdline::args_vector args;
100    args.push_back("config");
101    args.push_back("platform");
102    args.push_back("test_suites.foo.baz");
103
104    cmd_config cmd;
105    cmdline::ui_mock ui;
106    ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, fake_config()));
107
108    ATF_REQUIRE_EQ(2, ui.out_log().size());
109    ATF_REQUIRE_EQ("platform = the-platform", ui.out_log()[0]);
110    ATF_REQUIRE_EQ("test_suites.foo.baz = second", ui.out_log()[1]);
111    ATF_REQUIRE(ui.err_log().empty());
112}
113
114
115ATF_TEST_CASE_WITHOUT_HEAD(some__fail);
116ATF_TEST_CASE_BODY(some__fail)
117{
118    cmdline::args_vector args;
119    args.push_back("config");
120    args.push_back("platform");
121    args.push_back("unknown");
122    args.push_back("test_suites.foo.baz");
123
124    cmdline::init("progname");
125
126    cmd_config cmd;
127    cmdline::ui_mock ui;
128    ATF_REQUIRE_EQ(EXIT_FAILURE, cmd.main(&ui, args, fake_config()));
129
130    ATF_REQUIRE_EQ(2, ui.out_log().size());
131    ATF_REQUIRE_EQ("platform = the-platform", ui.out_log()[0]);
132    ATF_REQUIRE_EQ("test_suites.foo.baz = second", ui.out_log()[1]);
133    ATF_REQUIRE_EQ(1, ui.err_log().size());
134    ATF_REQUIRE(atf::utils::grep_string("unknown.*not defined",
135                                        ui.err_log()[0]));
136}
137
138
139ATF_INIT_TEST_CASES(tcs)
140{
141    ATF_ADD_TEST_CASE(tcs, all);
142    ATF_ADD_TEST_CASE(tcs, some__ok);
143    ATF_ADD_TEST_CASE(tcs, some__fail);
144}
145