blob: 81aae58f0ec63f184a9f1c01c743835809c44641 [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
Brad Bishop56d14d02020-10-09 15:28:51 -040021#include <filesystem>
Patrick Venturef78d9042018-11-01 15:39:53 -070022#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;
Matt Spinler597e05c2017-02-28 09:59:53 -060028
29constexpr auto masterDir = "/tmp";
30
31class TargetingTest : public ::testing::Test
32{
Patrick Venturef78d9042018-11-01 15:39:53 -070033 protected:
34 virtual void SetUp()
35 {
36 char dir[50];
37 strcpy(dir, masterDir);
38 strcat(dir, "/targetingXXXXXX");
Matt Spinler597e05c2017-02-28 09:59:53 -060039
Patrick Venturef78d9042018-11-01 15:39:53 -070040 auto path = mkdtemp(dir);
41 assert(path != nullptr);
Matt Spinler597e05c2017-02-28 09:59:53 -060042
Patrick Venturef78d9042018-11-01 15:39:53 -070043 _slaveBaseDir = path;
Matt Spinler597e05c2017-02-28 09:59:53 -060044
Patrick Venturef78d9042018-11-01 15:39:53 -070045 _slaveDir = _slaveBaseDir / "fsi1";
Brad Bishop56d14d02020-10-09 15:28:51 -040046 std::filesystem::create_directory(_slaveDir);
Patrick Venturef78d9042018-11-01 15:39:53 -070047 }
Matt Spinlerb6542342017-03-28 16:37:36 -050048
Patrick Venturef78d9042018-11-01 15:39:53 -070049 virtual void TearDown()
50 {
Brad Bishop56d14d02020-10-09 15:28:51 -040051 std::filesystem::remove_all(_slaveDir);
52 std::filesystem::remove_all(_slaveBaseDir);
Patrick Venturef78d9042018-11-01 15:39:53 -070053 }
Matt Spinler597e05c2017-02-28 09:59:53 -060054
Brad Bishop56d14d02020-10-09 15:28:51 -040055 std::filesystem::path _slaveBaseDir;
56 std::filesystem::path _slaveDir;
Matt Spinler597e05c2017-02-28 09:59:53 -060057};
58
Matt Spinler597e05c2017-02-28 09:59:53 -060059TEST_F(TargetingTest, CreateTargets)
60{
61
Patrick Venturef78d9042018-11-01 15:39:53 -070062 // Test that we always create the first Target
Matt Spinler597e05c2017-02-28 09:59:53 -060063 {
Matt Spinlerb6542342017-03-28 16:37:36 -050064 Targeting targets{masterDir, _slaveDir};
Matt Spinler597e05c2017-02-28 09:59:53 -060065 ASSERT_EQ(targets.size(), 1);
66
67 auto t = targets.begin();
68 ASSERT_EQ((*t)->getPos(), 0);
69
Matt Spinlerc3bffed2017-03-10 09:05:30 -060070 ASSERT_EQ((*t)->getCFAMPath(), masterDir);
Matt Spinler597e05c2017-02-28 09:59:53 -060071 }
72
Patrick Venturef78d9042018-11-01 15:39:53 -070073 // Test that we can create multiple Targets
Matt Spinler597e05c2017-02-28 09:59:53 -060074 {
Patrick Venturef78d9042018-11-01 15:39:53 -070075 // make some fake slave entries
Matt Spinlerb6542342017-03-28 16:37:36 -050076 std::ofstream(_slaveDir / "slave@01:00");
77 std::ofstream(_slaveDir / "slave@02:00");
78 std::ofstream(_slaveDir / "slave@03:00");
79 std::ofstream(_slaveDir / "slave@04:00");
Matt Spinler597e05c2017-02-28 09:59:53 -060080
Matt Spinlerb6542342017-03-28 16:37:36 -050081 Targeting targets{masterDir, _slaveDir};
Matt Spinler597e05c2017-02-28 09:59:53 -060082
83 ASSERT_EQ(targets.size(), 5);
84
85 int i = 0;
86
87 for (const auto& t : targets)
88 {
Brad Bishop56d14d02020-10-09 15:28:51 -040089 std::filesystem::path path;
Matt Spinler597e05c2017-02-28 09:59:53 -060090
91 ASSERT_EQ(t->getPos(), i);
92
93 if (0 == i)
94 {
Matt Spinlerb6542342017-03-28 16:37:36 -050095 path = masterDir;
Matt Spinler597e05c2017-02-28 09:59:53 -060096 }
97 else
98 {
Matt Spinlerb6542342017-03-28 16:37:36 -050099 std::ostringstream subdir;
100 subdir << "slave@0" << i << ":00/raw";
101
102 path = _slaveDir;
103 path /= subdir.str();
Matt Spinler597e05c2017-02-28 09:59:53 -0600104 }
105
Matt Spinlerb6542342017-03-28 16:37:36 -0500106 ASSERT_EQ(t->getCFAMPath(), path);
Matt Spinler597e05c2017-02-28 09:59:53 -0600107 i++;
108 }
109 }
110}
Matt Spinlerd9bdcf72017-03-09 15:06:23 -0600111
Matt Spinlerd9bdcf72017-03-09 15:06:23 -0600112void func1()
113{
114 std::cout << "Hello\n";
115}
116
117void func2()
118{
119 std::cout << "World\n";
120}
121
Brad Bishop63508a72020-10-27 18:55:01 -0400122REGISTER_PROCEDURE("hello", func1)
123REGISTER_PROCEDURE("world", func2)
Matt Spinlerd9bdcf72017-03-09 15:06:23 -0600124
Matt Spinlerd9bdcf72017-03-09 15:06:23 -0600125TEST(RegistrationTest, TestReg)
126{
127 int count = 0;
128 for (const auto& p : Registration::getProcedures())
129 {
130 std::cout << p.first << std::endl;
131 p.second();
132 count++;
133 }
134
135 ASSERT_EQ(count, 2);
136}