Skip to content

Commit

Permalink
Merge pull request #7738 from uyuni-project/master-fix/xml-rpc-create…
Browse files Browse the repository at this point in the history
…SystemRecord

Fix persisting the Cobbler UID in the database via XML-RPC createSystemRecord (bsc#1207532)
  • Loading branch information
meaksh authored Nov 22, 2023
2 parents 84ced3d + d32425a commit 6da81f2
Show file tree
Hide file tree
Showing 12 changed files with 531 additions and 267 deletions.
5 changes: 3 additions & 2 deletions java/Makefile.docker
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#

# Docker tests variables
EXECUTOR = docker
DOCKER_CONTAINER_BASE = systemsmanagement/uyuni/master/docker/containers/uyuni-master
DOCKER_REGISTRY = registry.opensuse.org
DOCKER_VOLUMES = -v "$(CURDIR)/../:/manager"
Expand All @@ -14,8 +15,8 @@ DOCKER_ENV =
all :: dockerrun_pg

dockerpull ::
docker pull $(DOCKER_REGISTRY)/$(DOCKER_CONTAINER_BASE)-pgsql-4eclipse
$(EXECUTOR) pull $(DOCKER_REGISTRY)/$(DOCKER_CONTAINER_BASE)-pgsql-4eclipse

dockerrun_pg ::
cp buildconf/test/rhn.conf.postgresql-example buildconf/test/rhn.conf
docker run $(DOCKER_COMMON_OPTS) $(DOCKER_PG_PORTS) $(DOCKER_VOLUMES) $(DOCKER_ENV) $(DOCKER_REGISTRY)/$(DOCKER_CONTAINER_BASE)-pgsql-4eclipse
$(EXECUTOR) run $(DOCKER_COMMON_OPTS) $(DOCKER_PG_PORTS) $(DOCKER_VOLUMES) $(DOCKER_ENV) $(DOCKER_REGISTRY)/$(DOCKER_CONTAINER_BASE)-pgsql-4eclipse
2 changes: 1 addition & 1 deletion java/buildconf/ivy/ivy-suse.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
<dependency org="suse" name="junit" rev="4.13.2" transitive="false"/>
<dependency org="org.junit.jupiter" name="junit-jupiter-api" rev="5.8.2" transitive="false"/>
<dependency org="org.junit.platform" name="junit-platform-commons" rev="1.8.2" transitive="false" />
<dependency org="org.junit.platform" name="junit-platform-console-standalone" rev="1.8.2" transitive="false" />
<dependency org="org.junit.platform" name="junit-platform-console-standalone" rev="1.8.2" transitive="false" />
<dependency org="org.opentest4j" name="opentest4j" rev="1.2.0" transitive="false"/>
<dependency org="org.apiguardian" name="apiguardian-api" rev="1.1.0" transitive="false"/>
<dependency org="org.jmock" name="jmock" rev="2.12.0" transitive="false"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,7 @@ public String getDefaultVirtBridge() {
* taskomatic.
* @return the Profile associated to this ks data
*/

public Profile getCobblerObject(User user) {
if (StringUtils.isBlank(getCobblerId())) {
return null;
Expand Down
4 changes: 4 additions & 0 deletions java/code/src/com/redhat/rhn/domain/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,10 @@ public void setSecret(String secretIn) {
}

/**
* This may be null in some cases:
* - If a server was bootstrapped with Salt and the key was accepted manually via "salt-key".
* - If a server was created by a user and the user was later on deleted.
*
* @return Returns the creator.
*/
public User getCreator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6585,6 +6585,10 @@ private KickstartData lookupKsData(String label, Org org) {
* @apidoc.returntype #return_int_success()
*/
public int createSystemRecord(User loggedInUser, Integer sid, String ksLabel) {
if (loggedInUser == null) {
throw new FaultException(-2, "loggedInUserNull", "The logged in user was null!");
}

Server server = null;
try {
server = SystemManager.lookupByIdAndUser(sid.longValue(),
Expand All @@ -6600,9 +6604,7 @@ public int createSystemRecord(User loggedInUser, Integer sid, String ksLabel) {
}

KickstartData ksData = lookupKsData(ksLabel, loggedInUser.getOrg());
CobblerSystemCreateCommand cmd = new CobblerSystemCreateCommand(
loggedInUser, ksData.getCobblerObject(loggedInUser).getName(),
ksData, server.getName(), loggedInUser.getOrg().getId());
CobblerSystemCreateCommand cmd = new CobblerSystemCreateCommand(loggedInUser, ksData, server);
cmd.store();

return 1;
Expand Down Expand Up @@ -6637,6 +6639,10 @@ public int createSystemRecord(User loggedInUser, Integer sid, String ksLabel) {
*/
public int createSystemRecord(User loggedInUser, String systemName, String ksLabel,
String kOptions, String comment, List<Map<String, String>> netDevices) {
if (loggedInUser == null) {
throw new FaultException(-2, "loggedInUserNull", "The logged in user was null!");
}

// Determine the user and lookup the kickstart profile
KickstartData ksData = lookupKsData(ksLabel, loggedInUser.getOrg());

Expand All @@ -6646,9 +6652,18 @@ public int createSystemRecord(User loggedInUser, String systemName, String ksLab
server.setOrg(loggedInUser.getOrg());

// Create cobbler command
CobblerUnregisteredSystemCreateCommand cmd;
cmd = new CobblerUnregisteredSystemCreateCommand(loggedInUser, server,
ksData.getCobblerObject(loggedInUser).getName());
org.cobbler.Profile profile = ksData.getCobblerObject(loggedInUser);
if (profile == null) {
throw new FaultException(-2, "ksLabelProfileError", String.format(
"The profile for the ksLabel \"%s\" could not be found!",
ksData.getLabel()
));
}
CobblerUnregisteredSystemCreateCommand cmd = new CobblerUnregisteredSystemCreateCommand(
loggedInUser,
server,
profile.getName()
);

// Set network device information to the server
for (Map<String, String> map : netDevices) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
import com.redhat.rhn.manager.entitlement.EntitlementManager;
import com.redhat.rhn.manager.errata.cache.ErrataCacheManager;
import com.redhat.rhn.manager.formula.FormulaMonitoringManager;
import com.redhat.rhn.manager.kickstart.cobbler.CobblerXMLRPCHelper;
import com.redhat.rhn.manager.profile.ProfileManager;
import com.redhat.rhn.manager.rhnpackage.test.PackageManagerTest;
import com.redhat.rhn.manager.ssm.SsmOperationManager;
Expand Down Expand Up @@ -178,6 +179,7 @@
import com.suse.manager.xmlrpc.dto.SystemEventDetailsDto;

import org.apache.commons.lang3.StringUtils;
import org.cobbler.CobblerConnection;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.imposters.ByteBuddyClassImposteriser;
Expand Down Expand Up @@ -3195,19 +3197,76 @@ public void testCreateSystemProfileNoHwAddress() throws Exception {
}
}

@Test
public void testCreateSystemRecordRegistered() throws Exception {
// Arrange
CobblerConnection connection = CobblerXMLRPCHelper.getConnection(admin);
connection.invokeMethod("new_profile");
var profileId = ((LinkedList<HashMap>) connection.invokeMethod("get_profiles")).get(0).get("uid");
SystemHandler mockedHandler = getMockedHandler();
KickstartData k = KickstartDataTest.createTestKickstartData(admin.getOrg());
k.setCobblerId(profileId.toString());
int systemId = mockedHandler.createSystemProfile(
admin,
"test system",
Collections.singletonMap("hwAddress", "aa:bb:cc:dd:ee:01")
);

// Act
int result = mockedHandler.createSystemRecord(admin, systemId, k.getLabel());

// Assert
assertEquals(1, result);
}

@Test
public void testCreateSystemRecordUnregistered() throws Exception {
// Arrange
CobblerConnection connection = CobblerXMLRPCHelper.getConnection(admin);
connection.invokeMethod("new_profile");
var profileId = ((LinkedList<HashMap>) connection.invokeMethod("get_profiles")).get(0).get("uid");
SystemHandler mockedHandler = getMockedHandler();
String systemName = "test system";
KickstartData k = KickstartDataTest.createTestKickstartData(admin.getOrg());
k.setCobblerId(profileId.toString());

Map<String, String> netDevices = Map.of(
"name", "dev1",
"ip", "127.0.0.1",
"mac", "00:00:00:00",
"dnsname", "test.com"
);
// Act
int result = mockedHandler.createSystemRecord(
admin,
systemName,
k.getLabel(),
"",
"",
List.of(netDevices)
);

// Assert
assertEquals(1, result);
}

/**
* Tests creating a system profile.
* @throws Exception if anything goes wrong
*/
@Test
public void testCreateSystemProfile() throws Exception {
// Arrange
String hwAddress = "aa:bb:cc:dd:ee:00";

// Act
int result = getMockedHandler().createSystemProfile(admin, "test system",
Collections.singletonMap("hwAddress", hwAddress));

// Assert
List<NetworkInterface> nics = NetworkInterfaceFactory
.lookupNetworkInterfacesByHwAddress(hwAddress)
.collect(Collectors.toList());

assertEquals(1, nics.size());
Server server = nics.get(0).getServer();
assertEquals("test system", server.getName());
Expand Down
Loading

0 comments on commit 6da81f2

Please sign in to comment.