vmware / build-tools-for-vmware-aria

Build Tools for VMware Aria provides development and release management tools for implementing automation solutions based on the VMware Aria Suite and VMware Cloud Director. The solution enables Virtual Infrastructure Administrators and Automation Developers to use standard DevOps practices for managing and deploying content.
Other
47 stars 22 forks source link

Mocking for configuration elements is incorrect #110

Closed pe1pip closed 1 year ago

pe1pip commented 1 year ago

Description

The mocking for configuration elements is incorrect. When there are no attributes in a config element vRO returns null when the attributes of a config element is accessed, in vRBT the mocking returns an empty array. Similarly, the Server.getConfigurationElementCategoryWithPath() should return null for a non-existent category and category.configurationElements should return null for an empty category.

Note that behaviour of category.allConfigurationElements is different, this returns an empty array if there are zero elements in a category.

Steps to Reproduce

  1. inspect this code in 'typescript/vro-scripting-api/src/configurations.ts':

    function getElementAttributes(categoryPath: string, elementName: string): Attribute[] {
        const categoryDescriptor = findDescriptorByPath(categoryPath);
        if (!categoryDescriptor) {
            return [];
        }
        const elementDescriptor = categoryDescriptor.elements[elementName];
        if (!elementDescriptor || !elementDescriptor.path) {
            return [];
        }
        return parseJsonFile<AttributeDescriptor[]>(elementDescriptor.path).map(attrInfo => {
            const attr = new Attribute();
            attr.name = attrInfo.name;
            attr.description = attrInfo.description;
            attr.type = attrInfo.type;
            attr.value = attrInfo.value != null ? convertAttrValue(attrInfo.type, attrInfo.value) : null;
            return attr;
        });
    }
  2. in vRO run:

    const c = Server.createConfigurationElement('dummy', 'dummy')
    System.log(typeof c.attributes)
    System.log(c.attributes)
    System.log(c.attributes === null)

Output:

2023-05-29 17:06:32.080 +02:00INFO(nl.rabobank.test/remco) object
2023-05-29 17:06:32.081 +02:00INFO(nl.rabobank.test/remco) null
2023-05-29 17:06:32.082 +02:00INFO(nl.rabobank.test/remco) true

Preconditions: [What are the preconditions to reproduce the issue]

Expected behavior:

mocking to return null when no attributes are present in a configuration element.

Actual behavior:

Mocking returns []

Reproduces how often: 100%

Component/s:

Affects Build/s: 2.32.0

Environment

Client

Server

Failure Logs

Related issues and PRs

n/a

Additional Context

n/a

Michaelpalacce commented 1 year ago

Should be fixed

pe1pip commented 1 year ago

@Michaelpalacce it seems you actually broke more than you fixed. Following test (demo.test.ts) works as expected in 2.33.0, but throws in 2.34.0:

function getConfigurationElementByPath (path: string, name: string): ConfigurationElement {
  const configurationElementCategory = Server.getConfigurationElementCategoryWithPath(path)
  const configurationElements = configurationElementCategory.allConfigurationElements

  const configurationElement = configurationElements.filter(function (configurationElement) {
    return configurationElement.name === name
  })

  if (configurationElement.length !== 1) {
    throw new Error('too many or zero matches')
  }
  return configurationElement[0]
}

function demo (): string {
  const environment = getConfigurationElementByPath('web-root', 'Environment')
  return `demo${environment.getAttributeWithKey('demo').value as string}`
}

describe('demo test', () => {
  beforeEach(() => {
    const envCfg = Server.createConfigurationElement('web-root', 'Environment')
    envCfg.setAttributeWithKey('demo', 'Mock', 'string')
  })
  afterEach(() => {
    Server.removeConfigurationElementCategory(Server.getConfigurationElementCategoryWithPath('web-root')) // cleanup all cfgElements
  })
  it('x', () => {
    expect(demo()).toBe('demoMock')
  })
})

expected behaviour is environment.getAttributeWithKey('demo').value to equal 'Mock', but environment.getAttributeWithKey() returns null, even if the attribute exists.