MountSessionTest.cpp 15 KB
Newer Older
1
/******************************************************************************
Eric Cano's avatar
Eric Cano committed
2
 *                      tapeserverdTest.cpp
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, 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 General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * 
 *
 * @author Castor Dev team, castor-dev@cern.ch
 *****************************************************************************/

25
26
27
28
#define __STDC_CONSTANT_MACROS // For using stdint macros (stdint is included
// by inttypes.h, so we shoot first)
#include <stdint.h>
#include <inttypes.h>
29
#include <gtest/gtest.h>
30
31
32
#include "castor/tape/tapeserver/client/ClientSimulator.hpp"
#include "castor/tape/tapeserver/client/ClientSimSingleReply.hpp"
#include "castor/tape/tapeserver/client/ClientProxy.hpp"
33
#include "castor/tape/tapeserver/daemon/CapabilityUtilsDummy.hpp"
34
#include "../threading/Threading.hpp"
Eric Cano's avatar
Eric Cano committed
35
36
#include "castor/log/StringLogger.hpp"
#include "MountSession.hpp"
37
38
#include "../system/Wrapper.hpp"
#include "Ctape.h"
39
40
41
42
#include "castor/tape/tapegateway/Volume.hpp"
#include "castor/tape/tapegateway/NoMoreFiles.hpp"
#include "castor/tape/tapegateway/EndNotificationErrorReport.hpp"
#include "castor/tape/tapegateway/NotificationAcknowledge.hpp"
43
#include "castor/tape/tapeserver/file/File.hpp"
Eric Cano's avatar
Eric Cano committed
44
45
#include "castor/tape/tapegateway/RetryPolicyElement.hpp"
#include "smc_struct.h"
46
47
48
49
#include "castor/legacymsg/VmgrProxyDummy.hpp"
#include "castor/legacymsg/VdqmProxyDummy.hpp"
#include "castor/legacymsg/RmcProxyDummy.hpp"
#include "castor/legacymsg/TapeserverProxyDummy.hpp"
Eric Cano's avatar
Eric Cano committed
50
51
52
53
54
55
56
#include <sys/mman.h>
#include <zlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

57

58
using namespace castor::tape::tapeserver;
59
using namespace castor::tape::tapeserver::daemon;
60
61
62
63
namespace unitTest {

class clientRunner: public castor::tape::threading::Thread {
public:
64
  clientRunner(client::ClientSimulator &client): m_sim(client) {}
65
66
67
68
private:
  void run() {
    m_sim.sessionLoop();
  }
69
  client::ClientSimulator & m_sim;
70
71
72
};

  
Eric Cano's avatar
Eric Cano committed
73
TEST(tapeServer, MountSessionGooddayRecall) {
74
75
76
77
  // TpcpClients only supports 32 bits session number
  // This number has to be less than 2^31 as in addition there is a mix
  // of signed and unsigned numbers
  // As the current ids in prod are ~30M, we are far from overflow (Feb 2013)
Eric Cano's avatar
Eric Cano committed
78
79
80
  // 0) Prepare the logger for everyone
  castor::log::StringLogger logger("tapeServerUnitTest");
  
81
  // 1) prepare the client and run it in another thread
82
83
  uint32_t volReq = 0xBEEF;
  std::string vid = "V12345";
84
  std::string density = "8000GC";
85
86
  client::ClientSimulator sim(volReq, vid, density, tapegateway::READ_TP,
    tapegateway::READ);
87
  client::ClientSimulator::ipPort clientAddr = sim.getCallbackAddress();
88
89
  clientRunner simRun(sim);
  simRun.start();
90
91
  
  // 2) Prepare the VDQM request
92
  castor::legacymsg::RtcpJobRqstMsgBody VDQMjob;
93
94
  snprintf(VDQMjob.clientHost, CA_MAXHOSTNAMELEN+1, "%d.%d.%d.%d",
    clientAddr.a, clientAddr.b, clientAddr.c, clientAddr.d);
95
96
  snprintf(VDQMjob.driveUnit, CA_MAXUNMLEN+1, "T10D6116");
  snprintf(VDQMjob.dgn, CA_MAXDGNLEN+1, "LIBXX");
97
98
99
  VDQMjob.clientPort = clientAddr.port;
  VDQMjob.volReqId = volReq;
  
100
101
102
  // 3) Prepare the necessary environment (logger, plus system wrapper), 
  // construct and run the session.
  castor::tape::System::mockWrapper mockSys;
103
104
  mockSys.delegateToFake();
  mockSys.disableGMockCallsCounting();
105
  mockSys.fake.setupForVirtualDriveSLC6();
106
107
108
109

  //delete is unnecessary
  //pointer with ownership will be passed to the application,
  //which will do the delete 
110
  mockSys.fake.m_pathToDrive["/dev/nst0"] = new castor::tape::drives::FakeDrive;
111
  
112
113
114
115
116
117
118
  // We can prepare files for reading on the drive
  {
    // Label the tape
    castor::tape::tapeFile::LabelSession ls(*mockSys.fake.m_pathToDrive["/dev/nst0"], 
        "V12345", true);
    mockSys.fake.m_pathToDrive["/dev/nst0"]->rewind();
    // And write to it
119
120
121
    castor::tape::tapeserver::client::ClientInterface::VolumeInfo volInfo;
    volInfo.vid="V12345";
    volInfo.clientType=castor::tape::tapegateway::READ_TP;
122
    castor::tape::tapeFile::WriteSession ws(*mockSys.fake.m_pathToDrive["/dev/nst0"],
123
       volInfo , 0, true);
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    // Write a few files on the virtual tape
    // Prepare the data
    uint8_t data[1000];
    castor::tape::SCSI::Structures::zeroStruct(&data);
    for (int fseq=1; fseq <= 10 ; fseq ++) {
      castor::tape::tapegateway::FileToRecallStruct ftr;
      castor::tape::tapegateway::FileToMigrateStruct ftm_temp;
      ftr.setFseq(fseq);
      ftm_temp.setFseq(fseq);
      ftr.setFileid(1000 + fseq);
      ftm_temp.setFileid(1000 + fseq);
      castor::tape::tapeFile::WriteFile wf(&ws, ftm_temp, 256*1024);
      // Cut up the position into the old-style BlockId0-3
      ftr.setBlockId0(wf.getPosition() >> 24);
      ftr.setBlockId1( (wf.getPosition() >> 16) & 0xFF);
      ftr.setBlockId2( (wf.getPosition() >> 8) & 0xFF);
      ftr.setBlockId3(wf.getPosition() & 0xFF);
Eric Cano's avatar
Eric Cano committed
141
142
      // Set the recall destination (/dev/null)
      ftr.setPath("/dev/null");
143
144
145
146
147
148
149
150
      // Write the data (one block)
      wf.write(data, sizeof(data));
      // Close the file
      wf.close();
      // Record the file for recall
      sim.addFileToRecall(ftr, sizeof(data));
    }
  }
151
152
153
  utils::TpconfigLines tpConfig;
  // Actual TPCONFIG lifted from prod
  tpConfig.push_back(utils::TpconfigLine("T10D6116", "T10KD6", 
154
  "/dev/tape_T10D6116", "8000GC", "acs0,1,1,6", "T10000"));
155
  tpConfig.push_back(utils::TpconfigLine("T10D6116", "T10KD6", 
156
  "/dev/tape_T10D6116", "5000GC", "acs0,1,1,6", "T10000"));
157
  MountSession::CastorConf castorConf;
158
  castorConf.rtcopydBufsz = 1024*1024; // 1 MB memory buffers
159
  castorConf.rtcopydNbBufs = 10;
160
161
  castorConf.tapebridgeBulkRequestRecallMaxBytes = UINT64_C(100)*1000*1000*1000;
  castorConf.tapebridgeBulkRequestRecallMaxFiles = 1000;
162
  castorConf.tapeserverdDiskThreads = 1;
163
  castor::legacymsg::RmcProxyDummy rmc;
164
  CapabilityUtilsDummy capUtils;
165
166
167
168
169
170
171
172
173
174
175
176
177
  castor::legacymsg::TapeserverProxyDummy initialProcess;
  char argv_container [] = "tapeserver\0XXXXXXXX\0YYYYYYYYY\0ZZZZZZZZZZZ\0";
  int argc = 4;
  char * argv [4];
  {
    std::string argv_ctn_str(argv_container);
    size_t pos = 0;
    for (int i=0; i<4; i++) {
      argv[i] = & (argv_container[pos]);
      argv_ctn_str.find_first_of("\0",pos);
    }
  }
  MountSession sess(argc, argv, "tapeHost", VDQMjob, logger, mockSys, tpConfig, 
178
    rmc, initialProcess, capUtils, castorConf);
Eric Cano's avatar
Eric Cano committed
179
  sess.execute();
180
  simRun.wait();
Eric Cano's avatar
Eric Cano committed
181
182
  std::string temp = logger.getLog();
  temp += "";
183
  ASSERT_EQ("V12345", sess.getVid());
184
185
}

186
187
188
189
190
191
192
193
194
TEST(tapeServer, MountSessionNoSuchDrive) {
  // TpcpClients only supports 32 bits session number
  // This number has to be less than 2^31 as in addition there is a mix
  // of signed and unsigned numbers
  // As the current ids in prod are ~30M, we are far from overflow (Feb 2013)
  // 1) prepare the client and run it in another thread
  uint32_t volReq = 0xBEEF;
  std::string vid = "V12345";
  std::string density = "8000GC";
195
196
  client::ClientSimulator sim(volReq, vid, density, tapegateway::READ_TP,
    tapegateway::READ);
197
  client::ClientSimulator::ipPort clientAddr = sim.getCallbackAddress();
198
199
200
201
  clientRunner simRun(sim);
  simRun.start();
  
  // 2) Prepare the VDQM request
202
  castor::legacymsg::RtcpJobRqstMsgBody VDQMjob;
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  snprintf(VDQMjob.clientHost, CA_MAXHOSTNAMELEN+1, "%d.%d.%d.%d",
    clientAddr.a, clientAddr.b, clientAddr.c, clientAddr.d);
  snprintf(VDQMjob.driveUnit, CA_MAXUNMLEN+1, "T10D6116");
  snprintf(VDQMjob.dgn, CA_MAXDGNLEN+1, "LIBXX");
  VDQMjob.clientPort = clientAddr.port;
  VDQMjob.volReqId = volReq;
  
  // 3) Prepare the necessary environment (logger, plus system wrapper), 
  // construct and run the session.
  castor::log::StringLogger logger("tapeServerUnitTest");
  castor::tape::System::mockWrapper mockSys;
  mockSys.delegateToFake();
  mockSys.disableGMockCallsCounting();
  mockSys.fake.setupForVirtualDriveSLC6();
  utils::TpconfigLines tpConfig;
  // Actual TPCONFIG lifted from prod
  tpConfig.push_back(utils::TpconfigLine("T10D6116", "T10KD6", 
220
  "/dev/noSuchTape", "8000GC", "acs0,1,1,6", "T10000"));
221
  tpConfig.push_back(utils::TpconfigLine("T10D6116", "T10KD6", 
222
  "/dev/noSuchTape", "5000GC", "acs0,1,1,6", "T10000"));
223
224
225
  MountSession::CastorConf castorConf;
  castorConf.rtcopydBufsz = 1024;
  castorConf.rtcopydNbBufs = 10;
226
227
228
229
  castor::legacymsg::VmgrProxyDummy vmgr;
  castor::legacymsg::VdqmProxyDummy vdqm(VDQMjob);
  castor::legacymsg::RmcProxyDummy rmc;
  castor::legacymsg::TapeserverProxyDummy initialProcess;
230
  CapabilityUtilsDummy capUtils;
231
232
233
234
235
236
237
238
239
240
241
242
  char argv_container [] = "tapeserver\0XXXXXXXX\0YYYYYYYYY\0ZZZZZZZZZZZ\0";
  int argc = 4;
  char * argv [4];
  {
    std::string argv_ctn_str(argv_container);
    size_t pos = 0;
    for (int i=0; i<4; i++) {
      argv[i] = & (argv_container[pos]);
      argv_ctn_str.find_first_of("\0",pos);
    }
  }
  MountSession sess(argc, argv, "tapeHost", VDQMjob, logger, mockSys, tpConfig, 
243
    rmc, initialProcess, capUtils, castorConf);
244
245
246
247
248
249
250
  sess.execute();
  simRun.wait();
  std::string temp = logger.getLog();
  temp += "";
  ASSERT_NE(std::string::npos, logger.getLog().find("Drive not found on this path"));
}

Eric Cano's avatar
Eric Cano committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
class tempFile {
public:
  tempFile(size_t size): m_size(size) {
    // Create a temporary file
    int randomFd = open("/dev/urandom", 0);
    castor::exception::Errnum::throwOnMinusOne(randomFd, "Error opening /dev/urandom");
    char path[100];
    strncpy(path, "/tmp/castorUnitTestMigrationSourceXXXXXX", 100);
    int tmpFileFd = mkstemp(path);
    castor::exception::Errnum::throwOnMinusOne(tmpFileFd, "Error creating a temporary file");
    m_path = path;
    char * buff = NULL;
    try {
      buff = new char[size];
      if(!buff) { throw castor::exception::Exception("Failed to allocate memory to read /dev/urandom"); }
      castor::exception::Errnum::throwOnMinusOne(read(randomFd, buff, size));
      castor::exception::Errnum::throwOnMinusOne(write(tmpFileFd, buff, size));
      m_checksum = adler32(0L, Z_NULL, 0);
      m_checksum = adler32(m_checksum, (const Bytef *)buff, size);
      close(randomFd);
      close(tmpFileFd);
Eric Cano's avatar
Eric Cano committed
272
      delete[] buff;
Eric Cano's avatar
Eric Cano committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
    } catch (...) {
      delete[] buff;
      unlink(m_path.c_str());
      throw;
    }
  }
  
  ~tempFile() {
    unlink(m_path.c_str());
  }
  const size_t m_size;
  const std::string path() const { return m_path; }
  uint32_t checksum() const { return m_checksum; }
private:
  std::string m_path;
  uint32_t m_checksum;
};

class tempFileVector: public std::vector<tempFile *> {
public:
  ~tempFileVector() {
    while(size()) {
      delete back();
      pop_back();
    }
  }
};

struct expectedResult {
  expectedResult(int fs, uint32_t cs): fSeq(fs), checksum(cs) {}
  int fSeq;
  uint32_t checksum;
};

TEST(tapeServer, MountSessionGooddayMigration) {
  // TpcpClients only supports 32 bits session number
  // This number has to be less than 2^31 as in addition there is a mix
  // of signed and unsigned numbers
  // As the current ids in prod are ~30M, we are far from overflow (Feb 2013)
  // 0) Prepare the logger for everyone
  castor::log::StringLogger logger("tapeServerUnitTest");
  
  // 1) prepare the client and run it in another thread
  uint32_t volReq = 0xBEEF;
  std::string vid = "V12345";
  std::string density = "8000GC";
  client::ClientSimulator sim(volReq, vid, density, tapegateway::WRITE_TP,
    tapegateway::WRITE);
  client::ClientSimulator::ipPort clientAddr = sim.getCallbackAddress();
  clientRunner simRun(sim);
  simRun.start();
  
  // 2) Prepare the VDQM request
Eric Cano's avatar
Eric Cano committed
326
  castor::legacymsg::RtcpJobRqstMsgBody VDQMjob;
Eric Cano's avatar
Eric Cano committed
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
  snprintf(VDQMjob.clientHost, CA_MAXHOSTNAMELEN+1, "%d.%d.%d.%d",
    clientAddr.a, clientAddr.b, clientAddr.c, clientAddr.d);
  snprintf(VDQMjob.driveUnit, CA_MAXUNMLEN+1, "T10D6116");
  snprintf(VDQMjob.dgn, CA_MAXDGNLEN+1, "LIBXX");
  VDQMjob.clientPort = clientAddr.port;
  VDQMjob.volReqId = volReq;
  
  // 3) Prepare the necessary environment (logger, plus system wrapper), 
  // construct and run the session.
  castor::tape::System::mockWrapper mockSys;
  mockSys.delegateToFake();
  mockSys.disableGMockCallsCounting();
  mockSys.fake.setupForVirtualDriveSLC6();

  //delete is unnecessary
  //pointer with ownership will be passed to the application,
  //which will do the delete 
  mockSys.fake.m_pathToDrive["/dev/nst0"] = new castor::tape::drives::FakeDrive;
  
  // Just label the tape
  castor::tape::tapeFile::LabelSession ls(*mockSys.fake.m_pathToDrive["/dev/nst0"], 
      "V12345", true);
  mockSys.fake.m_pathToDrive["/dev/nst0"]->rewind();
  
  tempFileVector tempFiles;
  std::vector<expectedResult> expected;
  // Prepare the files (in real filesystem as they will be opened by the rfio client)
  for (int fseq=1; fseq<=10; fseq++) {
    // Create the file from which we will recall
    std::auto_ptr<tempFile> tf(new tempFile(1000));
    // Prepare the migrationRequest
    castor::tape::tapegateway::FileToMigrateStruct ftm;
    ftm.setFileSize(tf->m_size);
    ftm.setFileid(1000 + fseq);
    ftm.setFseq(fseq);
    ftm.setPath(tf->path());
    sim.addFileToMigrate(ftm);
    expected.push_back(expectedResult(fseq, tf->checksum()));
    tempFiles.push_back(tf.release());
  }
  utils::TpconfigLines tpConfig;
  // Actual TPCONFIG lifted from prod
  tpConfig.push_back(utils::TpconfigLine("T10D6116", "T10KD6", 
370
  "/dev/tape_T10D6116", "8000GC", "acs0,1,1,6", "T10000"));
Eric Cano's avatar
Eric Cano committed
371
  tpConfig.push_back(utils::TpconfigLine("T10D6116", "T10KD6", 
372
  "/dev/tape_T10D6116", "5000GC", "acs0,1,1,6", "T10000"));
Eric Cano's avatar
Eric Cano committed
373
374
375
  MountSession::CastorConf castorConf;
  castorConf.rtcopydBufsz = 1024*1024; // 1 MB memory buffers
  castorConf.rtcopydNbBufs = 10;
376
377
  castorConf.tapebridgeBulkRequestMigrationMaxBytes = UINT64_C(100)*1000*1000*1000;
  castorConf.tapebridgeBulkRequestMigrationMaxFiles = 1000;
Eric Cano's avatar
Eric Cano committed
378
  castorConf.tapeserverdDiskThreads = 1;
379
380
381
382
  castor::legacymsg::VmgrProxyDummy vmgr;
  castor::legacymsg::VdqmProxyDummy vdqm(VDQMjob);
  castor::legacymsg::RmcProxyDummy rmc;
  castor::legacymsg::TapeserverProxyDummy initialProcess;
383
  CapabilityUtilsDummy capUtils;
384
385
386
387
388
389
390
391
392
393
394
395
  char argv_container [] = "tapeserver\0XXXXXXXX\0YYYYYYYYY\0ZZZZZZZZZZZ\0";
  int argc = 4;
  char * argv [4];
  {
    std::string argv_ctn_str(argv_container);
    size_t pos = 0;
    for (int i=0; i<4; i++) {
      argv[i] = & (argv_container[pos]);
      argv_ctn_str.find_first_of("\0",pos);
    }
  }
  MountSession sess(argc, argv, "tapeHost", VDQMjob, logger, mockSys, tpConfig, 
396
    rmc, initialProcess, capUtils, castorConf);
Eric Cano's avatar
Eric Cano committed
397
398
  sess.execute();
  simRun.wait();
399
400
401
402
  for (std::vector<struct expectedResult>::iterator i = expected.begin();
      i != expected.end(); i++) {
    ASSERT_EQ(i->checksum, sim.m_receivedChecksums[i->fSeq]);
  }
Eric Cano's avatar
Eric Cano committed
403
404
}

405
}
406