Skip to content

Commit

Permalink
Added test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-variacode committed Oct 21, 2024
1 parent fc24fc5 commit 7c8bca3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -724,40 +724,43 @@ public void ansibleInventoryList(NodeSetImpl nodes, AnsibleRunner.AnsibleRunnerB
}

Map<String, Object> all = InventoryList.getValue(allInventory, ALL);
Map<String, Object> children = InventoryList.getValue(all, CHILDREN);

for (Map.Entry<String, Object> pair : children.entrySet()) {
String hostGroup = pair.getKey();
Map<String, Object> hostNames = InventoryList.getType(pair.getValue());
Map<String, Object> hosts = InventoryList.getValue(hostNames, HOSTS);

if (null == hosts) {
throw new ResourceModelSourceException("Cannot find hosts for: " + hostGroup);
}
if (all != null) {
Map<String, Object> children = InventoryList.getValue(all, CHILDREN);

if (children != null) {
for (Map.Entry<String, Object> pair : children.entrySet()) {
String hostGroup = pair.getKey();
Map<String, Object> hostNames = InventoryList.getType(pair.getValue());
Map<String, Object> hosts = InventoryList.getValue(hostNames, HOSTS);

if (hosts != null) {
for (Map.Entry<String, Object> hostNode : hosts.entrySet()) {
NodeEntryImpl node = new NodeEntryImpl();
node.setTags(Set.of(hostGroup));
String hostName = hostNode.getKey();
node.setHostname(hostName);
node.setNodename(hostName);
Map<String, Object> nodeValues = InventoryList.getType(hostNode.getValue());

InventoryList.tagHandle(NodeTag.HOSTNAME, node, nodeValues);
InventoryList.tagHandle(NodeTag.USERNAME, node, nodeValues);
InventoryList.tagHandle(NodeTag.OS_FAMILY, node, nodeValues);
InventoryList.tagHandle(NodeTag.OS_NAME, node, nodeValues);
InventoryList.tagHandle(NodeTag.OS_ARCHITECTURE, node, nodeValues);
InventoryList.tagHandle(NodeTag.OS_VERSION, node, nodeValues);
InventoryList.tagHandle(NodeTag.DESCRIPTION, node, nodeValues);

nodeValues.forEach((key, value) -> {
if (value != null) {
node.setAttribute(key, value.toString());
}
});

for (Map.Entry<String, Object> hostNode : hosts.entrySet()) {
NodeEntryImpl node = new NodeEntryImpl();
node.setTags(Set.of(hostGroup));
String hostName = hostNode.getKey();
node.setHostname(hostName);
node.setNodename(hostName);
Map<String, Object> nodeValues = InventoryList.getType(hostNode.getValue());

InventoryList.tagHandle(NodeTag.HOSTNAME, node, nodeValues);
InventoryList.tagHandle(NodeTag.USERNAME, node, nodeValues);
InventoryList.tagHandle(NodeTag.OS_FAMILY, node, nodeValues);
InventoryList.tagHandle(NodeTag.OS_NAME, node, nodeValues);
InventoryList.tagHandle(NodeTag.OS_ARCHITECTURE, node, nodeValues);
InventoryList.tagHandle(NodeTag.OS_VERSION, node, nodeValues);
InventoryList.tagHandle(NodeTag.DESCRIPTION, node, nodeValues);

nodeValues.forEach((key, value) -> {
if (value != null) {
node.setAttribute(key, value.toString());
nodes.putNode(node);
}
}
});

nodes.putNode(node);
}
}
}
}
Expand Down Expand Up @@ -867,7 +870,7 @@ public List<String> listSecretsPathResourceModel(Map<String, Object> configurati
*/
public void validateAliases(String content) {
Map<String, Integer> checkMap = YamlUtil.checkAliasesAndAnchors(content);
if (!checkMap.isEmpty()) {
if (!checkMap.isEmpty() && checkMap.get(YamlUtil.ALIASES) > yamlMaxAliases) {
log.warn("The yaml inventory received has {} aliases and the maximum allowed is {}.", checkMap.get(YamlUtil.ALIASES), yamlMaxAliases);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,8 @@ class AnsibleResourceModelSourceSpec extends Specification {
plugin.ansibleInventoryListBuilder = inventoryListBuilder
plugin.getNodes()

then: "throws an exception because host tag is null"
ResourceModelSourceException exception = thrown()
exception.message.contains("Cannot find hosts for")
then: "not exception because all, children and host tags are null"
notThrown(Exception)
}

private AnsibleInventoryListBuilder mockInventoryList(int qtyNodes, boolean aliases = false) {
Expand Down

0 comments on commit 7c8bca3

Please sign in to comment.