blob: 1cc77827f80db3842f6ba5d5a2ebda3799d59721 [file] [log] [blame]
Bob King8e4cb642020-04-24 13:18:53 +08001/**
2 * Copyright © 2020 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 "action_environment.hpp"
Shawn McCarney45907cc2021-02-05 17:33:11 -060017#include "action_error.hpp"
Bob King8e4cb642020-04-24 13:18:53 +080018#include "compare_vpd_action.hpp"
19#include "id_map.hpp"
Shawn McCarney45907cc2021-02-05 17:33:11 -060020#include "mock_services.hpp"
21#include "mock_vpd.hpp"
Bob King8e4cb642020-04-24 13:18:53 +080022
23#include <exception>
24#include <memory>
25#include <stdexcept>
Shawn McCarney45907cc2021-02-05 17:33:11 -060026#include <string>
Bob King8e4cb642020-04-24 13:18:53 +080027#include <utility>
28
Shawn McCarney45907cc2021-02-05 17:33:11 -060029#include <gmock/gmock.h>
Bob King8e4cb642020-04-24 13:18:53 +080030#include <gtest/gtest.h>
31
Shawn McCarney45907cc2021-02-05 17:33:11 -060032using ::testing::Return;
33using ::testing::Throw;
34
Bob King8e4cb642020-04-24 13:18:53 +080035using namespace phosphor::power::regulators;
36
37TEST(CompareVPDActionTests, Constructor)
38{
Bob Kinga76898f2020-10-13 15:08:33 +080039 CompareVPDAction action{
40 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN",
41 "2D35"};
42 EXPECT_EQ(action.getFRU(),
43 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane");
Bob King8e4cb642020-04-24 13:18:53 +080044 EXPECT_EQ(action.getKeyword(), "CCIN");
45 EXPECT_EQ(action.getValue(), "2D35");
46}
47
48TEST(CompareVPDActionTests, Execute)
49{
Shawn McCarney45907cc2021-02-05 17:33:11 -060050 // Test where works: Actual VPD value is not an empty string
51 {
52 std::string fru{"/xyz/openbmc_project/inventory/system"};
53 std::string keyword{"Model"};
54
55 // Create MockServices object. VPD service will return "ABCD" as VPD
56 // value 4 times.
57 MockServices services{};
58 MockVPD& vpd = services.getMockVPD();
59 EXPECT_CALL(vpd, getValue(fru, keyword))
60 .Times(4)
61 .WillRepeatedly(Return("ABCD"));
62
63 IDMap idMap{};
64 ActionEnvironment environment{idMap, "", services};
65
66 // Test where returns true: actual value == expected value
67 {
68 CompareVPDAction action{fru, keyword, "ABCD"};
69 EXPECT_TRUE(action.execute(environment));
70 }
71
72 // Test where returns false: actual value != expected value
73 {
74 CompareVPDAction action{fru, keyword, "BEEF"};
75 EXPECT_FALSE(action.execute(environment));
76 }
77
78 // Test where returns false: expected value differs by case
79 {
80 CompareVPDAction action{fru, keyword, "abcd"};
81 EXPECT_FALSE(action.execute(environment));
82 }
83
84 // Test where returns false: expected value is an empty string
85 {
86 CompareVPDAction action{fru, keyword, ""};
87 EXPECT_FALSE(action.execute(environment));
88 }
89 }
90
91 // Test where works: Actual VPD value is an empty string
92 {
93 std::string fru{"/xyz/openbmc_project/inventory/system"};
94 std::string keyword{"Model"};
95
96 // Create MockServices object. VPD service will return "" as VPD value
97 // 2 times.
98 MockServices services{};
99 MockVPD& vpd = services.getMockVPD();
100 EXPECT_CALL(vpd, getValue(fru, keyword))
101 .Times(2)
102 .WillRepeatedly(Return(""));
103
104 IDMap idMap{};
105 ActionEnvironment environment{idMap, "", services};
106
107 // Test where returns true: actual value == expected value
108 {
109 CompareVPDAction action{fru, keyword, ""};
110 EXPECT_TRUE(action.execute(environment));
111 }
112
113 // Test where returns false: actual value != expected value
114 {
115 CompareVPDAction action{fru, keyword, "ABCD"};
116 EXPECT_FALSE(action.execute(environment));
117 }
118 }
119
120 // Test where fails: Exception thrown when trying to get actual VPD value
121 {
122 std::string fru{"/xyz/openbmc_project/inventory/system"};
123 std::string keyword{"Model"};
124
125 // Create MockServices object. VPD service will throw an exception.
126 MockServices services{};
127 MockVPD& vpd = services.getMockVPD();
128 EXPECT_CALL(vpd, getValue(fru, keyword))
129 .Times(1)
130 .WillOnce(
131 Throw(std::runtime_error{"D-Bus error: Invalid object path"}));
132
133 IDMap idMap{};
134 ActionEnvironment environment{idMap, "", services};
135
136 try
137 {
138 CompareVPDAction action{fru, keyword, "ABCD"};
139 action.execute(environment);
140 ADD_FAILURE() << "Should not have reached this line.";
141 }
142 catch (const ActionError& e)
143 {
144 EXPECT_STREQ(e.what(), "ActionError: compare_vpd: { fru: "
145 "/xyz/openbmc_project/inventory/system, "
146 "keyword: Model, value: ABCD }");
147 try
148 {
149 // Re-throw inner exception
150 std::rethrow_if_nested(e);
151 ADD_FAILURE() << "Should not have reached this line.";
152 }
153 catch (const std::runtime_error& re)
154 {
155 EXPECT_STREQ(re.what(), "D-Bus error: Invalid object path");
156 }
157 catch (...)
158 {
159 ADD_FAILURE() << "Should not have caught exception.";
160 }
161 }
162 catch (...)
163 {
164 ADD_FAILURE() << "Should not have caught exception.";
165 }
166 }
Bob King8e4cb642020-04-24 13:18:53 +0800167}
168
169TEST(CompareVPDActionTests, GetFRU)
170{
Bob Kinga76898f2020-10-13 15:08:33 +0800171 CompareVPDAction action{
172 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN",
173 "2D35"};
174 EXPECT_EQ(action.getFRU(),
175 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane");
Bob King8e4cb642020-04-24 13:18:53 +0800176}
177
178TEST(CompareVPDActionTests, GetKeyword)
179{
Bob Kinga76898f2020-10-13 15:08:33 +0800180 CompareVPDAction action{
181 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN",
182 "2D35"};
Bob King8e4cb642020-04-24 13:18:53 +0800183 EXPECT_EQ(action.getKeyword(), "CCIN");
184}
185
186TEST(CompareVPDActionTests, GetValue)
187{
Bob Kinga76898f2020-10-13 15:08:33 +0800188 CompareVPDAction action{
189 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN",
190 "2D35"};
Bob King8e4cb642020-04-24 13:18:53 +0800191 EXPECT_EQ(action.getValue(), "2D35");
192}
193
194TEST(CompareVPDActionTests, ToString)
195{
Bob Kinga76898f2020-10-13 15:08:33 +0800196 CompareVPDAction action{
197 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN",
198 "2D35"};
199 EXPECT_EQ(action.toString(), "compare_vpd: { fru: "
200 "/xyz/openbmc_project/inventory/system/"
201 "chassis/disk_backplane, keyword: "
202 "CCIN, value: 2D35 }");
Bob King8e4cb642020-04-24 13:18:53 +0800203}