Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 1 | #include "buildjson.hpp" |
| 2 | |
| 3 | #include <nlohmann/json.hpp> |
| 4 | |
| 5 | #include <gmock/gmock.h> |
| 6 | #include <gtest/gtest.h> |
| 7 | |
| 8 | namespace ipmi_flash |
| 9 | { |
| 10 | namespace |
| 11 | { |
| 12 | using ::testing::IsEmpty; |
| 13 | |
| 14 | using json = nlohmann::json; |
| 15 | |
| 16 | TEST(FirmwareJsonTest, InvalidHandlerType) |
| 17 | { |
| 18 | auto j2 = R"( |
| 19 | [{ |
| 20 | "blob" : "/flash/image", |
| 21 | "handler" : { |
| 22 | "type" : "unsupported", |
| 23 | "path" : "/run/initramfs/bmc-image" |
| 24 | }, |
| 25 | "actions" : { |
| 26 | "preparation" : { |
| 27 | "type" : "systemd", |
| 28 | "unit" : "phosphor-ipmi-flash-bmc-prepare.target" |
| 29 | }, |
| 30 | "verification" : { |
| 31 | "type" : "fileSystemdVerify", |
| 32 | "unit" : "phosphor-ipmi-flash-bmc-verify.target", |
| 33 | "path" : "/tmp/bmc.verify" |
| 34 | }, |
| 35 | "update" : { |
| 36 | "type" : "reboot" |
| 37 | } |
| 38 | } |
| 39 | }] |
| 40 | )"_json; |
| 41 | |
| 42 | EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty()); |
| 43 | } |
| 44 | |
| 45 | TEST(FirmwareJsonTest, InvalidPreparationType) |
| 46 | { |
| 47 | auto j2 = R"( |
| 48 | [{ |
| 49 | "blob" : "/flash/image", |
| 50 | "handler" : { |
| 51 | "type" : "file", |
| 52 | "path" : "/run/initramfs/bmc-image" |
| 53 | }, |
| 54 | "actions" : { |
| 55 | "preparation" : { |
| 56 | "type" : "superfun", |
| 57 | "unit" : "phosphor-ipmi-flash-bmc-prepare.target" |
| 58 | }, |
| 59 | "verification" : { |
| 60 | "type" : "fileSystemdVerify", |
| 61 | "unit" : "phosphor-ipmi-flash-bmc-verify.target", |
| 62 | "path" : "/tmp/bmc.verify" |
| 63 | }, |
| 64 | "update" : { |
| 65 | "type" : "reboot" |
| 66 | } |
| 67 | } |
| 68 | }] |
| 69 | )"_json; |
| 70 | |
| 71 | EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty()); |
| 72 | } |
| 73 | |
| 74 | TEST(FirmwareJsonTest, InvalidVerificationType) |
| 75 | { |
| 76 | auto j2 = R"( |
| 77 | [{ |
| 78 | "blob" : "/flash/image", |
| 79 | "handler" : { |
| 80 | "type" : "file", |
| 81 | "path" : "/run/initramfs/bmc-image" |
| 82 | }, |
| 83 | "actions" : { |
| 84 | "preparation" : { |
| 85 | "type" : "systemd", |
| 86 | "unit" : "phosphor-ipmi-flash-bmc-prepare.target" |
| 87 | }, |
| 88 | "verification" : { |
| 89 | "type" : "funtimes", |
| 90 | "unit" : "phosphor-ipmi-flash-bmc-verify.target", |
| 91 | "path" : "/tmp/bmc.verify" |
| 92 | }, |
| 93 | "update" : { |
| 94 | "type" : "reboot" |
| 95 | } |
| 96 | } |
| 97 | }] |
| 98 | )"_json; |
| 99 | |
| 100 | EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty()); |
| 101 | } |
| 102 | |
| 103 | TEST(FirmwareJsonTest, InvalidUpdateType) |
| 104 | { |
| 105 | auto j2 = R"( |
| 106 | [{ |
| 107 | "blob" : "/flash/image", |
| 108 | "handler" : { |
| 109 | "type" : "file", |
| 110 | "path" : "/run/initramfs/bmc-image" |
| 111 | }, |
| 112 | "actions" : { |
| 113 | "preparation" : { |
| 114 | "type" : "systemd", |
| 115 | "unit" : "phosphor-ipmi-flash-bmc-prepare.target" |
| 116 | }, |
| 117 | "verification" : { |
| 118 | "type" : "fileSystemdVerify", |
| 119 | "unit" : "phosphor-ipmi-flash-bmc-verify.target", |
| 120 | "path" : "/tmp/bmc.verify" |
| 121 | }, |
| 122 | "update" : { |
| 123 | "type" : "systemd" |
| 124 | } |
| 125 | } |
| 126 | }] |
| 127 | )"_json; |
| 128 | |
| 129 | EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty()); |
| 130 | } |
| 131 | |
| 132 | TEST(FirmwareJsonTest, MissingHandler) |
| 133 | { |
| 134 | auto j2 = R"( |
| 135 | [{ |
| 136 | "blob" : "/flash/image", |
| 137 | "actions" : { |
| 138 | "preparation" : { |
| 139 | "type" : "systemd", |
| 140 | "unit" : "phosphor-ipmi-flash-bmc-prepare.target" |
| 141 | }, |
| 142 | "verification" : { |
| 143 | "type" : "fileSystemdVerify", |
| 144 | "unit" : "phosphor-ipmi-flash-bmc-verify.target", |
| 145 | "path" : "/tmp/bmc.verify" |
| 146 | }, |
| 147 | "update" : { |
| 148 | "type" : "reboot" |
| 149 | } |
| 150 | } |
| 151 | }] |
| 152 | )"_json; |
| 153 | |
| 154 | EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty()); |
| 155 | } |
| 156 | |
| 157 | TEST(FirmwareJsonTest, MissingActions) |
| 158 | { |
| 159 | auto j2 = R"( |
| 160 | [{ |
| 161 | "blob" : "/flash/image", |
| 162 | "handler" : { |
| 163 | "type" : "file", |
| 164 | "path" : "/run/initramfs/bmc-image" |
| 165 | } |
| 166 | }] |
| 167 | )"_json; |
| 168 | |
| 169 | EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty()); |
| 170 | } |
| 171 | |
| 172 | TEST(FirmwareJsonTest, MissingActionPreparation) |
| 173 | { |
| 174 | auto j2 = R"( |
| 175 | [{ |
| 176 | "blob" : "/flash/image", |
| 177 | "handler" : { |
| 178 | "type" : "file", |
| 179 | "path" : "/run/initramfs/bmc-image" |
| 180 | }, |
| 181 | "actions" : { |
| 182 | "verification" : { |
| 183 | "type" : "fileSystemdVerify", |
| 184 | "unit" : "phosphor-ipmi-flash-bmc-verify.target", |
| 185 | "path" : "/tmp/bmc.verify" |
| 186 | }, |
| 187 | "update" : { |
| 188 | "type" : "reboot" |
| 189 | } |
| 190 | } |
| 191 | }] |
| 192 | )"_json; |
| 193 | |
| 194 | EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty()); |
| 195 | } |
| 196 | |
| 197 | TEST(FirmwareJsonTest, MissingActionVerification) |
| 198 | { |
| 199 | auto j2 = R"( |
| 200 | [{ |
| 201 | "blob" : "/flash/image", |
| 202 | "handler" : { |
| 203 | "type" : "file", |
| 204 | "path" : "/run/initramfs/bmc-image" |
| 205 | }, |
| 206 | "actions" : { |
| 207 | "preparation" : { |
| 208 | "type" : "systemd", |
| 209 | "unit" : "phosphor-ipmi-flash-bmc-prepare.target" |
| 210 | }, |
| 211 | "update" : { |
| 212 | "type" : "reboot" |
| 213 | } |
| 214 | } |
| 215 | }] |
| 216 | )"_json; |
| 217 | |
| 218 | EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty()); |
| 219 | } |
| 220 | |
| 221 | TEST(FirmwareJsonTest, MissingActionUpdate) |
| 222 | { |
| 223 | auto j2 = R"( |
| 224 | [{ |
| 225 | "blob" : "/flash/image", |
| 226 | "handler" : { |
| 227 | "type" : "file", |
| 228 | "path" : "/run/initramfs/bmc-image" |
| 229 | }, |
| 230 | "actions" : { |
| 231 | "preparation" : { |
| 232 | "type" : "systemd", |
| 233 | "unit" : "phosphor-ipmi-flash-bmc-prepare.target" |
| 234 | }, |
| 235 | "verification" : { |
| 236 | "type" : "fileSystemdVerify", |
| 237 | "unit" : "phosphor-ipmi-flash-bmc-verify.target", |
| 238 | "path" : "/tmp/bmc.verify" |
| 239 | } |
| 240 | } |
| 241 | }] |
| 242 | )"_json; |
| 243 | |
| 244 | EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty()); |
| 245 | } |
| 246 | |
| 247 | TEST(FirmwareJsonTest, TwoConfigsOneInvalidReturnsValid) |
| 248 | { |
| 249 | auto j2 = R"( |
| 250 | [{ |
| 251 | "blob" : "/flash/image", |
| 252 | "actions" : { |
| 253 | "preparation" : { |
| 254 | "type" : "systemd", |
| 255 | "unit" : "phosphor-ipmi-flash-bmc-prepare.target" |
| 256 | }, |
| 257 | "verification" : { |
| 258 | "type" : "fileSystemdVerify", |
| 259 | "unit" : "phosphor-ipmi-flash-bmc-verify.target", |
| 260 | "path" : "/tmp/bmc.verify" |
| 261 | }, |
| 262 | "update" : { |
| 263 | "type" : "reboot" |
| 264 | } |
| 265 | } |
| 266 | }, |
| 267 | { |
| 268 | "blob" : "/flash/image2", |
| 269 | "handler" : { |
| 270 | "type" : "file", |
| 271 | "path" : "/run/initramfs/bmc-image" |
| 272 | }, |
| 273 | "actions" : { |
| 274 | "preparation" : { |
| 275 | "type" : "systemd", |
| 276 | "unit" : "phosphor-ipmi-flash-bmc-prepare.target" |
| 277 | }, |
| 278 | "verification" : { |
| 279 | "type" : "fileSystemdVerify", |
| 280 | "unit" : "phosphor-ipmi-flash-bmc-verify.target", |
| 281 | "path" : "/tmp/bmc.verify" |
| 282 | }, |
| 283 | "update" : { |
| 284 | "type" : "reboot" |
| 285 | } |
| 286 | } |
| 287 | }] |
| 288 | )"_json; |
| 289 | |
| 290 | auto h = buildHandlerFromJson(j2); |
| 291 | EXPECT_EQ(h[0].blobId, "/flash/image2"); |
| 292 | EXPECT_EQ(h.size(), 1); |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * TODO: It may be worth individually using builders per type, and testing |
| 297 | * those. |
| 298 | * |
| 299 | * TODO: Only allow unique handler blob paths (tested at a higher level). |
| 300 | */ |
| 301 | |
| 302 | TEST(FirmwareJsonTest, VerifySystemdWithReboot) |
| 303 | { |
| 304 | auto j2 = R"( |
| 305 | [{ |
| 306 | "blob" : "/flash/image", |
| 307 | "handler" : { |
| 308 | "type" : "file", |
| 309 | "path" : "/run/initramfs/bmc-image" |
| 310 | }, |
| 311 | "actions" : { |
| 312 | "preparation" : { |
| 313 | "type" : "systemd", |
| 314 | "unit" : "phosphor-ipmi-flash-bmc-prepare.target" |
| 315 | }, |
| 316 | "verification" : { |
| 317 | "type" : "fileSystemdVerify", |
| 318 | "unit" : "phosphor-ipmi-flash-bmc-verify.target", |
| 319 | "path" : "/tmp/bmc.verify" |
| 320 | }, |
| 321 | "update" : { |
| 322 | "type" : "reboot" |
| 323 | } |
| 324 | } |
| 325 | }] |
| 326 | )"_json; |
| 327 | |
| 328 | auto h = buildHandlerFromJson(j2); |
| 329 | EXPECT_EQ(h[0].blobId, "/flash/image"); |
| 330 | EXPECT_FALSE(h[0].handler == nullptr); |
| 331 | EXPECT_FALSE(h[0].actions == nullptr); |
| 332 | EXPECT_FALSE(h[0].actions->preparation == nullptr); |
| 333 | EXPECT_FALSE(h[0].actions->verification == nullptr); |
| 334 | EXPECT_FALSE(h[0].actions->update == nullptr); |
| 335 | } |
| 336 | |
| 337 | TEST(FirmwareJsonTest, VerifyMultipleHandlersReturned) |
| 338 | { |
| 339 | auto j2 = R"( |
| 340 | [{ |
| 341 | "blob" : "/flash/image", |
| 342 | "handler" : { |
| 343 | "type" : "file", |
| 344 | "path" : "/run/initramfs/bmc-image" |
| 345 | }, |
| 346 | "actions" : { |
| 347 | "preparation" : { |
| 348 | "type" : "systemd", |
| 349 | "unit" : "phosphor-ipmi-flash-bmc-prepare.target" |
| 350 | }, |
| 351 | "verification" : { |
| 352 | "type" : "fileSystemdVerify", |
| 353 | "unit" : "phosphor-ipmi-flash-bmc-verify.target", |
| 354 | "path" : "/tmp/bmc.verify" |
| 355 | }, |
| 356 | "update" : { |
| 357 | "type" : "systemd", |
| 358 | "unit" : "phosphor-ipmi-flash-bmc-update.target" |
| 359 | } |
| 360 | } |
| 361 | }, |
| 362 | { |
| 363 | "blob" : "/flash/bios", |
| 364 | "handler" : { |
| 365 | "type" : "file", |
| 366 | "path" : "/run/initramfs/bmc-image" |
| 367 | }, |
| 368 | "actions" : { |
| 369 | "preparation" : { |
| 370 | "type" : "systemd", |
| 371 | "unit" : "phosphor-ipmi-flash-bmc-prepare.target" |
| 372 | }, |
| 373 | "verification" : { |
| 374 | "type" : "fileSystemdVerify", |
| 375 | "unit" : "phosphor-ipmi-flash-bmc-verify.target", |
| 376 | "path" : "/tmp/bmc.verify" |
| 377 | }, |
| 378 | "update" : { |
| 379 | "type" : "systemd", |
| 380 | "unit" : "phosphor-ipmi-flash-bmc-update.target" |
| 381 | } |
| 382 | } |
| 383 | }] |
| 384 | )"_json; |
| 385 | |
| 386 | auto h = buildHandlerFromJson(j2); |
| 387 | EXPECT_EQ(h.size(), 2); |
| 388 | EXPECT_EQ(h[0].blobId, "/flash/image"); |
| 389 | EXPECT_EQ(h[1].blobId, "/flash/bios"); |
| 390 | } |
| 391 | |
| 392 | TEST(FirmwareJsonTest, VerifyValidSingleNonReboot) |
| 393 | { |
| 394 | auto j2 = R"( |
| 395 | [{ |
| 396 | "blob" : "/flash/image", |
| 397 | "handler" : { |
| 398 | "type" : "file", |
| 399 | "path" : "/run/initramfs/bmc-image" |
| 400 | }, |
| 401 | "actions" : { |
| 402 | "preparation" : { |
| 403 | "type" : "systemd", |
| 404 | "unit" : "phosphor-ipmi-flash-bmc-prepare.target" |
| 405 | }, |
| 406 | "verification" : { |
| 407 | "type" : "fileSystemdVerify", |
| 408 | "unit" : "phosphor-ipmi-flash-bmc-verify.target", |
| 409 | "path" : "/tmp/bmc.verify" |
| 410 | }, |
| 411 | "update" : { |
| 412 | "type" : "systemd", |
| 413 | "unit" : "phosphor-ipmi-flash-bmc-update.target" |
| 414 | } |
| 415 | } |
| 416 | }] |
| 417 | )"_json; |
| 418 | |
| 419 | auto h = buildHandlerFromJson(j2); |
| 420 | EXPECT_EQ(h[0].blobId, "/flash/image"); |
| 421 | EXPECT_FALSE(h[0].handler == nullptr); |
| 422 | EXPECT_FALSE(h[0].actions == nullptr); |
| 423 | EXPECT_FALSE(h[0].actions->preparation == nullptr); |
| 424 | EXPECT_FALSE(h[0].actions->verification == nullptr); |
| 425 | EXPECT_FALSE(h[0].actions->update == nullptr); |
| 426 | } |
| 427 | |
| 428 | } // namespace |
| 429 | } // namespace ipmi_flash |