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 <experimental/filesystem>
22 #include <fstream>
23 
24 #include <gtest/gtest.h>
25 
26 using namespace openpower::util;
27 using namespace openpower::targeting;
28 namespace fs = std::experimental::filesystem;
29 
30 ProcedureMap Registration::procedures;
31 
32 constexpr auto masterDir = "/tmp";
33 
34 class TargetingTest : public ::testing::Test
35 {
36   protected:
37     virtual void SetUp()
38     {
39         char dir[50];
40         strcpy(dir, masterDir);
41         strcat(dir, "/targetingXXXXXX");
42 
43         auto path = mkdtemp(dir);
44         assert(path != nullptr);
45 
46         _slaveBaseDir = path;
47 
48         _slaveDir = _slaveBaseDir / "fsi1";
49         fs::create_directory(_slaveDir);
50     }
51 
52     virtual void TearDown()
53     {
54         fs::remove_all(_slaveDir);
55         fs::remove_all(_slaveBaseDir);
56     }
57 
58     fs::path _slaveBaseDir;
59     fs::path _slaveDir;
60 };
61 
62 TEST_F(TargetingTest, CreateTargets)
63 {
64 
65     // Test that we always create the first Target
66     {
67         Targeting targets{masterDir, _slaveDir};
68         ASSERT_EQ(targets.size(), 1);
69 
70         auto t = targets.begin();
71         ASSERT_EQ((*t)->getPos(), 0);
72 
73         ASSERT_EQ((*t)->getCFAMPath(), masterDir);
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 void func1()
116 {
117     std::cout << "Hello\n";
118 }
119 
120 void func2()
121 {
122     std::cout << "World\n";
123 }
124 
125 REGISTER_PROCEDURE("hello", func1);
126 REGISTER_PROCEDURE("world", func2);
127 
128 TEST(RegistrationTest, TestReg)
129 {
130     int count = 0;
131     for (const auto& p : Registration::getProcedures())
132     {
133         std::cout << p.first << std::endl;
134         p.second();
135         count++;
136     }
137 
138     ASSERT_EQ(count, 2);
139 }
140