xref: /openbmc/openpower-proc-control/test/utest.cpp (revision 63508a73e0eb47760ca2520517b16f6a9d177db2)
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 ProcedureMap Registration::procedures;
30 
31 constexpr auto masterDir = "/tmp";
32 
33 class TargetingTest : public ::testing::Test
34 {
35   protected:
36     virtual void SetUp()
37     {
38         char dir[50];
39         strcpy(dir, masterDir);
40         strcat(dir, "/targetingXXXXXX");
41 
42         auto path = mkdtemp(dir);
43         assert(path != nullptr);
44 
45         _slaveBaseDir = path;
46 
47         _slaveDir = _slaveBaseDir / "fsi1";
48         std::filesystem::create_directory(_slaveDir);
49     }
50 
51     virtual void TearDown()
52     {
53         std::filesystem::remove_all(_slaveDir);
54         std::filesystem::remove_all(_slaveBaseDir);
55     }
56 
57     std::filesystem::path _slaveBaseDir;
58     std::filesystem::path _slaveDir;
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     // Test that we can create multiple Targets
76     {
77         // make some fake slave entries
78         std::ofstream(_slaveDir / "slave@01:00");
79         std::ofstream(_slaveDir / "slave@02:00");
80         std::ofstream(_slaveDir / "slave@03:00");
81         std::ofstream(_slaveDir / "slave@04:00");
82 
83         Targeting targets{masterDir, _slaveDir};
84 
85         ASSERT_EQ(targets.size(), 5);
86 
87         int i = 0;
88 
89         for (const auto& t : targets)
90         {
91             std::filesystem::path path;
92 
93             ASSERT_EQ(t->getPos(), i);
94 
95             if (0 == i)
96             {
97                 path = masterDir;
98             }
99             else
100             {
101                 std::ostringstream subdir;
102                 subdir << "slave@0" << i << ":00/raw";
103 
104                 path = _slaveDir;
105                 path /= subdir.str();
106             }
107 
108             ASSERT_EQ(t->getCFAMPath(), path);
109             i++;
110         }
111     }
112 }
113 
114 void func1()
115 {
116     std::cout << "Hello\n";
117 }
118 
119 void func2()
120 {
121     std::cout << "World\n";
122 }
123 
124 REGISTER_PROCEDURE("hello", func1)
125 REGISTER_PROCEDURE("world", func2)
126 
127 TEST(RegistrationTest, TestReg)
128 {
129     int count = 0;
130     for (const auto& p : Registration::getProcedures())
131     {
132         std::cout << p.first << std::endl;
133         p.second();
134         count++;
135     }
136 
137     ASSERT_EQ(count, 2);
138 }
139