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