blob: 63033358178cb7f2b80e761dac7369382951b170 [file] [log] [blame]
Matt Spinler597e05c2017-02-28 09:59:53 -06001/**
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>
Matt Spinlerd9bdcf72017-03-09 15:06:23 -060020#include "registration.hpp"
Matt Spinler597e05c2017-02-28 09:59:53 -060021#include "targeting.hpp"
22
Matt Spinlerd9bdcf72017-03-09 15:06:23 -060023using namespace openpower::util;
Matt Spinler597e05c2017-02-28 09:59:53 -060024using namespace openpower::targeting;
25namespace fs = std::experimental::filesystem;
26
Matt Spinlerb6542342017-03-28 16:37:36 -050027ProcedureMap Registration::procedures;
28
Matt Spinler597e05c2017-02-28 09:59:53 -060029constexpr auto masterDir = "/tmp";
30
31class 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
Matt Spinlerb6542342017-03-28 16:37:36 -050044 _slaveBaseDir = path;
45
46 _slaveDir = _slaveBaseDir / "hub@00";
47 fs::create_directory(_slaveDir);
Matt Spinler597e05c2017-02-28 09:59:53 -060048 }
49
50 virtual void TearDown()
51 {
Matt Spinlerb6542342017-03-28 16:37:36 -050052 fs::remove_all(_slaveDir);
53 fs::remove_all(_slaveBaseDir);
Matt Spinler597e05c2017-02-28 09:59:53 -060054 }
55
Matt Spinlerb6542342017-03-28 16:37:36 -050056 fs::path _slaveBaseDir;
57 fs::path _slaveDir;
Matt Spinler597e05c2017-02-28 09:59:53 -060058};
59
60
61TEST_F(TargetingTest, CreateTargets)
62{
63
64 //Test that we always create the first Target
65 {
Matt Spinlerb6542342017-03-28 16:37:36 -050066 Targeting targets{masterDir, _slaveDir};
Matt Spinler597e05c2017-02-28 09:59:53 -060067 ASSERT_EQ(targets.size(), 1);
68
69 auto t = targets.begin();
70 ASSERT_EQ((*t)->getPos(), 0);
71
Matt Spinlerc3bffed2017-03-10 09:05:30 -060072 ASSERT_EQ((*t)->getCFAMPath(), masterDir);
Matt Spinler597e05c2017-02-28 09:59:53 -060073 }
74
75
76 //Test that we can create multiple Targets
77 {
78 //make some fake slave entries
Matt Spinlerb6542342017-03-28 16:37:36 -050079 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");
Matt Spinler597e05c2017-02-28 09:59:53 -060083
Matt Spinlerb6542342017-03-28 16:37:36 -050084 Targeting targets{masterDir, _slaveDir};
Matt Spinler597e05c2017-02-28 09:59:53 -060085
86 ASSERT_EQ(targets.size(), 5);
87
88 int i = 0;
89
90 for (const auto& t : targets)
91 {
Matt Spinlerb6542342017-03-28 16:37:36 -050092 fs::path path;
Matt Spinler597e05c2017-02-28 09:59:53 -060093
94 ASSERT_EQ(t->getPos(), i);
95
96 if (0 == i)
97 {
Matt Spinlerb6542342017-03-28 16:37:36 -050098 path = masterDir;
Matt Spinler597e05c2017-02-28 09:59:53 -060099 }
100 else
101 {
Matt Spinlerb6542342017-03-28 16:37:36 -0500102 std::ostringstream subdir;
103 subdir << "slave@0" << i << ":00/raw";
104
105 path = _slaveDir;
106 path /= subdir.str();
Matt Spinler597e05c2017-02-28 09:59:53 -0600107 }
108
Matt Spinlerb6542342017-03-28 16:37:36 -0500109 ASSERT_EQ(t->getCFAMPath(), path);
Matt Spinler597e05c2017-02-28 09:59:53 -0600110 i++;
111 }
112 }
113}
Matt Spinlerd9bdcf72017-03-09 15:06:23 -0600114
115
116void func1()
117{
118 std::cout << "Hello\n";
119}
120
121void func2()
122{
123 std::cout << "World\n";
124}
125
126REGISTER_PROCEDURE("hello", func1);
127REGISTER_PROCEDURE("world", func2);
128
129
130TEST(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}