Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 1 | #include "version_handler.hpp" |
| 2 | |
| 3 | #include <gmock/gmock.h> |
| 4 | #include <gtest/gtest.h> |
| 5 | |
| 6 | namespace ipmi_flash |
| 7 | { |
| 8 | namespace |
| 9 | { |
| 10 | using ::testing::IsEmpty; |
| 11 | |
| 12 | using json = nlohmann::json; |
| 13 | |
| 14 | TEST(VersionHandlerCreateTest, VerifyAllBlobHandlersPresent) |
| 15 | { |
| 16 | |
| 17 | // Need a triggerable mock |
| 18 | VersionInfoMap test; |
| 19 | test.try_emplace("blob0", "blob0", |
| 20 | std::make_unique<VersionActionPackMock>(), |
| 21 | std::make_unique<ImageHandlerMock>()); |
| 22 | ASSERT_THAT(h, ::testing::SizeIs(1)); |
| 23 | EXPECT_THAT(h[0].blobId, "/version/sink_seq"); |
| 24 | EXPECT_FALSE(h[0].actions == nullptr); |
| 25 | EXPECT_FALSE(h[0].handler == nullptr); |
| 26 | } |
| 27 | |
| 28 | TEST(VersionJsonTest, ValidConfigurationVersionBlobName) |
| 29 | { |
| 30 | auto h = VersionHandlersBuilder().buildHandlerFromJson(j2); |
| 31 | ASSERT_THAT(h, ::testing::SizeIs(1)); |
| 32 | EXPECT_THAT(h[0].blobId, "/version/sink_seq"); |
| 33 | EXPECT_FALSE(h[0].actions == nullptr); |
| 34 | EXPECT_FALSE(h[0].handler == nullptr); |
| 35 | } |
| 36 | |
| 37 | TEST(VersionJsonTest, MissingHandlerType) |
| 38 | { |
| 39 | auto j2 = R"( |
| 40 | [{ |
| 41 | "blob" : "/flash/image", |
| 42 | "version":{ |
| 43 | "handler": { |
| 44 | "path" : "/tmp/version_info" |
| 45 | }, |
| 46 | "actions": { |
| 47 | "open" : { |
| 48 | "type" : "systemd", |
| 49 | "unit" : "absolute"} |
| 50 | } |
| 51 | } |
| 52 | }] |
| 53 | )"_json; |
| 54 | EXPECT_THAT(VersionHandlersBuilder().buildHandlerFromJson(j2), IsEmpty()); |
| 55 | } |
| 56 | |
| 57 | TEST(VersionJsonTest, BadBlobName) |
| 58 | { |
| 59 | auto j2 = R"( |
| 60 | [{ |
| 61 | "blob" : "/bad/image", |
| 62 | "version":{ |
| 63 | "handler": { |
| 64 | "type" : "file", |
| 65 | "path" : "/tmp/version_info" |
| 66 | }, |
| 67 | "actions": { |
| 68 | "open" : { |
| 69 | "type" : "systemd", |
| 70 | "unit" : "absolute"} |
| 71 | } |
| 72 | } |
| 73 | }] |
| 74 | )"_json; |
| 75 | EXPECT_THAT(VersionHandlersBuilder().buildHandlerFromJson(j2), IsEmpty()); |
| 76 | } |
| 77 | |
| 78 | TEST(VersionJsonTest, MissingActions) |
| 79 | { |
| 80 | auto j2 = R"( |
| 81 | [{ |
| 82 | "blob" : "/flash/image", |
| 83 | "version":{ |
| 84 | "handler": { |
| 85 | "type" : "file", |
| 86 | "path" : "/tmp/version_info" |
| 87 | } |
| 88 | } |
| 89 | }] |
| 90 | )"_json; |
| 91 | EXPECT_THAT(VersionHandlersBuilder().buildHandlerFromJson(j2), IsEmpty()); |
| 92 | } |
| 93 | |
| 94 | TEST(VersionJsonTest, MissingOpenAction) |
| 95 | { |
| 96 | auto j2 = R"( |
| 97 | [{ |
| 98 | "blob" : "/flash/image", |
| 99 | "version":{ |
| 100 | "handler": { |
| 101 | "type" : "file", |
| 102 | "path" : "/tmp/version_info" |
| 103 | }, |
| 104 | "actions": {} |
| 105 | } |
| 106 | }] |
| 107 | )"_json; |
| 108 | EXPECT_THAT(VersionHandlersBuilder().buildHandlerFromJson(j2), IsEmpty()); |
| 109 | } |
| 110 | |
| 111 | TEST(VersionJsonTest, OneInvalidTwoValidSucceeds) |
| 112 | { |
| 113 | auto j2 = R"( |
| 114 | [{ |
| 115 | "blob" : "/flash/sink_seq0", |
| 116 | "version":{ |
| 117 | "handler": { |
| 118 | "type" : "file", |
| 119 | "path" : "/tmp/version_info" |
| 120 | }, |
| 121 | "actions":{ |
| 122 | "open" :{ |
| 123 | "type" : "systemd", |
| 124 | "unit" : "absolute" |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | }, |
| 129 | { |
| 130 | "blob" : "/version/sink_seq1", |
| 131 | "version":{ |
| 132 | "handler": { |
| 133 | "type" : "file", |
| 134 | "path" : "/tmp/version_info" |
| 135 | }, |
| 136 | "actions":{ |
| 137 | "open" :{ |
| 138 | "type" : "systemd", |
| 139 | "unit" : "absolute" |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | }, |
| 144 | { |
| 145 | "blob" : "/bad/sink_seq", |
| 146 | "version":{ |
| 147 | "handler": { |
| 148 | "type" : "file", |
| 149 | "path" : "/tmp/version_info" |
| 150 | }, |
| 151 | "actions":{ |
| 152 | "open" :{ |
| 153 | "type" : "systemd", |
| 154 | "unit" : "absolute" |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | ] |
| 160 | )"_json; |
| 161 | auto h = VersionHandlersBuilder().buildHandlerFromJson(j2); |
| 162 | ASSERT_THAT(h, ::testing::SizeIs(2)); |
| 163 | EXPECT_THAT(h[0].blobId, "/version/sink_seq0"); |
| 164 | EXPECT_THAT(h[1].blobId, "/version/sink_seq1"); |
| 165 | } |
| 166 | |
| 167 | TEST(VersionJsonTest, BlobNameIsTooShort) |
| 168 | { |
| 169 | auto j2 = R"( |
| 170 | [{ |
| 171 | "blob" : "/flash/", |
| 172 | "version":{ |
| 173 | "handler": { |
| 174 | "type" : "file", |
| 175 | "path" : "/tmp/version_info" |
| 176 | }, |
| 177 | "actions":{ |
| 178 | "open" :{ |
| 179 | "type" : "systemd", |
| 180 | "unit" : "absolute" |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | }] |
| 185 | )"_json; |
| 186 | EXPECT_THAT(VersionHandlersBuilder().buildHandlerFromJson(j2), IsEmpty()); |
| 187 | } |
| 188 | |
| 189 | TEST(VersionJsonTest, OpenSkipAction) |
| 190 | { |
| 191 | auto j2 = R"( |
| 192 | [{ |
| 193 | "blob" : "/flash/sink_seqs", |
| 194 | "version":{ |
| 195 | "handler": { |
| 196 | "type" : "file", |
| 197 | "path" : "/tmp/version_info" |
| 198 | }, |
| 199 | "actions":{ |
| 200 | "open" :{ |
| 201 | "type" : "skip" |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | }] |
| 206 | )"_json; |
| 207 | auto h = VersionHandlersBuilder().buildHandlerFromJson(j2); |
| 208 | EXPECT_THAT(h, ::testing::SizeIs(1)); |
| 209 | EXPECT_TRUE(h[0].blobId == "/version/sink_seqs"); |
| 210 | ASSERT_FALSE(h[0].actions == nullptr); |
| 211 | EXPECT_FALSE(h[0].actions->onOpen == nullptr); |
| 212 | } |
| 213 | |
| 214 | TEST(VersionJsonTest, OpenActionsWithDifferentModes) |
| 215 | { |
| 216 | auto j2 = R"( |
| 217 | [{ |
| 218 | "blob" : "/flash/blob1", |
| 219 | "version":{ |
| 220 | "handler": { |
| 221 | "type" : "file", |
| 222 | "path" : "/tmp/version_info" |
| 223 | }, |
| 224 | "actions":{ |
| 225 | "open" :{ |
| 226 | "type" : "systemd", |
| 227 | "unit" : "absolute", |
| 228 | "mode" : "replace-nope" |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | }, |
| 233 | { |
| 234 | "blob" : "/flash/blob2", |
| 235 | "version":{ |
| 236 | "handler": { |
| 237 | "type" : "file", |
| 238 | "path" : "/tmp/version_info" |
| 239 | }, |
| 240 | "actions":{ |
| 241 | "open" :{ |
| 242 | "type" : "systemd", |
| 243 | "unit" : "absolute", |
| 244 | "mode" : "replace-fake" |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | ] |
| 250 | )"_json; |
| 251 | auto h = VersionHandlersBuilder().buildHandlerFromJson(j2); |
| 252 | ASSERT_THAT(h, ::testing::SizeIs(2)); |
| 253 | |
| 254 | EXPECT_FALSE(h[0].handler == nullptr); |
| 255 | EXPECT_FALSE(h[0].actions == nullptr); |
| 256 | EXPECT_THAT(h[0].blobId, "/version/blob1"); |
| 257 | auto onOpen0 = reinterpret_cast<SystemdNoFile*>(h[0].actions->onOpen.get()); |
| 258 | EXPECT_THAT(onOpen0->getMode(), "replace-nope"); |
| 259 | |
| 260 | EXPECT_FALSE(h[1].handler == nullptr); |
| 261 | EXPECT_FALSE(h[1].actions == nullptr); |
| 262 | EXPECT_THAT(h[1].blobId, "/version/blob2"); |
| 263 | auto onOpen1 = reinterpret_cast<SystemdNoFile*>(h[1].actions->onOpen.get()); |
| 264 | EXPECT_THAT(onOpen1->getMode(), "replace-fake"); |
| 265 | } |
| 266 | } // namespace |
| 267 | } // namespace ipmi_flash |