Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2017 IBM Corporation |
| 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 | */ |
| 16 | #include <gtest/gtest.h> |
| 17 | #include <experimental/filesystem> |
| 18 | #include <fstream> |
| 19 | #include <stdlib.h> |
Matt Spinler | d9bdcf7 | 2017-03-09 15:06:23 -0600 | [diff] [blame] | 20 | #include "registration.hpp" |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 21 | #include "targeting.hpp" |
| 22 | |
Matt Spinler | d9bdcf7 | 2017-03-09 15:06:23 -0600 | [diff] [blame] | 23 | using namespace openpower::util; |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 24 | using namespace openpower::targeting; |
| 25 | namespace fs = std::experimental::filesystem; |
| 26 | |
Matt Spinler | b654234 | 2017-03-28 16:37:36 -0500 | [diff] [blame^] | 27 | ProcedureMap Registration::procedures; |
| 28 | |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 29 | constexpr auto masterDir = "/tmp"; |
| 30 | |
| 31 | class TargetingTest : public ::testing::Test |
| 32 | { |
| 33 | protected: |
| 34 | |
| 35 | virtual void SetUp() |
| 36 | { |
| 37 | char dir[50]; |
| 38 | strcpy(dir, masterDir); |
| 39 | strcat(dir, "/targetingXXXXXX"); |
| 40 | |
| 41 | auto path = mkdtemp(dir); |
| 42 | assert(path != nullptr); |
| 43 | |
Matt Spinler | b654234 | 2017-03-28 16:37:36 -0500 | [diff] [blame^] | 44 | _slaveBaseDir = path; |
| 45 | |
| 46 | _slaveDir = _slaveBaseDir / "hub@00"; |
| 47 | fs::create_directory(_slaveDir); |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | virtual void TearDown() |
| 51 | { |
Matt Spinler | b654234 | 2017-03-28 16:37:36 -0500 | [diff] [blame^] | 52 | fs::remove_all(_slaveDir); |
| 53 | fs::remove_all(_slaveBaseDir); |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 54 | } |
| 55 | |
Matt Spinler | b654234 | 2017-03-28 16:37:36 -0500 | [diff] [blame^] | 56 | fs::path _slaveBaseDir; |
| 57 | fs::path _slaveDir; |
Matt Spinler | 597e05c | 2017-02-28 09:59:53 -0600 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | |
| 61 | TEST_F(TargetingTest, CreateTargets) |
| 62 | { |
| 63 | |
| 64 | //Test that we always create the first Target |
| 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 | |
| 75 | |
| 76 | //Test that we can create multiple Targets |
| 77 | { |
| 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 | |
| 115 | |
| 116 | void func1() |
| 117 | { |
| 118 | std::cout << "Hello\n"; |
| 119 | } |
| 120 | |
| 121 | void func2() |
| 122 | { |
| 123 | std::cout << "World\n"; |
| 124 | } |
| 125 | |
| 126 | REGISTER_PROCEDURE("hello", func1); |
| 127 | REGISTER_PROCEDURE("world", func2); |
| 128 | |
| 129 | |
| 130 | TEST(RegistrationTest, TestReg) |
| 131 | { |
| 132 | int count = 0; |
| 133 | for (const auto& p : Registration::getProcedures()) |
| 134 | { |
| 135 | std::cout << p.first << std::endl; |
| 136 | p.second(); |
| 137 | count++; |
| 138 | } |
| 139 | |
| 140 | ASSERT_EQ(count, 2); |
| 141 | } |