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>
20 #include "targeting.hpp"
21 
22 using namespace openpower::targeting;
23 namespace fs = std::experimental::filesystem;
24 
25 constexpr auto masterDir = "/tmp";
26 
27 class TargetingTest : public ::testing::Test
28 {
29     protected:
30 
31         virtual void SetUp()
32         {
33             char dir[50];
34             strcpy(dir, masterDir);
35             strcat(dir, "/targetingXXXXXX");
36 
37             auto path = mkdtemp(dir);
38             assert(path != nullptr);
39 
40             _directory = path;
41         }
42 
43         virtual void TearDown()
44         {
45             fs::remove_all(_directory);
46         }
47 
48         std::string _directory;
49 };
50 
51 
52 TEST_F(TargetingTest, CreateTargets)
53 {
54 
55     //Test that we always create the first Target
56     {
57         Targeting targets{masterDir, _directory};
58         ASSERT_EQ(targets.size(), 1);
59 
60         auto t = targets.begin();
61         ASSERT_EQ((*t)->getPos(), 0);
62 
63         ASSERT_EQ((*t)->getPath(), masterDir);
64     }
65 
66 
67     //Test that we can create multiple Targets
68     {
69         //make some fake slave entries
70         std::ofstream(_directory + "/slave@01:00");
71         std::ofstream(_directory + "/slave@02:00");
72         std::ofstream(_directory + "/slave@03:00");
73         std::ofstream(_directory + "/slave@04:00");
74 
75         Targeting targets{masterDir, _directory};
76 
77         ASSERT_EQ(targets.size(), 5);
78 
79         int i = 0;
80 
81         for (const auto& t : targets)
82         {
83             std::ostringstream path;
84 
85             ASSERT_EQ(t->getPos(), i);
86 
87             if (0 == i)
88             {
89                 path << masterDir;
90             }
91             else
92             {
93                 path << _directory << "/slave@0" << i << ":00/raw";
94             }
95 
96             ASSERT_EQ(t->getPath(), path.str());
97             i++;
98         }
99     }
100 }
101