]>
Commit | Line | Data |
---|---|---|
f91f0fd5 TL |
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) 2020 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 "include/cephfs/libcephfs.h" | |
17 | #include "common/ceph_context.h" | |
18 | #include <errno.h> | |
19 | #include <fcntl.h> | |
20 | #include <unistd.h> | |
21 | #include <sys/types.h> | |
22 | #include <sys/stat.h> | |
23 | ||
24 | class MonConfig : public ::testing::Test | |
25 | { | |
26 | protected: | |
27 | struct ceph_mount_info *ca; | |
28 | ||
29 | void SetUp() override { | |
30 | ASSERT_EQ(0, ceph_create(&ca, NULL)); | |
31 | ASSERT_EQ(0, ceph_conf_read_file(ca, NULL)); | |
32 | ASSERT_EQ(0, ceph_conf_parse_env(ca, NULL)); | |
33 | } | |
34 | ||
35 | void TearDown() override { | |
36 | ceph_shutdown(ca); | |
37 | } | |
38 | ||
39 | // Helper to remove/unset all possible mon information from ConfigProxy | |
40 | void clear_mon_config(CephContext *cct) { | |
41 | auto& conf = cct->_conf; | |
42 | // Clear safe_to_start_threads, allowing updates to config values | |
43 | conf._clear_safe_to_start_threads(); | |
44 | ASSERT_EQ(0, conf.set_val("monmap", "", nullptr)); | |
45 | ASSERT_EQ(0, conf.set_val("mon_host", "", nullptr)); | |
46 | ASSERT_EQ(0, conf.set_val("mon_dns_srv_name", "", nullptr)); | |
47 | conf.set_safe_to_start_threads(); | |
48 | } | |
49 | ||
50 | // Helper to test basic operation on a mount | |
20effc67 | 51 | void use_mount(struct ceph_mount_info *mnt, std::string name_prefix) { |
f91f0fd5 TL |
52 | char name[20]; |
53 | snprintf(name, sizeof(name), "%s.%d", name_prefix.c_str(), getpid()); | |
54 | int fd = ceph_open(mnt, name, O_CREAT|O_RDWR, 0644); | |
55 | ASSERT_LE(0, fd); | |
56 | ||
57 | ceph_close(mnt, fd); | |
58 | } | |
59 | }; | |
60 | ||
61 | TEST_F(MonConfig, MonAddrsMissing) { | |
62 | CephContext *cct; | |
63 | ||
64 | // Test mount failure when there is no known mon config source | |
65 | cct = ceph_get_mount_context(ca); | |
66 | ASSERT_NE(nullptr, cct); | |
67 | clear_mon_config(cct); | |
68 | ||
69 | ASSERT_EQ(-ENOENT, ceph_mount(ca, NULL)); | |
70 | } | |
71 | ||
72 | TEST_F(MonConfig, MonAddrsInConfigProxy) { | |
73 | // Test a successful mount with default mon config source in ConfigProxy | |
74 | ASSERT_EQ(0, ceph_mount(ca, NULL)); | |
75 | ||
76 | use_mount(ca, "foo"); | |
77 | } | |
78 | ||
79 | TEST_F(MonConfig, MonAddrsInCct) { | |
80 | struct ceph_mount_info *cb; | |
81 | CephContext *cct; | |
82 | ||
83 | // Perform mount to bootstrap mon addrs in CephContext | |
84 | ASSERT_EQ(0, ceph_mount(ca, NULL)); | |
85 | ||
86 | // Reuse bootstrapped CephContext, clearing ConfigProxy mon addr sources | |
87 | cct = ceph_get_mount_context(ca); | |
88 | ASSERT_NE(nullptr, cct); | |
89 | clear_mon_config(cct); | |
90 | ASSERT_EQ(0, ceph_create_with_context(&cb, cct)); | |
91 | ||
92 | // Test a successful mount with only mon values in CephContext | |
93 | ASSERT_EQ(0, ceph_mount(cb, NULL)); | |
94 | ||
95 | use_mount(ca, "bar"); | |
96 | use_mount(cb, "bar"); | |
97 | ||
98 | ceph_shutdown(cb); | |
99 | } |