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