summaryrefslogtreecommitdiffstats
path: root/src/test/common/test_context.cc
blob: 6b00f61c17bcf4c3ea848f999e4184908aac8233 (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
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2014 Cloudwatt <libre.licensing@cloudwatt.com>
 *
 * Author: Loic Dachary <loic@dachary.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library 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 Library Public License for more details.
 *
 *
 */
#include "gtest/gtest.h"
#include "include/types.h"
#include "include/msgr.h"
#include "common/ceph_context.h"
#include "common/config_proxy.h"
#include "log/Log.h"

TEST(CephContext, do_command)
{
  CephContext *cct = (new CephContext(CEPH_ENTITY_TYPE_CLIENT))->get();

  cct->_conf->cluster = "ceph";

  string key("key");
  string value("value");
  cct->_conf.set_val(key.c_str(), value.c_str());
  cmdmap_t cmdmap;
  cmdmap["var"] = key;

  {
    stringstream ss;
    bufferlist out;
    std::unique_ptr<Formatter> f(Formatter::create("xml", "xml"));
    cct->do_command("config get", cmdmap, f.get(), ss, &out);
    f->flush(out);
    string s(out.c_str(), out.length());
    EXPECT_EQ("<config_get><key>" + value + "</key></config_get>", s);
  }

  {
    stringstream ss;
    bufferlist out;
    cmdmap_t bad_cmdmap; // no 'var' field
    std::unique_ptr<Formatter> f(Formatter::create("xml", "xml"));
    int r = cct->do_command("config get", bad_cmdmap, f.get(), ss, &out);
    if (r >= 0) {
      f->flush(out);
    }
    string s(out.c_str(), out.length());
    EXPECT_EQ(-EINVAL, r);
    EXPECT_EQ("", s);
    EXPECT_EQ("", ss.str()); // no error string :/
  }
  {
    stringstream ss;
    bufferlist out;
    cmdmap_t bad_cmdmap;
    bad_cmdmap["var"] = string("doesnotexist123");
    std::unique_ptr<Formatter> f(Formatter::create("xml", "xml"));
    int r = cct->do_command("config help", bad_cmdmap, f.get(), ss, &out);
    if (r >= 0) {
      f->flush(out);
    }
    string s(out.c_str(), out.length());
    EXPECT_EQ(-ENOENT, r);
    EXPECT_EQ("", s);
    EXPECT_EQ("Setting not found: 'doesnotexist123'", ss.str());
  }

  {
    stringstream ss;
    bufferlist out;
    std::unique_ptr<Formatter> f(Formatter::create("xml", "xml"));
    cct->do_command("config diff get", cmdmap, f.get(), ss, &out);
    f->flush(out);
    string s(out.c_str(), out.length());
    EXPECT_EQ("<config_diff_get><diff><key><default></default><override>" + value + "</override><final>value</final></key><rbd_default_features><default>61</default><final>61</final></rbd_default_features></diff></config_diff_get>", s);
  }
  cct->put();
}

TEST(CephContext, experimental_features)
{
  CephContext *cct = (new CephContext(CEPH_ENTITY_TYPE_CLIENT))->get();

  cct->_conf->cluster = "ceph";

  ASSERT_FALSE(cct->check_experimental_feature_enabled("foo"));
  ASSERT_FALSE(cct->check_experimental_feature_enabled("bar"));
  ASSERT_FALSE(cct->check_experimental_feature_enabled("baz"));

  cct->_conf.set_val("enable_experimental_unrecoverable_data_corrupting_features",
		      "foo,bar");
  cct->_conf.apply_changes(&cout);
  ASSERT_TRUE(cct->check_experimental_feature_enabled("foo"));
  ASSERT_TRUE(cct->check_experimental_feature_enabled("bar"));
  ASSERT_FALSE(cct->check_experimental_feature_enabled("baz"));

  cct->_conf.set_val("enable_experimental_unrecoverable_data_corrupting_features",
		      "foo bar");
  cct->_conf.apply_changes(&cout);
  ASSERT_TRUE(cct->check_experimental_feature_enabled("foo"));
  ASSERT_TRUE(cct->check_experimental_feature_enabled("bar"));
  ASSERT_FALSE(cct->check_experimental_feature_enabled("baz"));

  cct->_conf.set_val("enable_experimental_unrecoverable_data_corrupting_features",
		      "baz foo");
  cct->_conf.apply_changes(&cout);
  ASSERT_TRUE(cct->check_experimental_feature_enabled("foo"));
  ASSERT_FALSE(cct->check_experimental_feature_enabled("bar"));
  ASSERT_TRUE(cct->check_experimental_feature_enabled("baz"));

  cct->_conf.set_val("enable_experimental_unrecoverable_data_corrupting_features",
		      "*");
  cct->_conf.apply_changes(&cout);
  ASSERT_TRUE(cct->check_experimental_feature_enabled("foo"));
  ASSERT_TRUE(cct->check_experimental_feature_enabled("bar"));
  ASSERT_TRUE(cct->check_experimental_feature_enabled("baz"));

  cct->_log->flush();
}

/*
 * Local Variables:
 * compile-command: "cd ../.. ;
 *   make unittest_context &&
 *    valgrind \
 *    --max-stackframe=20000000 --tool=memcheck \
 *   ./unittest_context # --gtest_filter=CephContext.*
 * "
 * End:
 */