blob: fb73fd06342125a8027f6a1613c96bfe6902e98b [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
Matt Spinlerb6542342017-03-28 16:37:36 -050029ProcedureMap Registration::procedures;
30
Matt Spinler597e05c2017-02-28 09:59:53 -060031constexpr auto masterDir = "/tmp";
32
33class TargetingTest : public ::testing::Test
34{
Patrick Venturef78d9042018-11-01 15:39:53 -070035 protected:
36 virtual void SetUp()
37 {
38 char dir[50];
39 strcpy(dir, masterDir);
40 strcat(dir, "/targetingXXXXXX");
Matt Spinler597e05c2017-02-28 09:59:53 -060041
Patrick Venturef78d9042018-11-01 15:39:53 -070042 auto path = mkdtemp(dir);
43 assert(path != nullptr);
Matt Spinler597e05c2017-02-28 09:59:53 -060044
Patrick Venturef78d9042018-11-01 15:39:53 -070045 _slaveBaseDir = path;
Matt Spinler597e05c2017-02-28 09:59:53 -060046
Patrick Venturef78d9042018-11-01 15:39:53 -070047 _slaveDir = _slaveBaseDir / "fsi1";
Brad Bishop56d14d02020-10-09 15:28:51 -040048 std::filesystem::create_directory(_slaveDir);
Patrick Venturef78d9042018-11-01 15:39:53 -070049 }
Matt Spinlerb6542342017-03-28 16:37:36 -050050
Patrick Venturef78d9042018-11-01 15:39:53 -070051 virtual void TearDown()
52 {
Brad Bishop56d14d02020-10-09 15:28:51 -040053 std::filesystem::remove_all(_slaveDir);
54 std::filesystem::remove_all(_slaveBaseDir);
Patrick Venturef78d9042018-11-01 15:39:53 -070055 }
Matt Spinler597e05c2017-02-28 09:59:53 -060056
Brad Bishop56d14d02020-10-09 15:28:51 -040057 std::filesystem::path _slaveBaseDir;
58 std::filesystem::path _slaveDir;
Matt Spinler597e05c2017-02-28 09:59:53 -060059};
60
Matt Spinler597e05c2017-02-28 09:59:53 -060061TEST_F(TargetingTest, CreateTargets)
62{
63
Patrick Venturef78d9042018-11-01 15:39:53 -070064 // Test that we always create the first Target
Matt Spinler597e05c2017-02-28 09:59:53 -060065 {
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
Patrick Venturef78d9042018-11-01 15:39:53 -070075 // Test that we can create multiple Targets
Matt Spinler597e05c2017-02-28 09:59:53 -060076 {
Patrick Venturef78d9042018-11-01 15:39:53 -070077 // make some fake slave entries
Matt Spinlerb6542342017-03-28 16:37:36 -050078 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");
Matt Spinler597e05c2017-02-28 09:59:53 -060082
Matt Spinlerb6542342017-03-28 16:37:36 -050083 Targeting targets{masterDir, _slaveDir};
Matt Spinler597e05c2017-02-28 09:59:53 -060084
85 ASSERT_EQ(targets.size(), 5);
86
87 int i = 0;
88
89 for (const auto& t : targets)
90 {
Brad Bishop56d14d02020-10-09 15:28:51 -040091 std::filesystem::path path;
Matt Spinler597e05c2017-02-28 09:59:53 -060092
93 ASSERT_EQ(t->getPos(), i);
94
95 if (0 == i)
96 {
Matt Spinlerb6542342017-03-28 16:37:36 -050097 path = masterDir;
Matt Spinler597e05c2017-02-28 09:59:53 -060098 }
99 else
100 {
Matt Spinlerb6542342017-03-28 16:37:36 -0500101 std::ostringstream subdir;
102 subdir << "slave@0" << i << ":00/raw";
103
104 path = _slaveDir;
105 path /= subdir.str();
Matt Spinler597e05c2017-02-28 09:59:53 -0600106 }
107
Matt Spinlerb6542342017-03-28 16:37:36 -0500108 ASSERT_EQ(t->getCFAMPath(), path);
Matt Spinler597e05c2017-02-28 09:59:53 -0600109 i++;
110 }
111 }
112}
Matt Spinlerd9bdcf72017-03-09 15:06:23 -0600113
Matt Spinlerd9bdcf72017-03-09 15:06:23 -0600114void func1()
115{
116 std::cout << "Hello\n";
117}
118
119void func2()
120{
121 std::cout << "World\n";
122}
123
Brad Bishop63508a72020-10-27 18:55:01 -0400124REGISTER_PROCEDURE("hello", func1)
125REGISTER_PROCEDURE("world", func2)
Matt Spinlerd9bdcf72017-03-09 15:06:23 -0600126
Matt Spinlerd9bdcf72017-03-09 15:06:23 -0600127TEST(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}