Merge "Optimize kitchen tests for Travis CI"
diff --git a/Makefile b/Makefile
index 1043fbe..7747ec1 100644
--- a/Makefile
+++ b/Makefile
@@ -27,12 +27,14 @@
 
 all:
 	@echo "make install - Install into DESTDIR"
+	@echo "make lint    - Run lint tests"
 	@echo "make test    - Run tests"
 	@echo "make kitchen - Run Kitchen CI tests (create, converge, verify)"
 	@echo "make clean   - Cleanup after tests run"
 	@echo "make release-major  - Generate new major release"
 	@echo "make release-minor  - Generate new minor release"
 	@echo "make changelog      - Show changes since last release"
+	@echo "make test-model-validate      - Run salt jsonschema validation"
 
 install:
 	# Formula
@@ -45,9 +47,16 @@
 	[ -d $(DESTDIR)/$(RECLASSDIR)/service/$(FORMULANAME) ] || mkdir -p $(DESTDIR)/$(RECLASSDIR)/service/$(FORMULANAME)
 	cp -a metadata/service/* $(DESTDIR)/$(RECLASSDIR)/service/$(FORMULANAME)
 
+lint:
+	[ ! -d tests ] || (cd tests; ./run_tests.sh lint)
+
 test:
 	[ ! -d tests ] || (cd tests; ./run_tests.sh)
 
+test-model-validate:
+	# TODO make it actually fail
+	[ ! -d $(FORMULANAME)/schemas/ ] || (cd tests; ./run_tests.sh model-validate)
+
 release-major: check-changes
 	@echo "Current version is $(VERSION), new version is $(NEW_MAJOR_VERSION)"
 	@[ $(VERSION_MAJOR) != $(NEW_MAJOR_VERSION) ] || (echo "Major version $(NEW_MAJOR_VERSION) already released, nothing to do. Do you want release-minor?" && exit 1)
diff --git a/ntp/files/ntp.conf b/ntp/files/ntp.conf
index 5dd8534..1987879 100644
--- a/ntp/files/ntp.conf
+++ b/ntp/files/ntp.conf
@@ -62,4 +62,4 @@
 
 # Location of drift file
 driftfile /var/lib/ntp/ntp.drift
-logfile /var/log/ntp.log
\ No newline at end of file
+logfile /var/log/ntp.log
diff --git a/ntp/meta/meta.yml b/ntp/meta/meta.yml
new file mode 100644
index 0000000..097f691
--- /dev/null
+++ b/ntp/meta/meta.yml
@@ -0,0 +1,15 @@
+graph:
+{%- if pillar.get('ntp', {}).client is defined %}
+{%- from "ntp/map.jinja" import client with context %}
+- host: {{ grains.id }}
+  service: ntp.client
+  type: software-system
+  relations:
+  {%- for stratum in client.strata %}
+  - service: other-service
+    host_external: udp://{{ stratum }}
+    direction: source
+    type: udp
+  {%- endfor %}
+{%- endif %}
+
diff --git a/ntp/schemas/client.yaml b/ntp/schemas/client.yaml
new file mode 100644
index 0000000..becc91a
--- /dev/null
+++ b/ntp/schemas/client.yaml
@@ -0,0 +1,30 @@
+%YAML 1.1
+---
+"$schema": "http://json-schema.org/draft-06/schema#"
+title: NTP client role
+description: |
+  NTP service, client role.
+type: object
+additionalProperties: false
+
+required:
+- enabled
+
+properties:
+  enabled:
+    description: |
+      Enables NTP client service.
+    type: boolean
+  strata:
+    description: |
+      List of NTP stratums to keep the time in sync.
+    type: array
+    items:
+      $ref: "#/definitions/stratum"
+
+definitions:
+  stratum:
+    description: |
+      Hostname or IP address of the stratum server.
+    type: string
+    format: hostname-ip
diff --git a/ntp/schemas/server.yaml b/ntp/schemas/server.yaml
new file mode 100644
index 0000000..b578323
--- /dev/null
+++ b/ntp/schemas/server.yaml
@@ -0,0 +1,81 @@
+%YAML 1.1
+---
+"$schema": "http://json-schema.org/draft-06/schema#"
+title: NTP server role
+description: |
+  NTP service, server role.
+type: object
+additionalProperties: false
+
+required:
+- enabled
+
+properties:
+  enabled:
+    description: |
+      Enables NTP server service.
+    type: boolean
+  mode7:
+    description: |
+      Enables mode7 for the NTP server.
+    type: boolean
+  orphan:
+    description: |
+      Sets the orphan level of the NTP server.
+    type: number
+    minimum: 0
+    maximum: 8
+  restrict:
+    description: |
+      List of subnets that servers gives time to.
+    type: array
+    items:
+      $ref: "#/definitions/restrict"
+  peers:
+    description: |
+      List of peered NTP stratum services.
+    type: array
+    items:
+      $ref: "#/definitions/peer"
+  strata:
+    description: |
+      List of NTP stratums to keep the time in sync.
+    type: array
+    items:
+      $ref: "#/definitions/stratum"
+
+definitions:
+  restrict:
+    description: |
+      Restrict the service to given networks.
+    type: object
+    flowStyle: block
+    propertyOrder: [subnet, mask,options]
+    required: [subnet, mask]
+    properties:
+      subnet:
+        description: |
+          IP address of the network
+        type: string
+        style: inline
+      mask:
+        description: |
+          Subnet mask of the network
+        type: string
+        style: inline
+      options:
+        description: |
+          Additional options passed to the net [notrap nomodify]
+        type: string
+        style: inline
+    additionalProperties: false
+  peer:
+    description: |
+      Hostname or IP address of the peer server.
+    type: string
+    format: hostname-ip
+  stratum:
+    description: |
+      Hostname or IP address of the stratum server.
+    type: string
+    format: hostname-ip
diff --git a/tests/pip_requirements.txt b/tests/pip_requirements.txt
new file mode 100644
index 0000000..d89304b
--- /dev/null
+++ b/tests/pip_requirements.txt
@@ -0,0 +1 @@
+jsonschema
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 29fb975..7bd7278 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -1,5 +1,8 @@
 #!/usr/bin/env bash
 
+###
+# Script requirments:
+#  apt-get install -y python-yaml virtualenv git
 set -e
 [ -n "$DEBUG" ] && set -x
 
@@ -14,11 +17,13 @@
 VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
 MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
 DEPSDIR=${BUILDDIR}/deps
+SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"}
 
 SALT_FILE_DIR=${SALT_FILE_DIR:-${BUILDDIR}/file_root}
 SALT_PILLAR_DIR=${SALT_PILLAR_DIR:-${BUILDDIR}/pillar_root}
 SALT_CONFIG_DIR=${SALT_CONFIG_DIR:-${BUILDDIR}/salt}
 SALT_CACHE_DIR=${SALT_CACHE_DIR:-${SALT_CONFIG_DIR}/cache}
+SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods}
 
 SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
 
@@ -28,11 +33,11 @@
 
 ## Functions
 log_info() {
-    echo "[INFO] $*"
+    echo -e "[INFO] $*"
 }
 
 log_err() {
-    echo "[ERROR] $*" >&2
+    echo -e "[ERROR] $*" >&2
 }
 
 setup_virtualenv() {
@@ -40,6 +45,10 @@
     virtualenv $VENV_DIR
     source ${VENV_DIR}/bin/activate
     pip install salt${PIP_SALT_VERSION}
+    pip install reno
+    if [[ -f ${CURDIR}/pip_requirements.txt ]]; then
+       pip install -r ${CURDIR}/pip_requirements.txt
+    fi
 }
 
 setup_mock_bin() {
@@ -65,6 +74,7 @@
     [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
     [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
     [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
+    [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR}
 
     echo "base:" > ${SALT_FILE_DIR}/top.sls
     for pillar in ${PILLARDIR}/*.sls; do
@@ -76,6 +86,7 @@
     cat << EOF > ${SALT_CONFIG_DIR}/minion
 file_client: local
 cachedir: ${SALT_CACHE_DIR}
+extension_modules:  ${SALT_CACHE_EXTMODS_DIR}
 verify_env: False
 minion_id_caching: False
 
@@ -93,13 +104,14 @@
 }
 
 fetch_dependency() {
+    # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux"
     dep_name="$(echo $1|cut -d : -f 1)"
     dep_source="$(echo $1|cut -d : -f 2-)"
     dep_root="${DEPSDIR}/$(basename $dep_source .git)"
     dep_metadata="${dep_root}/metadata.yml"
 
-    [ -d /usr/share/salt-formulas/env/${dep_name} ] && log_info "Dependency $dep_name already present in system-wide salt env" && return 0
-    [ -d $dep_root ] && log_info "Dependency $dep_name already fetched" && return 0
+    [ -d /usr/share/salt-formulas/env/${dep_name} ] && { log_info "Dependency $dep_name already present in system-wide salt env"; return 0; }
+    [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
 
     log_info "Fetching dependency $dep_name"
     [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
@@ -109,6 +121,19 @@
     METADATA="${dep_metadata}" install_dependencies
 }
 
+link_modules(){
+    # Link modules *.py files to temporary salt-root
+    local SALT_ROOT=${1:-$SALT_FILE_DIR}
+    local SALT_ENV=${2:-$DEPSDIR}
+
+    mkdir -p "${SALT_ROOT}/_modules/"
+    # from git, development versions
+    find ${SALT_ENV} -maxdepth 3 -mindepth 3 -path '*_modules*' -iname "*.py" -type f -print0 | while read -d $'\0' file; do
+      ln -fs $(readlink -e ${file}) "$SALT_ROOT"/_modules/$(basename ${file}) ;
+    done
+    salt_run saltutil.sync_all
+}
+
 install_dependencies() {
     grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
     (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
@@ -124,20 +149,30 @@
 }
 
 salt_run() {
-    [ -e ${VEN_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
+    [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
     salt-call ${SALT_OPTS} $*
 }
 
 prepare() {
     [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
 
-    which salt-call || setup_virtualenv
+    [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
     setup_mock_bin
     setup_pillar
     setup_salt
     install_dependencies
 }
 
+lint_releasenotes() {
+    [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
+    reno lint ${CURDIR}/../
+}
+
+lint() {
+#    lint_releasenotes
+    log_err "TODO: lint_releasenotes"
+}
+
 run() {
     for pillar in ${PILLARDIR}/*.sls; do
         grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
@@ -152,7 +187,7 @@
             meta_name=$(basename ${meta})
             echo "Checking meta ${meta_name} ..."
             salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
-              || (log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1)
+              || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; }
             cat ${SALT_CACHE_DIR}/${meta_name}
         done
     done
@@ -161,7 +196,23 @@
 real_run() {
     for pillar in ${PILLARDIR}/*.sls; do
         state_name=$(basename ${pillar%.sls})
-        salt_run --id=${state_name} state.sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
+        salt_run --id=${state_name} state.sls ${FORMULA_NAME} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1; }
+    done
+}
+
+run_model_validate(){
+    [[ -d ${SCHEMARDIR} ]] || { log_err "${SCHEMARDIR} not found!"; return 1; }
+    # model validator require py modules
+    fetch_dependency "salt:https://github.com/salt-formulas/salt-formula-salt"
+    link_modules
+    # Rendered Example:
+    # salt-call --local -c /test1/maas/tests/build/salt --id=maas_cluster modelschema.model_validate maas cluster
+    for role in ${SCHEMARDIR}/*.yaml; do
+        state_name=$(basename "${role%*.yaml}")
+        minion_id="${state_name}"
+        # in case debug-reruns, usefull to make cleanup
+        [ -n "$DEBUG" ] && { salt_run saltutil.clear_cache; salt_run saltutil.refresh_pillar; salt_run saltutil.sync_all; }
+        salt_run --id=${minion_id} modelschema.model_validate ${FORMULA_NAME} ${state_name} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1 ; }
     done
 }
 
@@ -187,14 +238,23 @@
     prepare)
         prepare
         ;;
+    lint)
+        lint
+        ;;
     run)
         run
         ;;
     real-run)
         real_run
         ;;
+    model-validate)
+       prepare
+       run_model_validate
+        ;;
     *)
         prepare
+#        lint
         run
+        run_model_validate
         ;;
 esac