RecallReportPackerTest.cpp 5.33 KB
Newer Older
1
2
/*
 * @project        The CERN Tape Archive (CTA)
3
 * @copyright      Copyright(C) 2003-2021 CERN
4
5
6
7
 * @license        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.
David COME's avatar
David COME committed
8
 *
9
10
11
12
 *                 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.
David COME's avatar
David COME committed
13
 *
14
15
16
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17

David COME's avatar
David COME committed
18
#include "castor/tape/tapeserver/daemon/RecallReportPacker.hpp"
Victor Kotlyar's avatar
Victor Kotlyar committed
19
#include "common/log/StringLogger.hpp"
20
#include "common/exception/Exception.hpp"
21
22
#include "scheduler/OStoreDB/OStoreDBFactory.hpp"
#include "objectstore/BackendVFS.hpp"
23
24
#include "scheduler/testingMocks/MockRetrieveMount.hpp"
#include "scheduler/testingMocks/MockRetrieveJob.hpp"
25

26
#include <gmock/gmock.h>
David COME's avatar
David COME committed
27
28
#include <gtest/gtest.h>

29
using ::testing::_;
30
using ::testing::Invoke;
31
namespace unitTests{
32

33
class castor_tape_tapeserver_daemon_RecallReportPackerTest: public ::testing::Test {
34
35
36
37
38
39
40
41
protected:

  void SetUp() {
  }

  void TearDown() {
  }

42
}; // class castor_tape_tapeserver_daemon_RecallReportPackerTest
43

44
  class MockRetrieveJobExternalStats: public cta::MockRetrieveJob {
45
  public:
46
47
    MockRetrieveJobExternalStats(cta::RetrieveMount & rm, int & completes, int &failures):
    MockRetrieveJob(rm), completesRef(completes), failuresRef(failures) {}
48

49
    void asyncSetSuccessful() override {
50
      completesRef++;
51
    }
52

53
    void transferFailed(const std::string &failureReason, cta::log::LogContext&) override {
54
      failuresRef++;
55
    }
56
    
57
  private:
58
59
60
    int & completesRef;
    int & failuresRef;
  };
61

62
TEST_F(castor_tape_tapeserver_daemon_RecallReportPackerTest, RecallReportPackerNominal) {
63
64
  auto catalogue = cta::catalogue::DummyCatalogue();
  cta::MockRetrieveMount retrieveMount(catalogue);
65

66
67

  
68
  ::testing::InSequence dummy;
69
  std::unique_ptr<cta::RetrieveJob> job1;
70
  int job1completes(0), job1failures(0);
71
  {
72
73
    std::unique_ptr<cta::MockRetrieveJob> mockJob(
      new MockRetrieveJobExternalStats(retrieveMount, job1completes, job1failures));
74
75
    job1.reset(mockJob.release());
  }
76
  int job2completes(0), job2failures(0);
77
78
  std::unique_ptr<cta::RetrieveJob> job2;
  {
79
80
    std::unique_ptr<cta::MockRetrieveJob> mockJob(
      new MockRetrieveJobExternalStats(retrieveMount, job2completes, job2failures));
81
82
    job2.reset(mockJob.release());
  }
83

84
  cta::log::StringLogger log("dummy","castor_tape_tapeserver_RecallReportPackerNominal",cta::log::DEBUG);
Victor Kotlyar's avatar
Victor Kotlyar committed
85
  cta::log::LogContext lc(log);
86
  castor::tape::tapeserver::daemon::RecallReportPacker rrp(&retrieveMount,lc);
David COME's avatar
David COME committed
87
  rrp.startThreads();
88

89
90
  rrp.reportCompletedJob(std::move(job1));
  rrp.reportCompletedJob(std::move(job2));
91
  
92
  rrp.reportDriveStatus(cta::common::dataStructures::DriveStatus::Unmounting);
93
  
David COME's avatar
David COME committed
94
  rrp.reportEndOfSession();
95
//  rrp.reportTestGoingToEnd();
David COME's avatar
David COME committed
96
97
98
99
  rrp.waitThread();
  
  std::string temp = log.getLog();
  ASSERT_NE(std::string::npos, temp.find("Nominal RecallReportPacker::EndofSession has been reported"));
100
101
102
  ASSERT_EQ(1,job1completes);
  ASSERT_EQ(1,job2completes);
  ASSERT_EQ(1,retrieveMount.completes);
David COME's avatar
David COME committed
103
104
}

105
TEST_F(castor_tape_tapeserver_daemon_RecallReportPackerTest, RecallReportPackerBadBadEnd) {
106
107
  auto catalogue = cta::catalogue::DummyCatalogue();
  cta::MockRetrieveMount retrieveMount(catalogue);
108
109

  ::testing::InSequence dummy;
110
  std::unique_ptr<cta::RetrieveJob> job1;
111
  int job1completes(0), job1failures(0);
112
  {
113
114
    std::unique_ptr<cta::MockRetrieveJob> mockJob(
      new MockRetrieveJobExternalStats(retrieveMount, job1completes, job1failures));
115
116
    job1.reset(mockJob.release());
  }
117
  int job2completes(0), job2failures(0);
118
119
  std::unique_ptr<cta::RetrieveJob> job2;
  {
120
121
    std::unique_ptr<cta::MockRetrieveJob> mockJob(
      new MockRetrieveJobExternalStats(retrieveMount, job2completes, job2failures));
122
123
    job2.reset(mockJob.release());
  }
124
  int job3completes(0), job3failures(0);
125
126
  std::unique_ptr<cta::RetrieveJob> job3;
  {
127
128
    std::unique_ptr<cta::MockRetrieveJob> mockJob(
      new MockRetrieveJobExternalStats(retrieveMount, job3completes, job3failures));
129
130
    job3.reset(mockJob.release());
  }
131

132
  cta::log::StringLogger log("dummy","castor_tape_tapeserver_RecallReportPackerBadBadEnd",cta::log::DEBUG);
Victor Kotlyar's avatar
Victor Kotlyar committed
133
  cta::log::LogContext lc(log);
David COME's avatar
David COME committed
134

135
  castor::tape::tapeserver::daemon::RecallReportPacker rrp(&retrieveMount,lc);
David COME's avatar
David COME committed
136
137
  rrp.startThreads();

138
139
  rrp.reportCompletedJob(std::move(job1));
  rrp.reportCompletedJob(std::move(job2));
140
141

  const std::string error_msg = "ERROR_TEST_MSG";
142
  const cta::exception::Exception ex(error_msg);
143
  rrp.reportFailedJob(std::move(job3), ex);
144
  
145
  rrp.reportDriveStatus(cta::common::dataStructures::DriveStatus::Unmounting);
146
147

  rrp.reportEndOfSession();
148
//  rrp.reportTestGoingToEnd();
David COME's avatar
David COME committed
149
150
  rrp.waitThread();

151
  const std::string temp = log.getLog();
David COME's avatar
David COME committed
152
  ASSERT_NE(std::string::npos, temp.find(error_msg));
153
154
155
156
  ASSERT_EQ(1, job1completes);
  ASSERT_EQ(1, job2completes);
  ASSERT_EQ(1, job3failures);
  ASSERT_EQ(1, retrieveMount.completes);
David COME's avatar
David COME committed
157
158
159
}

}