Skip to content
This repository was archived by the owner on Nov 24, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- t3c: bug fix to consider plugin config files for reloading remap.config
- t3c: Change syncds so that it only warns on package version mismatch.
- atstccfg: add ##REFETCH## support to regex_revalidate.config processing.
- Added a Traffic Monitor integration test framework.

### Fixed
- [#5690](https://github.com/apache/trafficcontrol/issues/5690) - Fixed github action for added/modified db migration file.
Expand Down
3 changes: 3 additions & 0 deletions traffic_monitor/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@
#
#TM binary
traffic_monitor
tests/_integration/traffic_monitor_integration_test
tools/testcaches/testcaches
tools/testto/testto
2 changes: 1 addition & 1 deletion traffic_monitor/build.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
Expand All @@ -14,5 +15,4 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#!/usr/bin/env bash
go build -ldflags "-X main.GitRevision=`git rev-parse HEAD` -X main.BuildTimestamp=`date +'%Y-%M-%dT%H:%M:%S'`"
11 changes: 11 additions & 0 deletions traffic_monitor/health/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ package health
*/

import (
"errors"
"fmt"
"strconv"
"sync"
"time"

Expand All @@ -33,6 +35,15 @@ func (t Time) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%d", time.Time(t).Unix())), nil
}

func (t *Time) UnmarshalJSON(data []byte) error {
unixTime, err := strconv.ParseInt(string(data), 10, 64)
if err != nil {
return errors.New("health.Time (" + string(data) + ") must be a unix epoch integer: " + err.Error())
}
*t = Time(time.Unix(unixTime, 0))
return nil
}

// Event represents an event change in aggregated data. For example, a cache being marked as unavailable.
type Event struct {
Time Time `json:"time"`
Expand Down
37 changes: 37 additions & 0 deletions traffic_monitor/tests/_integration/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# This is a very simple Dockerfile.
# All it does is install and start the Traffic Monitor, given a Traffic Ops to point it to.
# It doesn't do any of the complex things the Dockerfiles in infrastructure/docker or infrastructure/cdn-in-a-box do, like inserting itself into Traffic Ops.
# It is designed for a very simple use case, where the complex orchestration of other Traffic Control components is done elsewhere (or manually).

FROM centos:8
MAINTAINER dev@trafficcontrol.apache.org

RUN dnf install -y initscripts epel-release golang glibc jq git
ENV GOPATH=/go

COPY ./tests/_integration/ /tm
WORKDIR /tm

RUN go get -u github.com/apache/trafficcontrol/lib/go-log github.com/apache/trafficcontrol/lib/go-tc github.com/apache/trafficcontrol/traffic_monitor
COPY . ${GOPATH}/src/github.com/apache/trafficcontrol/traffic_monitor/

RUN go test -c -o /traffic_monitor_integration_test

CMD ./Dockerfile_run.sh
165 changes: 165 additions & 0 deletions traffic_monitor/tests/_integration/Dockerfile_run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# The following environment variables must be set (ordinarily by `docker run -e` arguments):
# TO_URI
# TO_USER
# TO_PASS
# CDN
# Check that env vars are set
envvars=(TESTTO_URI TESTTO_PORT TESTCACHES_URI TESTCACHES_PORT_START TM_URI)
for v in $envvars; do
if [[ -z ${!v} ]]; then
echo "$v is unset"
exit 1
fi
done

TO_API_VERSION=4.0
CFG_FILE=/tm/traffic-monitor-integration-test.cfg

start() {
printf "DEBUG traffic_monitor_integration starting\n"

exec /traffic_monitor_integration_test -test.v -cfg $CFG_FILE
}

init() {
wait_for_endpoint "${TESTTO_URI}/api/${TO_API_VERSION}/servers"
wait_for_endpoint "${TESTCACHES_URI}:${TESTCACHES_PORT_START}/_astats"
wait_for_endpoint "${TM_URI}"
TESTCACHES_ADDRESS=$(ping testcaches -4 -c 1 | head -n 1 | grep -Eo '[0-9]+.[0-9]+.[0-9]+.[0-9]+')
TESTCACHES_GATEWAY=$(echo $TESTCACHES_ADDRESS | sed "s/\([0-9]\+.[0-9]\+.[0-9]\+.\)[0-9]/\11/")

jq "(.. | .address?) |= \"$TESTCACHES_ADDRESS\" | (.. | .gateway?) |= \"$TESTCACHES_GATEWAY\"" \
/tm/monitoring.json > /tm/monitoring.json.tmp && mv /tm/monitoring.json.tmp /tm/monitoring.json

curl -Lvsk ${TESTTO_URI}/api/${TO_API_VERSION}/cdns/fake/snapshot -X POST -d "@/tm/snapshot.json"

curl -Lvsk ${TESTTO_URI}/api/${TO_API_VERSION}/cdns/fake/configs/monitoring -X POST -d '@/tm/monitoring.json'

curl -Lvsk ${TESTTO_URI}/api/${TO_API_VERSION}/servers -X POST -d '
[
{
"cachegroup": "foo",
"cachegroupId": 0,
"cdnId": 1,
"cdnName": "fake",
"deliveryServices": null,
"fqdn": "trafficmonitor.traffic-monitor-integration.test",
"guid": "foo",
"hostName": "trafficmonitor",
"httpsPort": null,
"id": 1,
"iloIpAddress": null,
"iloIpGateway": null,
"iloIpNetmask": null,
"iloPassword": null,
"iloUsername": null,
"interfaceMtu": null,
"interfaceName": "bond0",
"ip6Address": null,
"ip6Gateway": null,
"interfaces": [
{
"ipAddresses": [
{
"address": "4.0.16.239.6",
"gateway": "4.0.16.239.1",
"serviceAddress": true
},
{
"address": "fc01:9400:1000:8::6",
"gateway": "fc01:9400:1000:8::1",
"serviceAddress": true
}
],
"maxBandwidth": null,
"monitor": true,
"mtu": 1500,
"name": "eth0"
}
],
"ipGateway": "4.0.0.0.1",
"ipNetmask": "255.255.255.0",
"lastUpdated": "2019",
"mgmtIpAddress": null,
"mgmtIpGateway": null,
"mgmtIpNetmask": null,
"offlineReason": "none",
"physLocation": "",
"physLocationId": 0,
"profile": "Monitor0",
"profileDesc": "nodesc",
"profileId": 0,
"rack": "",
"revalPending": false,
"routerHostName": "",
"routerPortName": "",
"status": "REPORTED",
"statusId": 0,
"tcpPort": 80,
"type": "RASCAL",
"typeId": 0,
"updPending": false,
"xmppId": "",
"xmppPasswd": ""
}
]
'

cat >$CFG_FILE <<-EOF
{
"trafficMonitor": {
"url": "$TM_URI"
},
"default": {
"session": {
"timeoutInSecs": 30
},
"log": {
"debug": "stdout",
"event": "stdout",
"info": "stdout",
"error": "stdout",
"warning": "stdout"
}
}
}
EOF

echo "INITIALIZED=1" >>/etc/environment
}

function wait_for_endpoint() {
try=0
while [ $(curl -Lsk --write-out "%{http_code}" "$1" -o /dev/null) -ne 200 ] ; do
echo "Waiting for $1 to return a 200 OK"
try=$(expr $try + 1)
if [[ $try -gt 5 ]]; then
echo "Unable to get $1"
exit 1
fi
sleep 5
done
}
export -f wait_for_endpoint

source /etc/environment
if [ -z "$INITIALIZED" ]; then init; fi
start
9 changes: 9 additions & 0 deletions traffic_monitor/tests/_integration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Traffic Monitor Integration Test Framework

## Building

From this directory, run `build_tests.sh`. This will build the Traffic Monitor RPM as well as run a docker build.

## Running

From this directory, run `docker-compose run tmintegrationtest`
25 changes: 25 additions & 0 deletions traffic_monitor/tests/_integration/build_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cd ../../../
./pkg traffic_monitor_build
rpm=`ls dist | grep monitor | grep -v log | grep -v src | grep "$(git rev-parse --short=8 HEAD)"`
if [ $? -ne 0 ]; then
echo "Unable to build TM"
exit 1;
fi

cp "dist/$rpm" "traffic_monitor/tests/_integration/tm/traffic_monitor.rpm"
cd -
docker-compose build
Loading