CatalogueTest.cpp 282 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
/*
 * The CERN Tape Archive (CTA) project
 * Copyright (C) 2015  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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "catalogue/ArchiveFileRow.hpp"
#include "catalogue/CatalogueFactory.hpp"
#include "catalogue/CatalogueTest.hpp"
#include "common/exception/Exception.hpp"
23
#include "common/exception/UserError.hpp"
24
#include "rdbms/ConnFactoryFactory.hpp"
25

26
#include <algorithm>
27
#include <gtest/gtest.h>
28
#include <limits>
29
30
31
32
33
34
#include <map>
#include <memory>
#include <set>

namespace unitTests {

35
36
37
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
38
39
40
cta_catalogue_CatalogueTest::cta_catalogue_CatalogueTest() {
  m_localAdmin.username = "local_admin_user";
  m_localAdmin.host = "local_admin_host";
41

Steven Murray's avatar
Steven Murray committed
42
43
  m_admin.username = "admin_user_name";
  m_admin.host = "admin_host";
44
45
46
47
48
49
50
51
52
}

//------------------------------------------------------------------------------
// Setup
//------------------------------------------------------------------------------
void cta_catalogue_CatalogueTest::SetUp() {
  using namespace cta;
  using namespace cta::catalogue;

53
  try {
54
55
    const rdbms::Login &login = GetParam()->create();
    auto connFactory = rdbms::ConnFactoryFactory::create(login);
56
    const uint64_t nbConns = 2;
57
58
59

    m_catalogue = CatalogueFactory::create(login, nbConns);
    m_conn = connFactory->create();
60

61
62
63
64
65
    {
      const std::list<common::dataStructures::AdminUser> adminUsers = m_catalogue->getAdminUsers();
      for(auto &adminUser: adminUsers) {
        m_catalogue->deleteAdminUser(adminUser.name);
      }
66
    }
67
68
69
70
71
    {
      const std::list<common::dataStructures::AdminHost> adminHosts = m_catalogue->getAdminHosts();
      for(auto &adminHost: adminHosts) {
        m_catalogue->deleteAdminHost(adminHost.name);
      }
72
    }
73
74
75
76
77
78
    {
      const std::list<common::dataStructures::ArchiveRoute> archiveRoutes = m_catalogue->getArchiveRoutes();
      for(auto &archiveRoute: archiveRoutes) {
        m_catalogue->deleteArchiveRoute(archiveRoute.diskInstanceName, archiveRoute.storageClassName,
          archiveRoute.copyNb);
      }
79
    }
80
81
82
83
84
    {
      const std::list<common::dataStructures::RequesterMountRule> rules = m_catalogue->getRequesterMountRules();
      for(auto &rule: rules) {
        m_catalogue->deleteRequesterMountRule(rule.diskInstance, rule.name);
      }
85
    }
86
87
88
89
90
91
    {
      const std::list<common::dataStructures::RequesterGroupMountRule> rules =
        m_catalogue->getRequesterGroupMountRules();
      for(auto &rule: rules) {
        m_catalogue->deleteRequesterGroupMountRule(rule.diskInstance, rule.name);
      }
92
    }
93
    {
94
      auto itor = m_catalogue->getArchiveFiles();
95
96
      while(itor.hasMore()) {
        const auto archiveFile = itor.next();
97
98
        m_catalogue->deleteArchiveFile(archiveFile.diskInstance, archiveFile.archiveFileID);
      }
99
    }
100
101
102
103
104
    {
      const std::list<common::dataStructures::Tape> tapes = m_catalogue->getTapes();
      for(auto &tape: tapes) {
        m_catalogue->deleteTape(tape.vid);
      }
105
    }
106
107
108
109
110
    {
      const std::list<common::dataStructures::StorageClass> storageClasses = m_catalogue->getStorageClasses();
      for(auto &storageClass: storageClasses) {
        m_catalogue->deleteStorageClass(storageClass.diskInstance, storageClass.name);
      }
111
    }
112
113
114
115
116
    {
      const std::list<common::dataStructures::TapePool> tapePools = m_catalogue->getTapePools();
      for(auto &tapePool: tapePools) {
        m_catalogue->deleteTapePool(tapePool.name);
      }
117
    }
118
119
120
121
122
    {
      const std::list<common::dataStructures::LogicalLibrary> logicalLibraries = m_catalogue->getLogicalLibraries();
      for(auto &logicalLibrary: logicalLibraries) {
        m_catalogue->deleteLogicalLibrary(logicalLibrary.name);
      }
123
    }
124
125
126
127
128
    {
      const std::list<common::dataStructures::MountPolicy> mountPolicies = m_catalogue->getMountPolicies();
      for(auto &mountPolicy: mountPolicies) {
        m_catalogue->deleteMountPolicy(mountPolicy.name);
      }
129
    }
130
131
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  }
}

//------------------------------------------------------------------------------
// TearDown
//------------------------------------------------------------------------------
void cta_catalogue_CatalogueTest::TearDown() {
  m_catalogue.reset();
}

//------------------------------------------------------------------------------
// tapeListToMap
//------------------------------------------------------------------------------
std::map<std::string, cta::common::dataStructures::Tape> cta_catalogue_CatalogueTest::tapeListToMap(
  const std::list<cta::common::dataStructures::Tape> &listOfTapes) {
  using namespace cta;

  try {
    std::map<std::string, cta::common::dataStructures::Tape> vidToTape;

152
    for (auto &tape: listOfTapes) {
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
      if(vidToTape.end() != vidToTape.find(tape.vid)) {
        throw exception::Exception(std::string("Duplicate VID: value=") + tape.vid);
      }
      vidToTape[tape.vid] = tape;
    }

    return vidToTape;
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
}

//------------------------------------------------------------------------------
// archiveFileItorToMap
//------------------------------------------------------------------------------
std::map<uint64_t, cta::common::dataStructures::ArchiveFile> cta_catalogue_CatalogueTest::archiveFileItorToMap(
  cta::catalogue::ArchiveFileItor &itor) {
  using namespace cta;

  try {
    std::map<uint64_t, common::dataStructures::ArchiveFile> m;
    while(itor.hasMore()) {
175
      const auto archiveFile = itor.next();
176
177
178
179
180
181
182
183
184
185
186
187
188
      if(m.end() != m.find(archiveFile.archiveFileID)) {
        exception::Exception ex;
        ex.getMessage() << "Archive file with ID " << archiveFile.archiveFileID << " is a duplicate";
        throw ex;
      }
      m[archiveFile.archiveFileID] = archiveFile;
    }
    return m;
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
}

189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//------------------------------------------------------------------------------
// adminUserListToMap
//------------------------------------------------------------------------------
std::map<std::string, cta::common::dataStructures::AdminUser> cta_catalogue_CatalogueTest::adminUserListToMap(
  const std::list<cta::common::dataStructures::AdminUser> &listOfAdminUsers) {
  using namespace cta;

  try {
    std::map<std::string, common::dataStructures::AdminUser> m;

    for(auto &adminUser: listOfAdminUsers) {
      if(m.end() != m.find(adminUser.name)) {
        exception::Exception ex;
        ex.getMessage() << "Admin user " << adminUser.name << " is a duplicate";
        throw ex;
      }
      m[adminUser.name] = adminUser;
    }
    return m;
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
}

213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//------------------------------------------------------------------------------
// adminHostListToMap
//------------------------------------------------------------------------------
std::map<std::string, cta::common::dataStructures::AdminHost> cta_catalogue_CatalogueTest::adminHostListToMap(
  const std::list<cta::common::dataStructures::AdminHost> &listOfAdminHosts) {
  using namespace cta;

  try {
    std::map<std::string, common::dataStructures::AdminHost> m;

    for(auto &adminHost: listOfAdminHosts) {
      if(m.end() != m.find(adminHost.name)) {
        exception::Exception ex;
        ex.getMessage() << "Admin host " << adminHost.name << " is a duplicate";
        throw ex;
      }
      m[adminHost.name] = adminHost;
    }
    return m;
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
}

237
238
239
240
241
TEST_P(cta_catalogue_CatalogueTest, createAdminUser) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getAdminUsers().empty());

242
243
  const std::string createAdminUserComment = "Create admin user";
  m_catalogue->createAdminUser(m_localAdmin, m_admin.username, createAdminUserComment);
244
245
246
247
248
249

  {
    std::list<common::dataStructures::AdminUser> admins;
    admins = m_catalogue->getAdminUsers();
    ASSERT_EQ(1, admins.size());

250
    const common::dataStructures::AdminUser a = admins.front();
251

252
253
254
255
256
257
    ASSERT_EQ(m_admin.username, a.name);
    ASSERT_EQ(createAdminUserComment, a.comment);
    ASSERT_EQ(m_localAdmin.username, a.creationLog.username);
    ASSERT_EQ(m_localAdmin.host, a.creationLog.host);
    ASSERT_EQ(m_localAdmin.username, a.lastModificationLog.username);
    ASSERT_EQ(m_localAdmin.host, a.lastModificationLog.host);
258
259
260
261
262
263
264
265
  }
}

TEST_P(cta_catalogue_CatalogueTest, createAdminUser_same_twice) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getAdminUsers().empty());

266
  m_catalogue->createAdminUser(m_localAdmin, m_admin.username, "comment 1");
267

268
  ASSERT_THROW(m_catalogue->createAdminUser(m_localAdmin, m_admin.username,
269
    "comment 2"), exception::UserError);
270
271
272
273
274
275
276
}

TEST_P(cta_catalogue_CatalogueTest, deleteAdminUser) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getAdminUsers().empty());

277
278
  const std::string createAdminUserComment = "Create admin user";
  m_catalogue->createAdminUser(m_localAdmin, m_admin.username, createAdminUserComment);
279
280
281
282
283
284

  {
    std::list<common::dataStructures::AdminUser> admins;
    admins = m_catalogue->getAdminUsers();
    ASSERT_EQ(1, admins.size());

285
    const common::dataStructures::AdminUser a = admins.front();
286

287
288
289
290
291
292
    ASSERT_EQ(m_admin.username, a.name);
    ASSERT_EQ(createAdminUserComment, a.comment);
    ASSERT_EQ(m_localAdmin.username, a.creationLog.username);
    ASSERT_EQ(m_localAdmin.host, a.creationLog.host);
    ASSERT_EQ(m_localAdmin.username, a.lastModificationLog.username);
    ASSERT_EQ(m_localAdmin.host, a.lastModificationLog.host);
293
294
  }

295
  m_catalogue->deleteAdminUser(m_admin.username);
296
297
298
299
300
301
302
303

  ASSERT_TRUE(m_catalogue->getAdminUsers().empty());
}

TEST_P(cta_catalogue_CatalogueTest, deleteAdminUser_non_existant) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getAdminUsers().empty());
304
  ASSERT_THROW(m_catalogue->deleteAdminUser("non_existant_admin_user"), exception::UserError);
305
306
}

307
308
309
310
311
TEST_P(cta_catalogue_CatalogueTest, modifyAdminUserComment) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getAdminUsers().empty());

312
313
  const std::string createAdminUserComment = "Create admin user";
  m_catalogue->createAdminUser(m_localAdmin, m_admin.username, createAdminUserComment);
314
315
316
317
318
319

  {
    std::list<common::dataStructures::AdminUser> admins;
    admins = m_catalogue->getAdminUsers();
    ASSERT_EQ(1, admins.size());

320
    const common::dataStructures::AdminUser a = admins.front();
321

322
323
324
325
326
327
    ASSERT_EQ(m_admin.username, a.name);
    ASSERT_EQ(createAdminUserComment, a.comment);
    ASSERT_EQ(m_localAdmin.username, a.creationLog.username);
    ASSERT_EQ(m_localAdmin.host, a.creationLog.host);
    ASSERT_EQ(m_localAdmin.username, a.lastModificationLog.username);
    ASSERT_EQ(m_localAdmin.host, a.lastModificationLog.host);
328
329
330
  }

  const std::string modifiedComment = "Modified comment";
331
  m_catalogue->modifyAdminUserComment(m_localAdmin, m_admin.username, modifiedComment);
332
333

  {
334
335
336
    std::list<common::dataStructures::AdminUser> admins;
    admins = m_catalogue->getAdminUsers();
    ASSERT_EQ(1, admins.size());
337

338
    const common::dataStructures::AdminUser a = admins.front();
339

340
341
342
343
344
345
    ASSERT_EQ(m_admin.username, a.name);
    ASSERT_EQ(modifiedComment, a.comment);
    ASSERT_EQ(m_localAdmin.username, a.creationLog.username);
    ASSERT_EQ(m_localAdmin.host, a.creationLog.host);
    ASSERT_EQ(m_localAdmin.username, a.lastModificationLog.username);
    ASSERT_EQ(m_localAdmin.host, a.lastModificationLog.host);
346
347
348
  }
}

349
350
351
352
353
354
TEST_P(cta_catalogue_CatalogueTest, modifyAdminUserComment_nonExtistentAdminUser) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getAdminUsers().empty());

  const std::string modifiedComment = "Modified comment";
355
  ASSERT_THROW(m_catalogue->modifyAdminUserComment(m_localAdmin, m_admin.username, modifiedComment),
356
357
358
    exception::UserError);
}

359
360
361
362
363
TEST_P(cta_catalogue_CatalogueTest, createAdminHost) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getAdminHosts().empty());

364
365
  const std::string createAdminHostComment = "Create admin host";
  m_catalogue->createAdminHost(m_localAdmin, m_admin.host, createAdminHostComment);
366
367
368
369

  {
    std::list<common::dataStructures::AdminHost> hosts;
    hosts = m_catalogue->getAdminHosts();
370
371
372
373
374
375
376
377
378
379
    ASSERT_EQ(1, hosts.size());

    const common::dataStructures::AdminHost h = hosts.front();

    ASSERT_EQ(m_admin.host, h.name);
    ASSERT_EQ(createAdminHostComment, h.comment);
    ASSERT_EQ(m_localAdmin.username, h.creationLog.username);
    ASSERT_EQ(m_localAdmin.host, h.creationLog.host);
    ASSERT_EQ(m_localAdmin.username, h.lastModificationLog.username);
    ASSERT_EQ(m_localAdmin.host, h.lastModificationLog.host);
380
381
382
383
384
385
  }
}

TEST_P(cta_catalogue_CatalogueTest, createAdminHost_same_twice) {
  using namespace cta;

386
  ASSERT_TRUE(m_catalogue->getAdminHosts().empty());
387

388
389
  const std::string createAdminHostComment = "Create admin host";
  m_catalogue->createAdminHost(m_localAdmin, m_admin.host, createAdminHostComment);
390

391
392
393
394
  {
    std::list<common::dataStructures::AdminHost> hosts;
    hosts = m_catalogue->getAdminHosts();
    ASSERT_EQ(1, hosts.size());
395

396
    const common::dataStructures::AdminHost h = hosts.front();
397

398
399
400
401
402
403
    ASSERT_EQ(m_admin.host, h.name);
    ASSERT_EQ(createAdminHostComment, h.comment);
    ASSERT_EQ(m_localAdmin.username, h.creationLog.username);
    ASSERT_EQ(m_localAdmin.host, h.creationLog.host);
    ASSERT_EQ(m_localAdmin.username, h.lastModificationLog.username);
    ASSERT_EQ(m_localAdmin.host, h.lastModificationLog.host);
404
405
  }

406
  ASSERT_THROW(m_catalogue->createAdminHost(m_localAdmin, m_admin.host, "comment 2"), exception::UserError);
407
408
409
410
411
412
413
}

TEST_P(cta_catalogue_CatalogueTest, deleteAdminHost) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getAdminHosts().empty());

414
415
  const std::string createAdminHostComment = "Create admin host";
  m_catalogue->createAdminHost(m_localAdmin, m_admin.host, createAdminHostComment);
416
417

  {
418
419
420
    std::list<common::dataStructures::AdminHost> hosts;
    hosts = m_catalogue->getAdminHosts();
    ASSERT_EQ(1, hosts.size());
421

422
    const common::dataStructures::AdminHost h = hosts.front();
423

424
425
426
427
428
429
    ASSERT_EQ(m_admin.host, h.name);
    ASSERT_EQ(createAdminHostComment, h.comment);
    ASSERT_EQ(m_localAdmin.username, h.creationLog.username);
    ASSERT_EQ(m_localAdmin.host, h.creationLog.host);
    ASSERT_EQ(m_localAdmin.username, h.lastModificationLog.username);
    ASSERT_EQ(m_localAdmin.host, h.lastModificationLog.host);
430
431
  }

432
  m_catalogue->deleteAdminHost(m_admin.host);
433
434
435
436
437
438
439
440

  ASSERT_TRUE(m_catalogue->getAdminHosts().empty());
}

TEST_P(cta_catalogue_CatalogueTest, deleteAdminHost_non_existant) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getAdminHosts().empty());
441
  ASSERT_THROW(m_catalogue->deleteAdminHost("non_exstant_admin_host"), exception::UserError);
442
443
}

444
445
446
447
448
TEST_P(cta_catalogue_CatalogueTest, modifyAdminHostComment) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getAdminHosts().empty());

449
450
  const std::string createAdminHostComment = "Create admin host";
  m_catalogue->createAdminHost(m_localAdmin, m_admin.host, createAdminHostComment);
451
452

  {
453
454
    std::list<common::dataStructures::AdminHost> hosts;
    hosts = m_catalogue->getAdminHosts();
455
456
    ASSERT_EQ(1, hosts.size());

457
    const common::dataStructures::AdminHost h = hosts.front();
458

459
460
461
462
463
464
    ASSERT_EQ(m_admin.host, h.name);
    ASSERT_EQ(createAdminHostComment, h.comment);
    ASSERT_EQ(m_localAdmin.username, h.creationLog.username);
    ASSERT_EQ(m_localAdmin.host, h.creationLog.host);
    ASSERT_EQ(m_localAdmin.username, h.lastModificationLog.username);
    ASSERT_EQ(m_localAdmin.host, h.lastModificationLog.host);
465
466
467
  }

  const std::string modifiedComment = "Modified comment";
468
  m_catalogue->modifyAdminHostComment(m_localAdmin, m_admin.host, modifiedComment);
469
470

  {
471
472
473
    std::list<common::dataStructures::AdminHost> hosts;
    hosts = m_catalogue->getAdminHosts();
    ASSERT_EQ(1, hosts.size());
474

475
    const common::dataStructures::AdminHost h = hosts.front();
476

477
478
479
480
481
482
    ASSERT_EQ(m_admin.host, h.name);
    ASSERT_EQ(modifiedComment, h.comment);
    ASSERT_EQ(m_localAdmin.username, h.creationLog.username);
    ASSERT_EQ(m_localAdmin.host, h.creationLog.host);
    ASSERT_EQ(m_localAdmin.username, h.lastModificationLog.username);
    ASSERT_EQ(m_localAdmin.host, h.lastModificationLog.host);
483
484
485
  }
}

486
487
488
TEST_P(cta_catalogue_CatalogueTest, isAdmin_false) {
  using namespace cta;

Steven Murray's avatar
Steven Murray committed
489
  ASSERT_FALSE(m_catalogue->isAdmin(m_admin));
490
491
492
493
494
}

TEST_P(cta_catalogue_CatalogueTest, isAdmin_true) {
  using namespace cta;

495
496
497
498
499
500
501
502
503
504
505
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
  ASSERT_TRUE(m_catalogue->getAdminUsers().empty());

  const std::string createAdminUserComment = "Create admin user";
  m_catalogue->createAdminUser(m_localAdmin, m_admin.username, createAdminUserComment);

  {
    std::list<common::dataStructures::AdminUser> admins;
    admins = m_catalogue->getAdminUsers();
    ASSERT_EQ(1, admins.size());

    const common::dataStructures::AdminUser a = admins.front();

    ASSERT_EQ(m_admin.username, a.name);
    ASSERT_EQ(createAdminUserComment, a.comment);
    ASSERT_EQ(m_localAdmin.username, a.creationLog.username);
    ASSERT_EQ(m_localAdmin.host, a.creationLog.host);
    ASSERT_EQ(m_localAdmin.username, a.lastModificationLog.username);
    ASSERT_EQ(m_localAdmin.host, a.lastModificationLog.host);
  }

  ASSERT_TRUE(m_catalogue->getAdminHosts().empty());

  const std::string createAdminHostComment = "Create admin host";
  m_catalogue->createAdminHost(m_localAdmin, m_admin.host, createAdminHostComment);

  {
    std::list<common::dataStructures::AdminHost> hosts;
    hosts = m_catalogue->getAdminHosts();
    ASSERT_EQ(1, hosts.size());

    const common::dataStructures::AdminHost h = hosts.front();

    ASSERT_EQ(m_admin.host, h.name);
    ASSERT_EQ(createAdminHostComment, h.comment);
    ASSERT_EQ(m_localAdmin.username, h.creationLog.username);
    ASSERT_EQ(m_localAdmin.host, h.creationLog.host);
    ASSERT_EQ(m_localAdmin.username, h.lastModificationLog.username);
    ASSERT_EQ(m_localAdmin.host, h.lastModificationLog.host);
  }
534

535
  ASSERT_TRUE(m_catalogue->isAdmin(m_admin));
536
537
538
539
540
541
542
}

TEST_P(cta_catalogue_CatalogueTest, createStorageClass) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getStorageClasses().empty());

543
544
545
546
  common::dataStructures::StorageClass storageClass;
  storageClass.diskInstance = "disk_instance";
  storageClass.name = "storage_class";
  storageClass.nbCopies = 2;
547
  storageClass.comment = "Create storage class";
Steven Murray's avatar
Steven Murray committed
548
  m_catalogue->createStorageClass(m_admin, storageClass);
549
550
551
552
553
554

  const std::list<common::dataStructures::StorageClass> storageClasses =
    m_catalogue->getStorageClasses();

  ASSERT_EQ(1, storageClasses.size());

555
556
557
558
  ASSERT_EQ(storageClass.diskInstance, storageClasses.front().diskInstance);
  ASSERT_EQ(storageClass.name, storageClasses.front().name);
  ASSERT_EQ(storageClass.nbCopies, storageClasses.front().nbCopies);
  ASSERT_EQ(storageClass.comment, storageClasses.front().comment);
559

560
  const common::dataStructures::EntryLog creationLog = storageClasses.front().creationLog;
Steven Murray's avatar
Steven Murray committed
561
562
  ASSERT_EQ(m_admin.username, creationLog.username);
  ASSERT_EQ(m_admin.host, creationLog.host);
563

564
  const common::dataStructures::EntryLog lastModificationLog = storageClasses.front().lastModificationLog;
565
566
567
568
569
570
  ASSERT_EQ(creationLog, lastModificationLog);
}

TEST_P(cta_catalogue_CatalogueTest, createStorageClass_same_twice) {
  using namespace cta;

571
572
573
574
  common::dataStructures::StorageClass storageClass;
  storageClass.diskInstance = "disk_instance";
  storageClass.name = "storage_class";
  storageClass.nbCopies = 2;
575
  storageClass.comment = "Create storage class";
Steven Murray's avatar
Steven Murray committed
576
577
  m_catalogue->createStorageClass(m_admin, storageClass);
  ASSERT_THROW(m_catalogue->createStorageClass(m_admin, storageClass), exception::UserError);
578
579
}

580
581
582
583
584
585
586
TEST_P(cta_catalogue_CatalogueTest, createStorageClass_same_name_different_disk_instance) {
  using namespace cta;

  common::dataStructures::StorageClass storageClass1;
  storageClass1.diskInstance = "disk_instance_1";
  storageClass1.name = "storage_class";
  storageClass1.nbCopies = 2;
587
  storageClass1.comment = "Create storage class";
588
589
590
591

  common::dataStructures::StorageClass storageClass2 = storageClass1;
  storageClass2.diskInstance = "disk_instance_2";

Steven Murray's avatar
Steven Murray committed
592
593
  m_catalogue->createStorageClass(m_admin, storageClass1);
  m_catalogue->createStorageClass(m_admin, storageClass2);
594
595
596
597
598
599

  const std::list<common::dataStructures::StorageClass> storageClasses = m_catalogue->getStorageClasses();

  ASSERT_EQ(2, storageClasses.size());

  {
600
601
602
603
    auto eqStorageClass1 = [&storageClass1](const common::dataStructures::StorageClass &obj) {
      return obj.diskInstance == storageClass1.diskInstance && obj.name == storageClass1.name;
    };
    auto itor = std::find_if(storageClasses.begin(), storageClasses.end(), eqStorageClass1);
604
605
606
607
    ASSERT_FALSE(itor == storageClasses.end());
  }

  {
608
609
610
611
    auto eqStorageClass2 = [&storageClass2](const common::dataStructures::StorageClass &obj) {
      return obj.diskInstance == storageClass2.diskInstance && obj.name == storageClass2.name;
    };
    auto itor = std::find_if(storageClasses.begin(), storageClasses.end(), eqStorageClass2);
612
613
614
615
    ASSERT_FALSE(itor == storageClasses.end());
  }
}

616
617
618
619
620
TEST_P(cta_catalogue_CatalogueTest, deleteStorageClass) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getStorageClasses().empty());

621
622
623
624
  common::dataStructures::StorageClass storageClass;
  storageClass.diskInstance = "disk_instance";
  storageClass.name = "storage_class";
  storageClass.nbCopies = 2;
625
  storageClass.comment = "Create storage class";
Steven Murray's avatar
Steven Murray committed
626
  m_catalogue->createStorageClass(m_admin, storageClass);
627
628
629
630
631
632

  const std::list<common::dataStructures::StorageClass> storageClasses =
    m_catalogue->getStorageClasses();

  ASSERT_EQ(1, storageClasses.size());

633
634
635
636
  ASSERT_EQ(storageClass.diskInstance, storageClasses.front().diskInstance);
  ASSERT_EQ(storageClass.name, storageClasses.front().name);
  ASSERT_EQ(storageClass.nbCopies, storageClasses.front().nbCopies);
  ASSERT_EQ(storageClass.comment, storageClasses.front().comment);
637

638
  const common::dataStructures::EntryLog creationLog = storageClasses.front().creationLog;
Steven Murray's avatar
Steven Murray committed
639
640
  ASSERT_EQ(m_admin.username, creationLog.username);
  ASSERT_EQ(m_admin.host, creationLog.host);
641

642
  const common::dataStructures::EntryLog lastModificationLog = storageClasses.front().lastModificationLog;
643
644
  ASSERT_EQ(creationLog, lastModificationLog);

645
  m_catalogue->deleteStorageClass(storageClass.diskInstance, storageClass.name);
646
647
648
649
650
651
652
  ASSERT_TRUE(m_catalogue->getStorageClasses().empty());
}

TEST_P(cta_catalogue_CatalogueTest, deleteStorageClass_non_existant) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getStorageClasses().empty());
653
654
  ASSERT_THROW(m_catalogue->deleteStorageClass("non_existant_disk_instance", "non_existant_storage_class"),
    exception::UserError);
655
656
}

657
658
659
660
661
662
663
664
665
666
TEST_P(cta_catalogue_CatalogueTest, modifyStorageClassNbCopies) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getStorageClasses().empty());

  common::dataStructures::StorageClass storageClass;
  storageClass.diskInstance = "disk_instance";
  storageClass.name = "storage_class";
  storageClass.nbCopies = 2;
  storageClass.comment = "Create storage class";
Steven Murray's avatar
Steven Murray committed
667
  m_catalogue->createStorageClass(m_admin, storageClass);
668
669
670
671
672
673
674
675
676
677
678
679

  {
    const std::list<common::dataStructures::StorageClass> storageClasses = m_catalogue->getStorageClasses();

    ASSERT_EQ(1, storageClasses.size());

    ASSERT_EQ(storageClass.diskInstance, storageClasses.front().diskInstance);
    ASSERT_EQ(storageClass.name, storageClasses.front().name);
    ASSERT_EQ(storageClass.nbCopies, storageClasses.front().nbCopies);
    ASSERT_EQ(storageClass.comment, storageClasses.front().comment);

    const common::dataStructures::EntryLog creationLog = storageClasses.front().creationLog;
Steven Murray's avatar
Steven Murray committed
680
681
    ASSERT_EQ(m_admin.username, creationLog.username);
    ASSERT_EQ(m_admin.host, creationLog.host);
682
683
684
685
686
687

    const common::dataStructures::EntryLog lastModificationLog = storageClasses.front().lastModificationLog;
    ASSERT_EQ(creationLog, lastModificationLog);
  }

  const uint64_t modifiedNbCopies = 5;
Steven Murray's avatar
Steven Murray committed
688
  m_catalogue->modifyStorageClassNbCopies(m_admin, storageClass.diskInstance, storageClass.name, modifiedNbCopies);
689
690
691
692
693
694
695
696
697
698
699
700

  {
    const std::list<common::dataStructures::StorageClass> storageClasses = m_catalogue->getStorageClasses();

    ASSERT_EQ(1, storageClasses.size());

    ASSERT_EQ(storageClass.diskInstance, storageClasses.front().diskInstance);
    ASSERT_EQ(storageClass.name, storageClasses.front().name);
    ASSERT_EQ(modifiedNbCopies, storageClasses.front().nbCopies);
    ASSERT_EQ(storageClass.comment, storageClasses.front().comment);

    const common::dataStructures::EntryLog creationLog = storageClasses.front().creationLog;
Steven Murray's avatar
Steven Murray committed
701
702
    ASSERT_EQ(m_admin.username, creationLog.username);
    ASSERT_EQ(m_admin.host, creationLog.host);
703
704
705
  }
}

706
707
708
709
710
711
712
713
TEST_P(cta_catalogue_CatalogueTest, modifyStorageClassNbCopies_nonExistentStorageClass) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getStorageClasses().empty());

  const std::string diskInstance = "disk_instance";
  const std::string storageClassName = "storage_class";
  const uint64_t nbCopies = 5;
Steven Murray's avatar
Steven Murray committed
714
  ASSERT_THROW(m_catalogue->modifyStorageClassNbCopies(m_admin, diskInstance, storageClassName, nbCopies),
715
716
717
    exception::UserError);
}

718
719
720
721
722
723
724
725
726
727
TEST_P(cta_catalogue_CatalogueTest, modifyStorageClassComment) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getStorageClasses().empty());

  common::dataStructures::StorageClass storageClass;
  storageClass.diskInstance = "disk_instance";
  storageClass.name = "storage_class";
  storageClass.nbCopies = 2;
  storageClass.comment = "Create storage class";
Steven Murray's avatar
Steven Murray committed
728
  m_catalogue->createStorageClass(m_admin, storageClass);
729
730
731
732
733
734
735
736
737
738
739
740

  {
    const std::list<common::dataStructures::StorageClass> storageClasses = m_catalogue->getStorageClasses();

    ASSERT_EQ(1, storageClasses.size());

    ASSERT_EQ(storageClass.diskInstance, storageClasses.front().diskInstance);
    ASSERT_EQ(storageClass.name, storageClasses.front().name);
    ASSERT_EQ(storageClass.nbCopies, storageClasses.front().nbCopies);
    ASSERT_EQ(storageClass.comment, storageClasses.front().comment);

    const common::dataStructures::EntryLog creationLog = storageClasses.front().creationLog;
Steven Murray's avatar
Steven Murray committed
741
742
    ASSERT_EQ(m_admin.username, creationLog.username);
    ASSERT_EQ(m_admin.host, creationLog.host);
743
744
745
746
747
748

    const common::dataStructures::EntryLog lastModificationLog = storageClasses.front().lastModificationLog;
    ASSERT_EQ(creationLog, lastModificationLog);
  }

  const std::string modifiedComment = "Modified comment";
Steven Murray's avatar
Steven Murray committed
749
  m_catalogue->modifyStorageClassComment(m_admin, storageClass.diskInstance, storageClass.name, modifiedComment);
750
751
752
753
754
755
756
757
758
759
760
761

  {
    const std::list<common::dataStructures::StorageClass> storageClasses = m_catalogue->getStorageClasses();

    ASSERT_EQ(1, storageClasses.size());

    ASSERT_EQ(storageClass.diskInstance, storageClasses.front().diskInstance);
    ASSERT_EQ(storageClass.name, storageClasses.front().name);
    ASSERT_EQ(storageClass.nbCopies, storageClasses.front().nbCopies);
    ASSERT_EQ(modifiedComment, storageClasses.front().comment);

    const common::dataStructures::EntryLog creationLog = storageClasses.front().creationLog;
Steven Murray's avatar
Steven Murray committed
762
763
    ASSERT_EQ(m_admin.username, creationLog.username);
    ASSERT_EQ(m_admin.host, creationLog.host);
764
765
766
  }
}

767
768
769
770
771
772
773
774
TEST_P(cta_catalogue_CatalogueTest, modifyStorageClassComment_nonExistentStorageClass) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getStorageClasses().empty());

  const std::string diskInstance = "disk_instance";
  const std::string storageClassName = "storage_class";
  const std::string comment = "Comment";
Steven Murray's avatar
Steven Murray committed
775
  ASSERT_THROW(m_catalogue->modifyStorageClassComment(m_admin, diskInstance, storageClassName, comment),
776
777
778
    exception::UserError);
}

779
780
781
782
783
784
TEST_P(cta_catalogue_CatalogueTest, createTapePool) {
  using namespace cta;
      
  ASSERT_TRUE(m_catalogue->getTapePools().empty());
      
  const std::string tapePoolName = "tape_pool";
785
786
787

  ASSERT_FALSE(m_catalogue->tapePoolExists(tapePoolName));

788
  const uint64_t nbPartialTapes = 2;
789
  const bool isEncrypted = true;
790
  const std::string comment = "Create tape pool";
Steven Murray's avatar
Steven Murray committed
791
  m_catalogue->createTapePool(m_admin, tapePoolName, nbPartialTapes, isEncrypted,
792
    comment);
793
794

  ASSERT_TRUE(m_catalogue->tapePoolExists(tapePoolName));
795
796
797
798
799
800
801
802
803
      
  const std::list<common::dataStructures::TapePool> pools =
    m_catalogue->getTapePools();
      
  ASSERT_EQ(1, pools.size());
      
  const common::dataStructures::TapePool pool = pools.front();
  ASSERT_EQ(tapePoolName, pool.name);
  ASSERT_EQ(nbPartialTapes, pool.nbPartialTapes);
804
  ASSERT_EQ(isEncrypted, pool.encryption);
805
806
807
  ASSERT_EQ(comment, pool.comment);

  const common::dataStructures::EntryLog creationLog = pool.creationLog;
Steven Murray's avatar
Steven Murray committed
808
809
  ASSERT_EQ(m_admin.username, creationLog.username);
  ASSERT_EQ(m_admin.host, creationLog.host);
810
811
812
813
814
815
816
817
818
819
820
  
  const common::dataStructures::EntryLog lastModificationLog =
    pool.lastModificationLog;
  ASSERT_EQ(creationLog, lastModificationLog);
}
  
TEST_P(cta_catalogue_CatalogueTest, createTapePool_same_twice) {
  using namespace cta;
  
  const std::string tapePoolName = "tape_pool";
  const uint64_t nbPartialTapes = 2;
821
  const bool isEncrypted = true;
822
  const std::string comment = "Create tape pool";
Steven Murray's avatar
Steven Murray committed
823
  m_catalogue->createTapePool(m_admin, tapePoolName, nbPartialTapes, isEncrypted,
824
    comment);
Steven Murray's avatar
Steven Murray committed
825
  ASSERT_THROW(m_catalogue->createTapePool(m_admin, tapePoolName, nbPartialTapes, isEncrypted, comment),
826
    exception::UserError);
827
828
829
830
831
832
833
834
835
}

TEST_P(cta_catalogue_CatalogueTest, deleteTapePool) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getTapePools().empty());

  const std::string tapePoolName = "tape_pool";
  const uint64_t nbPartialTapes = 2;
836
  const bool isEncrypted = true;
837
  const std::string comment = "Create tape pool";
Steven Murray's avatar
Steven Murray committed
838
  m_catalogue->createTapePool(m_admin, tapePoolName, nbPartialTapes, isEncrypted,
839
840
841
842
843
844
845
846
847
848
    comment);

  const std::list<common::dataStructures::TapePool> pools =
    m_catalogue->getTapePools();

  ASSERT_EQ(1, pools.size());

  const common::dataStructures::TapePool pool = pools.front();
  ASSERT_EQ(tapePoolName, pool.name);
  ASSERT_EQ(nbPartialTapes, pool.nbPartialTapes);
849
  ASSERT_EQ(isEncrypted, pool.encryption);
850
851
852
  ASSERT_EQ(comment, pool.comment);

  const common::dataStructures::EntryLog creationLog = pool.creationLog;
Steven Murray's avatar
Steven Murray committed
853
854
  ASSERT_EQ(m_admin.username, creationLog.username);
  ASSERT_EQ(m_admin.host, creationLog.host);
855
856
857
858
859
860
861
862
863
864
865
866
867

  const common::dataStructures::EntryLog lastModificationLog =
    pool.lastModificationLog;
  ASSERT_EQ(creationLog, lastModificationLog);

  m_catalogue->deleteTapePool(pool.name);
  ASSERT_TRUE(m_catalogue->getTapePools().empty());
}

TEST_P(cta_catalogue_CatalogueTest, deleteTapePool_non_existant) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getTapePools().empty());
868
  ASSERT_THROW(m_catalogue->deleteTapePool("non_existant_tape_pool"), exception::UserError);
869
870
}

871
872
873
874
875
876
877
TEST_P(cta_catalogue_CatalogueTest, modifyTapePoolNbPartialTapes) {
  using namespace cta;
      
  ASSERT_TRUE(m_catalogue->getTapePools().empty());
      
  const std::string tapePoolName = "tape_pool";
  const uint64_t nbPartialTapes = 2;
878
  const bool isEncrypted = true;
879
  const std::string comment = "Create tape pool";
Steven Murray's avatar
Steven Murray committed
880
  m_catalogue->createTapePool(m_admin, tapePoolName, nbPartialTapes, isEncrypted, comment);
881
882
883
884
885
886
887
888
889

  {
    const std::list<common::dataStructures::TapePool> pools = m_catalogue->getTapePools();
      
    ASSERT_EQ(1, pools.size());
      
    const common::dataStructures::TapePool pool = pools.front();
    ASSERT_EQ(tapePoolName, pool.name);
    ASSERT_EQ(nbPartialTapes, pool.nbPartialTapes);
890
    ASSERT_EQ(isEncrypted, pool.encryption);
891
892
893
    ASSERT_EQ(comment, pool.comment);

    const common::dataStructures::EntryLog creationLog = pool.creationLog;
Steven Murray's avatar
Steven Murray committed
894
895
    ASSERT_EQ(m_admin.username, creationLog.username);
    ASSERT_EQ(m_admin.host, creationLog.host);
896
897
898
899
900
901
  
    const common::dataStructures::EntryLog lastModificationLog = pool.lastModificationLog;
    ASSERT_EQ(creationLog, lastModificationLog);
  }

  const uint64_t modifiedNbPartialTapes = 5;
Steven Murray's avatar
Steven Murray committed
902
  m_catalogue->modifyTapePoolNbPartialTapes(m_admin, tapePoolName, modifiedNbPartialTapes);
903
904
905
906
907
908
909
910
911

  {
    const std::list<common::dataStructures::TapePool> pools = m_catalogue->getTapePools();
      
    ASSERT_EQ(1, pools.size());
      
    const common::dataStructures::TapePool pool = pools.front();
    ASSERT_EQ(tapePoolName, pool.name);
    ASSERT_EQ(modifiedNbPartialTapes, pool.nbPartialTapes);
912
    ASSERT_EQ(isEncrypted, pool.encryption);
913
914
915
    ASSERT_EQ(comment, pool.comment);

    const common::dataStructures::EntryLog creationLog = pool.creationLog;
Steven Murray's avatar
Steven Murray committed
916
917
    ASSERT_EQ(m_admin.username, creationLog.username);
    ASSERT_EQ(m_admin.host, creationLog.host);
918
919
  }
}
920

921
922
923
924
925
926
927
TEST_P(cta_catalogue_CatalogueTest, modifyTapePoolNbPartialTapes_nonExistentTapePool) {
  using namespace cta;
      
  ASSERT_TRUE(m_catalogue->getTapePools().empty());

  const std::string tapePoolName = "tape_pool";
  const uint64_t nbPartialTapes = 5;
Steven Murray's avatar
Steven Murray committed
928
  ASSERT_THROW(m_catalogue->modifyTapePoolNbPartialTapes(m_admin, tapePoolName, nbPartialTapes),
929
930
931
    exception::UserError);
}

932
933
934
935
936
937
938
TEST_P(cta_catalogue_CatalogueTest, modifyTapePoolComment) {
  using namespace cta;
      
  ASSERT_TRUE(m_catalogue->getTapePools().empty());
      
  const std::string tapePoolName = "tape_pool";
  const uint64_t nbPartialTapes = 2;
939
  const bool isEncrypted = true;
940
  const std::string comment = "Create tape pool";
Steven Murray's avatar
Steven Murray committed
941
  m_catalogue->createTapePool(m_admin, tapePoolName, nbPartialTapes, isEncrypted, comment);
942
943
944
945
946

  {
    const std::list<common::dataStructures::TapePool> pools = m_catalogue->getTapePools();
      
    ASSERT_EQ(1, pools.size());
947

948
949
950
    const common::dataStructures::TapePool pool = pools.front();
    ASSERT_EQ(tapePoolName, pool.name);
    ASSERT_EQ(nbPartialTapes, pool.nbPartialTapes);
951
    ASSERT_EQ(isEncrypted, pool.encryption);
952
953
954
    ASSERT_EQ(comment, pool.comment);

    const common::dataStructures::EntryLog creationLog = pool.creationLog;
Steven Murray's avatar
Steven Murray committed
955
956
    ASSERT_EQ(m_admin.username, creationLog.username);
    ASSERT_EQ(m_admin.host, creationLog.host);
957
958
959
960
961
962
  
    const common::dataStructures::EntryLog lastModificationLog = pool.lastModificationLog;
    ASSERT_EQ(creationLog, lastModificationLog);
  }

  const std::string modifiedComment = "Modified comment";
Steven Murray's avatar
Steven Murray committed
963
  m_catalogue->modifyTapePoolComment(m_admin, tapePoolName, modifiedComment);
964
965
966
967
968
969
970
971
972

  {
    const std::list<common::dataStructures::TapePool> pools = m_catalogue->getTapePools();
      
    ASSERT_EQ(1, pools.size());
      
    const common::dataStructures::TapePool pool = pools.front();
    ASSERT_EQ(tapePoolName, pool.name);
    ASSERT_EQ(nbPartialTapes, pool.nbPartialTapes);
973
    ASSERT_EQ(isEncrypted, pool.encryption);
974
975
976
    ASSERT_EQ(modifiedComment, pool.comment);

    const common::dataStructures::EntryLog creationLog = pool.creationLog;
Steven Murray's avatar
Steven Murray committed
977
978
    ASSERT_EQ(m_admin.username, creationLog.username);
    ASSERT_EQ(m_admin.host, creationLog.host);
979
980
  }
}
981

982
983
984
985
986
987
988
TEST_P(cta_catalogue_CatalogueTest, modifyTapePoolComment_nonExistentTapePool) {
  using namespace cta;
      
  ASSERT_TRUE(m_catalogue->getTapePools().empty());
      
  const std::string tapePoolName = "tape_pool";
  const std::string comment = "Comment";
Steven Murray's avatar
Steven Murray committed
989
  ASSERT_THROW(m_catalogue->modifyTapePoolComment(m_admin, tapePoolName, comment), exception::UserError);
990
991
}

992
993
994
995
996
997
998
999
1000
TEST_P(cta_catalogue_CatalogueTest, setTapePoolEncryption) {
  using namespace cta;
      
  ASSERT_TRUE(m_catalogue->getTapePools().empty());
      
  const std::string tapePoolName = "tape_pool";
  const uint64_t nbPartialTapes = 2;
  const bool isEncrypted = true;
  const std::string comment = "Create tape pool";
For faster browsing, not all history is shown. View entire blame