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:
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 
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 
59 TEST_F(TargetingTest, CreateTargets)
60 {
61 
62     // Test that we always create the first Target
63     {
64         Targeting targets{masterDir, _slaveDir};
65         ASSERT_EQ(targets.size(), 1);
66 
67         auto t = targets.begin();
68         ASSERT_EQ((*t)->getPos(), 0);
69 
70         ASSERT_EQ((*t)->getCFAMPath(), masterDir);
71     }
72 
73     // Test that we can create multiple Targets
74     {
75         // make some fake slave entries
76         std::ofstream(_slaveDir / "slave@01:00");
77         std::ofstream(_slaveDir / "slave@02:00");
78         std::ofstream(_slaveDir / "slave@03:00");
79         std::ofstream(_slaveDir / "slave@04:00");
80 
81         Targeting targets{masterDir, _slaveDir};
82 
83         ASSERT_EQ(targets.size(), 5);
84 
85         int i = 0;
86 
87         for (const auto& t : targets)
88         {
89             std::filesystem::path path;
90 
91             ASSERT_EQ(t->getPos(), i);
92 
93             if (0 == i)
94             {
95                 path = masterDir;
96             }
97             else
98             {
99                 std::ostringstream subdir;
100                 subdir << "slave@0" << i << ":00/raw";
101 
102                 path = _slaveDir;
103                 path /= subdir.str();
104             }
105 
106             ASSERT_EQ(t->getCFAMPath(), path);
107             i++;
108         }
109     }
110 }
111 
112 void func1()
113 {
114     std::cout << "Hello\n";
115 }
116 
117 void func2()
118 {
119     std::cout << "World\n";
120 }
121 
122 REGISTER_PROCEDURE("hello", func1)
123 REGISTER_PROCEDURE("world", func2)
124 
125 TEST(RegistrationTest, TestReg)
126 {
127     int count = 0;
128     for (const auto& p : Registration::getProcedures())
129     {
130         std::cout << p.first << std::endl;
131         p.second();
132         count++;
133     }
134 
135     ASSERT_EQ(count, 2);
136 }
137