DataTransferSessionTest.cpp 51.5 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/OStoreDB/OStoreDBFactory.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
  class MockArchiveJob: public cta::ArchiveJob {
  public:
146
147
148
    MockArchiveJob(): cta::ArchiveJob(*((cta::ArchiveMount *)NULL), 
        *((cta::NameServer *)NULL), cta::ArchiveFile(), 
        cta::RemotePathAndStatus(), cta::NameServerTapeFile()) {
149
150
151
152
153
154
155
156
    } 
      
    ~MockArchiveJob() throw() {
    } 
  };

  class MockRetrieveJob: public cta::RetrieveJob {
  public:
157
158
159
160
    MockRetrieveJob(): cta::RetrieveJob(*((cta::RetrieveMount *)NULL),
    cta::ArchiveFile(), 
    std::string(), cta::NameServerTapeFile(),
    cta::PositioningMethod::ByBlock) {} 
161
      
162
    ~MockRetrieveJob() throw() {} 
163
  };
164
165
};

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

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

  // 4) Create the scheduler
  cta::MockNameServer ns;
  cta::MockRemoteNS rns;
188
  cta::OStoreDBWrapper<cta::objectstore::BackendVFS> db("Unittest");
189
190
191
192
193
194
195
196
  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;
197
  
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  // 5) Create the tapepool, library and tape for the scheduler.
  // Make mounts immediate.
  ASSERT_NO_THROW(scheduler.createAdminUserWithoutAuthorizingRequester(requester, requester.getUser(), ""));
  ASSERT_NO_THROW(scheduler.createAdminHostWithoutAuthorizingRequester(requester, requester.getHost(), ""));
  ASSERT_NO_THROW(scheduler.createTapePool(requester, "TapePool", 1, ""));
  cta::MountCriteria immediateMount;
  immediateMount.maxAge = 0;
  immediateMount.maxBytesQueued = 1;
  immediateMount.maxFilesQueued = 1;
  immediateMount.quota = 10;
  ASSERT_NO_THROW(scheduler.setTapePoolMountCriteria("TapePool", cta::MountCriteriaByDirection(immediateMount, immediateMount)));                   
  ASSERT_NO_THROW(scheduler.createLogicalLibrary(requester, "T10KD6", ""));
  ASSERT_NO_THROW(scheduler.createTape(requester, "V12345", "T10KD6", "TapePool", 10*1000*1000, "8000GC", ""));
  
212
  // 5) Prepare files for reading by writing them to the mock system
213
214
215
  {
    // Label the tape
    castor::tape::tapeFile::LabelSession ls(*mockSys.fake.m_pathToDrive["/dev/nst0"], 
216
        "V12345");
217
218
    mockSys.fake.m_pathToDrive["/dev/nst0"]->rewind();
    // And write to it
219
    castor::tape::tapeserver::daemon::VolumeInfo volInfo;
220
    volInfo.vid="V12345";
221
    castor::tape::tapeFile::WriteSession ws(*mockSys.fake.m_pathToDrive["/dev/nst0"],
222
       volInfo , 0, true);
223
224
225

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

234
      // Create an archive file entry in the archive namespace
235
236
237
238
      std::ostringstream archiveFilePath;
      archiveFilePath << "/test" << fseq;
      const mode_t archiveFileMode = 0655;
      const uint64_t archiveFileSize = 256*1024;
239
      ASSERT_NO_THROW(ns.createFile(
240
241
242
        requester,
        archiveFilePath.str(),
        archiveFileMode,
243
        archiveFileSize));
244
      std::unique_ptr<cta::ArchiveFileStatus> status = ns.statFile(requester, archiveFilePath.str());
245
        
246
      // Write the file to tape
247
      std::unique_ptr<cta::RetrieveJob> ftr(new MockRetrieveJob());
248
      std::unique_ptr<cta::ArchiveJob> ftm(new MockArchiveJob());
249
      ftr->nameServerTapeFile.tapeFileLocation.fSeq = fseq;
250
      ftm->nameServerTapeFile.tapeFileLocation.fSeq = fseq;
251
252
      ftr->archiveFile.fileId = status->fileId;
      ftm->archiveFile.fileId = status->fileId;
253
      castor::tape::tapeFile::WriteFile wf(&ws, *ftm, archiveFileSize);
254
      ftr->nameServerTapeFile.tapeFileLocation.blockId = wf.getBlockId();
255
      ftr->remotePath = remoteFilePath.str();
256
257
258
259
      // Write the data (one block)
      wf.write(data, sizeof(data));
      // Close the file
      wf.close();
260

261
262
263
264
      // Create tape file entry in the archive namespace
      cta::NameServerTapeFile tapeFile;
      tapeFile.copyNb = 1;
      tapeFile.tapeFileLocation.fSeq = fseq;
265
      tapeFile.tapeFileLocation.blockId = wf.getBlockId();
266
267
268
269
270
271
272
273
274
275
276
277
      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));

278
279
280
      // Schedule the retrieval of the file
      std::list<std::string> archiveFilePaths;
      archiveFilePaths.push_back(archiveFilePath.str());
281
      ASSERT_NO_THROW(scheduler.queueRetrieveRequest(
282
283
        requester,
        archiveFilePaths,
284
        remoteFilePath.str()));
285
286
    }
  }
287
288

  // 6) Create the data transfer session
289
  DriveConfig driveConfig("T10D6116", "T10KD6", "/dev/tape_T10D6116", "manual");
290
291
292
293
294
295
  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;
296
297
298
299
  castor::messages::AcsProxyDummy acs;
  castor::mediachanger::MmcProxyDummy mmc;
  castor::legacymsg::RmcProxyDummy rmc;
  castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
300
  castor::server::ProcessCap capUtils;
301
  castor::messages::TapeserverProxyDummy initialProcess;
302
  DataTransferSession sess("tapeHost", logger, mockSys,
303
    driveConfig, mc, initialProcess, capUtils, castorConf, scheduler);
304
305

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

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

311
  // 9) Check the remote files exist and have the correct size
312
313
314
315
  for(auto pathItor = remoteFilePaths.cbegin(); pathItor !=
    remoteFilePaths.cend(); pathItor++) {
    struct stat statBuf;
    bzero(&statBuf, sizeof(statBuf));
316
    const int statRc = stat(pathItor->substr(7).c_str(), &statBuf); //remove the "file://" for stat-ing
317
    ASSERT_EQ(0, statRc);
318
    ASSERT_EQ(1000, statBuf.st_size); //same size of data
319
  }
320
}
321

322
TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessionWrongRecall) {
323
324
325
  // 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.
326
327

  // 0) Prepare the logger for everyone
328
329
  castor::log::StringLogger logger("tapeServerUnitTest");
  
330
  // 1) prepare the fake scheduler
331
  std::string vid = "V12345";
332
  // cta::MountType::Enum mountType = cta::MountType::RETRIEVE;
333
  std::string density = "8000GC";
334

335
336
337
338
339
340
341
342
  // 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 
343
  mockSys.fake.m_pathToDrive["/dev/nst0"] = new castor::tape::tapeserver::drive::FakeDrive;
344
345
346
347

  // 4) Create the scheduler
  cta::MockNameServer ns;
  cta::MockRemoteNS rns;
348
  cta::OStoreDBWrapper<cta::objectstore::BackendVFS> db("Unittest");
349
350
351
352
353
354
355
356
  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;
357
  
358
359
360
361
362
363
364
365
366
367
368
369
370
371
  // 5) Create the tapepool, library and tape for the scheduler.
  // Make mounts immediate.
  ASSERT_NO_THROW(scheduler.createAdminUserWithoutAuthorizingRequester(requester, requester.getUser(), ""));
  ASSERT_NO_THROW(scheduler.createAdminHostWithoutAuthorizingRequester(requester, requester.getHost(), ""));
  ASSERT_NO_THROW(scheduler.createTapePool(requester, "TapePool", 1, ""));
  cta::MountCriteria immediateMount;
  immediateMount.maxAge = 0;
  immediateMount.maxBytesQueued = 1;
  immediateMount.maxFilesQueued = 1;
  immediateMount.quota = 10;
  ASSERT_NO_THROW(scheduler.setTapePoolMountCriteria("TapePool", cta::MountCriteriaByDirection(immediateMount, immediateMount)));                   
  ASSERT_NO_THROW(scheduler.createLogicalLibrary(requester, "T10KD6", ""));
  ASSERT_NO_THROW(scheduler.createTape(requester, "V12345", "T10KD6", "TapePool", 10*1000*1000, "8000GC", ""));
  
372
  // 5) Prepare files for reading by writing them to the mock system
373
374
375
  {
    // Label the tape
    castor::tape::tapeFile::LabelSession ls(*mockSys.fake.m_pathToDrive["/dev/nst0"], 
376
        "V12345");
377
378
    mockSys.fake.m_pathToDrive["/dev/nst0"]->rewind();
    // And write to it
379
    castor::tape::tapeserver::daemon::VolumeInfo volInfo;
380
381
382
    volInfo.vid="V12345";
    castor::tape::tapeFile::WriteSession ws(*mockSys.fake.m_pathToDrive["/dev/nst0"],
       volInfo , 0, true);
383
384
385

    // Write a few files on the virtual tape and modify the archive name space
    // so that it is in sync
386
387
    uint8_t data[1000];
    castor::tape::SCSI::Structures::zeroStruct(&data);
388
389
    int fseq=1;
    {
390
391
      // Create a path to a remote destination file
      std::ostringstream remoteFilePath;
392
      remoteFilePath << "file://" << m_tmpDir << "/test" << fseq;
393
394
      remoteFilePaths.push_back(remoteFilePath.str());

395
      // Create an archive file entry in the archive namespace
396
397
398
      std::ostringstream archiveFilePath;
      archiveFilePath << "/test" << fseq;
      const mode_t archiveFileMode = 0655;
399
      const uint64_t archiveFileSize = 1000;
400
      ASSERT_NO_THROW(ns.createFile(
401
402
403
        requester,
        archiveFilePath.str(),
        archiveFileMode,
404
        archiveFileSize));
405
406

      // Write the file to tape
407
408
      std::unique_ptr<cta::RetrieveJob> ftr(new MockRetrieveJob());
      std::unique_ptr<cta::ArchiveJob> ftm_temp(new MockArchiveJob());
409
      ftr->nameServerTapeFile.tapeFileLocation.fSeq = fseq;
410
      ftm_temp->nameServerTapeFile.tapeFileLocation.fSeq = fseq;
411
412
      ftr->archiveFile.fileId = 1000 + fseq;
      ftm_temp->archiveFile.fileId = 1000 + fseq;
413
      castor::tape::tapeFile::WriteFile wf(&ws, *ftm_temp, archiveFileSize);
414
      ftr->nameServerTapeFile.tapeFileLocation.blockId = wf.getBlockId();
415
      ftr->remotePath = remoteFilePath.str();
416
417
418
419
      // Write the data (one block)
      wf.write(data, sizeof(data));
      // Close the file
      wf.close();
420

421
422
423
424
425
      // 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;
426
      tapeFile.tapeFileLocation.blockId = wf.getBlockId() + 10000;
427
428
429
430
431
432
433
434
435
436
437
438
      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));

439
440
441
      // Schedule the retrieval of the file
      std::list<std::string> archiveFilePaths;
      archiveFilePaths.push_back(archiveFilePath.str());
442
      ASSERT_NO_THROW(scheduler.queueRetrieveRequest(
443
444
        requester,
        archiveFilePaths,
445
        remoteFilePath.str()));
446
447
    }
  }
448
449

  // 6) Create the data transfer session
450
  DriveConfig driveConfig("T10D6116", "T10KD6", "/dev/tape_T10D6116", "manual");
451
452
453
454
455
456
  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;
457
458
459
460
  castor::messages::AcsProxyDummy acs;
  castor::mediachanger::MmcProxyDummy mmc;
  castor::legacymsg::RmcProxyDummy rmc;
  castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
461
462
  castor::server::ProcessCap capUtils;
  castor::messages::TapeserverProxyDummy initialProcess;
463
  DataTransferSession sess("tapeHost", logger, mockSys,
464
    driveConfig, mc, initialProcess, capUtils, castorConf, scheduler);
465
466
467
468
469

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

  // 8) Check the session git the correct VID
470
  ASSERT_EQ("V12345", sess.getVid());
471
472

  // 9) Check the remote files exist and have the correct size
473
474
  std::string temp = logger.getLog();
  ASSERT_NE(std::string::npos, logger.getLog().find("trying to position beyond the end of data"));
475
}
476

477
TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessionNoSuchDrive) {
478
479
480
481
482
  
  // 0) Prepare the logger for everyone
  castor::log::StringLogger logger("tapeServerUnitTest");
  
  // 1) prepare the fake scheduler
483
  std::string vid = "V12345";
484
  // cta::MountType::Enum mountType = cta::MountType::RETRIEVE;
485
  std::string density = "8000GC";
486

487
488
489
490
491
  // 3) Prepare the necessary environment (logger, plus system wrapper), 
  castor::tape::System::mockWrapper mockSys;
  mockSys.delegateToFake();
  mockSys.disableGMockCallsCounting();
  mockSys.fake.setupForVirtualDriveSLC6();
492
493
494
495
496
497
498
499
  //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;
500
  cta::OStoreDBWrapper<cta::objectstore::BackendVFS> db("Unittest");
501
502
503
504
505
  cta::Scheduler scheduler(ns, db, rns);

  // Always use the same requester
  const cta::SecurityIdentity requester;
  
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
  // 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;
  
  // 5) Create the tapepool, library and tape for the scheduler.
  // Make mounts immediate.
  ASSERT_NO_THROW(scheduler.createAdminUserWithoutAuthorizingRequester(requester, requester.getUser(), ""));
  ASSERT_NO_THROW(scheduler.createAdminHostWithoutAuthorizingRequester(requester, requester.getHost(), ""));
  ASSERT_NO_THROW(scheduler.createTapePool(requester, "TapePool", 1, ""));
  cta::MountCriteria immediateMount;
  immediateMount.maxAge = 0;
  immediateMount.maxBytesQueued = 1;
  immediateMount.maxFilesQueued = 1;
  immediateMount.quota = 10;
  ASSERT_NO_THROW(scheduler.setTapePoolMountCriteria("TapePool", cta::MountCriteriaByDirection(immediateMount, immediateMount)));                   
  ASSERT_NO_THROW(scheduler.createLogicalLibrary(requester, "T10KD6", ""));
  ASSERT_NO_THROW(scheduler.createTape(requester, "V12345", "T10KD6", "TapePool", 10*1000*1000, "8000GC", ""));
  
  // 5) Prepare files for reading by writing them to the mock system
  {
    // Label the tape
    castor::tape::tapeFile::LabelSession ls(*mockSys.fake.m_pathToDrive["/dev/nst0"], 
        "V12345");
    mockSys.fake.m_pathToDrive["/dev/nst0"]->rewind();
    // And write to it
    castor::tape::tapeserver::daemon::VolumeInfo volInfo;
    volInfo.vid="V12345";
    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);
    int fseq=1;
    {
      // Create a path to a remote destination file
      std::ostringstream remoteFilePath;
      remoteFilePath << "file://" << m_tmpDir << "/test" << fseq;
      remoteFilePaths.push_back(remoteFilePath.str());

      // Create an archive file entry in the archive namespace
      std::ostringstream archiveFilePath;
      archiveFilePath << "/test" << fseq;
      const mode_t archiveFileMode = 0655;
      const uint64_t archiveFileSize = 1000;
      ASSERT_NO_THROW(ns.createFile(
        requester,
        archiveFilePath.str(),
        archiveFileMode,
        archiveFileSize));

      // Write the file to tape
      std::unique_ptr<cta::RetrieveJob> ftr(new MockRetrieveJob());
      std::unique_ptr<cta::ArchiveJob> ftm_temp(new MockArchiveJob());
      ftr->nameServerTapeFile.tapeFileLocation.fSeq = fseq;
      ftm_temp->nameServerTapeFile.tapeFileLocation.fSeq = fseq;
      ftr->archiveFile.fileId = 1000 + fseq;
      ftm_temp->archiveFile.fileId = 1000 + fseq;
      castor::tape::tapeFile::WriteFile wf(&ws, *ftm_temp, archiveFileSize);
      ftr->nameServerTapeFile.tapeFileLocation.blockId = wf.getBlockId();
      ftr->remotePath = remoteFilePath.str();
      // Write the data (one block)
      wf.write(data, sizeof(data));
      // Close the file
      wf.close();

      // 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 + 1000;
      tapeFile.tapeFileLocation.blockId = wf.getBlockId();
      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));

      // Schedule the retrieval of the file
      std::list<std::string> archiveFilePaths;
      archiveFilePaths.push_back(archiveFilePath.str());
      ASSERT_NO_THROW(scheduler.queueRetrieveRequest(
        requester,
        archiveFilePaths,
        remoteFilePath.str()));
    }
  }
  
  DriveConfig driveConfig("T10D6116", "T10KD6", "/dev/noSuchDrive", "manual");
602
603
604
  DataTransferConfig castorConf;
  castorConf.bufsz = 1024;
  castorConf.nbBufs = 10;
605
606
607
608
  castor::messages::AcsProxyDummy acs;
  castor::mediachanger::MmcProxyDummy mmc;
  castor::legacymsg::RmcProxyDummy rmc;
  castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
609
  castor::messages::TapeserverProxyDummy initialProcess;
610
  castor::server::ProcessCapDummy capUtils;
611
  DataTransferSession sess("tapeHost", logger, mockSys,
612
    driveConfig, mc, initialProcess, capUtils, castorConf, scheduler);
613
  ASSERT_NO_THROW(sess.execute());
614
  std::string temp = logger.getLog();
615
  ASSERT_NE(std::string::npos, logger.getLog().find("Could not stat path: /dev/noSuchDrive"));
616
617
}

618
TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessionFailtoMount) {
619
620
  
  // 0) Prepare the logger for everyone
621
622
  castor::log::StringLogger logger("tapeServerUnitTest");
  
623
  // 1) prepare the fake scheduler
624
  std::string vid = "V12345";
625
  // cta::MountType::Enum mountType = cta::MountType::RETRIEVE;
626
  std::string density = "8000GC";
627

628
629
630
631
632
  // 3) Prepare the necessary environment (logger, plus system wrapper), 
  castor::tape::System::mockWrapper mockSys;
  mockSys.delegateToFake();
  mockSys.disableGMockCallsCounting();
  mockSys.fake.setupForVirtualDriveSLC6();
633
634
635
636
637
638
639
640
  //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;
641
  cta::OStoreDBWrapper<cta::objectstore::BackendVFS> db("Unittest");
642
643
644
645
646
647
648
649
  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;
650
651
652
653
654
655
656
657
658
659
660
661
662
663
  
  // 5) Create the tapepool, library and tape for the scheduler.
  // Make mounts immediate.
  ASSERT_NO_THROW(scheduler.createAdminUserWithoutAuthorizingRequester(requester, requester.getUser(), ""));
  ASSERT_NO_THROW(scheduler.createAdminHostWithoutAuthorizingRequester(requester, requester.getHost(), ""));
  ASSERT_NO_THROW(scheduler.createTapePool(requester, "TapePool", 1, ""));
  cta::MountCriteria immediateMount;
  immediateMount.maxAge = 0;
  immediateMount.maxBytesQueued = 1;
  immediateMount.maxFilesQueued = 1;
  immediateMount.quota = 10;
  ASSERT_NO_THROW(scheduler.setTapePoolMountCriteria("TapePool", cta::MountCriteriaByDirection(immediateMount, immediateMount)));                   
  ASSERT_NO_THROW(scheduler.createLogicalLibrary(requester, "T10KD6", ""));
  ASSERT_NO_THROW(scheduler.createTape(requester, "V12345", "T10KD6", "TapePool", 10*1000*1000, "8000GC", ""));
664
665
666
667
668
669
670
671
672

  //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
  {
673
    
674
675
    // Label the tape
    castor::tape::tapeFile::LabelSession ls(*mockSys.fake.m_pathToDrive["/dev/nst0"], 
676
        "V12345");
677
678
    mockSys.fake.m_pathToDrive["/dev/nst0"]->rewind();
    // And write to it
679
    castor::tape::tapeserver::daemon::VolumeInfo volInfo;
680
    volInfo.vid="V12345";
681
682
683
684
685
686
687
    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);
688
    for (int fseq=1; fseq <= 10 ; fseq ++) {
689
690
      // Create a path to a remote destination file
      std::ostringstream remoteFilePath;
691
      remoteFilePath << "file://" << m_tmpDir << "/test" << fseq;
692
693
      remoteFilePaths.push_back(remoteFilePath.str());

694
      // Create an archive file entry in the archive namespace
695
696
697
      std::ostringstream archiveFilePath;
      archiveFilePath << "/test" << fseq;
      const mode_t archiveFileMode = 0655;
698
      const uint64_t archiveFileSize = 1000;
699
      ASSERT_NO_THROW(ns.createFile(
700
701
702
        requester,
        archiveFilePath.str(),
        archiveFileMode,
703
        archiveFileSize));
704
705

      // Write the file to tape
706
      std::unique_ptr<cta::RetrieveJob> ftr(new MockRetrieveJob());
707
      std::unique_ptr<cta::ArchiveJob> ftm_temp(new MockArchiveJob());
708
      ftr->nameServerTapeFile.tapeFileLocation.fSeq = fseq;
709
      ftm_temp->nameServerTapeFile.tapeFileLocation.fSeq = fseq;
710
      ftr->archiveFile.fileId = 1000 + fseq;
711
712
      ftm_temp->archiveFile.fileId = 1000 + fseq;
      castor::tape::tapeFile::WriteFile wf(&ws, *ftm_temp, archiveFileSize);
713
714
      ftr->nameServerTapeFile.tapeFileLocation.blockId = wf.getPosition();
      ftr->remotePath = remoteFilePath.str();
715
716
717
718
719
      // Write the data (one block)
      wf.write(data, sizeof(data));
      // Close the file
      wf.close();

720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
      // 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));

737
738
739
      // Schedule the retrieval of the file
      std::list<std::string> archiveFilePaths;
      archiveFilePaths.push_back(archiveFilePath.str());
740
      ASSERT_NO_THROW(scheduler.queueRetrieveRequest(
741
742
        requester,
        archiveFilePaths,
743
        remoteFilePath.str()));
744
745
    }
  }
746
  DriveConfig driveConfig("T10D6116", "T10KD6", "/dev/tape_T10D6116", "manual");
747
748
749
750
751
752
753
754
755
756
757
758
  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;
759
  DataTransferSession sess("tapeHost", logger, mockSys,
760
    driveConfig, mc, initialProcess, capUtils, castorConf, scheduler);
761
  ASSERT_NO_THROW(sess.execute());
762
  std::string temp = logger.getLog();
763
  ASSERT_NE(std::string::npos, logger.getLog().find("Failed to mount the tape"));
764
765
}

766
TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessionEmptyOnVolReq) {
767
768
  
  // 0) Prepare the logger for everyone
769
770
  castor::log::StringLogger logger("tapeServerUnitTest");
  
771
  // 1) prepare the fake scheduler
772
  std::string vid = "V12345";
773
  // cta::MountType::Enum mountType = cta::MountType::RETRIEVE;
774
  std::string density = "8000GC";
775

776
777
778
779
780
  // 3) Prepare the necessary environment (logger, plus system wrapper), 
  castor::tape::System::mockWrapper mockSys;
  mockSys.delegateToFake();
  mockSys.disableGMockCallsCounting();
  mockSys.fake.setupForVirtualDriveSLC6();
781
  
782
783
784
  // The drive will not even be opened. so no need for one.
  mockSys.fake.m_pathToDrive["/dev/nst0"] = NULL;
  
785
786
787
  // 4) Create the scheduler
  cta::MockNameServer ns;
  cta::MockRemoteNS rns;
788
  cta::OStoreDBWrapper<cta::objectstore::BackendVFS> db("Unittest");
789
790
791
792
793
  cta::Scheduler scheduler(ns, db, rns);

  // Always use the same requester
  const cta::SecurityIdentity requester;
  
794
  DriveConfig driveConfig("T10D6116", "T10KD6", "/dev/tape_T10D6116", "manual");
795
796
797
798
799
800
801
802
803
804
805
806
  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;
807
  DataTransferSession sess("tapeHost", logger, mockSys,
808
    driveConfig, mc, initialProcess, capUtils, castorConf, scheduler);
809
  ASSERT_NO_THROW(sess.execute());
810
811
812
813
814
815
  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"));
}
816

817
TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessionGooddayMigration) {
818
  
Eric Cano's avatar
Eric Cano committed
819
820
821
  // 0) Prepare the logger for everyone
  castor::log::StringLogger logger("tapeServerUnitTest");
  
822
  // 1) prepare the fake scheduler
Eric Cano's avatar
Eric Cano committed
823
824
  std::string vid = "V12345";
  std::string density = "8000GC";
825

Eric Cano's avatar
Eric Cano committed
826
827
828
829
830
831
832
833
  // 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 
834
  mockSys.fake.m_pathToDrive["/dev/nst0"] = new castor::tape::tapeserver::drive::FakeDrive;
835
836
837
838
839
840
841
842
843
844
845
846
847

  // 4) Create the scheduler
  cta::MockNameServer ns;
  cta::MockRemoteNS rns;
  cta::OStoreDBWrapper<cta::objectstore::BackendVFS> db("Unittest");
  cta::Scheduler scheduler(ns, db, rns);

  // Always use the same requester
  const cta::SecurityIdentity requester;
  
  // Create the bootstrap admin user and host
  ASSERT_NO_THROW(scheduler.createAdminUserWithoutAuthorizingRequester(requester, requester.getUser(), "admin user"));
  ASSERT_NO_THROW(scheduler.createAdminHostWithoutAuthorizingRequester(requester, requester.getHost(), "admin host"));
Eric Cano's avatar
Eric Cano committed
848
  
849
850
  // create a single copy storage class
  ASSERT_NO_THROW(scheduler.createStorageClass(requester, "SINGLE", 1, 1, "comment"));
Eric Cano's avatar
Eric Cano committed
851
  
852
853
854
855
856
857
858
859
860
861
862
863
864
  // assign it to the root directory
  ASSERT_NO_THROW(scheduler.setDirStorageClass(requester, "/", "SINGLE"));
  
  // create the logical library
  ASSERT_NO_THROW(scheduler.createLogicalLibrary(requester, "illogical", "the illogical library"));
  
  // create the tape pool
  ASSERT_NO_THROW(scheduler.createTapePool(requester, "swimmingpool", 2, "the swimming pool"));
  
  // create the route
  ASSERT_NO_THROW(scheduler.createArchiveRoute(requester, "SINGLE", 1, "swimmingpool", "iArchive"));
  
  // create the tape
865
866
  std::string comment = "the magic tape";
  ASSERT_NO_THROW(scheduler.createTape(requester, "V12345", "illogical", "swimmingpool", 100000, "8000GC", comment));
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
  
  // List to remember the path of each remote file so that the existence of the
  // files can be tested for at the end of the test
  std::list<std::string> remoteFilePaths;

  //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();
  
  // We can prepare files for writing on the drive
  {    
    // Label the tape
    castor::tape::tapeFile::LabelSession ls(*mockSys.fake.m_pathToDrive["/dev/nst0"], "V12345");
    mockSys.fake.m_pathToDrive["/dev/nst0"]->rewind();
    
    // schedule the archivals
    for(int fseq=1; fseq <= 10 ; fseq ++) {      
      // Create a path to a remote destination file
      std::ostringstream remoteFilePath;
      remoteFilePath << "file:" << "/test" << fseq;
      remoteFilePaths.push_back(remoteFilePath.str());
      
      // Create the entry in the remote namespace (same user id of the requester)
      cta::RemotePath rpath(remoteFilePath.str());
      cta::RemoteFileStatus rstatus(requester.getUser(), 0777, 1000);
      rns.createEntry(rpath, rstatus);

      // Schedule the archival of the file
      std::list<std::string> remoteFilePathList;
      remoteFilePathList.push_back(remoteFilePath.str());
      ASSERT_NO_THROW(scheduler.queueArchiveRequest(requester, remoteFilePathList, rpath.getAfterScheme()));
    }
Eric Cano's avatar
Eric Cano committed
900
  }
901
  DriveConfig driveConfig("T10D6116", "T10KD6", "/dev/tape_T10D6116", "manual");
902
903
904
  DataTransferConfig castorConf;
  castorConf.bufsz = 1024*1024; // 1 MB memory buffers
  castorConf.nbBufs = 10;
905
906
  castorConf.bulkRequestRecallMaxBytes = UINT64_C(100)*1000*1000*1000;
  castorConf.bulkRequestRecallMaxFiles = 1000;
907
  castorConf.nbDiskThreads = 1;
908
909
910
911
  castor::messages::AcsProxyDummy acs;
  castor::mediachanger::MmcProxyDummy mmc;
  castor::legacymsg::RmcProxyDummy rmc;
  castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
912
  castor::server::ProcessCap capUtils;
913
  castor::messages::TapeserverProxyDummy initialProcess;
914
915
916
917
918
919
920
921
922
923
924
925
  DataTransferSession sess("tapeHost", logger, mockSys, driveConfig, mc, initialProcess, capUtils, castorConf, scheduler);
  ASSERT_NO_THROW(sess.execute());
  std::string temp = logger.getLog();
  temp += "";
  ASSERT_EQ("V12345", sess.getVid());
  for(auto i=remoteFilePaths.begin(); i!=remoteFilePaths.end(); i++) {
    cta::RemotePath rpath(*i);
    ASSERT_NO_THROW(ns.statFile(requester, rpath.getAfterScheme()));
    std::unique_ptr<cta::ArchiveFileStatus> stat(ns.statFile(requester, rpath.getAfterScheme()));
    ASSERT_NE((uint64_t)(stat.get()), NULL);
    ASSERT_EQ(stat->mode, 0777);
    ASSERT_EQ(stat->size, 1000);
926
  }
Eric Cano's avatar
Eric Cano committed
927
928
}

929
930
931
932
//
// 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.
///
933
TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessionMissingFilesMigration) {
934
  
935
936
937
  // 0) Prepare the logger for everyone
  castor::log::StringLogger logger("tapeServerUnitTest");
  
938
  // 1) prepare the fake scheduler
939
940
  std::string vid = "V12345";
  std::string density = "8000GC";
941

942
943
944
945
946
947
948
949
  // 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 
950
  mockSys.fake.m_pathToDrive["/dev/nst0"] = new castor::tape::tapeserver::drive::FakeDrive;
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978

  // 4) Create the scheduler
  cta::MockNameServer ns;
  cta::MockRemoteNS rns;
  cta::OStoreDBWrapper<cta::objectstore::BackendVFS> db("Unittest");
  cta::Scheduler scheduler(ns, db, rns);

  // Always use the same requester
  const cta::SecurityIdentity requester;
  
  // Create the bootstrap admin user and host
  ASSERT_NO_THROW(scheduler.createAdminUserWithoutAuthorizingRequester(requester, requester.getUser(), "admin user"));
  ASSERT_NO_THROW(scheduler.createAdminHostWithoutAuthorizingRequester(requester, requester.getHost(), "admin host"));
  
  // create a single copy storage class
  ASSERT_NO_THROW(scheduler.createStorageClass(requester, "SINGLE", 1, 1, "comment"));
  
  // assign it to the root directory
  ASSERT_NO_THROW(scheduler.setDirStorageClass(requester, "/", "SINGLE"));
  
  // create the logical library
  ASSERT_NO_THROW(scheduler.createLogicalLibrary(requester, "illogical", "the illogical library"));
  
  // create the tape pool
  ASSERT_NO_THROW(scheduler.createTapePool(requester, "swimmingpool", 2, "the swimming pool"));
  
  // create the route
  ASSERT_NO_THROW(scheduler.createArchiveRoute(requester, "SINGLE", 1, "swimmingpool", "iArchive"));
979
  
980
  // create the tape
981
982
  std::string comment = "the magic tape";
  ASSERT_NO_THROW(scheduler.createTape(requester, "V12345", "illogical", "swimmingpool", 100000, "8000GC", comment));
983
  
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
  // List to remember the path of each remote file so that the existence of the
  // files can be tested for at the end of the test
  std::list<std::string> remoteFilePaths;

  //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();
  
  // We can prepare files for writing on the drive
  {    
    // Label the tape
    castor::tape::tapeFile::LabelSession ls(*mockSys.fake.m_pathToDrive["/dev/nst0"], "V12345");
    mockSys.fake.m_pathToDrive["/dev/nst0"]->rewind();
    
    // schedule the archivals
    for(int fseq=1; fseq <= 10 ; fseq ++) {      
      // Create a path to a remote destination file
      std::ostringstream remoteFilePath;
      remoteFilePath << "file:" << "/test" << fseq;
      remoteFilePaths.push_back(remoteFilePath.str());
      
      // WE, ON PURPOSE, DO NOT create the entry in the remote namespace (see previous test))
      cta::RemotePath rpath(remoteFilePath.str());

      // Schedule the archival of the file
      std::list<std::string> remoteFilePathList;
      remoteFilePathList.push_back(remoteFilePath.str());
      ASSERT_NO_THROW(scheduler.queueArchiveRequest(requester, remoteFilePathList, rpath.getAfterScheme()));
    }
1014
  }
1015
  DriveConfig driveConfig("T10D6116", "T10KD6", "/dev/tape_T10D6116", "manual");
1016
1017
1018
  DataTransferConfig castorConf;
  castorConf.bufsz = 1024*1024; // 1 MB memory buffers
  castorConf.nbBufs = 10;
1019
1020
  castorConf.bulkRequestRecallMaxBytes = UINT64_C(100)*1000*1000*1000;
  castorConf.bulkRequestRecallMaxFiles = 1000;
1021
  castorConf.nbDiskThreads = 1;
1022
1023
1024
1025
  castor::messages::AcsProxyDummy acs;
  castor::mediachanger::MmcProxyDummy mmc;
  castor::legacymsg::RmcProxyDummy rmc;
  castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
1026
  castor::server::ProcessCap capUtils;
1027
  castor::messages::TapeserverProxyDummy initialProcess;
1028
1029
  DataTransferSession sess("tapeHost", logger, mockSys, driveConfig, mc, initialProcess, capUtils, castorConf, scheduler);
  ASSERT_THROW(sess.execute(), castor::exception::Exception); //this should throw because the remote NS open fails
1030
}
1031

1032
1033
1034
1035
1036
//
// 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
//
1037
TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessionTapeFullMigration) {
1038
  
1039
1040
1041
  // 0) Prepare the logger for everyone
  castor::log::StringLogger logger("tapeServerUnitTest");
  
1042
  // 1) prepare the fake scheduler
1043
1044
  std::string vid = "V12345";
  std::string density = "8000GC";
1045

1046
1047
1048
1049
1050
1051
1052
1053
  // 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 
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
  mockSys.fake.m_pathToDrive["/dev/nst0"] = new castor::tape::tapeserver::drive::FakeDrive;

  // 4) Create the scheduler
  cta::MockNameServer ns;
  cta::MockRemoteNS rns;
  cta::OStoreDBWrapper<cta::objectstore::BackendVFS> db("Unittest");
  cta::Scheduler scheduler(ns, db, rns);

  // Always use the same requester
  const cta::SecurityIdentity requester;
  
  // Create the bootstrap admin user and host
  ASSERT_NO_THROW(scheduler.createAdminUserWithoutAuthorizingRequester(requester, requester.getUser(), "admin user"));
  ASSERT_NO_THROW(scheduler.createAdminHostWithoutAuthorizingRequester(requester, requester.getHost(), "admin host"));
  
  // create a single copy storage class
  ASSERT_NO_THROW(scheduler.createStorageClass(requester, "SINGLE", 1, 1, "comment"));
  
  // assign it to the root directory
  ASSERT_NO_THROW(scheduler.setDirStorageClass(requester, "/", "SINGLE"));
  
  // create the logical library
  ASSERT_NO_THROW(scheduler.createLogicalLibrary(requester, "illogical", "the illogical library"));
  
  // create the tape pool
  ASSERT_NO_THROW(scheduler.createTapePool(requester, "swimmingpool", 2, "the swimming pool"));
  
  // create the route
  ASSERT_NO_THROW(scheduler.createArchiveRoute(requester, "SINGLE", 1, "swimmingpool", "iArchive"));
  
  // create the tape
1085
1086
  std::string comment = "the magic tape";
  ASSERT_NO_THROW(scheduler.createTape(requester, "V12345", "illogical", "swimmingpool", 100000, "8000GC", comment));
1087
1088
1089
1090
1091
1092
1093
1094
  
  // List to remember the path of each remote file so that the existence of the
  // files can be tested for at the end of the test
  std::list<std::string> remoteFilePaths;

  //delete is unnecessary
  //pointer with ownership will be passed to the application,
  //which will do the delete
1095
  const uint64_t tapeSize = 5000;
1096
  mockSys.fake.m_pathToDrive["/dev/nst0"] = new castor::tape::tapeserver::drive::FakeDrive(tapeSize);
1097
  
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
  // We can prepare files for writing on the drive
  {    
    // Label the tape
    castor::tape::tapeFile::LabelSession ls(*mockSys.fake.m_pathToDrive["/dev/nst0"], "V12345");
    mockSys.fake.m_pathToDrive["/dev/nst0"]->rewind();
    
    // schedule the archivals
    for(int fseq=1; fseq <= 10 ; fseq ++) {      
      // Create a path to a remote destination file
      std::ostringstream remoteFilePath;
      remoteFilePath << "file:" << "/test" << fseq;
      remoteFilePaths.push_back(remoteFilePath.str());
      
      // Create the entry in the remote namespace (same user id of the requester)
      cta::RemotePath rpath(remoteFilePath.str());
      cta::RemoteFileStatus rstatus(requester.getUser(), 0777, 1000);
      rns.createEntry(rpath, rstatus);

      // Schedule the archival of the file
      std::list<std::string> remoteFilePathList;
      remoteFilePathList.push_back(remoteFilePath.str());
      ASSERT_NO_THROW(scheduler.queueArchiveRequest(requester, remoteFilePathList, rpath.getAfterScheme()));
1120
1121
    }
  }
1122
  DriveConfig driveConfig("T10D6116", "T10KD6", "/dev/tape_T10D6116", "manual");
1123
1124
1125
  DataTransferConfig castorConf;
  castorConf.bufsz = 1024*1024; // 1 MB memory buffers
  castorConf.nbBufs = 10;