0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
fsTest.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 #include <Common/Compat.h>
23 
24 #include "FsTestThreadFunction.h"
25 
26 #include <FsBroker/Lib/Client.h>
27 
30 
31 #include <Common/Init.h>
32 #include <Common/Error.h>
33 #include <Common/FileUtils.h>
34 #include <Common/InetAddr.h>
35 #include <Common/Logger.h>
36 #include <Common/System.h>
37 #include <Common/Usage.h>
38 #include <Common/Thread.h>
39 #include <Common/StaticBuffer.h>
40 #include <Common/Config.h>
41 
42 #include <boost/thread/thread.hpp>
43 
44 #include <algorithm>
45 #include <fstream>
46 #include <iostream>
47 #include <string>
48 #include <vector>
49 
50 extern "C" {
51 #include <poll.h>
52 #include <signal.h>
53 #include <sys/types.h>
54 #include <unistd.h>
55 }
56 
57 using namespace Hypertable;
58 using namespace Tools::client::fsbroker;
59 using namespace std;
60 
61 namespace {
62  const char *usage[] = {
63  "usage: fsTest",
64  "",
65  " This program tests the operation of the DFS and DFS broker",
66  " by copying the file /usr/share/dict/words to the DFS via the",
67  " broker, then copying it back and making sure the returned copy",
68  " matches the original. It assumes the DFS broker is listenting",
69  " at localhost:38546",
70  (const char *)0
71  };
72 
73  void test_copy(FsBroker::Lib::ClientPtr &client, const string &testdir) {
74  string outfileA = testdir + "/output.a";
75  string outfileB = testdir + "/output.b";
76 
77  FsTestThreadFunction thread_func(client, "words");
78 
79  thread_func.set_dfs_file(outfileA);
80  thread_func.set_output_file("output.a");
81  Thread thread1(thread_func);
82 
83  thread_func.set_dfs_file(outfileB);
84  thread_func.set_output_file("output.b");
85  Thread thread2(thread_func);
86 
87  thread1.join();
88  thread2.join();
89 
90  if (system("cmp words output.a"))
91  exit(EXIT_FAILURE);
92 
93  if (system("cmp output.a output.b"))
94  exit(EXIT_FAILURE);
95  }
96 
97  void test_readdir(FsBroker::Lib::ClientPtr &client, const string &testdir) {
98  ofstream filestr ("fsTest.out");
99  vector<Filesystem::Dirent> listing;
100 
101  client->readdir(testdir, listing);
102 
103  sort(listing.begin(), listing.end());
104 
105  for (size_t i=0; i<listing.size(); i++) {
106  // Directory sizes are reported differently on various filesystems
107  if (listing[i].is_dir)
108  filestr << "0 " << listing[i].name << "/";
109  else
110  filestr << listing[i].length << " " << listing[i].name;
111  filestr << "\n";
112  }
113 
114  filestr.close();
115 
116  if (system("diff fsTest.out fsTest.golden"))
117  exit(EXIT_FAILURE);
118  }
119 
120  void test_rename(FsBroker::Lib::ClientPtr &client, const string &testdir) {
121  const char *magic = "the quick brown fox jumps over a lazy dog";
122  char buf[1024];
123  string file_a = testdir +"/filename.a";
124  string file_b = testdir +"/filename.b";
125  int fd = client->create(file_a, Filesystem::OPEN_FLAG_OVERWRITE, -1, -1, -1);
126  StaticBuffer sbuf((char *)magic, strlen(magic) + 1, false);
127  client->append(fd, sbuf);
128  client->close(fd);
129  client->rename(file_a, file_b);
130  fd = client->open(file_b, 0);
131  client->read(fd, buf, sizeof(buf));
132  HT_ASSERT(strcmp(buf, magic) == 0);
133  client->close(fd);
134  }
135 }
136 
137 
138 int main(int argc, char **argv) {
139  try {
140  struct sockaddr_in addr;
141  ConnectionManagerPtr conn_mgr;
143 
144  Config::init(argc, argv);
145 
146  if (argc != 1)
147  Usage::dump_and_exit(usage);
148 
149  System::initialize(argv[0]);
151 
152  uint16_t port = Config::properties->get_i16("FsBroker.Port");
153 
154  InetAddr::initialize(&addr, "localhost", port);
155 
156  conn_mgr = make_shared<ConnectionManager>();
157  client = make_shared<FsBroker::Lib::Client>(conn_mgr, addr, 15000);
158 
159  if (!client->wait_for_connection(15000)) {
160  HT_ERROR("Unable to connect to DFS");
161  return 1;
162  }
163  string testdir = format("/fsTest%d", (int)getpid());
164  client->mkdirs(testdir);
165 
166  test_copy(client, testdir);
167 
168  string subdir = testdir + "/mydir";;
169  client->mkdirs(subdir);
170  test_readdir(client, testdir);
171 
172  test_rename(client, testdir);
173 
174  client->rmdir(testdir);
175  }
176  catch (Exception &e) {
177  HT_ERROR_OUT << e << HT_END;
178  return 1;
179  }
180  catch (...) {
181  HT_ERROR_OUT << "unexpected exception caught" << HT_END;
182  return 1;
183  }
184 }
A memory buffer of static size.
Definition: StaticBuffer.h:45
Retrieves system information (hardware, installation directory, etc)
PropertiesPtr properties
This singleton map stores all options.
Definition: Config.cc:47
static void initialize(uint16_t reactor_count)
Initializes I/O reactors.
String format(const char *fmt,...)
Returns a String using printf like format facilities Vanilla snprintf is about 1.5x faster than this...
Definition: String.cc:37
Helper class for printing usage banners on the command line.
void init(int argc, char *argv[], const Desc *desc=NULL)
Initialize with default policy.
Definition: Init.h:95
static void initialize(const String &install_directory=String())
Initializes the static class members; checks header version vs.
Definition: System.h:72
STL namespace.
#define HT_ASSERT(_e_)
Definition: Logger.h:396
int main(int argc, char **argv)
Definition: fsTest.cc:138
File system utility functions.
std::shared_ptr< Client > ClientPtr
Smart pointer to Client.
Definition: Client.h:233
Logging routines and macros.
Compatibility Macros for C/C++.
Thread function class for fsbroker test.
Initialization helper for applications.
#define HT_END
Definition: Logger.h:220
A memory buffer of static size.
Importing boost::thread and boost::thread_group into the Hypertable namespace.
#define HT_ERROR_OUT
Definition: Logger.h:301
Declarations for FsTestThreadFunction.
Hypertable definitions
static bool initialize(sockaddr_in *addr, const char *host, uint16_t port)
Initialize a sockaddr_in structure from host:port.
Definition: InetAddr.cc:68
#define HT_ERROR(msg)
Definition: Logger.h:299
Declarations for ConnectionManager.
Internet address wrapper classes and utility functions.
Declarations for ReactorFactory.
This is a generic exception class for Hypertable.
Definition: Error.h:314
boost::thread Thread
Definition: Thread.h:45
Configuration settings.
std::shared_ptr< ConnectionManager > ConnectionManagerPtr
Smart pointer to ConnectionManager.
static void dump_and_exit(const char **usage, int rcode=1)
Same as dump, but performs _exit(rcode) afterwards.
Definition: Usage.cc:41
Error codes, Exception handling, error logging.
Declarations for Client.