Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / osd / test_pg_transaction.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) 2016 Red Hat
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 <gtest/gtest.h>
16 #include "osd/PGTransaction.h"
17
18 TEST(pgtransaction, simple)
19 {
20   hobject_t h;
21   PGTransaction t;
22   ASSERT_TRUE(t.empty());
23   t.nop(h);
24   ASSERT_FALSE(t.empty());
25   unsigned num = 0;
26   t.safe_create_traverse(
27     [&](const pair<const hobject_t, PGTransaction::ObjectOperation> &p) {
28       ASSERT_EQ(p.first, h);
29       using T = PGTransaction::ObjectOperation::Init;
30       ASSERT_TRUE(boost::get<T::None>(&p.second.init_type));
31       ++num;
32     });
33   ASSERT_EQ(num, 1u);
34 }
35
36 TEST(pgtransaction, clone_safe_create_traverse)
37 {
38   hobject_t h, h2;
39   h2.snap = 1;
40   PGTransaction t;
41   ASSERT_TRUE(t.empty());
42   t.nop(h2);
43   ASSERT_FALSE(t.empty());
44   t.clone(h, h2);
45   unsigned num = 0;
46   t.safe_create_traverse(
47     [&](const pair<const hobject_t, PGTransaction::ObjectOperation> &p) {
48       using T = PGTransaction::ObjectOperation::Init;
49       if (num == 0) {
50         ASSERT_EQ(p.first, h);
51         ASSERT_TRUE(boost::get<T::Clone>(&p.second.init_type));
52         ASSERT_EQ(
53           boost::get<T::Clone>(&p.second.init_type)->source,
54           h2);
55       } else if (num == 1) {
56         ASSERT_EQ(p.first, h2);
57         ASSERT_TRUE(boost::get<T::None>(&p.second.init_type));
58       } else {
59         ASSERT_LT(num, 2u);
60       }
61       ++num;
62     });
63 }
64
65 TEST(pgtransaction, clone_safe_create_traverse2)
66 {
67   hobject_t h, h2, h3;
68   h.snap = 10;
69   h2.snap = 5;
70   h3.snap = 3;
71   PGTransaction t;
72   ASSERT_TRUE(t.empty());
73   t.nop(h3);
74   ASSERT_FALSE(t.empty());
75   t.clone(h, h2);
76   t.remove(h2);
77   t.clone(h2, h3);
78   unsigned num = 0;
79   t.safe_create_traverse(
80     [&](const pair<const hobject_t, PGTransaction::ObjectOperation> &p) {
81       using T = PGTransaction::ObjectOperation::Init;
82       if (num == 0) {
83         ASSERT_EQ(p.first, h);
84         ASSERT_TRUE(boost::get<T::Clone>(&p.second.init_type));
85         ASSERT_EQ(
86           boost::get<T::Clone>(&p.second.init_type)->source,
87           h2);
88       } else if (num == 1) {
89         ASSERT_EQ(p.first, h2);
90         ASSERT_TRUE(boost::get<T::Clone>(&p.second.init_type));
91         ASSERT_EQ(
92           boost::get<T::Clone>(&p.second.init_type)->source,
93           h3);
94       } else if (num == 2) {
95         ASSERT_EQ(p.first, h3);
96         ASSERT_TRUE(boost::get<T::None>(&p.second.init_type));
97       } else {
98         ASSERT_LT(num, 3u);
99       }
100       ++num;
101     });
102 }
103
104 TEST(pgtransaction, clone_safe_create_traverse3)
105 {
106   hobject_t h, h2, h3;
107   h.snap = 10;
108   h2.snap = 5;
109   h3.snap = 3;
110   PGTransaction t;
111   t.remove(h);
112   t.remove(h2);
113   t.clone(h2, h3);
114   unsigned num = 0;
115   t.safe_create_traverse(
116     [&](const pair<const hobject_t, PGTransaction::ObjectOperation> &p) {
117       using T = PGTransaction::ObjectOperation::Init;
118       if (p.first == h) {
119         ASSERT_TRUE(p.second.is_delete());
120       } else if (p.first == h2) {
121         ASSERT_TRUE(boost::get<T::Clone>(&p.second.init_type));
122         ASSERT_EQ(
123           boost::get<T::Clone>(&p.second.init_type)->source,
124           h3);
125       }
126       ASSERT_LT(num, 2u);
127       ++num;
128     });
129 }