]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/mds/TestSessionFilter.cc
import ceph 12.2.12
[ceph.git] / ceph / src / test / mds / TestSessionFilter.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2012 Inktank
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #include <iostream>
16
17 #include "include/stringify.h"
18 #include "mds/SessionMap.h"
19
20 #include "gtest/gtest.h"
21
22 typedef std::vector<std::string> args_eg;
23 typedef std::vector<args_eg> args_eg_set;
24
25 TEST(MDSSessionFilter, ParseGood)
26 {
27 args_eg_set examples = {
28 {"id=34"},
29 {"auth_name=foxtrot"},
30 {"state=reconnecting"},
31 {"reconnecting=true"},
32 {"client_metadata.root=/foo/bar"},
33 {},
34 {"id=123"},
35 {"id=34", "client_metadata.root=/foo/bar", "auth_name=foxtrot",
36 "state=reconnecting", "reconnecting=true"}
37 };
38
39 for (auto ex : examples) {
40 SessionFilter f;
41 std::stringstream ss;
42
43 std::cout << "Testing '" << ex << "'" << std::endl;
44 int r = f.parse(ex, &ss);
45 ASSERT_EQ(r, 0);
46 ASSERT_TRUE(ss.str().empty());
47 }
48 }
49
50 TEST(MDSSessionFilter, ParseBad)
51 {
52 args_eg_set examples = {
53 {"rhubarb"},
54 {"id="},
55 {"id=custard"},
56 {"=custard"},
57 {"reconnecting=MAYBE"},
58 {"reconnecting=2"}
59 };
60
61 for (auto ex : examples) {
62 SessionFilter f;
63 std::stringstream ss;
64
65 std::cout << "Testing '" << ex << "'" << std::endl;
66 int r = f.parse(ex, &ss);
67 ASSERT_EQ(r, -EINVAL);
68 ASSERT_FALSE(ss.str().empty());
69 }
70 }
71
72 TEST(MDSSessionFilter, IdEquality)
73 {
74 SessionFilter filter;
75 std::stringstream ss;
76 filter.parse({"id=123"}, &ss);
77 Session *a = new Session(nullptr);;
78 Session *b = new Session(nullptr);;
79 a->info.inst.name.parse("client.123");
80 b->info.inst.name.parse("client.456");
81
82 ASSERT_TRUE(filter.match(*a, [](client_t c) -> bool {return false;}));
83 ASSERT_FALSE(filter.match(*b, [](client_t c) -> bool {return false;}));
84 a->put();
85 b->put();
86 }
87
88 TEST(MDSSessionFilter, StateEquality)
89 {
90 SessionFilter filter;
91 std::stringstream ss;
92 filter.parse({"state=closing"}, &ss);
93 Session *a = new Session(nullptr);
94 a->set_state(Session::STATE_CLOSING);
95 Session *b = new Session(nullptr);
96 b->set_state(Session::STATE_OPENING);
97
98 ASSERT_TRUE(filter.match(*a, [](client_t c) -> bool {return false;}));
99 ASSERT_FALSE(filter.match(*b, [](client_t c) -> bool {return false;}));
100 a->put();
101 b->put();
102 }
103
104 TEST(MDSSessionFilter, AuthEquality)
105 {
106 SessionFilter filter;
107 std::stringstream ss;
108 filter.parse({"auth_name=rhubarb"}, &ss);
109 Session *a = new Session(nullptr);
110 a->info.auth_name.set_id("rhubarb");
111 Session *b = new Session(nullptr);
112 b->info.auth_name.set_id("custard");
113
114 ASSERT_TRUE(filter.match(*a, [](client_t c) -> bool {return false;}));
115 ASSERT_FALSE(filter.match(*b, [](client_t c) -> bool {return false;}));
116 a->put();
117 b->put();
118 }
119
120 TEST(MDSSessionFilter, MetadataEquality)
121 {
122 SessionFilter filter;
123 std::stringstream ss;
124 int r = filter.parse({"client_metadata.root=/rhubarb"}, &ss);
125 ASSERT_EQ(r, 0);
126 Session *a = new Session(nullptr);
127 a->set_client_metadata(std::map<std::string,std::string>({{"root", "/rhubarb"}}));
128 Session *b = new Session(nullptr);
129 b->set_client_metadata(std::map<std::string,std::string>({{"root", "/custard"}}));
130
131 ASSERT_TRUE(filter.match(*a, [](client_t c) -> bool {return false;}));
132 ASSERT_FALSE(filter.match(*b, [](client_t c) -> bool {return false;}));
133 a->put();
134 b->put();
135 }
136
137 TEST(MDSSessionFilter, ReconnectingEquality)
138 {
139 SessionFilter filter;
140 std::stringstream ss;
141 int r = filter.parse({"reconnecting=true"}, &ss);
142 ASSERT_EQ(r, 0);
143 Session *a = new Session(nullptr);
144
145 ASSERT_TRUE(filter.match(*a, [](client_t c) -> bool {return true;}));
146 ASSERT_FALSE(filter.match(*a, [](client_t c) -> bool {return false;}));
147 a->put();
148 }