blob: cf412243d095396319ac37410dbd6aaa386461ab [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{
Patrick Venturef78d9042018-11-01 15:39:53 -070061 // Test that we always create the first Target
Matt Spinler597e05c2017-02-28 09:59:53 -060062 {
Matt Spinlerb6542342017-03-28 16:37:36 -050063 Targeting targets{masterDir, _slaveDir};
Matt Spinler597e05c2017-02-28 09:59:53 -060064 ASSERT_EQ(targets.size(), 1);
65
66 auto t = targets.begin();
67 ASSERT_EQ((*t)->getPos(), 0);
68
Matt Spinlerc3bffed2017-03-10 09:05:30 -060069 ASSERT_EQ((*t)->getCFAMPath(), masterDir);
Matt Spinler597e05c2017-02-28 09:59:53 -060070 }
71
Patrick Venturef78d9042018-11-01 15:39:53 -070072 // Test that we can create multiple Targets
Matt Spinler597e05c2017-02-28 09:59:53 -060073 {
Patrick Venturef78d9042018-11-01 15:39:53 -070074 // make some fake slave entries
Matt Spinlerb6542342017-03-28 16:37:36 -050075 std::ofstream(_slaveDir / "slave@01:00");
76 std::ofstream(_slaveDir / "slave@02:00");
77 std::ofstream(_slaveDir / "slave@03:00");
78 std::ofstream(_slaveDir / "slave@04:00");
Matt Spinler597e05c2017-02-28 09:59:53 -060079
Matt Spinlerb6542342017-03-28 16:37:36 -050080 Targeting targets{masterDir, _slaveDir};
Matt Spinler597e05c2017-02-28 09:59:53 -060081
82 ASSERT_EQ(targets.size(), 5);
83
84 int i = 0;
85
86 for (const auto& t : targets)
87 {
Brad Bishop56d14d02020-10-09 15:28:51 -040088 std::filesystem::path path;
Matt Spinler597e05c2017-02-28 09:59:53 -060089
90 ASSERT_EQ(t->getPos(), i);
91
92 if (0 == i)
93 {
Matt Spinlerb6542342017-03-28 16:37:36 -050094 path = masterDir;
Matt Spinler597e05c2017-02-28 09:59:53 -060095 }
96 else
97 {
Matt Spinlerb6542342017-03-28 16:37:36 -050098 std::ostringstream subdir;
99 subdir << "slave@0" << i << ":00/raw";
100
101 path = _slaveDir;
102 path /= subdir.str();
Matt Spinler597e05c2017-02-28 09:59:53 -0600103 }
104
Matt Spinlerb6542342017-03-28 16:37:36 -0500105 ASSERT_EQ(t->getCFAMPath(), path);
Matt Spinler597e05c2017-02-28 09:59:53 -0600106 i++;
107 }
108 }
109}
Matt Spinlerd9bdcf72017-03-09 15:06:23 -0600110
Matt Spinlerd9bdcf72017-03-09 15:06:23 -0600111void func1()
112{
113 std::cout << "Hello\n";
114}
115
116void func2()
117{
118 std::cout << "World\n";
119}
120
Brad Bishop63508a72020-10-27 18:55:01 -0400121REGISTER_PROCEDURE("hello", func1)
122REGISTER_PROCEDURE("world", func2)
Matt Spinlerd9bdcf72017-03-09 15:06:23 -0600123
Matt Spinlerd9bdcf72017-03-09 15:06:23 -0600124TEST(RegistrationTest, TestReg)
125{
126 int count = 0;
127 for (const auto& p : Registration::getProcedures())
128 {
129 std::cout << p.first << std::endl;
130 p.second();
131 count++;
132 }
133
134 ASSERT_EQ(count, 2);
135}