Skip to content

Commit

Permalink
timescaledb bats test working
Browse files Browse the repository at this point in the history
  • Loading branch information
wagmarcel committed Dec 19, 2024
1 parent facf5e5 commit abeaf7d
Showing 1 changed file with 78 additions and 17 deletions.
95 changes: 78 additions & 17 deletions test/bats/test-timescaledb-bridge/timescaledb-bridge-test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ POSTGRES_POD=acid-cluster-0
POSTGRES_USERNAME=ngb
POSTGRES_DATABASE=tsdb
POSTGRES_SECRET=ngb.acid-cluster.credentials.postgresql.acid.zalan.do
ENTITY=/tmp/entity.txt
ATTRIBUTES_PROPERTY=/tmp/property.txt
ATTRIBUTES_PROPERTY2=/tmp/property2.txt
ATTRIBUTES_RELATIONSHIP=/tmp/relationship.txt
ATTRIBUTES_TOPIC=iff.ngsild.attributes
ENTITIES_TOPIC=iff.ngsild.entities
KAFKA_BOOTSTRAP=my-cluster-kafka-bootstrap:9092
RANDOMID=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 24; echo)
URN=urn:iff:test1:${RANDOMID}
Expand All @@ -39,9 +41,10 @@ cat << EOF > ${ATTRIBUTES_PROPERTY}
"entityId": "${URN}",
"name": "${STATE}",
"type": "${PROPERTY}",
"https://uri.etsi.org/ngsi-ld/hasValue": "${VALUE}",
"attributeValue": "${VALUE}",
"nodeType": "@id",
"index": 0}
"datasetId": "@none"
}
EOF

cat << EOF > ${ATTRIBUTES_PROPERTY2}
Expand All @@ -50,7 +53,7 @@ cat << EOF > ${ATTRIBUTES_PROPERTY2}
"entityId": "${URN4}",
"name": "${STATE}",
"type": "${PROPERTY}",
"https://uri.etsi.org/ngsi-ld/hasValue": "${VALUE2}",
"attributeValue": "${VALUE2}",
"nodeType": "@value",
"valueType": "http://www.w3.org/2001/XMLSchema#string",
"index": 0
Expand All @@ -63,25 +66,32 @@ cat << EOF > ${ATTRIBUTES_RELATIONSHIP}
"entityId": "${URN2}",
"name": "${REL}",
"type": "${RELATIONSHIP}",
"https://uri.etsi.org/ngsi-ld/hasObject": "${URN3}",
"attributeValue": "${URN3}",
"nodeType": "@id",
"index": 0
}
EOF

cat << EOF > ${TSDB_EXPECTED}
cat << EOF > ${ENTITY}
{
"id": "${IDSTATE}",
"entityId": "${URN}",
"name": "${STATE}",
"type": "${PROPERTY}",
"https://uri.etsi.org/ngsi-ld/hasValue": "${VALUE2}",
"nodeType": "@value",
"valueType": "http://www.w3.org/2001/XMLSchema#string",
"index": 0
"id": "${URN}",
"type": "https://example.com/type"
}
EOF

# cat << EOF > ${TSDB_EXPECTED}
# {
# "id": "${IDSTATE}",
# "entityId": "${URN}",
# "name": "${STATE}",
# "type": "${PROPERTY}",
# "https://uri.etsi.org/ngsi-ld/hasValue": "${VALUE2}",
# "nodeType": "@value",
# "valueType": "http://www.w3.org/2001/XMLSchema#string",
# "index": 0
# }
# EOF

# send data to kafka bridge
# $1: file to send
# $2: kafka topic
Expand All @@ -94,12 +104,22 @@ send_to_kafka_bridge() {
get_datapoints() {
urn=$1
targetfile=$2
echo 'select json_agg(t) from public.entityhistory as t where t."entityId" = ' \'"$urn"\' ';' | \
echo 'select json_agg(t) from public.attributes as t where t."entityId" = ' \'"$urn"\' ';' | \
PGPASSWORD=${POSTGRES_PASSWORD} psql -t -h localhost -U ${POSTGRES_USERNAME} -d ${POSTGRES_DATABASE} -A | \
jq -S 'map(del(.modifiedAt, .observedAt))' >"$targetfile"
}


# receive entity datapoints
# $1: payload to post
get_entity_datapoints() {
urn=$1
targetfile=$2
echo 'select json_agg(t) from public.entities as t where t."id" = ' \'"$urn"\' ';' | \
PGPASSWORD=${POSTGRES_PASSWORD} psql -t -h localhost -U ${POSTGRES_USERNAME} -d ${POSTGRES_DATABASE} -A | \
jq -S 'map(del(.modifiedAt, .observedAt))' >"$targetfile"
}

# check s
# $1: retrieved tsdb objects
# $2: expected tsdb objects
Expand All @@ -111,9 +131,13 @@ check_tsdb_sample1() {
"attributeId": "https://industry-fusion.com/types/v0.9/state",
"attributeType": "https://uri.etsi.org/ngsi-ld/Property",
"datasetId": "@none",
"deleted": false,
"entityId": "$URN",
"index": 0,
"id": "$IDSTATE",
"lang": null,
"nodeType": "@id",
"parentId": null,
"unitType": null,
"value": "state",
"valueType": null
}
Expand All @@ -132,9 +156,13 @@ check_tsdb_sample2() {
"attributeId": "https://industry-fusion.com/types/v0.9/relationship",
"attributeType": "https://uri.etsi.org/ngsi-ld/Relationship",
"datasetId": "@none",
"deleted": false,
"entityId": "$URN",
"index": 0,
"id": "$IDREL",
"lang": null,
"nodeType": "@id",
"parentId": null,
"unitType": null,
"value": "$URN3",
"valueType": null
}
Expand All @@ -150,16 +178,37 @@ check_tsdb_sample3() {
"attributeId": "https://industry-fusion.com/types/v0.9/state",
"attributeType": "https://uri.etsi.org/ngsi-ld/Property",
"datasetId": "@none",
"deleted": false,
"entityId": "$URN4",
"index": 0,
"id": "$IDSTATE2",
"lang": null,
"nodeType": "@value",
"parentId": null,
"unitType": null,
"value": "$VALUE2",
"valueType": "http://www.w3.org/2001/XMLSchema#string"
}
]
EOF
}


# check entity
# $1: retrieved tsdb objects
# $2: expected tsdb objects
check_tsdb_sample4() {
URN=$2
cat << EOF | diff "$1" - >&3
[
{
"deleted": false,
"id": "$URN",
"type": "https://example.com/type"
}
]
EOF
}

setup() {
# shellcheck disable=SC2086
[ "$DEBUG" = "true" ] || (exec ${SUDO} kubefwd -n ${IFFNAMESPACE} -l "app.kubernetes.io/name in (kafka)" svc) &
Expand Down Expand Up @@ -207,4 +256,16 @@ teardown(){
echo "# Now checking result."
run check_tsdb_sample3 "$TSDB_RESULT" "$URN4"
[ "$status" -eq "0" ]
}

@test "verify timescaledb-bridge is forwarding entity" {
$SKIP
echo "# Sending property to Kafka"
send_to_kafka_bridge ${ENTITY} ${ENTITIES_TOPIC}
sleep 2
get_entity_datapoints "$URN" "${TSDB_RESULT}"
cat "${TSDB_RESULT}"
echo "# Now checking result."
run check_tsdb_sample4 "$TSDB_RESULT" "$URN"
[ "$status" -eq "0" ]
}

0 comments on commit abeaf7d

Please sign in to comment.