|
19 | 19 | import org.apache.kafka.clients.admin.Admin;
|
20 | 20 | import org.apache.kafka.clients.admin.MockAdminClient;
|
21 | 21 | import org.apache.kafka.common.security.token.delegation.DelegationToken;
|
| 22 | +import org.apache.kafka.common.utils.Exit; |
22 | 23 |
|
23 | 24 | import org.junit.jupiter.api.Test;
|
24 | 25 |
|
@@ -109,4 +110,156 @@ private DelegationTokenCommand.DelegationTokenCommandOptions getExpireOpts(Strin
|
109 | 110 | String[] args = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--expire", "--expiry-time-period", "-1", "--hmac", hmac};
|
110 | 111 | return new DelegationTokenCommand.DelegationTokenCommandOptions(args);
|
111 | 112 | }
|
| 113 | + |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testCheckArgsMissingRequiredArgs() { |
| 117 | + Exit.setExitProcedure((exitCode, message) -> { |
| 118 | + throw new RuntimeException("Exit with code " + exitCode + ": " + message); |
| 119 | + }); |
| 120 | + try { |
| 121 | + String[] argsCreateMissingBootstrap = {"--command-config", "testfile", "--create", "--max-life-time-period", "604800000"}; |
| 122 | + DelegationTokenCommand.DelegationTokenCommandOptions optsCreateMissingBootstrap = new DelegationTokenCommand.DelegationTokenCommandOptions(argsCreateMissingBootstrap); |
| 123 | + assertThrows(RuntimeException.class, optsCreateMissingBootstrap::checkArgs); |
| 124 | + |
| 125 | + String[] argsCreateMissingConfig = {"--bootstrap-server", "localhost:9092", "--create", "--max-life-time-period", "604800000"}; |
| 126 | + DelegationTokenCommand.DelegationTokenCommandOptions optsCreateMissingConfig = new DelegationTokenCommand.DelegationTokenCommandOptions(argsCreateMissingConfig); |
| 127 | + assertThrows(RuntimeException.class, optsCreateMissingConfig::checkArgs); |
| 128 | + |
| 129 | + String[] argsCreateMissingMaxLife = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--create"}; |
| 130 | + DelegationTokenCommand.DelegationTokenCommandOptions optsCreateMissingMaxLife = new DelegationTokenCommand.DelegationTokenCommandOptions(argsCreateMissingMaxLife); |
| 131 | + assertThrows(RuntimeException.class, optsCreateMissingMaxLife::checkArgs); |
| 132 | + |
| 133 | + String[] argsRenewMissingBootstrap = {"--command-config", "testfile", "--renew", "--hmac", "test-hmac", "--renew-time-period", "604800000"}; |
| 134 | + DelegationTokenCommand.DelegationTokenCommandOptions optsRenewMissingBootstrap = new DelegationTokenCommand.DelegationTokenCommandOptions(argsRenewMissingBootstrap); |
| 135 | + assertThrows(RuntimeException.class, optsRenewMissingBootstrap::checkArgs); |
| 136 | + |
| 137 | + String[] argsRenewMissingConfig = {"--bootstrap-server", "localhost:9092", "--renew", "--hmac", "test-hmac", "--renew-time-period", "604800000"}; |
| 138 | + DelegationTokenCommand.DelegationTokenCommandOptions optsRenewMissingConfig = new DelegationTokenCommand.DelegationTokenCommandOptions(argsRenewMissingConfig); |
| 139 | + assertThrows(RuntimeException.class, optsRenewMissingConfig::checkArgs); |
| 140 | + |
| 141 | + String[] argsRenewMissingHmac = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--renew", "--renew-time-period", "604800000"}; |
| 142 | + DelegationTokenCommand.DelegationTokenCommandOptions optsRenewMissingHmac = new DelegationTokenCommand.DelegationTokenCommandOptions(argsRenewMissingHmac); |
| 143 | + assertThrows(RuntimeException.class, optsRenewMissingHmac::checkArgs); |
| 144 | + |
| 145 | + String[] argsRenewMissingRenewTime = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--renew", "--hmac", "test-hmac"}; |
| 146 | + DelegationTokenCommand.DelegationTokenCommandOptions optsRenewMissingRenewTime = new DelegationTokenCommand.DelegationTokenCommandOptions(argsRenewMissingRenewTime); |
| 147 | + assertThrows(RuntimeException.class, optsRenewMissingRenewTime::checkArgs); |
| 148 | + |
| 149 | + String[] argsExpireMissingBootstrap = {"--command-config", "testfile", "--expire", "--hmac", "test-hmac", "--expiry-time-period", "604800000"}; |
| 150 | + DelegationTokenCommand.DelegationTokenCommandOptions optsExpireMissingBootstrap = new DelegationTokenCommand.DelegationTokenCommandOptions(argsExpireMissingBootstrap); |
| 151 | + assertThrows(RuntimeException.class, optsExpireMissingBootstrap::checkArgs); |
| 152 | + |
| 153 | + String[] argsExpireMissingConfig = {"--bootstrap-server", "localhost:9092", "--expire", "--hmac", "test-hmac", "--expiry-time-period", "604800000"}; |
| 154 | + DelegationTokenCommand.DelegationTokenCommandOptions optsExpireMissingConfig = new DelegationTokenCommand.DelegationTokenCommandOptions(argsExpireMissingConfig); |
| 155 | + assertThrows(RuntimeException.class, optsExpireMissingConfig::checkArgs); |
| 156 | + |
| 157 | + String[] argsExpireMissingHmac = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--expire", "--expiry-time-period", "604800000"}; |
| 158 | + DelegationTokenCommand.DelegationTokenCommandOptions optsExpireMissingHmac = new DelegationTokenCommand.DelegationTokenCommandOptions(argsExpireMissingHmac); |
| 159 | + assertThrows(RuntimeException.class, optsExpireMissingHmac::checkArgs); |
| 160 | + |
| 161 | + String[] argsExpireMissingExpiryTime = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--expire", "--hmac", "test-hmac"}; |
| 162 | + DelegationTokenCommand.DelegationTokenCommandOptions optsExpireMissingExpiryTime = new DelegationTokenCommand.DelegationTokenCommandOptions(argsExpireMissingExpiryTime); |
| 163 | + assertThrows(RuntimeException.class, optsExpireMissingExpiryTime::checkArgs); |
| 164 | + |
| 165 | + String[] argsDescribeMissingBootstrap = {"--command-config", "testfile", "--describe"}; |
| 166 | + DelegationTokenCommand.DelegationTokenCommandOptions optsDescribeMissingBootstrap = new DelegationTokenCommand.DelegationTokenCommandOptions(argsDescribeMissingBootstrap); |
| 167 | + assertThrows(RuntimeException.class, optsDescribeMissingBootstrap::checkArgs); |
| 168 | + |
| 169 | + String[] argsDescribeMissingConfig = {"--bootstrap-server", "localhost:9092", "--describe"}; |
| 170 | + DelegationTokenCommand.DelegationTokenCommandOptions optsDescribeMissingConfig = new DelegationTokenCommand.DelegationTokenCommandOptions(argsDescribeMissingConfig); |
| 171 | + assertThrows(RuntimeException.class, optsDescribeMissingConfig::checkArgs); |
| 172 | + } finally { |
| 173 | + Exit.resetExitProcedure(); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void testCheckArgsInvalidArgs() { |
| 179 | + Exit.setExitProcedure((exitCode, message) -> { |
| 180 | + throw new RuntimeException("Exit with code " + exitCode + ": " + message); |
| 181 | + }); |
| 182 | + try { |
| 183 | + String[] argsCreateWithHmac = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--create", "--max-life-time-period", "604800000", "--hmac", "test-hmac"}; |
| 184 | + DelegationTokenCommand.DelegationTokenCommandOptions optsCreateWithHmac = new DelegationTokenCommand.DelegationTokenCommandOptions(argsCreateWithHmac); |
| 185 | + assertThrows(RuntimeException.class, optsCreateWithHmac::checkArgs); |
| 186 | + |
| 187 | + String[] argsCreateWithRenewTime = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--create", "--max-life-time-period", "604800000", "--renew-time-period", "604800000"}; |
| 188 | + DelegationTokenCommand.DelegationTokenCommandOptions optsCreateWithRenewTime = new DelegationTokenCommand.DelegationTokenCommandOptions(argsCreateWithRenewTime); |
| 189 | + assertThrows(RuntimeException.class, optsCreateWithRenewTime::checkArgs); |
| 190 | + |
| 191 | + String[] argsCreateWithExpiryTime = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--create", "--max-life-time-period", "604800000", "--expiry-time-period", "604800000"}; |
| 192 | + DelegationTokenCommand.DelegationTokenCommandOptions optsCreateWithExpiryTime = new DelegationTokenCommand.DelegationTokenCommandOptions(argsCreateWithExpiryTime); |
| 193 | + assertThrows(RuntimeException.class, optsCreateWithExpiryTime::checkArgs); |
| 194 | + |
| 195 | + String[] argsRenewWithRenewPrincipals = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--renew", "--hmac", "test-hmac", "--renew-time-period", "604800000", "--renewer-principal", "User:renewer"}; |
| 196 | + DelegationTokenCommand.DelegationTokenCommandOptions optsRenewWithRenewPrincipals = new DelegationTokenCommand.DelegationTokenCommandOptions(argsRenewWithRenewPrincipals); |
| 197 | + assertThrows(RuntimeException.class, optsRenewWithRenewPrincipals::checkArgs); |
| 198 | + |
| 199 | + String[] argsRenewWithMaxLife = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--renew", "--hmac", "test-hmac", "--renew-time-period", "604800000", "--max-life-time-period", "604800000"}; |
| 200 | + DelegationTokenCommand.DelegationTokenCommandOptions optsRenewWithMaxLife = new DelegationTokenCommand.DelegationTokenCommandOptions(argsRenewWithMaxLife); |
| 201 | + assertThrows(RuntimeException.class, optsRenewWithMaxLife::checkArgs); |
| 202 | + |
| 203 | + String[] argsRenewWithExpiryTime = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--renew", "--hmac", "test-hmac", "--renew-time-period", "604800000", "--expiry-time-period", "604800000"}; |
| 204 | + DelegationTokenCommand.DelegationTokenCommandOptions optsRenewWithExpiryTime = new DelegationTokenCommand.DelegationTokenCommandOptions(argsRenewWithExpiryTime); |
| 205 | + assertThrows(RuntimeException.class, optsRenewWithExpiryTime::checkArgs); |
| 206 | + |
| 207 | + String[] argsRenewWithOwner = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--renew", "--hmac", "test-hmac", "--renew-time-period", "604800000", "--owner-principal", "User:owner"}; |
| 208 | + DelegationTokenCommand.DelegationTokenCommandOptions optsRenewWithOwner = new DelegationTokenCommand.DelegationTokenCommandOptions(argsRenewWithOwner); |
| 209 | + assertThrows(RuntimeException.class, optsRenewWithOwner::checkArgs); |
| 210 | + |
| 211 | + String[] argsExpireWithRenew = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--expire", "--renew", "--hmac", "test-hmac", "--expiry-time-period", "604800000"}; |
| 212 | + DelegationTokenCommand.DelegationTokenCommandOptions optsExpireWithRenew = new DelegationTokenCommand.DelegationTokenCommandOptions(argsExpireWithRenew); |
| 213 | + assertThrows(RuntimeException.class, optsExpireWithRenew::checkArgs); |
| 214 | + |
| 215 | + String[] argsExpireWithMaxLife = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--expire", "--hmac", "test-hmac", "--expiry-time-period", "604800000", "--max-life-time-period", "604800000"}; |
| 216 | + DelegationTokenCommand.DelegationTokenCommandOptions optsExpireWithMaxLife = new DelegationTokenCommand.DelegationTokenCommandOptions(argsExpireWithMaxLife); |
| 217 | + assertThrows(RuntimeException.class, optsExpireWithMaxLife::checkArgs); |
| 218 | + |
| 219 | + String[] argsExpireWithRenewTime = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--expire", "--hmac", "test-hmac", "--expiry-time-period", "604800000", "--renew-time-period", "604800000"}; |
| 220 | + DelegationTokenCommand.DelegationTokenCommandOptions optsExpireWithRenewTime = new DelegationTokenCommand.DelegationTokenCommandOptions(argsExpireWithRenewTime); |
| 221 | + assertThrows(RuntimeException.class, optsExpireWithRenewTime::checkArgs); |
| 222 | + |
| 223 | + String[] argsExpireWithOwner = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--expire", "--hmac", "test-hmac", "--expiry-time-period", "604800000", "--owner-principal", "User:owner"}; |
| 224 | + DelegationTokenCommand.DelegationTokenCommandOptions optsExpireWithOwner = new DelegationTokenCommand.DelegationTokenCommandOptions(argsExpireWithOwner); |
| 225 | + assertThrows(RuntimeException.class, optsExpireWithOwner::checkArgs); |
| 226 | + |
| 227 | + String[] argsDescribeWithRenewTime = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--describe", "--renew-time-period", "604800000"}; |
| 228 | + DelegationTokenCommand.DelegationTokenCommandOptions optsDescribeWithRenewTime = new DelegationTokenCommand.DelegationTokenCommandOptions(argsDescribeWithRenewTime); |
| 229 | + assertThrows(RuntimeException.class, optsDescribeWithRenewTime::checkArgs); |
| 230 | + |
| 231 | + String[] argsDescribeWithExpiryTime = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--describe", "--expiry-time-period", "604800000"}; |
| 232 | + DelegationTokenCommand.DelegationTokenCommandOptions optsDescribeWithExpiryTime = new DelegationTokenCommand.DelegationTokenCommandOptions(argsDescribeWithExpiryTime); |
| 233 | + assertThrows(RuntimeException.class, optsDescribeWithExpiryTime::checkArgs); |
| 234 | + |
| 235 | + String[] argsDescribeWithMaxLife = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--describe", "--max-life-time-period", "604800000"}; |
| 236 | + DelegationTokenCommand.DelegationTokenCommandOptions optsDescribeWithMaxLife = new DelegationTokenCommand.DelegationTokenCommandOptions(argsDescribeWithMaxLife); |
| 237 | + assertThrows(RuntimeException.class, optsDescribeWithMaxLife::checkArgs); |
| 238 | + |
| 239 | + String[] argsDescribeWithHmac = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--describe", "--hmac", "test-hmac"}; |
| 240 | + DelegationTokenCommand.DelegationTokenCommandOptions optsDescribeWithHmac = new DelegationTokenCommand.DelegationTokenCommandOptions(argsDescribeWithHmac); |
| 241 | + assertThrows(RuntimeException.class, optsDescribeWithHmac::checkArgs); |
| 242 | + } finally { |
| 243 | + Exit.resetExitProcedure(); |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + @Test |
| 248 | + public void testCheckArgsValidOperations() { |
| 249 | + String[] argsCreate = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--create", "--max-life-time-period", "604800000"}; |
| 250 | + DelegationTokenCommand.DelegationTokenCommandOptions optsCreate = new DelegationTokenCommand.DelegationTokenCommandOptions(argsCreate); |
| 251 | + optsCreate.checkArgs(); |
| 252 | + |
| 253 | + String[] argsRenew = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--renew", "--hmac", "test-hmac", "--renew-time-period", "604800000"}; |
| 254 | + DelegationTokenCommand.DelegationTokenCommandOptions optsRenew = new DelegationTokenCommand.DelegationTokenCommandOptions(argsRenew); |
| 255 | + optsRenew.checkArgs(); |
| 256 | + |
| 257 | + String[] argsExpire = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--expire", "--hmac", "test-hmac", "--expiry-time-period", "604800000"}; |
| 258 | + DelegationTokenCommand.DelegationTokenCommandOptions optsExpire = new DelegationTokenCommand.DelegationTokenCommandOptions(argsExpire); |
| 259 | + optsExpire.checkArgs(); |
| 260 | + |
| 261 | + String[] argsDescribe = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--describe"}; |
| 262 | + DelegationTokenCommand.DelegationTokenCommandOptions optsDescribe = new DelegationTokenCommand.DelegationTokenCommandOptions(argsDescribe); |
| 263 | + optsDescribe.checkArgs(); |
| 264 | + } |
112 | 265 | }
|
0 commit comments