blob: 1d1f4316384e059359b251a8ad6ae769d5db70c9 [file] [log] [blame]
Matt Spinler597e05c2017-02-28 09:59:53 -06001/**
Patrick Venturee84b4dd2018-11-01 16:06:31 -07002 * Copyright (C) 2017 IBM Corporation
Matt Spinler597e05c2017-02-28 09:59:53 -06003 *
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 */
Matt Spinlerd9bdcf72017-03-09 15:06:23 -060016#include "registration.hpp"
Matt Spinler597e05c2017-02-28 09:59:53 -060017#include "targeting.hpp"
18
Patrick Venturef78d9042018-11-01 15:39:53 -070019#include <stdlib.h>
20
21#include <experimental/filesystem>
22#include <fstream>
23
24#include <gtest/gtest.h>
25
Matt Spinlerd9bdcf72017-03-09 15:06:23 -060026using namespace openpower::util;
Matt Spinler597e05c2017-02-28 09:59:53 -060027using namespace openpower::targeting;
28namespace fs = std::experimental::filesystem;
29
Matt Spinlerb6542342017-03-28 16:37:36 -050030ProcedureMap Registration::procedures;
31
Matt Spinler597e05c2017-02-28 09:59:53 -060032constexpr auto masterDir = "/tmp";
33
34class TargetingTest : public ::testing::Test
35{
Patrick Venturef78d9042018-11-01 15:39:53 -070036 protected:
37 virtual void SetUp()
38 {
39 char dir[50];
40 strcpy(dir, masterDir);
41 strcat(dir, "/targetingXXXXXX");
Matt Spinler597e05c2017-02-28 09:59:53 -060042
Patrick Venturef78d9042018-11-01 15:39:53 -070043 auto path = mkdtemp(dir);
44 assert(path != nullptr);
Matt Spinler597e05c2017-02-28 09:59:53 -060045
Patrick Venturef78d9042018-11-01 15:39:53 -070046 _slaveBaseDir = path;
Matt Spinler597e05c2017-02-28 09:59:53 -060047
Patrick Venturef78d9042018-11-01 15:39:53 -070048 _slaveDir = _slaveBaseDir / "fsi1";
49 fs::create_directory(_slaveDir);
50 }
Matt Spinlerb6542342017-03-28 16:37:36 -050051
Patrick Venturef78d9042018-11-01 15:39:53 -070052 virtual void TearDown()
53 {
54 fs::remove_all(_slaveDir);
55 fs::remove_all(_slaveBaseDir);
56 }
Matt Spinler597e05c2017-02-28 09:59:53 -060057
Patrick Venturef78d9042018-11-01 15:39:53 -070058 fs::path _slaveBaseDir;
59 fs::path _slaveDir;
Matt Spinler597e05c2017-02-28 09:59:53 -060060};
61
Matt Spinler597e05c2017-02-28 09:59:53 -060062TEST_F(TargetingTest, CreateTargets)
63{
64
Patrick Venturef78d9042018-11-01 15:39:53 -070065 // Test that we always create the first Target
Matt Spinler597e05c2017-02-28 09:59:53 -060066 {
Matt Spinlerb6542342017-03-28 16:37:36 -050067 Targeting targets{masterDir, _slaveDir};
Matt Spinler597e05c2017-02-28 09:59:53 -060068 ASSERT_EQ(targets.size(), 1);
69
70 auto t = targets.begin();
71 ASSERT_EQ((*t)->getPos(), 0);
72
Matt Spinlerc3bffed2017-03-10 09:05:30 -060073 ASSERT_EQ((*t)->getCFAMPath(), masterDir);
Matt Spinler597e05c2017-02-28 09:59:53 -060074 }
75
Patrick Venturef78d9042018-11-01 15:39:53 -070076 // Test that we can create multiple Targets
Matt Spinler597e05c2017-02-28 09:59:53 -060077 {
Patrick Venturef78d9042018-11-01 15:39:53 -070078 // 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
Matt Spinlerd9bdcf72017-03-09 15:06:23 -0600115void func1()
116{
117 std::cout << "Hello\n";
118}
119
120void func2()
121{
122 std::cout << "World\n";
123}
124
125REGISTER_PROCEDURE("hello", func1);
126REGISTER_PROCEDURE("world", func2);
127
Matt Spinlerd9bdcf72017-03-09 15:06:23 -0600128TEST(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}