MigrationReportPackerTest.cpp 7.77 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
/******************************************************************************
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * 
 *
 * @author Castor Dev team, castor-dev@cern.ch
 *****************************************************************************/
23
24

#include "castor/log/StringLogger.hpp"
25
#include "castor/tape/tapeserver/daemon/MigrationReportPacker.hpp"
26
#include "castor/tape/tapeserver/drive/DriveInterface.hpp"
Steven Murray's avatar
Steven Murray committed
27
#include "catalogue/CatalogueFactory.hpp"
28
#include "scheduler/testingMocks/MockArchiveMount.hpp"
29

30
31
#include <gtest/gtest.h>

32
33
34
35
using ::testing::_;
using ::testing::Invoke;
using namespace castor::tape;

36
namespace unitTests {
37
  
38
  class castor_tape_tapeserver_daemon_MigrationReportPackerTest: public ::testing::Test {
39
40
41
  protected:

    void SetUp() {
42
      using namespace cta;
Steven Murray's avatar
Steven Murray committed
43
44
      using namespace cta::catalogue;

45
      rdbms::Login catalogueLogin(rdbms::Login::DBTYPE_IN_MEMORY, "", "", "");
46
      const uint64_t nbConns = 1;
47
      m_catalogue = CatalogueFactory::create(catalogueLogin, nbConns);
48
    }
49
50

    void TearDown() {
Steven Murray's avatar
Steven Murray committed
51
      m_catalogue.reset();
52
    }
53

Steven Murray's avatar
Steven Murray committed
54
55
    std::unique_ptr<cta::catalogue::Catalogue> m_catalogue;

56
  }; // class castor_tape_tapeserver_daemon_MigrationReportPackerTest
57
  
58
59
  class MockArchiveJobExternalStats: public cta::MockArchiveJob {
  public:
60
    MockArchiveJobExternalStats(cta::ArchiveMount & am, cta::catalogue::Catalogue & catalogue, 
61
       int & completes, int &failures):
62
    MockArchiveJob(am, catalogue), completesRef(completes), failuresRef(failures) {}
63
64
65
66
67
68
    
    virtual void complete() {
      completesRef++;
    }
    
    
69
    virtual void failed(const cta::exception::Exception& ex) {
70
71
72
73
74
75
76
77
      failuresRef++;
    }
    
  private:
    int & completesRef;
    int & failuresRef;
  };
  
78
  TEST_F(castor_tape_tapeserver_daemon_MigrationReportPackerTest, MigrationReportPackerNominal) {
Steven Murray's avatar
Steven Murray committed
79
    cta::MockArchiveMount tam(*m_catalogue);
80
81
82

    ::testing::InSequence dummy;
    std::unique_ptr<cta::ArchiveJob> job1;
83
    int job1completes(0), job1failures(0);
84
    {
85
      std::unique_ptr<cta::MockArchiveJob> mockJob(
Steven Murray's avatar
Steven Murray committed
86
        new MockArchiveJobExternalStats(tam, *m_catalogue, job1completes, job1failures));
87
      job1.reset(mockJob.release());
88
    }
89
    std::unique_ptr<cta::ArchiveJob> job2;
90
    int job2completes(0), job2failures(0);
91
    {
92
      std::unique_ptr<cta::MockArchiveJob> mockJob(
Steven Murray's avatar
Steven Murray committed
93
        new MockArchiveJobExternalStats(tam, *m_catalogue, job2completes, job2failures));
94
      job2.reset(mockJob.release());
95
    }
96
97
98
99
100
101

    castor::log::StringLogger log("castor_tape_tapeserver_daemon_MigrationReportPackerNominal");
    castor::log::LogContext lc(log);
    tapeserver::daemon::MigrationReportPacker mrp(&tam,lc);
    mrp.startThreads();

102
103
    mrp.reportCompletedJob(std::move(job1));
    mrp.reportCompletedJob(std::move(job2));
104
105
106
107

    const tapeserver::drive::compressionStats statsCompress;
    mrp.reportFlush(statsCompress);
    mrp.reportEndOfSession();
108
    mrp.reportTestGoingToEnd();
109
110
111
112
    mrp.waitThread(); //here

    std::string temp = log.getLog();
    ASSERT_NE(std::string::npos, temp.find("Reported to the client that a batch of files was written on tape"));
113
114
115
    ASSERT_EQ(1, tam.completes);
    ASSERT_EQ(1, job1completes);
    ASSERT_EQ(1, job2completes);
116
117
  }

118
  TEST_F(castor_tape_tapeserver_daemon_MigrationReportPackerTest, MigrationReportPackerFailure) {
Steven Murray's avatar
Steven Murray committed
119
    cta::MockArchiveMount tam(*m_catalogue);
120
121
122
123

    ::testing::InSequence dummy;
    std::unique_ptr<cta::ArchiveJob> job1;
    {
Steven Murray's avatar
Steven Murray committed
124
      std::unique_ptr<cta::MockArchiveJob> mockJob(new cta::MockArchiveJob(tam, *m_catalogue));
125
      job1.reset(mockJob.release());
126
    }
127
128
    std::unique_ptr<cta::ArchiveJob> job2;
    {
Steven Murray's avatar
Steven Murray committed
129
      std::unique_ptr<cta::MockArchiveJob> mockJob(new cta::MockArchiveJob(tam, *m_catalogue));
130
      job2.reset(mockJob.release());
131
    }
132
    std::unique_ptr<cta::ArchiveJob> job3;
133
    int job3completes(0), job3failures(0);
134
    {
135
      std::unique_ptr<cta::MockArchiveJob> mockJob(
Steven Murray's avatar
Steven Murray committed
136
        new MockArchiveJobExternalStats(tam, *m_catalogue, job3completes, job3failures));
137
138
      job3.reset(mockJob.release());
    }
139
    
140
141
142
143
    castor::log::StringLogger log("castor_tape_tapeserver_daemon_MigrationReportPackerFailure");
    castor::log::LogContext lc(log);  
    tapeserver::daemon::MigrationReportPacker mrp(&tam,lc);
    mrp.startThreads();
144

145
146
    mrp.reportCompletedJob(std::move(job1));
    mrp.reportCompletedJob(std::move(job2));
147
148
149
150
151
152
153
154

    const std::string error_msg = "ERROR_TEST_MSG";
    const castor::exception::Exception ex(error_msg);
    mrp.reportFailedJob(std::move(job3),ex);

    const tapeserver::drive::compressionStats statsCompress;
    mrp.reportFlush(statsCompress);
    mrp.reportEndOfSession();
155
    mrp.reportTestGoingToEnd();
156
157
158
159
    mrp.waitThread();

    std::string temp = log.getLog();
    ASSERT_NE(std::string::npos, temp.find(error_msg));
160
161
    ASSERT_EQ(1, tam.completes);
    ASSERT_EQ(1, job3failures);
162
163
  }

164
  TEST_F(castor_tape_tapeserver_daemon_MigrationReportPackerTest, MigrationReportPackerOneByteFile) {
Steven Murray's avatar
Steven Murray committed
165
    cta::MockArchiveMount tam(*m_catalogue);
166
167
168

    ::testing::InSequence dummy;
    std::unique_ptr<cta::ArchiveJob> migratedBigFile;
169
    int migratedBigFileCompletes(0), migratedBigFileFailures(0);
170
    {
171
      std::unique_ptr<cta::MockArchiveJob> mockJob(
Steven Murray's avatar
Steven Murray committed
172
        new MockArchiveJobExternalStats(tam, *m_catalogue, migratedBigFileCompletes, migratedBigFileFailures));
173
174
175
      migratedBigFile.reset(mockJob.release());
    }
    std::unique_ptr<cta::ArchiveJob> migratedFileSmall;
176
    int migratedFileSmallCompletes(0), migratedFileSmallFailures(0);
177
    {
178
      std::unique_ptr<cta::MockArchiveJob> mockJob(
Steven Murray's avatar
Steven Murray committed
179
        new MockArchiveJobExternalStats(tam, *m_catalogue, migratedFileSmallCompletes, migratedFileSmallFailures));
180
181
182
      migratedFileSmall.reset(mockJob.release());
    }
    std::unique_ptr<cta::ArchiveJob> migratedNullFile;
183
    int migratedNullFileCompletes(0), migratedNullFileFailures(0);
184
    {
185
      std::unique_ptr<cta::MockArchiveJob> mockJob(
Steven Murray's avatar
Steven Murray committed
186
        new MockArchiveJobExternalStats(tam, *m_catalogue, migratedNullFileCompletes, migratedNullFileFailures));
187
188
189
      migratedNullFile.reset(mockJob.release());
    }

190
191
192
    migratedBigFile->archiveFile.fileSize=100000;
    migratedFileSmall->archiveFile.fileSize=1;
    migratedNullFile->archiveFile.fileSize=0;
193
194
195
196
197
198
    
    castor::log::StringLogger log("castor_tape_tapeserver_daemon_MigrationReportPackerOneByteFile");
    castor::log::LogContext lc(log);  
    tapeserver::daemon::MigrationReportPacker mrp(&tam,lc);
    mrp.startThreads();

199
200
201
    mrp.reportCompletedJob(std::move(migratedBigFile));
    mrp.reportCompletedJob(std::move(migratedFileSmall));
    mrp.reportCompletedJob(std::move(migratedNullFile));
202
203
204
205
    tapeserver::drive::compressionStats stats;
    stats.toTape=(100000+1)/3;
    mrp.reportFlush(stats);
    mrp.reportEndOfSession();
206
    mrp.reportTestGoingToEnd();
207
208
209
210
    mrp.waitThread();

    std::string temp = log.getLog();
    ASSERT_NE(std::string::npos, temp.find("Reported to the client that a batch of files was written on tape"));
211
212
213
214
    ASSERT_EQ(1, tam.completes);
    ASSERT_EQ(1, migratedBigFileCompletes);
    ASSERT_EQ(1, migratedFileSmallCompletes);
    ASSERT_EQ(1, migratedNullFileCompletes);
215
  } 
216
}