Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 1 | /** |
Patrick Venture | e84b4dd | 2018-11-01 16:06:31 -0700 | [diff] [blame] | 2 | * Copyright (C) 2017 IBM Corporation |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 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 | */ |
Matt Spinler | d9bdcf7 | 2017-03-09 15:06:23 -0600 | [diff] [blame] | 16 | #include "registration.hpp" |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 17 | #include "targeting.hpp" |
| 18 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | |
Brad Bishop | 56d14d0 | 2020-10-09 15:28:51 -0400 | [diff] [blame^] | 21 | #include <filesystem> |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 22 | #include <fstream> |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | |
Matt Spinler | d9bdcf7 | 2017-03-09 15:06:23 -0600 | [diff] [blame] | 26 | using namespace openpower::util; |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 27 | using namespace openpower::targeting; |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 28 | |
Matt Spinler | b654234 | 2017-03-28 16:37:36 -0500 | [diff] [blame] | 29 | ProcedureMap Registration::procedures; |
| 30 | |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 31 | constexpr auto masterDir = "/tmp"; |
| 32 | |
| 33 | class TargetingTest : public ::testing::Test |
| 34 | { |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 35 | protected: |
| 36 | virtual void SetUp() |
| 37 | { |
| 38 | char dir[50]; |
| 39 | strcpy(dir, masterDir); |
| 40 | strcat(dir, "/targetingXXXXXX"); |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 41 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 42 | auto path = mkdtemp(dir); |
| 43 | assert(path != nullptr); |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 44 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 45 | _slaveBaseDir = path; |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 46 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 47 | _slaveDir = _slaveBaseDir / "fsi1"; |
Brad Bishop | 56d14d0 | 2020-10-09 15:28:51 -0400 | [diff] [blame^] | 48 | std::filesystem::create_directory(_slaveDir); |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 49 | } |
Matt Spinler | b654234 | 2017-03-28 16:37:36 -0500 | [diff] [blame] | 50 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 51 | virtual void TearDown() |
| 52 | { |
Brad Bishop | 56d14d0 | 2020-10-09 15:28:51 -0400 | [diff] [blame^] | 53 | std::filesystem::remove_all(_slaveDir); |
| 54 | std::filesystem::remove_all(_slaveBaseDir); |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 55 | } |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 56 | |
Brad Bishop | 56d14d0 | 2020-10-09 15:28:51 -0400 | [diff] [blame^] | 57 | std::filesystem::path _slaveBaseDir; |
| 58 | std::filesystem::path _slaveDir; |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 59 | }; |
| 60 | |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 61 | TEST_F(TargetingTest, CreateTargets) |
| 62 | { |
| 63 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 64 | // Test that we always create the first Target |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 65 | { |
Matt Spinler | b654234 | 2017-03-28 16:37:36 -0500 | [diff] [blame] | 66 | Targeting targets{masterDir, _slaveDir}; |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 67 | ASSERT_EQ(targets.size(), 1); |
| 68 | |
| 69 | auto t = targets.begin(); |
| 70 | ASSERT_EQ((*t)->getPos(), 0); |
| 71 | |
Matt Spinler | c3bffed | 2017-03-10 09:05:30 -0600 | [diff] [blame] | 72 | ASSERT_EQ((*t)->getCFAMPath(), masterDir); |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 73 | } |
| 74 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 75 | // Test that we can create multiple Targets |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 76 | { |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 77 | // make some fake slave entries |
Matt Spinler | b654234 | 2017-03-28 16:37:36 -0500 | [diff] [blame] | 78 | 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 Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 82 | |
Matt Spinler | b654234 | 2017-03-28 16:37:36 -0500 | [diff] [blame] | 83 | Targeting targets{masterDir, _slaveDir}; |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 84 | |
| 85 | ASSERT_EQ(targets.size(), 5); |
| 86 | |
| 87 | int i = 0; |
| 88 | |
| 89 | for (const auto& t : targets) |
| 90 | { |
Brad Bishop | 56d14d0 | 2020-10-09 15:28:51 -0400 | [diff] [blame^] | 91 | std::filesystem::path path; |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 92 | |
| 93 | ASSERT_EQ(t->getPos(), i); |
| 94 | |
| 95 | if (0 == i) |
| 96 | { |
Matt Spinler | b654234 | 2017-03-28 16:37:36 -0500 | [diff] [blame] | 97 | path = masterDir; |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 98 | } |
| 99 | else |
| 100 | { |
Matt Spinler | b654234 | 2017-03-28 16:37:36 -0500 | [diff] [blame] | 101 | std::ostringstream subdir; |
| 102 | subdir << "slave@0" << i << ":00/raw"; |
| 103 | |
| 104 | path = _slaveDir; |
| 105 | path /= subdir.str(); |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 106 | } |
| 107 | |
Matt Spinler | b654234 | 2017-03-28 16:37:36 -0500 | [diff] [blame] | 108 | ASSERT_EQ(t->getCFAMPath(), path); |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 109 | i++; |
| 110 | } |
| 111 | } |
| 112 | } |
Matt Spinler | d9bdcf7 | 2017-03-09 15:06:23 -0600 | [diff] [blame] | 113 | |
Matt Spinler | d9bdcf7 | 2017-03-09 15:06:23 -0600 | [diff] [blame] | 114 | void func1() |
| 115 | { |
| 116 | std::cout << "Hello\n"; |
| 117 | } |
| 118 | |
| 119 | void func2() |
| 120 | { |
| 121 | std::cout << "World\n"; |
| 122 | } |
| 123 | |
| 124 | REGISTER_PROCEDURE("hello", func1); |
| 125 | REGISTER_PROCEDURE("world", func2); |
| 126 | |
Matt Spinler | d9bdcf7 | 2017-03-09 15:06:23 -0600 | [diff] [blame] | 127 | TEST(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 | } |