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