Skip to content

Commit

Permalink
Added spok test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-variacode committed Oct 23, 2024
1 parent 5b2bd5e commit af523bc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -712,16 +712,16 @@ public void ansibleInventoryList(NodeSetImpl nodes, AnsibleRunner.AnsibleRunnerB

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

if (isTagMapEmpty(all, ALL)) {
if (isTagMapValid(all, ALL)) {
Map<String, Object> children = InventoryList.getValue(all, CHILDREN);

if (isTagMapEmpty(children, CHILDREN)) {
if (isTagMapValid(children, 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 (isTagMapEmpty(hosts, HOSTS)) {
if (isTagMapValid(hosts, HOSTS)) {
for (Map.Entry<String, Object> hostNode : hosts.entrySet()) {
NodeEntryImpl node = new NodeEntryImpl();
node.setTags(Set.of(hostGroup));
Expand Down Expand Up @@ -858,7 +858,7 @@ public List<String> listSecretsPathResourceModel(Map<String, Object> configurati
* @param tagName The name of the tag to validate.
* @return True if the tag is empty, false otherwise.
*/
private boolean isTagMapEmpty(Map<String, Object> tagMap, String tagName) {
private boolean isTagMapValid(Map<String, Object> tagMap, String tagName) {
if (tagMap == null) {
log.warn("Tag '{}' is empty!", tagName);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,50 @@ class AnsibleResourceModelSourceSpec extends Specification {
thrown(ResourceModelSourceException)
}

private AnsibleInventoryListBuilder mockInventoryList(int qtyNodes) {
void "tag hosts is empty"() {
given:
Framework framework = Mock(Framework) {
getBaseDir() >> Mock(File) {
getAbsolutePath() >> '/tmp'
}
}
ResourceModelSource plugin = new AnsibleResourceModelSource(framework)
Properties config = new Properties()
config.put('project', 'project_1')
config.put(AnsibleDescribable.ANSIBLE_GATHER_FACTS, 'false')
plugin.configure(config)
Services services = Mock(Services) {
getService(KeyStorageTree.class) >> Mock(KeyStorageTree)
}
plugin.setServices(services)
plugin.yamlDataSize = 2

when: "inventory with null hosts"
AnsibleInventoryListBuilder inventoryListBuilder = mockInventoryList(1, true)
plugin.ansibleInventoryListBuilder = inventoryListBuilder
plugin.getNodes()

then: "no exception thrown due to null tag is handled"
notThrown(Exception)
}

private AnsibleInventoryListBuilder mockInventoryList(int qtyNodes, boolean nullHosts = false) {
return Mock(AnsibleInventoryListBuilder) {
build() >> Mock(AnsibleInventoryList) {
getNodeList() >> createNodes(qtyNodes)
getNodeList() >> createNodes(qtyNodes, nullHosts)
}
}
}

private static String createNodes(int qty) {
private static String createNodes(int qty, boolean nullHosts) {
Yaml yaml = new Yaml()
Map<String, Object> host = [:]
for (int i=1; i <= qty; i++) {
String nodeName = "node-$i"
String hostValue = "any-name-$i"
host.put((nodeName), ['hostname' : (hostValue)])
}
def hosts = ['hosts' : host]
def hosts = ['hosts' : nullHosts ? null : host]
def groups = ['ungrouped' : hosts]
def children = ['children' : groups]
def all = ['all' : children]
Expand Down

0 comments on commit af523bc

Please sign in to comment.