InMemoryCatalogueTest.cpp 115 KB
Newer Older
Steven Murray's avatar
WIP    
Steven Murray committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * 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/>.
 */

19
#include "catalogue/ArchiveFileRow.hpp"
20
#include "catalogue/CatalogueFactory.hpp"
21
#include "catalogue/UserError.hpp"
22
#include "common/exception/Exception.hpp"
23
24

#include <gtest/gtest.h>
25
#include <map>
26
#include <memory>
Steven Murray's avatar
Steven Murray committed
27
#include <set>
28
29
30

namespace unitTests {

31
class cta_catalogue_InMemoryCatalogueTest : public ::testing::Test {
32
public:
33
  cta_catalogue_InMemoryCatalogueTest():
34
    m_bootstrapComment("bootstrap") {
Steven Murray's avatar
Steven Murray committed
35

36
    m_cliSI.username = "cli_user_name";
Steven Murray's avatar
Steven Murray committed
37
38
    m_cliSI.host = "cli_host";

39
    m_bootstrapAdminSI.username = "bootstrap_admin_user_name";
Steven Murray's avatar
Steven Murray committed
40
41
    m_bootstrapAdminSI.host = "bootstrap_host";

42
    m_adminSI.username = "admin_user_name";
Steven Murray's avatar
Steven Murray committed
43
    m_adminSI.host = "admin_host";
44
45
  }

46
47
48
protected:

  virtual void SetUp() {
49
    using namespace cta;
Steven Murray's avatar
Steven Murray committed
50
51
    using namespace cta::catalogue;

52
    const DbLogin catalogueLogin(DbLogin::DBTYPE_IN_MEMORY, "", "", "");
53
    m_catalogue.reset(CatalogueFactory::create(catalogueLogin));
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

    {
      const std::list<common::dataStructures::AdminUser> adminUsers = m_catalogue->getAdminUsers();
      for(auto &adminUser: adminUsers) {
        m_catalogue->deleteAdminUser(adminUser.name);
      }
    }
    {
      const std::list<common::dataStructures::AdminHost> adminHosts = m_catalogue->getAdminHosts();
      for(auto &adminHost: adminHosts) {
        m_catalogue->deleteAdminHost(adminHost.name);
      }
    }
    {
      const std::list<common::dataStructures::ArchiveRoute> archiveRoutes = m_catalogue->getArchiveRoutes();
      for(auto &archiveRoute: archiveRoutes) {
        m_catalogue->deleteArchiveRoute(archiveRoute.storageClassName, archiveRoute.copyNb);
      }
    }
    {
74
75
76
77
78
79
80
81
82
83
      const std::list<common::dataStructures::RequesterMountRule> rules = m_catalogue->getRequesterMountRules();
      for(auto &rule: rules) {
        m_catalogue->deleteRequesterMountRule(rule.name);
      }
    }
    {
      const std::list<common::dataStructures::RequesterGroupMountRule> rules =
        m_catalogue->getRequesterGroupMountRules();
      for(auto &rule: rules) {
        m_catalogue->deleteRequesterGroupMountRule(rule.name);
84
85
86
      }
    }
    {
87
88
89
      std::unique_ptr<ArchiveFileItor> itor = m_catalogue->getArchiveFileItor();
      while(itor->hasMore()) {
        m_catalogue->deleteArchiveFile(itor->next().archiveFileID);
90
91
92
93
94
95
96
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
      }
    }
    {
      const std::list<common::dataStructures::Tape> tapes = m_catalogue->getTapes("", "", "", "", "", "", "", "");
      for(auto &tape: tapes) {
        m_catalogue->deleteTape(tape.vid);
      }
    }
    {
      const std::list<common::dataStructures::StorageClass> storageClasses = m_catalogue->getStorageClasses();
      for(auto &storageClass: storageClasses) {
        m_catalogue->deleteStorageClass(storageClass.name);
      }
    }
    {
      const std::list<common::dataStructures::TapePool> tapePools = m_catalogue->getTapePools();
      for(auto &tapePool: tapePools) {
        m_catalogue->deleteTapePool(tapePool.name);
      }
    }
    {
      const std::list<common::dataStructures::LogicalLibrary> logicalLibraries = m_catalogue->getLogicalLibraries();
      for(auto &logicalLibrary: logicalLibraries) {
        m_catalogue->deleteLogicalLibrary(logicalLibrary.name);
      }
    }
    {
      const std::list<common::dataStructures::MountPolicy> mountPolicies = m_catalogue->getMountPolicies();
      for(auto &mountPolicy: mountPolicies) {
        m_catalogue->deleteMountPolicy(mountPolicy.name);
      }
    }
122
123
124
  }

  virtual void TearDown() {
Steven Murray's avatar
Steven Murray committed
125
    m_catalogue.reset();
126
  }
127

128
  std::unique_ptr<cta::catalogue::Catalogue> m_catalogue;
Steven Murray's avatar
Steven Murray committed
129
130
131
132
  const std::string m_bootstrapComment;
  cta::common::dataStructures::SecurityIdentity m_cliSI;
  cta::common::dataStructures::SecurityIdentity m_bootstrapAdminSI;
  cta::common::dataStructures::SecurityIdentity m_adminSI;
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

  /**
   * Creates a map from VID to tape given the specified list of tapes.
   *
   * @param listOfTapes The list of tapes from which the map shall be created.
   * @return The map from VID to tape.
   */
  std::map<std::string, cta::common::dataStructures::Tape> tapeListToMap(
    const std::list<cta::common::dataStructures::Tape> &listOfTapes) {
    using namespace cta;

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

      for (auto &&tape: listOfTapes) {
        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());
    }
  }
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

  /**
   * Creates a map from archive file ID to archive file from the specified iterator.
   *
   * @param itor Iterator over archive files.
   * @return Map from archive file ID to archive file.
   */
  std::map<uint64_t, cta::common::dataStructures::ArchiveFile> archiveFileItorToMap(cta::catalogue::ArchiveFileItor &itor) {
    using namespace cta;

    try {
      std::map<uint64_t, common::dataStructures::ArchiveFile> m;
      while(itor.hasMore()) {
        const common::dataStructures::ArchiveFile archiveFile = itor.next();
        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());
    }
  }
185
186
};

187
TEST_F(cta_catalogue_InMemoryCatalogueTest, createBootstrapAdminAndHostNoAuth) {
188
189
  using namespace cta;

190
191
  ASSERT_TRUE(m_catalogue->getAdminUsers().empty());
  ASSERT_TRUE(m_catalogue->getAdminHosts().empty());
192

193
  m_catalogue->createBootstrapAdminAndHostNoAuth(
194
    m_cliSI, m_bootstrapAdminSI.username, m_bootstrapAdminSI.host, m_bootstrapComment);
195

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

201
    const common::dataStructures::AdminUser admin = admins.front();
Steven Murray's avatar
Steven Murray committed
202
    ASSERT_EQ(m_bootstrapComment, admin.comment);
203

204
    const common::dataStructures::EntryLog creationLog = admin.creationLog;
205
    ASSERT_EQ(m_cliSI.username, creationLog.username);
Steven Murray's avatar
Steven Murray committed
206
    ASSERT_EQ(m_cliSI.host, creationLog.host);
207

208
    const common::dataStructures::EntryLog lastModificationLog =
209
210
      admin.lastModificationLog;
    ASSERT_EQ(creationLog, lastModificationLog);
211
  }
212
213
214

  {
    std::list<common::dataStructures::AdminHost> hosts;
215
    hosts = m_catalogue->getAdminHosts();
216
217
218
219
220
221
    ASSERT_EQ(1, hosts.size());

    const common::dataStructures::AdminHost host = hosts.front();
    ASSERT_EQ(m_bootstrapComment, host.comment);

    const common::dataStructures::EntryLog creationLog = host.creationLog;
222
    ASSERT_EQ(m_cliSI.username, creationLog.username);
223
224
225
226
227
228
    ASSERT_EQ(m_cliSI.host, creationLog.host);

    const common::dataStructures::EntryLog lastModificationLog =
      host.lastModificationLog;
    ASSERT_EQ(creationLog, lastModificationLog);
  }
229
230
}

231
TEST_F(cta_catalogue_InMemoryCatalogueTest, createAdminUser) {
232
233
  using namespace cta;

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

236
  m_catalogue->createBootstrapAdminAndHostNoAuth(
237
    m_cliSI, m_bootstrapAdminSI.username, m_bootstrapAdminSI.host, m_bootstrapComment);
238
239

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

    const common::dataStructures::AdminUser admin = admins.front();
Steven Murray's avatar
Steven Murray committed
245
    ASSERT_EQ(m_bootstrapComment, admin.comment);
246

247
    const common::dataStructures::EntryLog creationLog = admin.creationLog;
248
    ASSERT_EQ(m_cliSI.username, creationLog.username);
Steven Murray's avatar
Steven Murray committed
249
    ASSERT_EQ(m_cliSI.host, creationLog.host);
250
251

    const common::dataStructures::EntryLog lastModificationLog =
252
253
      admin.lastModificationLog;
    ASSERT_EQ(creationLog, lastModificationLog);
254
255
  }

256
  const std::string createAdminUserComment = "create admin user";
257
  m_catalogue->createAdminUser(m_bootstrapAdminSI, m_adminSI.username, createAdminUserComment);
258

259
  {
260
    std::list<common::dataStructures::AdminUser> admins;
261
    admins = m_catalogue->getAdminUsers();
262
263
264
265
266
267
268
    ASSERT_EQ(2, admins.size());

    const common::dataStructures::AdminUser a1 = admins.front();
    admins.pop_front();
    const common::dataStructures::AdminUser a2 = admins.front();

    ASSERT_NE(a1, a2);
269
270
    ASSERT_TRUE((a1.name == m_bootstrapAdminSI.username && a2.name == m_adminSI.username) ||
      (a2.name == m_bootstrapAdminSI.username && a1.name == m_adminSI.username));
271

272
273
    if(a1.name == m_bootstrapAdminSI.username) {
      ASSERT_EQ(m_bootstrapAdminSI.username, a1.name);
274
      ASSERT_EQ(m_bootstrapComment, a1.comment);
275
      ASSERT_EQ(m_cliSI.username, a1.creationLog.username);
276
      ASSERT_EQ(m_cliSI.host, a1.creationLog.host);
277
      ASSERT_EQ(m_cliSI.username, a1.lastModificationLog.username);
278
279
      ASSERT_EQ(m_cliSI.host, a1.lastModificationLog.host);

280
      ASSERT_EQ(m_adminSI.username, a2.name);
281
      ASSERT_EQ(createAdminUserComment, a2.comment);
282
      ASSERT_EQ(m_bootstrapAdminSI.username, a2.creationLog.username);
283
      ASSERT_EQ(m_bootstrapAdminSI.host, a2.creationLog.host);
284
      ASSERT_EQ(m_bootstrapAdminSI.username, a2.lastModificationLog.username);
285
      ASSERT_EQ(m_bootstrapAdminSI.host, a2.lastModificationLog.host);
Steven Murray's avatar
Steven Murray committed
286
    } else {
287
      ASSERT_EQ(m_bootstrapAdminSI.username, a2.name);
288
      ASSERT_EQ(m_bootstrapComment, a2.comment);
289
      ASSERT_EQ(m_cliSI.username, a2.creationLog.username);
290
      ASSERT_EQ(m_cliSI.host, a2.creationLog.host);
291
      ASSERT_EQ(m_cliSI.username, a2.lastModificationLog.username);
292
293
      ASSERT_EQ(m_cliSI.host, a2.lastModificationLog.host);

294
      ASSERT_EQ(m_adminSI.username, a1.name);
295
      ASSERT_EQ(createAdminUserComment, a1.comment);
296
      ASSERT_EQ(m_bootstrapAdminSI.username, a1.creationLog.username);
297
      ASSERT_EQ(m_bootstrapAdminSI.host, a1.creationLog.host);
298
      ASSERT_EQ(m_bootstrapAdminSI.username, a1.lastModificationLog.username);
299
      ASSERT_EQ(m_bootstrapAdminSI.host, a1.lastModificationLog.host);
Steven Murray's avatar
Steven Murray committed
300
    }
301
  }
302
303
}

304
TEST_F(cta_catalogue_InMemoryCatalogueTest, createAdminUser_same_twice) {
305
306
  using namespace cta;

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

309
  m_catalogue->createBootstrapAdminAndHostNoAuth(
310
    m_cliSI, m_bootstrapAdminSI.username, m_bootstrapAdminSI.host, m_bootstrapComment);
311
312
313

  {
    std::list<common::dataStructures::AdminUser> admins;
314
    admins = m_catalogue->getAdminUsers();
315
316
317
318
319
320
    ASSERT_EQ(1, admins.size());

    const common::dataStructures::AdminUser admin = admins.front();
    ASSERT_EQ(m_bootstrapComment, admin.comment);

    const common::dataStructures::EntryLog creationLog = admin.creationLog;
321
    ASSERT_EQ(m_cliSI.username, creationLog.username);
322
323
324
325
326
327
328
    ASSERT_EQ(m_cliSI.host, creationLog.host);

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

329
  m_catalogue->createAdminUser(m_bootstrapAdminSI, m_adminSI.username, "comment 1");
330

331
  ASSERT_THROW(m_catalogue->createAdminUser(m_bootstrapAdminSI, m_adminSI.username,
332
    "comment 2"), catalogue::UserError);
333
334
}

335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
TEST_F(cta_catalogue_InMemoryCatalogueTest, deleteAdminUser) {
  using namespace cta;

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

  m_catalogue->createBootstrapAdminAndHostNoAuth(
    m_cliSI, m_bootstrapAdminSI.username, m_bootstrapAdminSI.host, m_bootstrapComment);

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

    const common::dataStructures::AdminUser admin = admins.front();
    ASSERT_EQ(m_bootstrapComment, admin.comment);

    const common::dataStructures::EntryLog creationLog = admin.creationLog;
    ASSERT_EQ(m_cliSI.username, creationLog.username);
    ASSERT_EQ(m_cliSI.host, creationLog.host);

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

  m_catalogue->deleteAdminUser(m_bootstrapAdminSI.username);

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

TEST_F(cta_catalogue_InMemoryCatalogueTest, deleteAdminUser_non_existant) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getAdminUsers().empty());
  ASSERT_THROW(m_catalogue->deleteAdminUser("non_existant_sdmin_user"), catalogue::UserError);
}

372
TEST_F(cta_catalogue_InMemoryCatalogueTest, createAdminHost) {
373
374
  using namespace cta;

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

377
  m_catalogue->createBootstrapAdminAndHostNoAuth(
378
    m_cliSI, m_bootstrapAdminSI.username, m_bootstrapAdminSI.host, m_bootstrapComment);
379
380
381

  {
    std::list<common::dataStructures::AdminUser> admins;
382
    admins = m_catalogue->getAdminUsers();
383
384
385
386
387
388
    ASSERT_EQ(1, admins.size());

    const common::dataStructures::AdminUser admin = admins.front();
    ASSERT_EQ(m_bootstrapComment, admin.comment);

    const common::dataStructures::EntryLog creationLog = admin.creationLog;
389
    ASSERT_EQ(m_cliSI.username, creationLog.username);
390
391
392
393
394
395
396
    ASSERT_EQ(m_cliSI.host, creationLog.host);

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

397
  const std::string createAdminHostComment = "create host user";
398
  const std::string anotherAdminHost = "another_admin_host";
399
  m_catalogue->createAdminHost(m_bootstrapAdminSI,
400
    anotherAdminHost, createAdminHostComment);
401
402
403

  {
    std::list<common::dataStructures::AdminHost> hosts;
404
    hosts = m_catalogue->getAdminHosts();
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
    ASSERT_EQ(2, hosts.size());

    const common::dataStructures::AdminHost h1 = hosts.front();
    hosts.pop_front();
    const common::dataStructures::AdminHost h2 = hosts.front();

    ASSERT_NE(h1, h2);
    ASSERT_TRUE(
      (h1.name == m_bootstrapAdminSI.host && h2.name == anotherAdminHost)
      ||
      (h2.name == anotherAdminHost && h1.name == m_bootstrapAdminSI.host)
    );

    if(h1.name == m_bootstrapAdminSI.host) {
      ASSERT_EQ(m_bootstrapAdminSI.host, h1.name);
      ASSERT_EQ(m_bootstrapComment, h1.comment);
421
      ASSERT_EQ(m_cliSI.username, h1.creationLog.username);
422
      ASSERT_EQ(m_cliSI.host, h1.creationLog.host);
423
      ASSERT_EQ(m_cliSI.username, h1.lastModificationLog.username);
424
425
426
427
      ASSERT_EQ(m_cliSI.host, h1.lastModificationLog.host);

      ASSERT_EQ(anotherAdminHost, h2.name);
      ASSERT_EQ(createAdminHostComment, h2.comment);
428
      ASSERT_EQ(m_bootstrapAdminSI.username, h2.creationLog.username);
429
      ASSERT_EQ(m_bootstrapAdminSI.host, h2.creationLog.host);
430
      ASSERT_EQ(m_bootstrapAdminSI.username, h2.lastModificationLog.username);
431
432
433
434
      ASSERT_EQ(m_bootstrapAdminSI.host, h2.lastModificationLog.host);
    } else {
      ASSERT_EQ(m_bootstrapAdminSI.host, h2.name);
      ASSERT_EQ(m_bootstrapComment, h2.comment);
435
      ASSERT_EQ(m_cliSI.username, h2.creationLog.username);
436
      ASSERT_EQ(m_cliSI.host, h2.creationLog.host);
437
      ASSERT_EQ(m_cliSI.username, h2.lastModificationLog.username);
438
439
440
441
      ASSERT_EQ(m_cliSI.host, h2.lastModificationLog.host);

      ASSERT_EQ(anotherAdminHost, h1.name);
      ASSERT_EQ(createAdminHostComment, h1.comment);
442
      ASSERT_EQ(m_bootstrapAdminSI.username, h1.creationLog.username);
443
      ASSERT_EQ(m_bootstrapAdminSI.host, h1.creationLog.host);
444
      ASSERT_EQ(m_bootstrapAdminSI.username, h1.lastModificationLog.username);
445
446
447
448
449
      ASSERT_EQ(m_bootstrapAdminSI.host, h1.lastModificationLog.host);
    }
  }
}

450
TEST_F(cta_catalogue_InMemoryCatalogueTest, createAdminHost_same_twice) {
451
452
  using namespace cta;

453
  m_catalogue->createBootstrapAdminAndHostNoAuth(
454
    m_cliSI, m_bootstrapAdminSI.username, m_bootstrapAdminSI.host, m_bootstrapComment);
455
456
457

  {
    std::list<common::dataStructures::AdminUser> admins;
458
    admins = m_catalogue->getAdminUsers();
459
460
461
462
463
464
    ASSERT_EQ(1, admins.size());

    const common::dataStructures::AdminUser admin = admins.front();
    ASSERT_EQ(m_bootstrapComment, admin.comment);

    const common::dataStructures::EntryLog creationLog = admin.creationLog;
465
    ASSERT_EQ(m_cliSI.username, creationLog.username);
466
467
468
469
470
471
472
473
474
    ASSERT_EQ(m_cliSI.host, creationLog.host);

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

  const std::string anotherAdminHost = "another_admin_host";

475
  m_catalogue->createAdminHost(m_bootstrapAdminSI, anotherAdminHost, "comment 1");
476

477
  ASSERT_THROW(m_catalogue->createAdminHost(m_bootstrapAdminSI,
478
    anotherAdminHost, "comment 2"), catalogue::UserError);
479
480
}

481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
TEST_F(cta_catalogue_InMemoryCatalogueTest, deleteAdminHost) {
  using namespace cta;

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

  m_catalogue->createBootstrapAdminAndHostNoAuth(
    m_cliSI, m_bootstrapAdminSI.username, m_bootstrapAdminSI.host, m_bootstrapComment);

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

    const common::dataStructures::AdminUser admin = admins.front();
    ASSERT_EQ(m_bootstrapComment, admin.comment);

    const common::dataStructures::EntryLog creationLog = admin.creationLog;
    ASSERT_EQ(m_cliSI.username, creationLog.username);
    ASSERT_EQ(m_cliSI.host, creationLog.host);

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

  m_catalogue->deleteAdminHost(m_bootstrapAdminSI.host);

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

TEST_F(cta_catalogue_InMemoryCatalogueTest, deleteAdminHost_non_existant) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getAdminHosts().empty());
  ASSERT_THROW(m_catalogue->deleteAdminHost("non_exstant_admin_host"), catalogue::UserError);
}

518
TEST_F(cta_catalogue_InMemoryCatalogueTest, isAdmin_false) {
519
520
  using namespace cta;

521
  ASSERT_FALSE(m_catalogue->isAdmin(m_cliSI));
Steven Murray's avatar
Steven Murray committed
522
523
}

524
TEST_F(cta_catalogue_InMemoryCatalogueTest, isAdmin_true) {
Steven Murray's avatar
Steven Murray committed
525
526
  using namespace cta;

527
  m_catalogue->createBootstrapAdminAndHostNoAuth(
528
    m_cliSI, m_bootstrapAdminSI.username, m_bootstrapAdminSI.host, m_bootstrapComment);
529

530
  ASSERT_TRUE(m_catalogue->isAdmin(m_bootstrapAdminSI));
531
532
}

533
TEST_F(cta_catalogue_InMemoryCatalogueTest, createStorageClass) {
534
535
  using namespace cta;

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

538
539
540
  const std::string storageClassName = "storage_class";
  const uint64_t nbCopies = 2;
  const std::string comment = "create storage class";
541
  m_catalogue->createStorageClass(m_cliSI, storageClassName, nbCopies, comment);
542
543

  const std::list<common::dataStructures::StorageClass> storageClasses =
544
    m_catalogue->getStorageClasses();
545
546
547
548
549
550
551
552
553
554

  ASSERT_EQ(1, storageClasses.size());

  const common::dataStructures::StorageClass storageClass =
    storageClasses.front();
  ASSERT_EQ(storageClassName, storageClass.name);
  ASSERT_EQ(nbCopies, storageClass.nbCopies);
  ASSERT_EQ(comment, storageClass.comment);

  const common::dataStructures::EntryLog creationLog = storageClass.creationLog;
555
  ASSERT_EQ(m_cliSI.username, creationLog.username);
556
557
558
559
560
561
562
  ASSERT_EQ(m_cliSI.host, creationLog.host);

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

563
TEST_F(cta_catalogue_InMemoryCatalogueTest, createStorageClass_same_twice) {
564
565
566
567
568
  using namespace cta;

  const std::string storageClassName = "storage_class";
  const uint64_t nbCopies = 2;
  const std::string comment = "create storage class";
569
570
  m_catalogue->createStorageClass(m_cliSI, storageClassName, nbCopies, comment);
  ASSERT_THROW(m_catalogue->createStorageClass(m_cliSI,
571
    storageClassName, nbCopies, comment), catalogue::UserError);
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
602
603
604
605
606
607
608
609
610
611
612
613
TEST_F(cta_catalogue_InMemoryCatalogueTest, deleteStorageClass) {
  using namespace cta;

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

  const std::string storageClassName = "storage_class";
  const uint64_t nbCopies = 2;
  const std::string comment = "create storage class";
  m_catalogue->createStorageClass(m_cliSI, storageClassName, nbCopies, comment);

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

  ASSERT_EQ(1, storageClasses.size());

  const common::dataStructures::StorageClass storageClass =
    storageClasses.front();
  ASSERT_EQ(storageClassName, storageClass.name);
  ASSERT_EQ(nbCopies, storageClass.nbCopies);
  ASSERT_EQ(comment, storageClass.comment);

  const common::dataStructures::EntryLog creationLog = storageClass.creationLog;
  ASSERT_EQ(m_cliSI.username, creationLog.username);
  ASSERT_EQ(m_cliSI.host, creationLog.host);

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

  m_catalogue->deleteStorageClass(storageClass.name);
  ASSERT_TRUE(m_catalogue->getStorageClasses().empty());
}

TEST_F(cta_catalogue_InMemoryCatalogueTest, deleteStorageClass_non_existant) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getStorageClasses().empty());
  ASSERT_THROW(m_catalogue->deleteStorageClass("non_existant_storage_class"), catalogue::UserError);
}

614
TEST_F(cta_catalogue_InMemoryCatalogueTest, createTapePool) {
615
616
  using namespace cta;
      
617
  ASSERT_TRUE(m_catalogue->getTapePools().empty());
618
619
620
621
622
      
  const std::string tapePoolName = "tape_pool";
  const uint64_t nbPartialTapes = 2;
  const bool is_encrypted = true;
  const std::string comment = "create tape pool";
623
  m_catalogue->createTapePool(m_cliSI, tapePoolName, nbPartialTapes, is_encrypted,
624
    comment);
625
626
      
  const std::list<common::dataStructures::TapePool> pools =
627
    m_catalogue->getTapePools();
628
629
630
631
632
633
634
635
636
637
      
  ASSERT_EQ(1, pools.size());
      
  const common::dataStructures::TapePool pool = pools.front();
  ASSERT_EQ(tapePoolName, pool.name);
  ASSERT_EQ(nbPartialTapes, pool.nbPartialTapes);
  ASSERT_EQ(is_encrypted, pool.encryption);
  ASSERT_EQ(comment, pool.comment);

  const common::dataStructures::EntryLog creationLog = pool.creationLog;
638
  ASSERT_EQ(m_cliSI.username, creationLog.username);
639
640
641
642
643
644
645
  ASSERT_EQ(m_cliSI.host, creationLog.host);
  
  const common::dataStructures::EntryLog lastModificationLog =
    pool.lastModificationLog;
  ASSERT_EQ(creationLog, lastModificationLog);
}
  
646
TEST_F(cta_catalogue_InMemoryCatalogueTest, createTapePool_same_twice) {
647
648
649
650
651
652
  using namespace cta;
  
  const std::string tapePoolName = "tape_pool";
  const uint64_t nbPartialTapes = 2;
  const bool is_encrypted = true;
  const std::string comment = "create tape pool";
653
  m_catalogue->createTapePool(m_cliSI, tapePoolName, nbPartialTapes, is_encrypted,
654
    comment);
655
656
  ASSERT_THROW(m_catalogue->createTapePool(m_cliSI, tapePoolName, nbPartialTapes, is_encrypted, comment),
    catalogue::UserError);
657
658
}

659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
TEST_F(cta_catalogue_InMemoryCatalogueTest, deleteTapePool) {
  using namespace cta;

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

  const std::string tapePoolName = "tape_pool";
  const uint64_t nbPartialTapes = 2;
  const bool is_encrypted = true;
  const std::string comment = "create tape pool";
  m_catalogue->createTapePool(m_cliSI, tapePoolName, nbPartialTapes, is_encrypted,
    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);
  ASSERT_EQ(is_encrypted, pool.encryption);
  ASSERT_EQ(comment, pool.comment);

  const common::dataStructures::EntryLog creationLog = pool.creationLog;
  ASSERT_EQ(m_cliSI.username, creationLog.username);
  ASSERT_EQ(m_cliSI.host, creationLog.host);

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

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

TEST_F(cta_catalogue_InMemoryCatalogueTest, deleteTapePool_non_existant) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getTapePools().empty());
  ASSERT_THROW(m_catalogue->deleteTapePool("non_existant_tape_pool"), catalogue::UserError);
}

701
TEST_F(cta_catalogue_InMemoryCatalogueTest, createArchiveRoute) {
702
703
  using namespace cta;
      
704
  ASSERT_TRUE(m_catalogue->getArchiveRoutes().empty());
705

706
707
  const std::string storageClassName = "storage_class";
  const uint64_t nbCopies = 2;
708
  m_catalogue->createStorageClass(m_cliSI, storageClassName, nbCopies, "create storage class");
709
710
711
712
      
  const std::string tapePoolName = "tape_pool";
  const uint64_t nbPartialTapes = 2;
  const bool is_encrypted = true;
713
  m_catalogue->createTapePool(m_cliSI, tapePoolName, nbPartialTapes, is_encrypted, "create tape pool");
714
715
716

  const uint64_t copyNb = 1;
  const std::string comment = "create archive route";
717
  m_catalogue->createArchiveRoute(m_cliSI, storageClassName, copyNb, tapePoolName,
718
    comment);
719
720
      
  const std::list<common::dataStructures::ArchiveRoute> routes =
721
    m_catalogue->getArchiveRoutes();
722
723
724
725
726
727
728
729
730
731
      
  ASSERT_EQ(1, routes.size());
      
  const common::dataStructures::ArchiveRoute route = routes.front();
  ASSERT_EQ(storageClassName, route.storageClassName);
  ASSERT_EQ(copyNb, route.copyNb);
  ASSERT_EQ(tapePoolName, route.tapePoolName);
  ASSERT_EQ(comment, route.comment);

  const common::dataStructures::EntryLog creationLog = route.creationLog;
732
  ASSERT_EQ(m_cliSI.username, creationLog.username);
733
734
735
736
737
  ASSERT_EQ(m_cliSI.host, creationLog.host);
  
  const common::dataStructures::EntryLog lastModificationLog =
    route.lastModificationLog;
  ASSERT_EQ(creationLog, lastModificationLog);
738
739

  common::dataStructures::TapeCopyToPoolMap copyToPoolMap =
740
    m_catalogue->getTapeCopyToPoolMap(storageClassName);
741
742
743
744
  ASSERT_EQ(1, copyToPoolMap.size());
  std::pair<uint64_t, std::string> maplet = *(copyToPoolMap.begin());
  ASSERT_EQ(copyNb, maplet.first);
  ASSERT_EQ(tapePoolName, maplet.second);
745
746
}
  
747
TEST_F(cta_catalogue_InMemoryCatalogueTest, createArchiveRouteTapePool_same_twice) {
748
749
750
751
  using namespace cta;
  
  const std::string storageClassName = "storage_class";
  const uint64_t nbCopies = 2;
752
  m_catalogue->createStorageClass(m_cliSI, storageClassName, nbCopies, "create storage class");
753
754
755
756
      
  const std::string tapePoolName = "tape_pool";
  const uint64_t nbPartialTapes = 2;
  const bool is_encrypted = true;
757
  m_catalogue->createTapePool(m_cliSI, tapePoolName, nbPartialTapes, is_encrypted,
758
    "create tape pool");
759
760
761

  const uint64_t copyNb = 1;
  const std::string comment = "create archive route";
762
  m_catalogue->createArchiveRoute(m_cliSI, storageClassName, copyNb, tapePoolName,
763
    comment);
764
  ASSERT_THROW(m_catalogue->createArchiveRoute(m_cliSI,
765
766
767
768
    storageClassName, copyNb, tapePoolName, comment),
    exception::Exception);
}

769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
TEST_F(cta_catalogue_InMemoryCatalogueTest, deleteArchiveRoute) {
  using namespace cta;

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

  const std::string storageClassName = "storage_class";
  const uint64_t nbCopies = 2;
  m_catalogue->createStorageClass(m_cliSI, storageClassName, nbCopies, "create storage class");

  const std::string tapePoolName = "tape_pool";
  const uint64_t nbPartialTapes = 2;
  const bool is_encrypted = true;
  m_catalogue->createTapePool(m_cliSI, tapePoolName, nbPartialTapes, is_encrypted, "create tape pool");

  const uint64_t copyNb = 1;
  const std::string comment = "create archive route";
  m_catalogue->createArchiveRoute(m_cliSI, storageClassName, copyNb, tapePoolName,
    comment);

  const std::list<common::dataStructures::ArchiveRoute> routes =
    m_catalogue->getArchiveRoutes();

  ASSERT_EQ(1, routes.size());

  const common::dataStructures::ArchiveRoute route = routes.front();
  ASSERT_EQ(storageClassName, route.storageClassName);
  ASSERT_EQ(copyNb, route.copyNb);
  ASSERT_EQ(tapePoolName, route.tapePoolName);
  ASSERT_EQ(comment, route.comment);

  const common::dataStructures::EntryLog creationLog = route.creationLog;
  ASSERT_EQ(m_cliSI.username, creationLog.username);
  ASSERT_EQ(m_cliSI.host, creationLog.host);

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

  common::dataStructures::TapeCopyToPoolMap copyToPoolMap =
    m_catalogue->getTapeCopyToPoolMap(storageClassName);
  ASSERT_EQ(1, copyToPoolMap.size());
  std::pair<uint64_t, std::string> maplet = *(copyToPoolMap.begin());
  ASSERT_EQ(copyNb, maplet.first);
  ASSERT_EQ(tapePoolName, maplet.second);

  m_catalogue->deleteArchiveRoute(storageClassName, copyNb);

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

TEST_F(cta_catalogue_InMemoryCatalogueTest, deleteArchiveRoute_non_existant) {
  using namespace cta;

  ASSERT_TRUE(m_catalogue->getArchiveRoutes().empty());
  ASSERT_THROW(m_catalogue->deleteArchiveRoute("non_existant_storage_class", 1234), catalogue::UserError);
}

826
TEST_F(cta_catalogue_InMemoryCatalogueTest, createArchiveRoute_deleteStorageClass) {
827
  using namespace cta;
828

829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
  ASSERT_TRUE(m_catalogue->getArchiveRoutes().empty());

  const std::string storageClassName = "storage_class";
  const uint64_t nbCopies = 2;
  m_catalogue->createStorageClass(m_cliSI, storageClassName, nbCopies, "create storage class");

  const std::string tapePoolName = "tape_pool";
  const uint64_t nbPartialTapes = 2;
  const bool is_encrypted = true;
  m_catalogue->createTapePool(m_cliSI, tapePoolName, nbPartialTapes, is_encrypted, "create tape pool");

  const uint64_t copyNb = 1;
  const std::string comment = "create archive route";
  m_catalogue->createArchiveRoute(m_cliSI, storageClassName, copyNb, tapePoolName,
                               comment);

  const std::list<common::dataStructures::ArchiveRoute> routes =
    m_catalogue->getArchiveRoutes();

  ASSERT_EQ(1, routes.size());

  const common::dataStructures::ArchiveRoute route = routes.front();
  ASSERT_EQ(storageClassName, route.storageClassName);
  ASSERT_EQ(copyNb, route.copyNb);
  ASSERT_EQ(tapePoolName, route.tapePoolName);
  ASSERT_EQ(comment, route.comment);

  const common::dataStructures::EntryLog creationLog = route.creationLog;
857
  ASSERT_EQ(m_cliSI.username, creationLog.username);
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
  ASSERT_EQ(m_cliSI.host, creationLog.host);

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

  common::dataStructures::TapeCopyToPoolMap copyToPoolMap =
    m_catalogue->getTapeCopyToPoolMap(storageClassName);
  ASSERT_EQ(1, copyToPoolMap.size());
  std::pair<uint64_t, std::string> maplet = *(copyToPoolMap.begin());
  ASSERT_EQ(copyNb, maplet.first);
  ASSERT_EQ(tapePoolName, maplet.second);

  ASSERT_THROW(m_catalogue->deleteStorageClass(storageClassName), exception::Exception);
}

874
TEST_F(cta_catalogue_InMemoryCatalogueTest, createLogicalLibrary) {
875
876
877
  using namespace cta;
      
  ASSERT_TRUE(m_catalogue->getLogicalLibraries().empty());
878
879
880
      
  const std::string logicalLibraryName = "logical_library";
  const std::string comment = "create logical library";
881
  m_catalogue->createLogicalLibrary(m_cliSI, logicalLibraryName, comment);
882
883
      
  const std::list<common::dataStructures::LogicalLibrary> libs =
884
    m_catalogue->getLogicalLibraries();
885
886
887
888
889
890
891
892
      
  ASSERT_EQ(1, libs.size());
      
  const common::dataStructures::LogicalLibrary lib = libs.front();
  ASSERT_EQ(logicalLibraryName, lib.name);
  ASSERT_EQ(comment, lib.comment);

  const common::dataStructures::EntryLog creationLog = lib.creationLog;
893
  ASSERT_EQ(m_cliSI.username, creationLog.username);
894
895
896
897
898
899
900
  ASSERT_EQ(m_cliSI.host, creationLog.host);
  
  const common::dataStructures::EntryLog lastModificationLog =
    lib.lastModificationLog;
  ASSERT_EQ(creationLog, lastModificationLog);
}
  
901
TEST_F(cta_catalogue_InMemoryCatalogueTest, createLogicalLibrary_same_twice) {
902
903
904
905
  using namespace cta;
  
  const std::string logicalLibraryName = "logical_library";
  const std::string comment = "create logical library";
906
  m_catalogue->createLogicalLibrary(m_cliSI, logicalLibraryName, comment);
907
  ASSERT_THROW(m_catalogue->createLogicalLibrary(m_cliSI, logicalLibraryName, comment), catalogue::UserError);
908
909
}

910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
TEST_F(cta_catalogue_InMemoryCatalogueTest, deleteLogicalLibrary) {
  using namespace cta;
      
  ASSERT_TRUE(m_catalogue->getLogicalLibraries().empty());
      
  const std::string logicalLibraryName = "logical_library";
  const std::string comment = "create logical library";
  m_catalogue->createLogicalLibrary(m_cliSI, logicalLibraryName, comment);
      
  const std::list<common::dataStructures::LogicalLibrary> libs =
    m_catalogue->getLogicalLibraries();
      
  ASSERT_EQ(1, libs.size());
      
  const common::dataStructures::LogicalLibrary lib = libs.front();
  ASSERT_EQ(logicalLibraryName, lib.name);
  ASSERT_EQ(comment, lib.comment);

  const common::dataStructures::EntryLog creationLog = lib.creationLog;
  ASSERT_EQ(m_cliSI.username, creationLog.username);
  ASSERT_EQ(m_cliSI.host, creationLog.host);
  
  const common::dataStructures::EntryLog lastModificationLog =
    lib.lastModificationLog;
  ASSERT_EQ(creationLog, lastModificationLog);

  m_catalogue->deleteLogicalLibrary(logicalLibraryName);
  ASSERT_TRUE(m_catalogue->getLogicalLibraries().empty());
}

TEST_F(cta_catalogue_InMemoryCatalogueTest, deleteLogicalLibrary_non_existant) {
  using namespace cta;
      
  ASSERT_TRUE(m_catalogue->getLogicalLibraries().empty());
  ASSERT_THROW(m_catalogue->deleteLogicalLibrary("non_existant_logical_library"), catalogue::UserError);
}

947
TEST_F(cta_catalogue_InMemoryCatalogueTest, createTape) {
948
949
  using namespace cta;

950
  ASSERT_TRUE(m_catalogue->getTapes("", "", "", "", "", "", "", "").empty());
951
952
953
954
955
956
957
958
959
960

  const std::string vid = "vid";
  const std::string logicalLibraryName = "logical_library_name";
  const std::string tapePoolName = "tape_pool_name";
  const std::string encryptionKey = "encryption_key";
  const uint64_t capacityInBytes = (uint64_t)10 * 1000 * 1000 * 1000 * 1000;
  const bool disabledValue = true;
  const bool fullValue = false;
  const std::string comment = "create tape";

961
  m_catalogue->createLogicalLibrary(m_cliSI, logicalLibraryName,
962
    "create logical library");
963
964
  m_catalogue->createTapePool(m_cliSI, tapePoolName, 2, true, "create tape pool");
  m_catalogue->createTape(m_cliSI, vid, logicalLibraryName, tapePoolName,
965
966
967
968
    encryptionKey, capacityInBytes, disabledValue, fullValue,
    comment);

  const std::list<common::dataStructures::Tape> tapes =
969
    m_catalogue->getTapes("", "", "", "", "", "", "", "");
970
971
972
973
974
975
976
977
978
979
980
981
982
983

  ASSERT_EQ(1, tapes.size());

  const common::dataStructures::Tape tape = tapes.front();
  ASSERT_EQ(vid, tape.vid);
  ASSERT_EQ(logicalLibraryName, tape.logicalLibraryName);
  ASSERT_EQ(tapePoolName, tape.tapePoolName);
  ASSERT_EQ(encryptionKey, tape.encryptionKey);
  ASSERT_EQ(capacityInBytes, tape.capacityInBytes);
  ASSERT_TRUE(disabledValue == tape.disabled);
  ASSERT_TRUE(fullValue == tape.full);
  ASSERT_EQ(comment, tape.comment);

  const common::dataStructures::EntryLog creationLog = tape.creationLog;
984
  ASSERT_EQ(m_cliSI.username, creationLog.username);
985
986
987
988
989
990
991
  ASSERT_EQ(m_cliSI.host, creationLog.host);

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

992
TEST_F(cta_catalogue_InMemoryCatalogueTest, createTape_same_twice) {
993
994
995
996
997
998
999
1000
  using namespace cta;

  const std::string vid = "vid";
  const std::string logicalLibraryName = "logical_library_name";
  const std::string tapePoolName = "tape_pool_name";
  const std::string encryptionKey = "encryption_key";
  const uint64_t capacityInBytes = (uint64_t)10 * 1000 * 1000 * 1000 * 1000;
  const bool disabledValue = true;