DataTransferSessionTest.cpp 40.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/******************************************************************************
 *
 * 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
 *****************************************************************************/

24
25
26
27
#define __STDC_CONSTANT_MACROS // For using stdint macros (stdint is included
// by inttypes.h, so we shoot first)
#include <stdint.h>
#include <inttypes.h>
28
#include <gtest/gtest.h>
29

30
#include "castor/legacymsg/RmcProxyDummy.hpp"
31
#include "castor/log/StringLogger.hpp"
32
33
34
#include "castor/mediachanger/MediaChangerFacade.hpp"
#include "castor/mediachanger/MmcProxyDummy.hpp"
#include "castor/messages/AcsProxyDummy.hpp"
35
#include "castor/messages/TapeserverProxyDummy.hpp"
36
#include "castor/server/ProcessCapDummy.hpp"
37
#include "castor/server/Threading.hpp"
38
#include "castor/tape/tapeserver/daemon/DataTransferSession.hpp"
39
#include "castor/tape/tapeserver/daemon/VolumeInfo.hpp"
40
41
#include "castor/tape/tapeserver/system/Wrapper.hpp"
#include "castor/tape/tapeserver/file/File.hpp"
42
#include "castor/tape/tapeserver/drive/FakeDrive.hpp"
43
44
#include "common/exception/Exception.hpp"
#include "common/Utils.hpp"
45
#include "Ctape.h"
46
#include "scheduler/Scheduler.hpp"
47
#include "smc_struct.h"
48
49
#include "nameserver/mockNS/MockNameServer.hpp"
#include "remotens/MockRemoteNS.hpp"
50
#include "scheduler/DummyScheduler.hpp"
51
#include "scheduler/mockDB/MockSchedulerDatabase.hpp"
52
#include "scheduler/MountType.hpp"
53

54
#include <dirent.h>
55
#include <fcntl.h>
56
#include <stdexcept>
Eric Cano's avatar
Eric Cano committed
57
58
59
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
60
61
#include <unistd.h>
#include <zlib.h>
Eric Cano's avatar
Eric Cano committed
62

63
using namespace castor::tape::tapeserver;
64
using namespace castor::tape::tapeserver::daemon;
65
66
namespace unitTest {

67
68
class castor_tape_tapeserver_daemon_DataTransferSessionTest: public
  ::testing::Test {
69
70
71
protected:

  void SetUp() {
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    strncpy(m_tmpDir, "/tmp/DataTransferSessionTestXXXXXX", sizeof(m_tmpDir));
    if(!mkdtemp(m_tmpDir)) {
      const std::string errMsg = cta::Utils::errnoToString(errno);
      std::ostringstream msg;
      msg << "Failed to create directory with template"
        " /tmp/DataTransferSessionTestXXXXXX: " << errMsg;
      bzero(m_tmpDir, sizeof(m_tmpDir));
      throw cta::exception::Exception(msg.str());
    }

    struct stat statBuf;
    bzero(&statBuf, sizeof(statBuf));
    if(stat(m_tmpDir, &statBuf)) {
      const std::string errMsg = cta::Utils::errnoToString(errno);
      std::ostringstream msg;
      msg << "Failed to stat directory " << m_tmpDir << ": " << errMsg;
      throw cta::exception::Exception(msg.str());
    }

    std::ostringstream cmd;
    cmd << "touch " << m_tmpDir << "/hello";
    system(cmd.str().c_str());
94
95
96
  }

  void TearDown() {
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    // If Setup() created a temporary directory
    if(m_tmpDir) {

      // Openn the directory
      std::unique_ptr<DIR, std::function<int(DIR*)>>
        dir(opendir(m_tmpDir), closedir);
      if(NULL == dir.get()) {
        const std::string errMsg = cta::Utils::errnoToString(errno);
        std::ostringstream msg;
        msg << "Failed to open directory " << m_tmpDir << ": " << errMsg;
        throw cta::exception::Exception(msg.str());
      }

      // Delete each of the files within the directory
      struct dirent *entry = NULL;
      while((entry = readdir(dir.get()))) {
        const std::string entryName(entry->d_name);
        if(entryName != "." && entryName != "..") {
          const std::string entryPath = std::string(m_tmpDir) + "/" + entryName;
          if(unlink(entryPath.c_str())) {
            const std::string errMsg = cta::Utils::errnoToString(errno);
            std::ostringstream msg;
            msg << "Failed to unlink " << entryPath;
            throw cta::exception::Exception(msg.str());
          }
        }
      }

      // Delete the now empty directory
      if(rmdir(m_tmpDir)) {
        const std::string errMsg = cta::Utils::errnoToString(errno);
        std::ostringstream msg;
        msg << "Failed to delete directory " << m_tmpDir << ": " << errMsg;
        throw cta::exception::Exception(msg.str());
      }
    }
133
  }
134

135
136
137
138
139
140
141
  /**
   * Temporary directory created with mkdtemp that will be used to contain the
   * destination remote files of the tests that need to create them.
   *
   * Please note that a new temporary directory is created and deleted for each
   * test by the Setup() and TearDown() methods.
   */
142
143
  char m_tmpDir[100];

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  class MockArchiveJob: public cta::ArchiveJob {
  public:
    MockArchiveJob() {
    } 
      
    ~MockArchiveJob() throw() {
    } 
  };

  class MockRetrieveJob: public cta::RetrieveJob {
  public:
    MockRetrieveJob() {
    } 
      
    ~MockRetrieveJob() throw() {
    } 
  };
161
162
};

163
TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessionGooddayRecall) {
Eric Cano's avatar
Eric Cano committed
164
165
166
  // 0) Prepare the logger for everyone
  castor::log::StringLogger logger("tapeServerUnitTest");
  
167
  // 1) prepare the fake scheduler
168
  std::string vid = "V12345";
169
  // cta::MountType::Enum mountType = cta::MountType::RETRIEVE;
170
  std::string density = "8000GC";
171

172
173
  // 3) Prepare the necessary environment (logger, plus system wrapper), 
  castor::tape::System::mockWrapper mockSys;
174
175
  mockSys.delegateToFake();
  mockSys.disableGMockCallsCounting();
176
  mockSys.fake.setupForVirtualDriveSLC6();
177
178
179
  //delete is unnecessary
  //pointer with ownership will be passed to the application,
  //which will do the delete 
180
  mockSys.fake.m_pathToDrive["/dev/nst0"] = new castor::tape::tapeserver::drive::FakeDrive;
181
182
183
184
185
186
187
188
189
190
191
192
193

  // 4) Create the scheduler
  cta::MockNameServer ns;
  cta::MockRemoteNS rns;
  cta::MockSchedulerDatabase db;
  cta::Scheduler scheduler(ns, db, rns);

  // Always use the same requester
  const cta::SecurityIdentity requester;

  // List to remember the path of each remote file so that the existance of the
  // files can be tested for at the end of the test
  std::list<std::string> remoteFilePaths;
194
  
195
  // 5) Prepare files for reading by writing them to the mock system
196
197
198
  {
    // Label the tape
    castor::tape::tapeFile::LabelSession ls(*mockSys.fake.m_pathToDrive["/dev/nst0"], 
199
        "V12345");
200
201
    mockSys.fake.m_pathToDrive["/dev/nst0"]->rewind();
    // And write to it
202
    castor::tape::tapeserver::daemon::VolumeInfo volInfo;
203
    volInfo.vid="V12345";
204
    castor::tape::tapeFile::WriteSession ws(*mockSys.fake.m_pathToDrive["/dev/nst0"],
205
       volInfo , 0, true);
206
207
208

    // Write a few files on the virtual tape and modify the archive name space
    // so that it is in sync
209
210
211
    uint8_t data[1000];
    castor::tape::SCSI::Structures::zeroStruct(&data);
    for (int fseq=1; fseq <= 10 ; fseq ++) {
212
213
      // Create a path to a remote destination file
      std::ostringstream remoteFilePath;
214
      remoteFilePath << "file:" << m_tmpDir << "/test" << fseq;
215
216
      remoteFilePaths.push_back(remoteFilePath.str());

217
      // Create an archive file entry in the archive namespace
218
219
220
221
      std::ostringstream archiveFilePath;
      archiveFilePath << "/test" << fseq;
      const mode_t archiveFileMode = 0655;
      const uint64_t archiveFileSize = 256*1024;
222
      ASSERT_NO_THROW(ns.createFile(
223
224
225
        requester,
        archiveFilePath.str(),
        archiveFileMode,
226
227
        archiveFileSize));
        
228
      // Write the file to tape
229
      std::unique_ptr<cta::RetrieveJob> ftr(new MockRetrieveJob());
230
      std::unique_ptr<cta::ArchiveJob> ftm(new MockArchiveJob());
231
      ftr->tapeFileLocation.fSeq = fseq;
232
      ftm->tapeFileLocation.fSeq = fseq;
233
      ftr->archiveFile.fileId = 1000 + fseq;
234
235
      ftm->archiveFile.fileId = 1000 + fseq;
      castor::tape::tapeFile::WriteFile wf(&ws, *ftm, archiveFileSize);
236
      ftr->tapeFileLocation.blockId = wf.getPosition();
237
      ftr->remotePathAndStatus.path = remoteFilePath.str();
238
239
240
241
      // Write the data (one block)
      wf.write(data, sizeof(data));
      // Close the file
      wf.close();
242

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
      // Create tape file entry in the archive namespace
      cta::NameServerTapeFile tapeFile;
      tapeFile.copyNb = 1;
      tapeFile.tapeFileLocation.fSeq = fseq;
      tapeFile.tapeFileLocation.blockId = wf.getPosition();
      tapeFile.tapeFileLocation.vid = volInfo.vid;
      tapeFile.tapeFileLocation.copyNb = 1;
      tapeFile.size = archiveFileSize;
      tapeFile.compressedSize = archiveFileSize; // No compression
      cta::Checksum tapeFileChecksum(cta::Checksum::CHECKSUMTYPE_ADLER32,
        cta::ByteArray(cta::Utils::getAdler32(data, sizeof data)));
      tapeFile.checksum = tapeFileChecksum;
      ASSERT_NO_THROW(ns.addTapeFile(
        requester,
        archiveFilePath.str(),
        tapeFile));

260
261
262
263
264
265
266
      // Schedule the retrieval of the file
      std::list<std::string> archiveFilePaths;
      archiveFilePaths.push_back(archiveFilePath.str());
      scheduler. queueRetrieveRequest(
        requester,
        archiveFilePaths,
        remoteFilePath.str());
267
268
    }
  }
269
270

  // 6) Create the data transfer session
271
  DriveConfig driveConfig("T10D6116", "T10KD6", "/dev/tape_T10D6116", "manual");
272
273
274
275
276
277
  DataTransferConfig castorConf;
  castorConf.bufsz = 1024*1024; // 1 MB memory buffers
  castorConf.nbBufs = 10;
  castorConf.bulkRequestRecallMaxBytes = UINT64_C(100)*1000*1000*1000;
  castorConf.bulkRequestRecallMaxFiles = 1000;
  castorConf.nbDiskThreads = 1;
278
279
280
281
  castor::messages::AcsProxyDummy acs;
  castor::mediachanger::MmcProxyDummy mmc;
  castor::legacymsg::RmcProxyDummy rmc;
  castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
282
  castor::server::ProcessCap capUtils;
283
  castor::messages::TapeserverProxyDummy initialProcess;
284
  DataTransferSession sess("tapeHost", logger, mockSys,
285
    driveConfig, mc, initialProcess, capUtils, castorConf, scheduler);
286
287

  // 7) Run the data transfer session
288
  ASSERT_NO_THROW(sess.execute());
289
290

  // 8) Check the session git the correct VID
291
  ASSERT_EQ("V12345", sess.getVid());
292

293
  // 9) Check the remote files exist and have the correct size
294
295
296
297
298
299
300
301
  for(auto pathItor = remoteFilePaths.cbegin(); pathItor !=
    remoteFilePaths.cend(); pathItor++) {
    struct stat statBuf;
    bzero(&statBuf, sizeof(statBuf));
    const int statRc = stat(pathItor->c_str(), &statBuf);
    ASSERT_EQ(0, statRc);
    ASSERT_EQ(256*1024, statBuf.st_size);
  }
302
}
303

304
TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessionWrongRecall) {
305
306
307
  // This test is the same as the previous one, with 
  // wrong parameters set for the recall, so that we fail 
  // to recall the first file and cancel the second.
308
309

  // 0) Prepare the logger for everyone
310
311
  castor::log::StringLogger logger("tapeServerUnitTest");
  
312
  // 1) prepare the fake scheduler
313
  std::string vid = "V12345";
314
  // cta::MountType::Enum mountType = cta::MountType::RETRIEVE;
315
  std::string density = "8000GC";
316

317
318
319
320
321
322
323
324
  // 3) Prepare the necessary environment (logger, plus system wrapper), 
  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 
325
  mockSys.fake.m_pathToDrive["/dev/nst0"] = new castor::tape::tapeserver::drive::FakeDrive;
326
327
328
329
330
331
332
333
334
335
336
337
338

  // 4) Create the scheduler
  cta::MockNameServer ns;
  cta::MockRemoteNS rns;
  cta::MockSchedulerDatabase db;
  cta::Scheduler scheduler(ns, db, rns);

  // Always use the same requester
  const cta::SecurityIdentity requester;

  // List to remember the path of each remote file so that the existance of the
  // files can be tested for at the end of the test
  std::list<std::string> remoteFilePaths;
339
  
340
  // 5) Prepare files for reading by writing them to the mock system
341
342
343
  {
    // Label the tape
    castor::tape::tapeFile::LabelSession ls(*mockSys.fake.m_pathToDrive["/dev/nst0"], 
344
        "V12345");
345
346
    mockSys.fake.m_pathToDrive["/dev/nst0"]->rewind();
    // And write to it
347
    castor::tape::tapeserver::daemon::VolumeInfo volInfo;
348
349
350
    volInfo.vid="V12345";
    castor::tape::tapeFile::WriteSession ws(*mockSys.fake.m_pathToDrive["/dev/nst0"],
       volInfo , 0, true);
351
352
353

    // Write a few files on the virtual tape and modify the archive name space
    // so that it is in sync
354
355
356
    uint8_t data[1000];
    castor::tape::SCSI::Structures::zeroStruct(&data);
    for (int fseq=1; fseq <= 10 ; fseq ++) {
357
358
359
360
361
      // Create a path to a remote destination file
      std::ostringstream remoteFilePath;
      remoteFilePath << "file:" << m_tmpDir << "/test" << fseq;
      remoteFilePaths.push_back(remoteFilePath.str());

362
      // Create an archive file entry in the archive namespace
363
364
365
366
      std::ostringstream archiveFilePath;
      archiveFilePath << "/test" << fseq;
      const mode_t archiveFileMode = 0655;
      const uint64_t archiveFileSize = 256*1024;
367
      ASSERT_NO_THROW(ns.createFile(
368
369
370
        requester,
        archiveFilePath.str(),
        archiveFileMode,
371
        archiveFileSize));
372
373

      // Write the file to tape
374
375
376
377
378
379
      std::unique_ptr<cta::RetrieveJob> ftr(new MockRetrieveJob());
      std::unique_ptr<cta::ArchiveJob> ftm_temp(new MockArchiveJob());
      ftr->tapeFileLocation.fSeq = fseq;
      ftm_temp->tapeFileLocation.fSeq = fseq;
      ftr->archiveFile.fileId = 1000 + fseq;
      ftm_temp->archiveFile.fileId = 1000 + fseq;
380
      castor::tape::tapeFile::WriteFile wf(&ws, *ftm_temp, archiveFileSize);
381
      ftr->tapeFileLocation.blockId = wf.getPosition();
382
      ftr->remotePathAndStatus.path = remoteFilePath.str();
383
384
385
386
      // Write the data (one block)
      wf.write(data, sizeof(data));
      // Close the file
      wf.close();
387

388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
      // Create tape file entry in the archive namespace that is beyond the end
      // of data
      cta::NameServerTapeFile tapeFile;
      tapeFile.copyNb = 1;
      tapeFile.tapeFileLocation.fSeq = fseq + 10000;
      tapeFile.tapeFileLocation.blockId = wf.getPosition() + 10000;
      tapeFile.tapeFileLocation.vid = volInfo.vid;
      tapeFile.tapeFileLocation.copyNb = 1;
      tapeFile.size = archiveFileSize;
      tapeFile.compressedSize = archiveFileSize; // No compression
      cta::Checksum tapeFileChecksum(cta::Checksum::CHECKSUMTYPE_ADLER32,
        cta::ByteArray(cta::Utils::getAdler32(data, sizeof data)));
      tapeFile.checksum = tapeFileChecksum;
      ASSERT_NO_THROW(ns.addTapeFile(
        requester,
        archiveFilePath.str(),
        tapeFile));

406
407
408
409
410
411
412
      // Schedule the retrieval of the file
      std::list<std::string> archiveFilePaths;
      archiveFilePaths.push_back(archiveFilePath.str());
      scheduler. queueRetrieveRequest(
        requester,
        archiveFilePaths,
        remoteFilePath.str());
413
414
    }
  }
415
416

  // 6) Create the data transfer session
417
  DriveConfig driveConfig("T10D6116", "T10KD6", "/dev/tape_T10D6116", "manual");
418
419
420
421
422
423
  DataTransferConfig castorConf;
  castorConf.bufsz = 1024*1024; // 1 MB memory buffers
  castorConf.nbBufs = 10;
  castorConf.bulkRequestRecallMaxBytes = UINT64_C(100)*1000*1000*1000;
  castorConf.bulkRequestRecallMaxFiles = 1000;
  castorConf.nbDiskThreads = 1;
424
425
426
427
  castor::messages::AcsProxyDummy acs;
  castor::mediachanger::MmcProxyDummy mmc;
  castor::legacymsg::RmcProxyDummy rmc;
  castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
428
429
  castor::server::ProcessCap capUtils;
  castor::messages::TapeserverProxyDummy initialProcess;
430
  DataTransferSession sess("tapeHost", logger, mockSys,
431
    driveConfig, mc, initialProcess, capUtils, castorConf, scheduler);
432
433
434
435
436

  // 7) Run the data transfer session
  ASSERT_NO_THROW(sess.execute());

  // 8) Check the session git the correct VID
437
  ASSERT_EQ("V12345", sess.getVid());
438
439
440
441
442
443
444
445
446
447

  // 9) Check the remote files exist and have the correct size
  for(auto pathItor = remoteFilePaths.cbegin(); pathItor !=
    remoteFilePaths.cend(); pathItor++) {
    struct stat statBuf;
    bzero(&statBuf, sizeof(statBuf));
    const int statRc = stat(pathItor->c_str(), &statBuf);
    ASSERT_EQ(0, statRc);
    ASSERT_EQ(256*1024, statBuf.st_size);
  }
448
}
449

450
TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessionNoSuchDrive) {
451
452
453
454
455
  
  // 0) Prepare the logger for everyone
  castor::log::StringLogger logger("tapeServerUnitTest");
  
  // 1) prepare the fake scheduler
456
  std::string vid = "V12345";
457
  // cta::MountType::Enum mountType = cta::MountType::RETRIEVE;
458
  std::string density = "8000GC";
459

460
461
462
463
464
  // 3) Prepare the necessary environment (logger, plus system wrapper), 
  castor::tape::System::mockWrapper mockSys;
  mockSys.delegateToFake();
  mockSys.disableGMockCallsCounting();
  mockSys.fake.setupForVirtualDriveSLC6();
465
466
467
468
469
470
471
472
473
474
475
476
477
478
  //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::tapeserver::drive::FakeDrive;

  // 4) Create the scheduler
  cta::MockNameServer ns;
  cta::MockRemoteNS rns;
  cta::MockSchedulerDatabase db;
  cta::Scheduler scheduler(ns, db, rns);

  // Always use the same requester
  const cta::SecurityIdentity requester;
  
479
  DriveConfig driveConfig("T10D6116", "T10KD6", "/dev/noSuchTape", "manual");
480
481
482
  DataTransferConfig castorConf;
  castorConf.bufsz = 1024;
  castorConf.nbBufs = 10;
483
484
485
486
  castor::messages::AcsProxyDummy acs;
  castor::mediachanger::MmcProxyDummy mmc;
  castor::legacymsg::RmcProxyDummy rmc;
  castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
487
  castor::messages::TapeserverProxyDummy initialProcess;
488
  castor::server::ProcessCapDummy capUtils;
489
  DataTransferSession sess("tapeHost", logger, mockSys,
490
    driveConfig, mc, initialProcess, capUtils, castorConf, scheduler);
491
  ASSERT_NO_THROW(sess.execute());
492
493
494
495
  std::string temp = logger.getLog();
  ASSERT_NE(std::string::npos, logger.getLog().find("Drive not found on this path"));
}

496
TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessionFailtoMount) {
497
498
  
  // 0) Prepare the logger for everyone
499
500
  castor::log::StringLogger logger("tapeServerUnitTest");
  
501
  // 1) prepare the fake scheduler
502
  std::string vid = "V12345";
503
  // cta::MountType::Enum mountType = cta::MountType::RETRIEVE;
504
  std::string density = "8000GC";
505

506
507
508
509
510
  // 3) Prepare the necessary environment (logger, plus system wrapper), 
  castor::tape::System::mockWrapper mockSys;
  mockSys.delegateToFake();
  mockSys.disableGMockCallsCounting();
  mockSys.fake.setupForVirtualDriveSLC6();
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
  //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::tapeserver::drive::FakeDrive;

  // 4) Create the scheduler
  cta::MockNameServer ns;
  cta::MockRemoteNS rns;
  cta::MockSchedulerDatabase db;
  cta::Scheduler scheduler(ns, db, rns);

  // Always use the same requester
  const cta::SecurityIdentity requester;
  
  // List to remember the path of each remote file so that the existance of the
  // files can be tested for at the end of the test
  std::list<std::string> remoteFilePaths;
528
529
530
531
532
533
534
535
536

  //delete is unnecessary
  //pointer with ownership will be passed to the application,
  //which will do the delete 
  const bool failOnMount=true;
  mockSys.fake.m_pathToDrive["/dev/nst0"] = new castor::tape::tapeserver::drive::FakeDrive(failOnMount);
  
  // We can prepare files for reading on the drive
  {
537
    
538
539
    // Label the tape
    castor::tape::tapeFile::LabelSession ls(*mockSys.fake.m_pathToDrive["/dev/nst0"], 
540
        "V12345");
541
542
    mockSys.fake.m_pathToDrive["/dev/nst0"]->rewind();
    // And write to it
543
    castor::tape::tapeserver::daemon::VolumeInfo volInfo;
544
    volInfo.vid="V12345";
545
546
547
548
549
550
551
    castor::tape::tapeFile::WriteSession ws(*mockSys.fake.m_pathToDrive["/dev/nst0"],
       volInfo , 0, true);

    // Write a few files on the virtual tape and modify the archive name space
    // so that it is in sync
    uint8_t data[1000];
    castor::tape::SCSI::Structures::zeroStruct(&data);
552
    for (int fseq=1; fseq <= 10 ; fseq ++) {
553
554
555
556
557
      // Create a path to a remote destination file
      std::ostringstream remoteFilePath;
      remoteFilePath << "file:" << m_tmpDir << "/test" << fseq;
      remoteFilePaths.push_back(remoteFilePath.str());

558
      // Create an archive file entry in the archive namespace
559
560
561
562
      std::ostringstream archiveFilePath;
      archiveFilePath << "/test" << fseq;
      const mode_t archiveFileMode = 0655;
      const uint64_t archiveFileSize = 256*1024;
563
      ASSERT_NO_THROW(ns.createFile(
564
565
566
        requester,
        archiveFilePath.str(),
        archiveFileMode,
567
        archiveFileSize));
568
569

      // Write the file to tape
570
      std::unique_ptr<cta::RetrieveJob> ftr(new MockRetrieveJob());
571
      std::unique_ptr<cta::ArchiveJob> ftm_temp(new MockArchiveJob());
572
      ftr->tapeFileLocation.fSeq = fseq;
573
      ftm_temp->tapeFileLocation.fSeq = fseq;
574
      ftr->archiveFile.fileId = 1000 + fseq;
575
576
577
578
579
580
581
582
583
      ftm_temp->archiveFile.fileId = 1000 + fseq;
      castor::tape::tapeFile::WriteFile wf(&ws, *ftm_temp, archiveFileSize);
      ftr->tapeFileLocation.blockId = wf.getPosition();
      ftr->remotePathAndStatus.path = remoteFilePath.str();
      // Write the data (one block)
      wf.write(data, sizeof(data));
      // Close the file
      wf.close();

584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
      // Create tape file entry in the archive namespace
      cta::NameServerTapeFile tapeFile;
      tapeFile.copyNb = 1;
      tapeFile.tapeFileLocation.fSeq = fseq;
      tapeFile.tapeFileLocation.blockId = wf.getPosition();
      tapeFile.tapeFileLocation.vid = volInfo.vid;
      tapeFile.tapeFileLocation.copyNb = 1;
      tapeFile.size = archiveFileSize;
      tapeFile.compressedSize = archiveFileSize; // No compression
      cta::Checksum tapeFileChecksum(cta::Checksum::CHECKSUMTYPE_ADLER32,
        cta::ByteArray(cta::Utils::getAdler32(data, sizeof data)));
      tapeFile.checksum = tapeFileChecksum;
      ASSERT_NO_THROW(ns.addTapeFile(
        requester,
        archiveFilePath.str(),
        tapeFile));

601
602
603
604
605
606
607
      // Schedule the retrieval of the file
      std::list<std::string> archiveFilePaths;
      archiveFilePaths.push_back(archiveFilePath.str());
      scheduler. queueRetrieveRequest(
        requester,
        archiveFilePaths,
        remoteFilePath.str());
608
609
    }
  }
610
  DriveConfig driveConfig("T10D6116", "T10KD6", "/dev/tape_T10D6116", "manual");
611
612
613
614
615
616
617
618
619
620
621
622
  DataTransferConfig castorConf;
  castorConf.bufsz = 1024*1024; // 1 MB memory buffers
  castorConf.nbBufs = 10;
  castorConf.bulkRequestRecallMaxBytes = UINT64_C(100)*1000*1000*1000;
  castorConf.bulkRequestRecallMaxFiles = 1000;
  castorConf.nbDiskThreads = 3;
  castor::messages::AcsProxyDummy acs;
  castor::mediachanger::MmcProxyDummy mmc;
  castor::legacymsg::RmcProxyDummy rmc;
  castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
  castor::server::ProcessCap capUtils;
  castor::messages::TapeserverProxyDummy initialProcess;
623
  DataTransferSession sess("tapeHost", logger, mockSys,
624
    driveConfig, mc, initialProcess, capUtils, castorConf, scheduler);
625
  ASSERT_NO_THROW(sess.execute());
626
627
628
629
630
  std::string temp = logger.getLog();
  temp += "";
  ASSERT_EQ("V12345", sess.getVid());
}

631
TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessionEmptyOnVolReq) {
632
633
  
  // 0) Prepare the logger for everyone
634
635
  castor::log::StringLogger logger("tapeServerUnitTest");
  
636
  // 1) prepare the fake scheduler
637
  std::string vid = "V12345";
638
  // cta::MountType::Enum mountType = cta::MountType::RETRIEVE;
639
  std::string density = "8000GC";
640

641
642
643
644
645
  // 3) Prepare the necessary environment (logger, plus system wrapper), 
  castor::tape::System::mockWrapper mockSys;
  mockSys.delegateToFake();
  mockSys.disableGMockCallsCounting();
  mockSys.fake.setupForVirtualDriveSLC6();
646
  
647
648
649
  // The drive will not even be opened. so no need for one.
  mockSys.fake.m_pathToDrive["/dev/nst0"] = NULL;
  
650
651
652
653
654
655
656
657
658
  // 4) Create the scheduler
  cta::MockNameServer ns;
  cta::MockRemoteNS rns;
  cta::MockSchedulerDatabase db;
  cta::Scheduler scheduler(ns, db, rns);

  // Always use the same requester
  const cta::SecurityIdentity requester;
  
659
  DriveConfig driveConfig("T10D6116", "T10KD6", "/dev/tape_T10D6116", "manual");
660
661
662
663
664
665
666
667
668
669
670
671
  DataTransferConfig castorConf;
  castorConf.bufsz = 1024*1024; // 1 MB memory buffers
  castorConf.nbBufs = 10;
  castorConf.bulkRequestRecallMaxBytes = UINT64_C(100)*1000*1000*1000;
  castorConf.bulkRequestRecallMaxFiles = 1000;
  castorConf.nbDiskThreads = 3;
  castor::messages::AcsProxyDummy acs;
  castor::mediachanger::MmcProxyDummy mmc;
  castor::legacymsg::RmcProxyDummy rmc;
  castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
  castor::server::ProcessCap capUtils;
  castor::messages::TapeserverProxyDummy initialProcess;
672
  DataTransferSession sess("tapeHost", logger, mockSys,
673
    driveConfig, mc, initialProcess, capUtils, castorConf, scheduler);
674
  ASSERT_NO_THROW(sess.execute());
675
676
677
678
679
680
  std::string temp = logger.getLog();
  temp += "";
  ASSERT_EQ("", sess.getVid());
  // We should not have logged any error
  ASSERT_EQ(std::string::npos, logger.getLog().find("LVL=E"));
}
681

682
class TempFileForData {
Eric Cano's avatar
Eric Cano committed
683
public:
684
  TempFileForData(size_t size): m_size(size) {
Eric Cano's avatar
Eric Cano committed
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
    // 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
703
      delete[] buff;
Eric Cano's avatar
Eric Cano committed
704
705
706
707
708
709
710
    } catch (...) {
      delete[] buff;
      unlink(m_path.c_str());
      throw;
    }
  }
  
711
  ~TempFileForData() {
Eric Cano's avatar
Eric Cano committed
712
713
714
715
716
717
718
719
720
721
    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;
};

722
class tempFileVector: public std::vector<TempFileForData *> {
Eric Cano's avatar
Eric Cano committed
723
724
725
726
727
728
729
730
731
732
public:
  ~tempFileVector() {
    while(size()) {
      delete back();
      pop_back();
    }
  }
};

struct expectedResult {
733
734
  expectedResult(int fs, uint32_t cs, int eCode = 0):
    fSeq(fs), checksum(cs), errorCode(eCode) {}
Eric Cano's avatar
Eric Cano committed
735
736
  int fSeq;
  uint32_t checksum;
737
  int errorCode;
Eric Cano's avatar
Eric Cano committed
738
};
739
/*
740
TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessionGooddayMigration) {
Eric Cano's avatar
Eric Cano committed
741
742
743
744
745
746
747
748
749
750
751
  // 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";
752
753
  client::ClientSimulator sim(volReq, vid, density,
    castor::tape::tapegateway::WRITE_TP, castor::tape::tapegateway::WRITE);
Eric Cano's avatar
Eric Cano committed
754
755
756
757
758
759
760
761
762
763
764
765
766
767
  client::ClientSimulator::ipPort clientAddr = sim.getCallbackAddress();
  clientRunner simRun(sim);
  simRun.start();
  
  // 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 
768
  mockSys.fake.m_pathToDrive["/dev/nst0"] = new castor::tape::tapeserver::drive::FakeDrive;
Eric Cano's avatar
Eric Cano committed
769
770
771
  
  // Just label the tape
  castor::tape::tapeFile::LabelSession ls(*mockSys.fake.m_pathToDrive["/dev/nst0"], 
772
      "V12345");
Eric Cano's avatar
Eric Cano committed
773
774
775
776
777
778
779
  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
780
    std::unique_ptr<TempFileForData> tf(new TempFileForData(1000));
Eric Cano's avatar
Eric Cano committed
781
    // Prepare the migrationRequest
782
    MockArchiveJob ftm;
Eric Cano's avatar
Eric Cano committed
783
    ftm.setFileSize(tf->m_size);
784
785
    ftm.archiveFile.fileId = 1000 + fseq;
    ftm.tapeFileLocation.fSeq = fseq;
Eric Cano's avatar
Eric Cano committed
786
787
788
789
790
    ftm.setPath(tf->path());
    sim.addFileToMigrate(ftm);
    expected.push_back(expectedResult(fseq, tf->checksum()));
    tempFiles.push_back(tf.release());
  }
791
  DriveConfig driveConfig("T10D6116", "T10KD6", "/dev/tape_T10D6116", "manual");
792
793
794
795
796
797
  DataTransferConfig castorConf;
  castorConf.bufsz = 1024*1024; // 1 MB memory buffers
  castorConf.nbBufs = 10;
  castorConf.bulkRequestMigrationMaxBytes = UINT64_C(100)*1000*1000*1000;
  castorConf.bulkRequestMigrationMaxFiles = 1000;
  castorConf.nbDiskThreads = 1;
798
799
800
801
  castor::messages::AcsProxyDummy acs;
  castor::mediachanger::MmcProxyDummy mmc;
  castor::legacymsg::RmcProxyDummy rmc;
  castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
802
  castor::messages::TapeserverProxyDummy initialProcess;
803
  castor::server::ProcessCapDummy capUtils;
804
805
806
807
  cta::MockNameServer ns;
  cta::MockRemoteNS rns;
  cta::MockSchedulerDatabase db;
  cta::Scheduler scheduler(ns, db, rns);
808
  DataTransferSession sess("tapeHost", logger, mockSys,
809
    driveConfig, mc, initialProcess, capUtils, castorConf, scheduler);
Eric Cano's avatar
Eric Cano committed
810
811
  sess.execute();
  simRun.wait();
812
813
814
815
  for (std::vector<struct expectedResult>::iterator i = expected.begin();
      i != expected.end(); i++) {
    ASSERT_EQ(i->checksum, sim.m_receivedChecksums[i->fSeq]);
  }
816
  ASSERT_EQ(0, sim.m_sessionErrorCode);
Eric Cano's avatar
Eric Cano committed
817
818
}

819
820
821
822
//
// This test is the same as the previous one, except that the files are deleted
// from filesystem immediately. The disk tasks will then fail on open.
///
823
TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessionMissingFilesMigration) {
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
  // 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,
    castor::tape::tapegateway::WRITE_TP, castor::tape::tapegateway::WRITE);
  client::ClientSimulator::ipPort clientAddr = sim.getCallbackAddress();
  clientRunner simRun(sim);
  simRun.start();
  
  // 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 
851
  mockSys.fake.m_pathToDrive["/dev/nst0"] = new castor::tape::tapeserver::drive::FakeDrive;
852
853
854
  
  // Just label the tape
  castor::tape::tapeFile::LabelSession ls(*mockSys.fake.m_pathToDrive["/dev/nst0"], 
855
      "V12345");
856
857
858
859
860
  mockSys.fake.m_pathToDrive["/dev/nst0"]->rewind();
  
  // Prepare the files, but delete them immediately. The migration will fail.
  for (int fseq=1; fseq<=10; fseq++) {
    // Create the file from which we will recall
861
    std::unique_ptr<TempFileForData> tf(new TempFileForData(1000));
862
    // Prepare the migrationRequest
863
    MockArchiveJob ftm;
864
    ftm.setFileSize(tf->m_size);
865
866
    ftm.archiveFile.fileId = 1000 + fseq;
    ftm.tapeFileLocation.fSeq = fseq;
867
868
869
    ftm.setPath(tf->path());
    sim.addFileToMigrate(ftm);
  }
870
  DriveConfig driveConfig("T10D6116", "T10KD6", "/dev/tape_T10D6116", "manual");
871
872
873
874
875
876
  DataTransferConfig castorConf;
  castorConf.bufsz = 1024*1024; // 1 MB memory buffers
  castorConf.nbBufs = 10;
  castorConf.bulkRequestMigrationMaxBytes = UINT64_C(100)*1000*1000*1000;
  castorConf.bulkRequestMigrationMaxFiles = 1000;
  castorConf.nbDiskThreads = 1;
877
878
879
880
  castor::messages::AcsProxyDummy acs;
  castor::mediachanger::MmcProxyDummy mmc;
  castor::legacymsg::RmcProxyDummy rmc;
  castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
881
882
  castor::messages::TapeserverProxyDummy initialProcess;
  castor::server::ProcessCapDummy capUtils;
883
884
885
886
  cta::MockNameServer ns;
  cta::MockRemoteNS rns;
  cta::MockSchedulerDatabase db;
  cta::Scheduler scheduler(ns, db, rns);
887
  DataTransferSession sess("tapeHost", logger, mockSys,
888
    driveConfig, mc, initialProcess, capUtils, castorConf, scheduler);
889
890
  sess.execute();
  simRun.wait();
891
  ASSERT_EQ(SEINTERNAL, sim.m_sessionErrorCode);
892
}
893

894
895
896
897
898
//
// This test is identical to the good day migration, but the tape will accept
// only a finite number of bytes and hence we will report a full tape skip the
// last migrations
//
899
TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessionTapeFullMigration) {
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
  // 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,
    castor::tape::tapegateway::WRITE_TP, castor::tape::tapegateway::WRITE);
  client::ClientSimulator::ipPort clientAddr = sim.getCallbackAddress();
  clientRunner simRun(sim);
  simRun.start();
  
  // 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 
  const uint64_t tapeSize = 5000;
928
  mockSys.fake.m_pathToDrive["/dev/nst0"] = new castor::tape::tapeserver::drive::FakeDrive(tapeSize);
929
930
931
  
  // Just label the tape
  castor::tape::tapeFile::LabelSession ls(*mockSys.fake.m_pathToDrive["/dev/nst0"], 
932
      "V12345");
933
934
935
936
937
938
939
940
941
942
  mockSys.fake.m_pathToDrive["/dev/nst0"]->rewind();
  
  tempFileVector tempFiles;
  std::vector<expectedResult> expected;
  uint64_t remainingSpace = tapeSize;
  bool failedFileDone = false;
  // 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
    const size_t fileSize = 1000;
943
    std::unique_ptr<TempFileForData> tf(new TempFileForData(fileSize));
944
    // Prepare the migrationRequest
945
    MockArchiveJob ftm;
946
    ftm.setFileSize(tf->m_size);
947
948
    ftm.archiveFile.fileId = 1000 + fseq;
    ftm.tapeFileLocation.fSeq = fseq;
949
950
951
952
953
954
955
956
957
958
959
960
    ftm.setPath(tf->path());
    sim.addFileToMigrate(ftm);
    if (fileSize + 6 * 80 < remainingSpace) {
      expected.push_back(expectedResult(fseq, tf->checksum()));
      remainingSpace -= fileSize + 6 * 80;
    } else if (!failedFileDone) {
      // We add also the report for the first file (which will come in error)
      expected.push_back(expectedResult(fseq, 0, ENOSPC));
      failedFileDone = true;
    }
    tempFiles.push_back(tf.release());
  }
961
  DriveConfig driveConfig("T10D6116", "T10KD6", "/dev/tape_T10D6116", "manual");
962
963
964
965
966
967
  DataTransferConfig castorConf;
  castorConf.bufsz = 1024*1024; // 1 MB memory buffers
  castorConf.nbBufs = 10;
  castorConf.bulkRequestMigrationMaxBytes = UINT64_C(100)*1000*1000*1000;
  castorConf.bulkRequestMigrationMaxFiles = 1000;
  castorConf.nbDiskThreads = 1;
968
969
970
971
  castor::messages::AcsProxyDummy acs;
  castor::mediachanger::MmcProxyDummy mmc;
  castor::legacymsg::RmcProxyDummy rmc;
  castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
972
973
  castor::messages::TapeserverProxyDummy initialProcess;
  castor::server::ProcessCapDummy capUtils;
974
975
976
977
  cta::MockNameServer ns;
  cta::MockRemoteNS rns;
  cta::MockSchedulerDatabase db;
  cta::Scheduler scheduler(ns, db, rns);
978
  DataTransferSession sess("tapeHost", logger, mockSys,
979
    driveConfig, mc, initialProcess, capUtils, castorConf, scheduler);
980
981
982
983
984
985
986
987
988
989
  sess.execute();
  simRun.wait();
  for (std::vector<struct expectedResult>::iterator i = expected.begin();
      i != expected.end(); i++) {
    if (!i->errorCode) {
      ASSERT_EQ(i->checksum, sim.m_receivedChecksums[i->fSeq]);
    } else {
      ASSERT_EQ(i->errorCode, sim.m_receivedErrorCodes[i->fSeq]);
    }
  }
990
991
992
  ASSERT_EQ(ENOSPC, sim.m_sessionErrorCode);
}

993
TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessionTapeFullOnFlushMigration) {
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
  // 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,
    castor::tape::tapegateway::WRITE_TP, castor::tape::tapegateway::WRITE);
  client::ClientSimulator::ipPort clientAddr = sim.getCallbackAddress();
  clientRunner simRun(sim);
  simRun.start();
  
  // 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 
  const uint64_t tapeSize = 5000;
  mockSys.fake.m_pathToDrive["/dev/nst0"] =
1023
1024
      new castor::tape::tapeserver::drive::FakeDrive(tapeSize,
        castor::tape::tapeserver::drive::FakeDrive::OnFlush);
1025
1026
1027
  
  // Just label the tape
  castor::tape::tapeFile::LabelSession ls(*mockSys.fake.m_pathToDrive["/dev/nst0"], 
1028
      "V12345");
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
  mockSys.fake.m_pathToDrive["/dev/nst0"]->rewind();
  
  tempFileVector tempFiles;
  std::vector<expectedResult> expected;
  uint64_t remainingSpace = tapeSize;
  bool failedFileDone = false;
  // 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
    const size_t fileSize = 1000;
1039
    std::unique_ptr<TempFileForData> tf(new TempFileForData(fileSize));
1040
    // Prepare the migrationRequest
1041
    MockArchiveJob ftm;
1042
    ftm.setFileSize(tf->m_size);
1043
1044
    ftm.archiveFile.fileId = 1000 + fseq;
    ftm.tapeFileLocation.fSeq = fseq;
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
    ftm.setPath(tf->path());
    sim.addFileToMigrate(ftm);
    if (fileSize + 6 * 80 < remainingSpace) {
      expected.push_back(expectedResult(fseq, tf->checksum()));
      remainingSpace -= fileSize + 6 * 80;
    } else if (!failedFileDone) {
      // We expect no report for this file anymore (but the report for the 
      // session)
      failedFileDone = true;
    }
    tempFiles.push_back(tf.release());
  }
1057
  DriveConfig driveConfig("T10D6116", "T10KD6", "/dev/tape_T10D6116", "manual");
1058
1059
1060
1061
1062
1063
  DataTransferConfig castorConf;
  castorConf.bufsz = 1024*1024; // 1 MB memory buffers
  castorConf.nbBufs = 10;
  castorConf.bulkRequestMigrationMaxBytes = UINT64_C(100)*1000*1000*1000;
  castorConf.bulkRequestMigrationMaxFiles = 1000;
  castorConf.nbDiskThreads = 1;
1064
1065
1066
1067
  castor::messages::AcsProxyDummy acs;
  castor::mediachanger::MmcProxyDummy mmc;
  castor::legacymsg::RmcProxyDummy rmc;
  castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
1068
1069
  castor::messages::TapeserverProxyDummy initialProcess;
  castor::server::ProcessCapDummy capUtils;
1070
1071
1072
1073
  cta::MockNameServer ns;
  cta::MockRemoteNS rns;
  cta::MockSchedulerDatabase db;
  cta::Scheduler scheduler(ns, db, rns);
1074
  DataTransferSession sess("tapeHost", logger, mockSys,
1075
    driveConfig, mc, initialProcess, capUtils, castorConf, scheduler);
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
  sess.execute();
  simRun.wait();
  for (std::vector<struct expectedResult>::iterator i = expected.begin();
      i != expected.end(); i++) {
    if (!i->errorCode) {
      ASSERT_EQ(i->checksum, sim.m_receivedChecksums[i->fSeq]);
    } else {
      ASSERT_EQ(i->errorCode, sim.m_receivedErrorCodes[i->fSeq]);
    }
  }
  ASSERT_EQ(ENOSPC, sim.m_sessionErrorCode);
1087
}
1088
*/
1089

1090
} // namespace unitTest