RootEntryTest.cpp 21.4 KB
Newer Older
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
20
#include <gtest/gtest.h>
#include "BackendVFS.hpp"
21
#include "common/exception/Exception.hpp"
22
#include "objectstore/SerializersExceptions.hpp"
23
#include "RootEntry.hpp"
24
#include "Agent.hpp"
25
26
#include "AgentRegister.hpp"
#include "TapePool.hpp"
27

28
29
namespace unitTests {

30
TEST(ObjectStore, RootEntryBasicAccess) {
31
  cta::objectstore::BackendVFS be;
32
33
34
35
36
37
38
39
40
41
42
43
  { 
    // Try to create the root entry
    cta::objectstore::RootEntry re(be);
    re.initialize();
    re.insert();
  }
  {
    // Try to read back and dump the root entry
    cta::objectstore::RootEntry re(be);
    ASSERT_THROW(re.fetch(), cta::exception::Exception);
    cta::objectstore::ScopedSharedLock lock(re);
    ASSERT_NO_THROW(re.fetch());
44
    re.dump();
45
46
47
48
49
50
51
52
53
54
  }
  {
    // Try to allocate the agent register
    // Try to read back and dump the root entry
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock (re);
    // Create an agent
    cta::objectstore::Agent agent(be);
    agent.generateName("unitTest");
    re.fetch();
55
56
    cta::objectstore::CreationLog cl(cta::UserIdentity(99, 99),
      "unittesthost", time(NULL), "Creation of unit test agent register");
57
58
    re.addOrGetAgentRegisterPointerAndCommit(agent, cl);
    ASSERT_NO_THROW(re.getAgentRegisterAddress());
59
60
61
    re.commit();
    //agent.registerSelf();
  }
62
  // Delete the root entry
63
64
  cta::objectstore::RootEntry re(be);
  cta::objectstore::ScopedExclusiveLock lock(re);
Eric Cano's avatar
Eric Cano committed
65
66
67
  re.fetch();
  re.removeAgentRegisterAndCommit();
  re.removeIfEmpty();
Steven Murray's avatar
Steven Murray committed
68
  ASSERT_FALSE(re.exists());
69
70
}

71
TEST(ObjectStore, RootEntryAdminHosts) {
72
73
74
75
76
77
78
79
80
81
82
83
  cta::objectstore::BackendVFS be;
  { 
    // Try to create the root entry
    cta::objectstore::RootEntry re(be);
    re.initialize();
    re.insert();
  }
  {
    // Add 2 admin hosts to the root entry
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    ASSERT_NO_THROW(re.fetch());
84
85
    cta::objectstore::CreationLog cl(cta::UserIdentity(99, 99),
      "unittesthost", time(NULL), "Creation of unit test agent register");
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    re.addAdminHost("adminHost1", cl);
    re.addAdminHost("adminHost2", cl);
    re.commit();
  }
  {
    // Check that the admin hosts made it
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedSharedLock lock(re);
    re.fetch();
    ASSERT_TRUE(re.isAdminHost("adminHost1"));
    ASSERT_TRUE(re.isAdminHost("adminHost2"));
    ASSERT_FALSE(re.isAdminHost("adminHost3"));
  }
  {
    // Check that we can remove existing and non-existing hosts
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    re.fetch();
    re.removeAdminHost("adminHost1");
105
    ASSERT_THROW(re.removeAdminHost("noSuch"), cta::objectstore::RootEntry::NoSuchAdminHost);
106
107
    re.commit();
  }
108
109
110
111
112
  {
    // Check that we cannot re-add an existing host
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    re.fetch();
113
114
    cta::objectstore::CreationLog cl(cta::UserIdentity(99, 99),
      "unittesthost", time(NULL), "Creation of unit test agent register");
115
116
117
    ASSERT_THROW(re.addAdminHost("adminHost2", cl),
       cta::objectstore::RootEntry::DuplicateEntry);
  }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  {
    // Check that we got the expected result
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedSharedLock lock(re);
    re.fetch();
    ASSERT_FALSE(re.isAdminHost("adminHost1"));
    ASSERT_TRUE(re.isAdminHost("adminHost2"));
    ASSERT_FALSE(re.isAdminHost("adminHost3"));
    ASSERT_EQ(1, re.dumpAdminHosts().size());
    ASSERT_EQ("adminHost2", re.dumpAdminHosts().front().hostname);
  }
  // Delete the root entry
  cta::objectstore::RootEntry re(be);
  cta::objectstore::ScopedExclusiveLock lock(re);
Eric Cano's avatar
Eric Cano committed
132
133
  re.fetch();
  re.removeIfEmpty();
Steven Murray's avatar
Steven Murray committed
134
  ASSERT_FALSE(re.exists());
135
136
}
  
137
TEST(ObjectStore, RootEntryAdminUsers) {
138
139
140
141
142
143
144
  cta::objectstore::BackendVFS be;
  { 
    // Try to create the root entry
    cta::objectstore::RootEntry re(be);
    re.initialize();
    re.insert();
  }
145
146
147
148
149
  cta::objectstore::UserIdentity user1(123, 456);
  cta::objectstore::UserIdentity user1prime(123, 789);
  cta::objectstore::UserIdentity user2(234, 345);
  cta::objectstore::UserIdentity user2prime(234, 56);
  cta::objectstore::UserIdentity user3(345, 345);
150
151
152
153
154
  {
    // Add 2 admin users to the root entry
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    ASSERT_NO_THROW(re.fetch());
155
156
    cta::objectstore::CreationLog cl(cta::UserIdentity(99, 99),
      "unittesthost", time(NULL), "Creation of unit test agent register");
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    re.addAdminUser(user1, cl);
    re.addAdminUser(user2, cl);
    re.commit();
  }
  {
    // Check that the admin hosts made it and that uid is the only meaningful
    // criteria
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedSharedLock lock(re);
    re.fetch();
    ASSERT_TRUE(re.isAdminUser(user1));
    ASSERT_TRUE(re.isAdminUser(user1prime));
    ASSERT_TRUE(re.isAdminUser(user2));
    ASSERT_TRUE(re.isAdminUser(user2prime));
    ASSERT_FALSE(re.isAdminUser(user3));

  }
  {
    // Check that we can remove existing and non-existing hosts
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    re.fetch();
    re.removeAdminUser(user1prime);
180
    ASSERT_THROW(re.removeAdminUser(user3), cta::objectstore::RootEntry::NoSuchAdminUser);
181
182
    re.commit();
  }
183
184
185
186
187
  {
    // Check that we cannot re-add an existing user
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    re.fetch();
188
189
    cta::objectstore::CreationLog cl(cta::UserIdentity(99, 99),
      "unittesthost", time(NULL), "Creation of unit test agent register");
190
191
192
    ASSERT_THROW(re.addAdminUser(user2, cl),
       cta::objectstore::RootEntry::DuplicateEntry);
  }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
  {
    // Check that we got the expected result
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedSharedLock lock(re);
    re.fetch();
    ASSERT_FALSE(re.isAdminUser(user1));
    ASSERT_FALSE(re.isAdminUser(user1prime));
    ASSERT_TRUE(re.isAdminUser(user2));
    ASSERT_TRUE(re.isAdminUser(user2prime));
    ASSERT_FALSE(re.isAdminUser(user3));
    ASSERT_EQ(1, re.dumpAdminUsers().size());
    ASSERT_EQ(user2.uid, re.dumpAdminUsers().front().user.uid);
  }
  
  // Delete the root entry
  cta::objectstore::RootEntry re(be);
  cta::objectstore::ScopedExclusiveLock lock(re);
Eric Cano's avatar
Eric Cano committed
210
211
  re.fetch();
  re.removeIfEmpty();
Steven Murray's avatar
Steven Murray committed
212
  ASSERT_FALSE(re.exists());
213
214
}

215
TEST(ObjectStore, RootEntryStorageClassesAndArchiveRoutes) {
216
217
218
219
220
221
222
  cta::objectstore::BackendVFS be;
  { 
    // Try to create the root entry
    cta::objectstore::RootEntry re(be);
    re.initialize();
    re.insert();
  }
223
224
    cta::objectstore::CreationLog cl(cta::UserIdentity(99, 99),
      "unittesthost", time(NULL), "Creation of unit test agent register");
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  {
    // Add 2 storage classes to the root entry
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    ASSERT_NO_THROW(re.fetch());
    re.addStorageClass("class1", 1, cl);
    re.addStorageClass("class2", 2, cl);
    re.addStorageClass("class3", 3, cl);
    // Check that we cannot add a storage class with 0 copies
    ASSERT_THROW(re.addStorageClass("class0", 0, cl),
      cta::objectstore::RootEntry::InvalidCopyNumber);
    re.commit();
  }
  {
    // Check that the storage classes made it, and that non existing storage
    // classes show up
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedSharedLock lock(re);
    re.fetch();
    ASSERT_NO_THROW(re.getStorageClassCopyCount("class1"));
    ASSERT_NO_THROW(re.getStorageClassCopyCount("class2"));
    ASSERT_NO_THROW(re.getStorageClassCopyCount("class3"));
    ASSERT_THROW(re.getStorageClassCopyCount("class4"),
      cta::objectstore::serializers::NotFound);
  }
  {
    // Check that we can remove storage class and non-existing ones
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    re.fetch();
    ASSERT_NO_THROW(re.removeStorageClass("class2"));
256
    ASSERT_THROW(re.removeStorageClass("class4"), cta::objectstore::RootEntry::NoSuchStorageClass);
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
    re.commit();
  }
  {
    // Check that we cannot re-add an existing storage class
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    re.fetch();
    ASSERT_THROW(re.addStorageClass("class1", 1, cl),
       cta::objectstore::RootEntry::DuplicateEntry);
  }
  {
    // Check that we got the expected result
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedSharedLock lock(re);
    re.fetch();
    ASSERT_NO_THROW(re.getStorageClassCopyCount("class1"));
    ASSERT_THROW(re.getStorageClassCopyCount("class2"),
      cta::objectstore::serializers::NotFound);
    ASSERT_NO_THROW(re.getStorageClassCopyCount("class3"));
    ASSERT_THROW(re.getStorageClassCopyCount("class4"),
      cta::objectstore::serializers::NotFound);
    ASSERT_EQ(2, re.dumpStorageClasses().size());
    ASSERT_EQ("class1", re.dumpStorageClasses().front().storageClass);
    ASSERT_EQ("class3", re.dumpStorageClasses().back().storageClass);
  }
  {
    // Check that we can add storage routes
    // We store an incomplete route for class3
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    re.fetch();
288
289
    ASSERT_NO_THROW(re.addArchiveRoute("class1", 1, "pool1_0", cl));
    ASSERT_NO_THROW(re.addArchiveRoute("class3", 3, "pool3_2", cl));
290
291
292
293
294
295
296
297
298
299
    re.commit();
  }
  {
    // Check that we can get routes, but not if the routing is incomplete for
    // the storage class
    // Check that we can add storage routes
    // We store an incomplete route for class3
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedSharedLock lock(re);
    re.fetch();
300
301
    ASSERT_NO_THROW(re.getArchiveRoutes("class1"));
    ASSERT_THROW(re.getArchiveRoutes("class3"),
302
303
304
      cta::objectstore::RootEntry::IncompleteEntry);
  }
  {
305
    //Check that we can complete a storage class, and cannot overwite the routes
306
307
308
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    re.fetch();
309
310
311
    ASSERT_NO_THROW(re.addArchiveRoute("class3", 1, "pool3_0", cl));
    ASSERT_NO_THROW(re.addArchiveRoute("class3", 2, "pool3_1", cl));
    ASSERT_THROW(re.addArchiveRoute("class3", 3, "pool3_2b", cl), cta::objectstore::RootEntry::ArchiveRouteAlreadyExists);
312
313
    re.commit();
    re.fetch();
314
    ASSERT_NO_THROW(re.getArchiveRoutes("class3"));
315
316
317
318
319
320
321
322
323
324
325
  }
  {
    // Check that resizing storage classes down and up work as expected
    //Check that we can complete a storage class, and overwrite the routes
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    re.fetch();
    ASSERT_NO_THROW(re.setStorageClassCopyCount("class1", 2, cl));
    ASSERT_NO_THROW(re.setStorageClassCopyCount("class3", 2, cl));
    re.commit();
    re.fetch();
326
    ASSERT_THROW(re.getArchiveRoutes("class1"),
327
      cta::objectstore::RootEntry::IncompleteEntry);
328
329
    ASSERT_NO_THROW(re.getArchiveRoutes("class3"));
    ASSERT_EQ(2,re.getArchiveRoutes("class3").size());
330
  }
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  {
    // Check that dump works in all circumstances
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedSharedLock lock(re);
    re.fetch();
    ASSERT_NO_THROW(
    {
      re.dumpAdminUsers();
      re.dumpAdminHosts();
      re.dumpStorageClasses();
      re.dumpLibraries();
      //re.dumpTapePool();
    });
  }
Eric Cano's avatar
Eric Cano committed
345
346
347
348
349
  {
    // remove the remaining storage classes
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    re.fetch();
350
351
352
    re.removeArchiveRoute("class1", 1);
    re.removeArchiveRoute("class3", 1);
    re.removeArchiveRoute("class3", 2);
Eric Cano's avatar
Eric Cano committed
353
354
355
356
    re.removeStorageClass("class1");
    re.removeStorageClass("class3");
    re.commit();
  }
357
358
359
  // Delete the root entry
  cta::objectstore::RootEntry re(be);
  cta::objectstore::ScopedExclusiveLock lock(re);
Eric Cano's avatar
Eric Cano committed
360
361
  re.fetch();
  re.removeIfEmpty();
Steven Murray's avatar
Steven Murray committed
362
  ASSERT_FALSE(re.exists());
363
}
364

365
TEST(ObjectStore, RootEntryibraries) {
366
  cta::objectstore::BackendVFS be;
367
368
    cta::objectstore::CreationLog cl(cta::UserIdentity(99, 99),
      "unittesthost", time(NULL), "Creation of unit test agent register");
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
  { 
    // Try to create the root entry
    cta::objectstore::RootEntry re(be);
    re.initialize();
    re.insert();
  }
  {
    // Add 2 libraries to the root entry
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    ASSERT_NO_THROW(re.fetch());
    re.addLibrary("library1", cl);
    re.addLibrary("library2", cl);
    re.commit();
  }
  {
    // Check that the admin hosts made it
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedSharedLock lock(re);
    re.fetch();
    ASSERT_TRUE(re.libraryExists("library1"));
    ASSERT_TRUE(re.libraryExists("library2"));
    ASSERT_FALSE(re.libraryExists("library3"));
  }
  {
    // Check that we can remove existing and non-existing hosts
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    re.fetch();
    re.removeLibrary("library1");
399
    ASSERT_THROW(re.removeAdminHost("library3"), cta::objectstore::RootEntry::NoSuchAdminHost);
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
    re.commit();
  }
  {
    // Check that we cannot re-add an existing host
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    re.fetch();
    ASSERT_THROW(re.addLibrary("library2", cl),
       cta::objectstore::RootEntry::DuplicateEntry);
  }
  {
    // Check that we got the expected result
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedSharedLock lock(re);
    re.fetch();
    ASSERT_FALSE(re.libraryExists("library1"));
    ASSERT_TRUE(re.libraryExists("library2"));
    ASSERT_FALSE(re.libraryExists("library3"));
    ASSERT_EQ(1, re.dumpLibraries().size());
419
    ASSERT_EQ("library2", re.dumpLibraries().front().library);
420
421
422
423
  }
  // Delete the root entry
  cta::objectstore::RootEntry re(be);
  cta::objectstore::ScopedExclusiveLock lock(re);
Eric Cano's avatar
Eric Cano committed
424
425
  re.fetch();
  re.removeIfEmpty();
Steven Murray's avatar
Steven Murray committed
426
  ASSERT_FALSE(re.exists());
427
}
428

429
TEST (ObjectStore, RootEntryTapePools) {
Eric Cano's avatar
Eric Cano committed
430
  cta::objectstore::BackendVFS be;
431
432
    cta::objectstore::CreationLog cl(cta::UserIdentity(99, 99),
      "unittesthost", time(NULL), "Creation of unit test agent register");
Eric Cano's avatar
Eric Cano committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
  cta::objectstore::Agent ag(be);
  ag.initialize();
  ag.generateName("UnitTests");
  { 
    // Try to create the root entry and allocate the agent register
    cta::objectstore::RootEntry re(be);
    re.initialize();
    re.insert();
    cta::objectstore::ScopedExclusiveLock rel(re);
    re.addOrGetAgentRegisterPointerAndCommit(ag, cl);
  }
  ag.insertAndRegisterSelf();
  std::string tpAddr1, tpAddr2;
  {
    // Create the tape pools
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    re.fetch();
    ASSERT_THROW(re.getTapePoolAddress("tapePool1"),
      cta::objectstore::RootEntry::NotAllocated);
453
    tpAddr1 = re.addOrGetTapePoolAndCommit("tapePool1", 100, 5, 5, ag, cl);
Eric Cano's avatar
Eric Cano committed
454
455
456
457
458
459
460
461
462
463
    // Check that we car read it
    cta::objectstore::TapePool tp(tpAddr1, be);
    cta::objectstore::ScopedSharedLock tpl(tp);
    ASSERT_NO_THROW(tp.fetch());
  }
  {
    // Create another pool
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    re.fetch();
464
    tpAddr2 = re.addOrGetTapePoolAndCommit("tapePool2", 100, 5, 5,ag, cl);
Eric Cano's avatar
Eric Cano committed
465
466
467
468
469
470
471
472
473
474
475
    ASSERT_TRUE(be.exists(tpAddr2));
  }
  {
    // Remove the other pool
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    re.fetch();
    re.removeTapePoolAndCommit("tapePool2");
    ASSERT_FALSE(be.exists(tpAddr2));
  }
  // Unregister the agent
476
  cta::objectstore::ScopedExclusiveLock agl(ag);
Eric Cano's avatar
Eric Cano committed
477
478
479
480
481
482
483
484
485
  ag.removeAndUnregisterSelf();
  // Delete the root entry
  cta::objectstore::RootEntry re(be);
  cta::objectstore::ScopedExclusiveLock lock(re);
  re.fetch();
  re.removeAgentRegisterAndCommit();
  re.removeTapePoolAndCommit("tapePool1");
  ASSERT_FALSE(be.exists(tpAddr1));
  re.removeIfEmpty();
Steven Murray's avatar
Steven Murray committed
486
  ASSERT_FALSE(re.exists());
Eric Cano's avatar
Eric Cano committed
487
}
488

489
TEST (ObjectStore, RootEntryDriveRegister) {
Eric Cano's avatar
Eric Cano committed
490
491
492
493
494
495
496
  cta::objectstore::BackendVFS be;
  { 
    // Try to create the root entry
    cta::objectstore::RootEntry re(be);
    re.initialize();
    re.insert();
  }
497
498
    cta::objectstore::CreationLog cl(cta::UserIdentity(99, 99),
      "unittesthost", time(NULL), "Creation of unit test agent register");
Eric Cano's avatar
Eric Cano committed
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
534
535
536
537
538
539
  cta::objectstore::Agent ag(be);
  ag.initialize();
  ag.generateName("UnitTests");
  { 
    // Try to create the root entry and allocate the agent register
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock rel(re);
    re.fetch();
    re.addOrGetAgentRegisterPointerAndCommit(ag, cl);
  }
  ag.insertAndRegisterSelf();
  std::string driveRegisterAddress;
  {
    // create the drive register
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock rel(re);
    re.fetch();
    ASSERT_THROW(re.getDriveRegisterAddress(),
      cta::objectstore::RootEntry::NotAllocated);
    ASSERT_NO_THROW(
      driveRegisterAddress = re.addOrGetDriveRegisterPointerAndCommit(ag, cl));
    ASSERT_TRUE(be.exists(driveRegisterAddress));
  }
  {
    // delete the drive register
    // create the drive register
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock rel(re);
    re.fetch();
    re.removeDriveRegisterAndCommit();
    ASSERT_FALSE(be.exists(driveRegisterAddress));
  }
  // Unregister the agent
  cta::objectstore::ScopedExclusiveLock agl(ag);
  ag.removeAndUnregisterSelf();
  // Delete the root entry
  cta::objectstore::RootEntry re(be);
  cta::objectstore::ScopedExclusiveLock lock(re);
  re.fetch();
  re.removeAgentRegisterAndCommit();
  re.removeIfEmpty();
Steven Murray's avatar
Steven Murray committed
540
  ASSERT_FALSE(re.exists());
Eric Cano's avatar
Eric Cano committed
541
}
542

543
TEST(ObjectStore, RootEntryAgentRegister) {
544
545
546
547
548
549
550
  cta::objectstore::BackendVFS be;
  { 
    // Try to create the root entry
    cta::objectstore::RootEntry re(be);
    re.initialize();
    re.insert();
  }
551
552
    cta::objectstore::CreationLog cl(cta::UserIdentity(99, 99),
      "unittesthost", time(NULL), "Creation of unit test agent register");
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
  cta::objectstore::Agent ag(be);
  ag.generateName("UnitTests");
  std::string arAddr;
  {
    // Create the agent register
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    re.fetch();
    ASSERT_THROW(re.getAgentRegisterAddress(),
      cta::objectstore::RootEntry::NotAllocated);
    arAddr = re.addOrGetAgentRegisterPointerAndCommit(ag, cl);
    // Check that we car read it
    cta::objectstore::AgentRegister ar(arAddr, be);
    cta::objectstore::ScopedSharedLock arl(ar);
    ASSERT_NO_THROW(ar.fetch());
  }
  {
Eric Cano's avatar
Eric Cano committed
570
    // Delete the agent register
571
572
573
574
575
576
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock lock(re);
    re.fetch();
    // Check that we still get the same agent register
    ASSERT_EQ(arAddr, re.getAgentRegisterAddress());
    ASSERT_EQ(arAddr, re.addOrGetAgentRegisterPointerAndCommit(ag, cl));
Eric Cano's avatar
Eric Cano committed
577
578
579
    // Remove it
    ASSERT_NO_THROW(re.removeAgentRegisterAndCommit());
    // Check that the object is gone
Steven Murray's avatar
Steven Murray committed
580
    ASSERT_FALSE(be.exists(arAddr));
581
582
583
584
  }
  // Delete the root entry
  cta::objectstore::RootEntry re(be);
  cta::objectstore::ScopedExclusiveLock lock(re);
Eric Cano's avatar
Eric Cano committed
585
586
  re.fetch();
  re.removeIfEmpty();
Steven Murray's avatar
Steven Murray committed
587
  ASSERT_FALSE(re.exists());
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
614
615
616
617
618
TEST (ObjectStore, RootEntrySchedulerGlobalLock) {
  cta::objectstore::BackendVFS be;
  { 
    // Try to create the root entry
    cta::objectstore::RootEntry re(be);
    re.initialize();
    re.insert();
  }
    cta::objectstore::CreationLog cl(cta::UserIdentity(99, 99),
      "unittesthost", time(NULL), "Creation of unit test scheduler global lock");
  cta::objectstore::Agent ag(be);
  ag.initialize();
  ag.generateName("UnitTests");
  { 
    // Try to create the root entry and allocate the agent register
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock rel(re);
    re.fetch();
    re.addOrGetAgentRegisterPointerAndCommit(ag, cl);
  }
  ag.insertAndRegisterSelf();
  std::string schedulerGlobalLockAddress;
  {
    // create the drive register
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock rel(re);
    re.fetch();
    ASSERT_THROW(re.getDriveRegisterAddress(),
      cta::objectstore::RootEntry::NotAllocated);
619
    ASSERT_NO_THROW(
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
      schedulerGlobalLockAddress = re.addOrGetSchedulerGlobalLockAndCommit(ag, cl));
    ASSERT_TRUE(be.exists(schedulerGlobalLockAddress));
  }
  {
    // delete the drive register
    // create the drive register
    cta::objectstore::RootEntry re(be);
    cta::objectstore::ScopedExclusiveLock rel(re);
    re.fetch();
    re.removeSchedulerGlobalLockAndCommit();
    ASSERT_FALSE(be.exists(schedulerGlobalLockAddress));
  }
  // Unregister the agent
  cta::objectstore::ScopedExclusiveLock agl(ag);
  ag.removeAndUnregisterSelf();
  // Delete the root entry
  cta::objectstore::RootEntry re(be);
  cta::objectstore::ScopedExclusiveLock lock(re);
  re.fetch();
  re.removeAgentRegisterAndCommit();
  re.removeIfEmpty();
Steven Murray's avatar
Steven Murray committed
641
  ASSERT_FALSE(re.exists());
642
643
}

644
}