SEBA-557 tox and delayering of testservice

Change-Id: I007c230f8862097e53de2f1092b520e139f64585
diff --git a/.dockerignore b/.dockerignore
index 53b1902..db90520 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -8,3 +8,5 @@
 lib/xos-config/.tox
 lib/xos-util/.tox
 lib/xos-genx/.tox
+testservice/.tox
+testservice/venv-service
diff --git a/testservice/Dockerfile.synchronizer b/testservice/Dockerfile.synchronizer
index f3a2d69..37131c5 100644
--- a/testservice/Dockerfile.synchronizer
+++ b/testservice/Dockerfile.synchronizer
@@ -12,14 +12,31 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# docker build -t smbaker/testservice-synchronizer:test -f Dockerfile.synchronizer .
+# docker build -t smbaker/testservice-synchronizer:test -f Dockerfile.synchronizer ..
 
 # xosproject/testservice
 
-FROM xosproject/xos-synchronizer-base:candidate
+FROM xosproject/alpine-grpc-base:0.9.0
 
-COPY xos/synchronizer /opt/xos/synchronizers/testservice
-COPY VERSION /opt/xos/synchronizers/testservice/
+# Add libraries
+COPY lib /opt/xos/lib
+COPY VERSION /opt/xos
+
+# Install non-xos pip packages
+COPY testservice/requirements.txt /tmp/requirements.txt
+RUN pip install -r /tmp/requirements.txt \
+ && pip freeze > /var/xos/pip_freeze_requirements_`date -u +%Y%m%dT%H%M%S`
+
+# Install xos packages using pip so their dependencies are installed
+RUN pip install -e /opt/xos/lib/xos-config \
+ && pip install -e /opt/xos/lib/xos-genx \
+ && pip install -e /opt/xos/lib/xos-kafka \
+ && pip install -e /opt/xos/lib/xos-api \
+ && pip install -e /opt/xos/lib/xos-synchronizer \
+ && pip freeze > /var/xos/pip_freeze_libraries_`date -u +%Y%m%dT%H%M%S`
+
+# Install the synchronizer
+COPY testservice/xos/synchronizer /opt/xos/synchronizers/testservice
 
 ENTRYPOINT []
 
@@ -54,5 +71,5 @@
       org.opencord.component.xos.vcs-url=$org_opencord_component_xos_vcs_url \
       org.opencord.component.xos.vcs-ref=$org_opencord_component_xos_vcs_ref
 
-#CMD ["/usr/bin/python", "/opt/xos/synchronizers/testservice/testservice-synchronizer.py"]
-CMD ["/bin/bash", "-c", "echo test && python /opt/xos/synchronizers/testservice/testservice-synchronizer.py"]
+CMD ["/usr/bin/python", "/opt/xos/synchronizers/testservice/testservice-synchronizer.py"]
+
diff --git a/testservice/Makefile b/testservice/Makefile
new file mode 100644
index 0000000..86eef39
--- /dev/null
+++ b/testservice/Makefile
@@ -0,0 +1,80 @@
+# Copyright 2019-present Open Networking Foundation
+#
+# 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.
+
+# Configure shell
+SHELL = bash -e -o pipefail
+
+# Variables
+VERSION                  ?= $(shell cat ../VERSION)
+SERVICE_NAME             ?= $(notdir $(abspath .))
+SYNCHRONIZER_NAME        ?= testservice-synchronizer
+
+## Docker related
+DOCKER_REGISTRY          ?=
+DOCKER_REPOSITORY        ?=
+DOCKER_BUILD_ARGS        ?=
+DOCKER_TAG               ?= ${VERSION}
+DOCKER_IMAGENAME         := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}${SYNCHRONIZER_NAME}:${DOCKER_TAG}
+
+## Docker labels. Only set ref and commit date if committed
+DOCKER_LABEL_VCS_URL     ?= $(shell git remote get-url $(shell git remote))
+DOCKER_LABEL_VCS_REF     ?= $(shell git diff-index --quiet HEAD -- && git rev-parse HEAD || echo "unknown")
+DOCKER_LABEL_COMMIT_DATE ?= $(shell git diff-index --quiet HEAD -- && git show -s --format=%cd --date=iso-strict HEAD || echo "unknown" )
+DOCKER_LABEL_BUILD_DATE  ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
+
+## Migration related - paths are relative to the xos subdirectory within this repo
+XOS_DIR                  ?= "../.."
+SERVICES_DIR             ?= "../.."
+
+all: test
+
+docker-build:
+	docker build $(DOCKER_BUILD_ARGS) \
+    -t ${DOCKER_IMAGENAME} \
+    --build-arg org_label_schema_version="${VERSION}" \
+    --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
+    --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
+    --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
+    --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
+    -f Dockerfile.synchronizer ..
+
+docker-push:
+	docker push ${DOCKER_IMAGENAME}
+
+test: test-unit test-migration
+
+test-unit:
+	tox
+
+venv-service:
+	virtualenv $@;\
+    source ./$@/bin/activate ; set -u ;\
+    pip install -r requirements.txt xosmigrate~=3.0.1 xoskafka~=3.0.1
+
+create-migration: venv-service
+	source ./venv-service/bin/activate; set -u;\
+    cd xos; xos-migrate --xos-dir ${XOS_DIR} --services-dir ${SERVICES_DIR} -s ${SERVICE_NAME}
+
+test-migration: venv-service
+	source ./venv-service/bin/activate; set -u;\
+    cd xos; xos-migrate --xos-dir ${XOS_DIR} --services-dir ${SERVICES_DIR} -s ${SERVICE_NAME} --check
+
+clean:
+	find . -name '*.pyc' | xargs rm -f
+	rm -rf \
+    .tox \
+    venv-service \
+    xos/.coverage \
+    xos/coverage.xml \
+    xos/nose2-results.xml
diff --git a/testservice/VERSION b/testservice/VERSION
deleted file mode 100644
index 8acdd82..0000000
--- a/testservice/VERSION
+++ /dev/null
@@ -1 +0,0 @@
-0.0.1
diff --git a/testservice/requirements.txt b/testservice/requirements.txt
new file mode 100644
index 0000000..c99c1ac
--- /dev/null
+++ b/testservice/requirements.txt
@@ -0,0 +1 @@
+kafkaloghandler~=0.9.0
diff --git a/testservice/tox.ini b/testservice/tox.ini
new file mode 100644
index 0000000..c6e3bf1
--- /dev/null
+++ b/testservice/tox.ini
@@ -0,0 +1,48 @@
+; Copyright 2019-present Open Networking Foundation
+;
+; 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.
+
+[tox]
+envlist = py27,py35,py36,py37
+skip_missing_interpreters = true
+skipsdist = True
+
+[testenv]
+deps =
+  ../lib/xos-config
+  ../lib/xos-kafka
+  ../lib/xos-api
+  ../lib/xos-synchronizer
+  nose2
+  flake8
+
+changedir = xos
+commands =
+  nose2 -c ../tox.ini --verbose --junit-xml
+  flake8
+
+[flake8]
+max-line-length = 119
+per-file-ignores = synchronizer/migrations/*.py:E501
+
+[unittest]
+plugins = nose2.plugins.junitxml
+
+[junit-xml]
+path = nose2-results.xml
+
+[coverage]
+always-on = True
+coverage-report =
+  term
+  xml
diff --git a/testservice/xos/synchronizer/migrations/0001_initial.py b/testservice/xos/synchronizer/migrations/0001_initial.py
index e4a87ae..285db11 100644
--- a/testservice/xos/synchronizer/migrations/0001_initial.py
+++ b/testservice/xos/synchronizer/migrations/0001_initial.py
@@ -28,7 +28,7 @@
     initial = True
 
     dependencies = [
-        ('core', '0009_auto_20190313_1442'),
+        ('core', '0002_initial_data'),
     ]
 
     operations = [
diff --git a/testservice/xos/synchronizer/model_policies/model_policy_testservice_serviceinstance.py b/testservice/xos/synchronizer/model_policies/model_policy_testservice_serviceinstance.py
index 71dfab7..cf40ef0 100644
--- a/testservice/xos/synchronizer/model_policies/model_policy_testservice_serviceinstance.py
+++ b/testservice/xos/synchronizer/model_policies/model_policy_testservice_serviceinstance.py
@@ -20,7 +20,7 @@
 from xossynchronizer.model_policies.policy import Policy
 
 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
-from helpers import put_signal, wait_for_signal
+from helpers import put_signal, wait_for_signal  # noqa: E402
 
 DUP_FIELD_NAMES = ["name",
                    "some_integer",
diff --git a/testservice/xos/synchronizer/steps/sync_testservice_serviceinstance.py b/testservice/xos/synchronizer/steps/sync_testservice_serviceinstance.py
index 5c4b11a..41d5e94 100644
--- a/testservice/xos/synchronizer/steps/sync_testservice_serviceinstance.py
+++ b/testservice/xos/synchronizer/steps/sync_testservice_serviceinstance.py
@@ -23,7 +23,7 @@
 from multistructlog import create_logger
 
 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
-from helpers import put_signal, wait_for_signal
+from helpers import put_signal, wait_for_signal  # noqa: E402
 
 log = create_logger(Config().get('logging'))
 
diff --git a/testservice/xos/synchronizer/testservice-synchronizer.py b/testservice/xos/synchronizer/testservice-synchronizer.py
index 86313be..fcde8b4 100644
--- a/testservice/xos/synchronizer/testservice-synchronizer.py
+++ b/testservice/xos/synchronizer/testservice-synchronizer.py
@@ -26,15 +26,21 @@
 from xossynchronizer import Synchronizer
 from xosconfig import Config
 
-base_config_file = os.path.abspath(os.path.dirname(
-    os.path.realpath(__file__)) + '/config.yaml')
-mounted_config_file = os.path.abspath(os.path.dirname(
-    os.path.realpath(__file__)) + '/mounted_config.yaml')
 
-if os.path.isfile(mounted_config_file):
-    Config.init(base_config_file, 'synchronizer-config-schema.yaml',
-                mounted_config_file)
-else:
-    Config.init(base_config_file, 'synchronizer-config-schema.yaml')
+def main():
+    base_config_file = os.path.abspath(os.path.dirname(
+        os.path.realpath(__file__)) + '/config.yaml')
+    mounted_config_file = os.path.abspath(os.path.dirname(
+        os.path.realpath(__file__)) + '/mounted_config.yaml')
 
-Synchronizer().run()
+    if os.path.isfile(mounted_config_file):
+        Config.init(base_config_file, 'synchronizer-config-schema.yaml',
+                    mounted_config_file)
+    else:
+        Config.init(base_config_file, 'synchronizer-config-schema.yaml')
+
+    Synchronizer().run()
+
+
+if __name__ == "__main__":
+    main()