Skip to content

Commit

Permalink
change to referential integrity
Browse files Browse the repository at this point in the history
  • Loading branch information
ajbalogh committed Nov 12, 2024
1 parent 9699138 commit a07ffb3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
19 changes: 16 additions & 3 deletions protos/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,30 @@ message ValidationRequest {

message ValidationError {
oneof type {
// optional fields must be set to a value
string optional = 1;

// oneof fields must have one field set
string oneof = 2;

// map keys must match a name field in the object
string map = 3;
string repeated = 4;
string connection = 5;

// referential integrity check failed
// the following cases fall under this type:
// connection structure is incorrectly formatted
// connection pieces are not present in the inventory or device instances
// binding infrastructure path is incorrectly formatted
// binding infrastructure path pieces are not present in the infrastructure
// inventory
string referential_integrity = 4;
}
}

message ValidationResponse {
repeated ValidationError errors = 1;
repeated ValidationError warnings = 2;
repeated string warnings = 2;
repeated string info = 3;
}

service Service {
Expand Down
14 changes: 9 additions & 5 deletions src/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,36 @@ def _validate_component_connection(self, device, connection: str):
except ValueError:
self._validation_response.errors.append(
ValidationError(
connection=f"Connection:{connection} in device:{device.name} is incorrectly formatted"
referential_integrity=f"Infrastructure.devices[{device.name}].connections:[{connection}] is incorrectly formatted"
)
)

def _validate_component(self, device, name, index):
if name not in device.components:
self._validation_response.errors.append(
ValidationError(connection=f"Component:{name} not present in device:{device.name}")
ValidationError(
referential_integrity=f"Infrastructure.devices[{device.name}].components[{name}] does not exist"
)
)
try:
index = int(index)
if index < 0 or index > device.components[name].count - 1:
self._validation_response.errors.append(
ValidationError(
connection=f"Component:{name} index:{index} must be >= 0 and <{device.components[name].count}"
referential_integrity=f"Component:{name} index:{index} must be >= 0 and <{device.components[name].count}"
)
)
except ValueError:
self._validation_response.errors.append(
ValidationError(connection=f"Index:{index} must be a valid integer")
ValidationError(referential_integrity=f"Index:{index} must be a valid integer")
)

def _validate_link_name(self, device, name: str):
if name not in device.links:
self._validation_response.errors.append(
ValidationError(connection=f"{device.name} does not contain a link:{name}")
ValidationError(
referential_integrity=f"Infrastructure.devices[{device.name}].links[{name}] does not exist"
)
)

def _validate_oneof(self, object, name):
Expand Down
9 changes: 5 additions & 4 deletions src/tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,25 @@ def test_missing_bandwidth():
assert response.errors[0].WhichOneof("type") == "oneof"


def test_invalid_component_connection():
"""Test that a component connection is valid"""
def test_referential_integrity():
"""Referential integrity tests"""
device = Device(name="host")
mii = Link(name="mii", type=LinkType.LINK_CUSTOM, bandwidth=Bandwidth(gbps=100))
device.links[mii.name].CopyFrom(mii)
asic = Component(name="asic", count=1)
nic = Component(name="nic", count=1)
device.components[asic.name].CopyFrom(asic)
device.components[nic.name].CopyFrom(nic)
device.connections.append(f"bad.device.component.connection")
device.connections.append(f"{asic.name}.x.{mii.name}.null.-1")
inventory = Inventory()
inventory.devices[device.name].CopyFrom(device)
infrastructure = Infrastructure(inventory=inventory)
request = ValidationRequest(infrastructure=infrastructure)
response = Service().validate(request=request)
print(response)
assert len(response.errors) == 3
assert response.errors[0].WhichOneof("type") == "connection"
for error in response.errors:
assert error.WhichOneof("type") == "referential_integrity"


if __name__ == "__main__":
Expand Down

0 comments on commit a07ffb3

Please sign in to comment.