1 /**
2  * Copyright © 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 <gtest/gtest.h>
17 #include <experimental/filesystem>
18 #include <fstream>
19 #include <stdlib.h>
20 #include "registration.hpp"
21 #include "targeting.hpp"
22 
23 using namespace openpower::util;
24 using namespace openpower::targeting;
25 namespace fs = std::experimental::filesystem;
26 
27 ProcedureMap Registration::procedures;
28 
29 constexpr auto masterDir = "/tmp";
30 
31 class TargetingTest : public ::testing::Test
32 {
33     protected:
34 
35         virtual void SetUp()
36         {
37             char dir[50];
38             strcpy(dir, masterDir);
39             strcat(dir, "/targetingXXXXXX");
40 
41             auto path = mkdtemp(dir);
42             assert(path != nullptr);
43 
44             _slaveBaseDir = path;
45 
46             _slaveDir = _slaveBaseDir / "hub@00";
47             fs::create_directory(_slaveDir);
48         }
49 
50         virtual void TearDown()
51         {
52             fs::remove_all(_slaveDir);
53             fs::remove_all(_slaveBaseDir);
54         }
55 
56         fs::path _slaveBaseDir;
57         fs::path _slaveDir;
58 };
59 
60 
61 TEST_F(TargetingTest, CreateTargets)
62 {
63 
64     //Test that we always create the first Target
65     {
66         Targeting targets{masterDir, _slaveDir};
67         ASSERT_EQ(targets.size(), 1);
68 
69         auto t = targets.begin();
70         ASSERT_EQ((*t)->getPos(), 0);
71 
72         ASSERT_EQ((*t)->getCFAMPath(), masterDir);
73     }
74 
75 
76     //Test that we can create multiple Targets
77     {
78         //make some fake slave entries
79         std::ofstream(_slaveDir / "slave@01:00");
80         std::ofstream(_slaveDir / "slave@02:00");
81         std::ofstream(_slaveDir / "slave@03:00");
82         std::ofstream(_slaveDir / "slave@04:00");
83 
84         Targeting targets{masterDir, _slaveDir};
85 
86         ASSERT_EQ(targets.size(), 5);
87 
88         int i = 0;
89 
90         for (const auto& t : targets)
91         {
92             fs::path path;
93 
94             ASSERT_EQ(t->getPos(), i);
95 
96             if (0 == i)
97             {
98                 path = masterDir;
99             }
100             else
101             {
102                 std::ostringstream subdir;
103                 subdir << "slave@0" << i << ":00/raw";
104 
105                 path = _slaveDir;
106                 path /= subdir.str();
107             }
108 
109             ASSERT_EQ(t->getCFAMPath(), path);
110             i++;
111         }
112     }
113 }
114 
115 
116 void func1()
117 {
118     std::cout << "Hello\n";
119 }
120 
121 void func2()
122 {
123     std::cout << "World\n";
124 }
125 
126 REGISTER_PROCEDURE("hello", func1);
127 REGISTER_PROCEDURE("world", func2);
128 
129 
130 TEST(RegistrationTest, TestReg)
131 {
132     int count = 0;
133     for (const auto& p : Registration::getProcedures())
134     {
135         std::cout << p.first << std::endl;
136         p.second();
137         count++;
138     }
139 
140     ASSERT_EQ(count, 2);
141 }
142