Skip to content

Commit 4d096ea

Browse files
committed
CLOUDSTACK-8537 test for the sake of testing the fix seems so trivial but no testing is available for it at all. when bugs arise test extension should be the start point here.
Signed-off-by: Daan Hoogland <daan.hoogland@gmail.com> This closes apache#357
1 parent 6e3c6e8 commit 4d096ea

2 files changed

Lines changed: 71 additions & 4 deletions

File tree

‎server/src/com/cloud/server/ManagementServerImpl.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe
745745
@Inject
746746
private InstanceGroupDao _vmGroupDao;
747747
@Inject
748-
private SSHKeyPairDao _sshKeyPairDao;
748+
protected SSHKeyPairDao _sshKeyPairDao;
749749
@Inject
750750
private LoadBalancerDao _loadbalancerDao;
751751
@Inject
@@ -3645,7 +3645,7 @@ private void checkForKeyByPublicKey(RegisterSSHKeyPairCmd cmd, Account owner) th
36453645
* @param owner
36463646
* @throws InvalidParameterValueException
36473647
*/
3648-
private void checkForKeyByName(RegisterSSHKeyPairCmd cmd, Account owner) throws InvalidParameterValueException {
3648+
protected void checkForKeyByName(RegisterSSHKeyPairCmd cmd, Account owner) throws InvalidParameterValueException {
36493649
SSHKeyPairVO existingPair = _sshKeyPairDao.findByName(owner.getAccountId(), owner.getDomainId(), cmd.getName());
36503650
if (existingPair != null) {
36513651
throw new InvalidParameterValueException("A key pair with name '" + cmd.getName() + "' already exists for this account.");
@@ -3679,7 +3679,7 @@ private String getPublicKeyFromKeyKeyMaterial(String key) throws InvalidParamete
36793679
* @param cmd
36803680
* @return
36813681
*/
3682-
private Account getOwner(RegisterSSHKeyPairCmd cmd) {
3682+
protected Account getOwner(RegisterSSHKeyPairCmd cmd) {
36833683
Account caller = getCaller();
36843684

36853685
Account owner = _accountMgr.finalizeOwner(caller, cmd.getAccountName(), cmd.getDomainId(), cmd.getProjectId());
@@ -3689,7 +3689,7 @@ private Account getOwner(RegisterSSHKeyPairCmd cmd) {
36893689
/**
36903690
* @return
36913691
*/
3692-
private Account getCaller() {
3692+
protected Account getCaller() {
36933693
Account caller = CallContext.current().getCallingAccount();
36943694
return caller;
36953695
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.server;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.mockito.Mock;
22+
import org.mockito.Mockito;
23+
import org.mockito.Spy;
24+
import org.mockito.runners.MockitoJUnitRunner;
25+
26+
import org.apache.cloudstack.api.command.user.ssh.RegisterSSHKeyPairCmd;
27+
28+
import com.cloud.exception.InvalidParameterValueException;
29+
import com.cloud.user.Account;
30+
import com.cloud.user.SSHKeyPairVO;
31+
import com.cloud.user.dao.SSHKeyPairDao;
32+
33+
@RunWith(MockitoJUnitRunner.class)
34+
public class ManagementServerImplTest {
35+
36+
@Mock
37+
RegisterSSHKeyPairCmd regCmd;
38+
@Mock
39+
SSHKeyPairVO existingPair;
40+
@Mock
41+
Account account;
42+
@Mock
43+
SSHKeyPairDao sshKeyPairDao;
44+
ManagementServerImpl ms = new ManagementServerImpl();
45+
@Spy
46+
ManagementServerImpl spy;
47+
48+
@Test(expected = InvalidParameterValueException.class)
49+
public void testExistingPairRegistration() {
50+
String accountName = "account";
51+
String publicKeyString = "very public";
52+
// setup owner with domainid
53+
Mockito.doReturn(account).when(spy).getCaller();
54+
Mockito.doReturn(account).when(spy).getOwner(regCmd);
55+
// mock _sshKeyPairDao.findByName to return null
56+
Mockito.doNothing().when(spy).checkForKeyByName(regCmd, account);
57+
// mock _sshKeyPairDao.findByPublicKey to return a known object
58+
Mockito.doReturn(accountName).when(regCmd).getAccountName();
59+
Mockito.doReturn(publicKeyString).when(regCmd).getPublicKey();
60+
Mockito.doReturn("name").when(regCmd).getName();
61+
spy._sshKeyPairDao = sshKeyPairDao;
62+
Mockito.doReturn(1L).when(account).getAccountId();
63+
Mockito.doReturn(1L).when(account).getDomainId();
64+
Mockito.doReturn(existingPair).when(sshKeyPairDao).findByPublicKey(1L, 1L, publicKeyString);
65+
spy.registerSSHKeyPair(regCmd);
66+
}
67+
}

0 commit comments

Comments
 (0)