1 /**
2 * Copyright (C) 2017 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "registration.hpp"
17 #include "targeting.hpp"
18
19 #include <stdlib.h>
20
21 #include <filesystem>
22 #include <fstream>
23
24 #include <gtest/gtest.h>
25
26 using namespace openpower::util;
27 using namespace openpower::targeting;
28
29 constexpr auto masterDir = "/tmp";
30
31 class TargetingTest : public ::testing::Test
32 {
33 protected:
SetUp()34 virtual void SetUp()
35 {
36 char dir[50];
37 strcpy(dir, masterDir);
38 strcat(dir, "/targetingXXXXXX");
39
40 auto path = mkdtemp(dir);
41 assert(path != nullptr);
42
43 _slaveBaseDir = path;
44
45 _slaveDir = _slaveBaseDir / "fsi1";
46 std::filesystem::create_directory(_slaveDir);
47 }
48
TearDown()49 virtual void TearDown()
50 {
51 std::filesystem::remove_all(_slaveDir);
52 std::filesystem::remove_all(_slaveBaseDir);
53 }
54
55 std::filesystem::path _slaveBaseDir;
56 std::filesystem::path _slaveDir;
57 };
58
TEST_F(TargetingTest,CreateTargets)59 TEST_F(TargetingTest, CreateTargets)
60 {
61 // Test that we always create the first Target
62 {
63 Targeting targets{masterDir, _slaveDir};
64 ASSERT_EQ(targets.size(), 1);
65
66 auto t = targets.begin();
67 ASSERT_EQ((*t)->getPos(), 0);
68
69 ASSERT_EQ((*t)->getCFAMPath(), masterDir);
70 }
71
72 // Test that we can create multiple Targets
73 {
74 // make some fake slave entries
75 std::ofstream(_slaveDir / "slave@01:00");
76 std::ofstream(_slaveDir / "slave@02:00");
77 std::ofstream(_slaveDir / "slave@03:00");
78 std::ofstream(_slaveDir / "slave@04:00");
79
80 Targeting targets{masterDir, _slaveDir};
81
82 ASSERT_EQ(targets.size(), 5);
83
84 int i = 0;
85
86 for (const auto& t : targets)
87 {
88 std::filesystem::path path;
89
90 ASSERT_EQ(t->getPos(), i);
91
92 if (0 == i)
93 {
94 path = masterDir;
95 }
96 else
97 {
98 std::ostringstream subdir;
99 subdir << "slave@0" << i << ":00/raw";
100
101 path = _slaveDir;
102 path /= subdir.str();
103 }
104
105 ASSERT_EQ(t->getCFAMPath(), path);
106 i++;
107 }
108 }
109 }
110
func1()111 void func1()
112 {
113 std::cout << "Hello\n";
114 }
115
func2()116 void func2()
117 {
118 std::cout << "World\n";
119 }
120
121 REGISTER_PROCEDURE("hello", func1)
122 REGISTER_PROCEDURE("world", func2)
123
TEST(RegistrationTest,TestReg)124 TEST(RegistrationTest, TestReg)
125 {
126 int count = 0;
127 for (const auto& p : Registration::getProcedures())
128 {
129 std::cout << p.first << std::endl;
130 p.second();
131 count++;
132 }
133
134 ASSERT_EQ(count, 2);
135 }
136