ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
testWorkspace.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_test
4  * @copyright (c) Copyright ESO 2022
5  * All Rights Reserved
6  * ESO (eso.org) is an Intergovernmental Organisation, and therefore special legal conditions apply.
7  *
8  * @brief daq::dpm::Workspace integration tests
9  */
10 #include <fstream>
11 #include <thread>
12 
13 #include <gtest/gtest.h>
14 
15 #include <daq/dpm/workspace.hpp>
16 #include <daq/error/report.hpp>
17 
18 namespace fs = std::filesystem;
19 
20 class WorkspaceTest : public ::testing::Test {
21 public:
23  }
24  void SetUp() override {
25  std::string tpl("daqLibdpmTest-workspace-XXXXXX");
26  mkdtemp(tpl.data());
27  m_root = fs::current_path() / tpl;
28 
29  ASSERT_FALSE(m_root.empty());
30 
31  m_spec = R"(
32  {
33  "id": "TEST.ID",
34  "target": {
35  "fileId": "TEST.FILEID",
36  "source": {
37  "sourceName": "dcs",
38  "origin": "dcs-host:/path/to/somefile.fits",
39  "path": "dcs/somefile.fits"
40  }
41  },
42  "sources": [
43  ]
44  }
45  )"_json;
46  }
47 
48  void TearDown() override {
49  if (!m_root.empty()) {
50  fs::remove_all(m_root);
51  }
52  }
53 
54 protected:
55  fs::path m_root;
57 };
58 
59 class DaqWorkspaceTest : public WorkspaceTest {};
60 
61 TEST_F(WorkspaceTest, GetPath) {
62  daq::dpm::WorkspaceImpl ws(m_root);
63  EXPECT_EQ(m_root, ws.GetPath());
64 }
65 
66 TEST_F(WorkspaceTest, ConstructionInitializeWorkspaceWithExistingRoot) {
67  ASSERT_FALSE(fs::exists(m_root / "in-progress"));
68  ASSERT_FALSE(fs::exists(m_root / "archive"));
69  daq::dpm::WorkspaceImpl workspace(m_root);
70 
71  ASSERT_TRUE(fs::exists(m_root / "in-progress"));
72  ASSERT_TRUE(fs::exists(m_root / "archive"));
73 }
74 
75 TEST_F(WorkspaceTest, ConstructionInitializeWorkspaceWithNonexistingRoot) {
76  fs::remove(m_root);
77  ASSERT_FALSE(fs::exists(m_root / "in-progress"));
78  ASSERT_FALSE(fs::exists(m_root / "archive"));
79  daq::dpm::WorkspaceImpl workspace(m_root);
80 
81  ASSERT_TRUE(fs::exists(m_root / "in-progress"));
82  ASSERT_TRUE(fs::exists(m_root / "archive"));
83 }
84 
85 TEST_F(WorkspaceTest, ConstructionFailsIfRootIsAfile) {
86  fs::remove(m_root);
87  std::ofstream(m_root).put('z');
88  EXPECT_THROW(daq::dpm::WorkspaceImpl{m_root}, std::invalid_argument);
89 }
90 
91 TEST_F(WorkspaceTest, InitializeDaq) {
92  try {
93  daq::dpm::WorkspaceImpl ws(m_root);
94  auto daq_ws = ws.InitializeDaq("DAQ");
95  auto daq_ws_root = m_root / "in-progress" / "DAQ";
96  EXPECT_EQ(daq_ws->GetPath(), daq_ws_root);
97  EXPECT_TRUE(fs::exists(daq_ws_root)) << "Path: " << daq_ws_root;
98  } catch (...) {
99  daq::error::NestedExceptionReporter r(std::current_exception());
100  throw std::runtime_error(r.Str());
101  }
102 }
103 
104 TEST_F(WorkspaceTest, ArchiveDaq) {
105  try {
106  daq::dpm::WorkspaceImpl ws(m_root);
107  auto daq_ws = ws.InitializeDaq("DAQ");
108  daq_ws->StoreStatus({});
109  ws.ArchiveDaq("DAQ");
110  auto daq_ws_root = m_root / "archive" / "DAQ";
111  EXPECT_TRUE(fs::exists(daq_ws_root)) << "Path: " << daq_ws_root;
112  EXPECT_TRUE(fs::exists(daq_ws_root / "status.json"));
113  EXPECT_FALSE(fs::exists(m_root / "in-progress" / "DAQ"));
114  } catch (...) {
115  daq::error::NestedExceptionReporter r(std::current_exception());
116  throw std::runtime_error(r.Str());
117  }
118 }
119 
120 TEST_F(WorkspaceTest, RemoveDaq) {
121  try {
122  daq::dpm::WorkspaceImpl ws(m_root);
123  auto daq_ws = ws.InitializeDaq("DAQ");
124  ws.RemoveDaq("DAQ");
125  EXPECT_FALSE(fs::exists(m_root / "in-progress" / "DAQ"));
126  EXPECT_FALSE(fs::exists(m_root / "archive" / "DAQ"));
127  } catch (...) {
128  daq::error::NestedExceptionReporter r(std::current_exception());
129  throw std::runtime_error(r.Str());
130  }
131 }
132 
133 TEST_F(DaqWorkspaceTest, LoadStoreSpecification) {
134  try {
135  daq::dpm::DaqWorkspaceImpl ws(m_root, m_root);
136  EXPECT_FALSE(fs::exists(m_root / "specification.json"));
137  ws.StoreSpecification(m_spec.dump());
138  EXPECT_TRUE(fs::exists(m_root / "specification.json"));
139 
140  auto spec = ws.LoadSpecification();
141  EXPECT_EQ(spec.id, "TEST.ID");
142  } catch (...) {
143  daq::error::NestedExceptionReporter r(std::current_exception());
144  throw std::runtime_error(r.Str());
145  }
146 }
147 
148 TEST_F(DaqWorkspaceTest, LoadStoreStatus) {
149  try {
150  daq::dpm::DaqWorkspaceImpl ws(m_root, m_root);
151  EXPECT_FALSE(fs::exists(m_root / "status.json"));
152  daq::Status store_status = {};
153  store_status.id = "DAQ";
154  store_status.state = daq::State::Merging;
155  ws.StoreStatus(store_status);
156  EXPECT_TRUE(fs::exists(m_root / "status.json"));
157  EXPECT_EQ(store_status, ws.LoadStatus());
158  } catch (...) {
159  daq::error::NestedExceptionReporter r(std::current_exception());
160  throw std::runtime_error(r.Str());
161  }
162 }
163 
164 TEST_F(DaqWorkspaceTest, LoadStoreLookup) {
165  try {
166  daq::dpm::DaqWorkspaceImpl ws(m_root, m_root);
167  EXPECT_FALSE(fs::exists(m_root / "sources.json"));
169  ws.StoreSourceLookup(store);
170  EXPECT_TRUE(fs::exists(m_root / "sources.json"));
171  EXPECT_EQ(store, ws.LoadSourceLookup());
172  } catch (...) {
173  daq::error::NestedExceptionReporter r(std::current_exception());
174  throw std::runtime_error(r.Str());
175  }
176 }
DaqWorkspaceTest
Definition: testWorkspace.cpp:59
daq::dpm::DaqWorkspaceImpl::LoadSourceLookup
auto LoadSourceLookup() const -> SourceResolver::Mapping override
Definition: workspace.cpp:251
daq::dpm::DaqWorkspaceImpl
Definition: workspace.hpp:218
WorkspaceTest::m_root
fs::path m_root
Definition: testWorkspace.cpp:55
WorkspaceTest::WorkspaceTest
WorkspaceTest()
Definition: testWorkspace.cpp:22
daq::error::NestedExceptionReporter::Str
std::string Str() const
Convenience function for constructing a std::string from the exception.
Definition: report.cpp:97
report.hpp
daq::dpm::SourceResolver::Mapping
std::map< SourceFile, std::string > Mapping
Definition: sourceResolver.hpp:48
daq::dpm::WorkspaceImpl::RemoveDaq
void RemoveDaq(std::string const &daq_id) override
Removes workspace and all containing files for DAQ without archiving it.
Definition: workspace.cpp:163
daq::error::NestedExceptionReporter
Adapter object intended to be used in contexts without direct access to the output-stream object.
Definition: report.hpp:54
daq::State::Merging
@ Merging
DAQ is being merged.
workspace.hpp
daq::dpm::Workspace interface and implementation declaration
daq::dpm::DaqWorkspaceImpl::StoreSpecification
void StoreSpecification(std::string const &specification) const override
Get file name of the data product specification stored in StoreSpecification()
Definition: workspace.cpp:287
daq::dpm::WorkspaceImpl::InitializeDaq
auto InitializeDaq(std::string const &daq_id) -> std::unique_ptr< DaqWorkspace > override
Initializes new DAQ Workspace.
Definition: workspace.cpp:153
WorkspaceTest::TearDown
void TearDown() override
Definition: testWorkspace.cpp:48
daq::Status::id
std::string id
Definition: status.hpp:136
WorkspaceTest::SetUp
void SetUp() override
Definition: testWorkspace.cpp:24
daq::dpm::WorkspaceImpl::ArchiveDaq
auto ArchiveDaq(std::string const &daq_id) -> std::filesystem::path override
Archives specified DAQ witout deleting any files, typically by moving it to a specific location in th...
Definition: workspace.cpp:167
daq::dpm::DaqWorkspaceImpl::StoreStatus
void StoreStatus(Status const &status) const override
Definition: workspace.cpp:232
daq::dpm::WorkspaceImpl::GetPath
auto GetPath() const -> std::filesystem::path override
Definition: workspace.hpp:194
daq::dpm::DaqWorkspaceImpl::LoadSpecification
auto LoadSpecification() const -> DpSpec override
Get file name of the data product specification stored in StoreSpecification()
Definition: workspace.cpp:275
daq::Status
Non observable status object that keeps stores status of data acquisition.
Definition: status.hpp:120
daq::Status::state
State state
Definition: status.hpp:138
WorkspaceTest::m_spec
nlohmann::json m_spec
Definition: testWorkspace.cpp:56
daq::dpm::DaqWorkspaceImpl::StoreSourceLookup
void StoreSourceLookup(SourceResolver::Mapping const &status) const override
Definition: workspace.cpp:261
daq::dpm::WorkspaceImpl
Implementation of daq::dpm::Workspace.
Definition: workspace.hpp:175
WorkspaceTest
Definition: testWorkspace.cpp:20
daq::dpm::DaqWorkspaceImpl::LoadStatus
auto LoadStatus() const -> Status override
Definition: workspace.cpp:222
TEST_F
TEST_F(WorkspaceTest, GetPath)
Definition: testWorkspace.cpp:61
daq::json
nlohmann::json json
Definition: json.cpp:20