SEBA-516 scripts to regenerate migration test images;
Fix migration test error on optional field service_secret

Change-Id: I97b81c7d4c0b5eca30ea7255ef5b2fd7a5200052
diff --git a/src/test/cord-api/Tests/xos-migration-tests/migration-test2.patch b/src/test/cord-api/Tests/xos-migration-tests/migration-test2.patch
new file mode 100644
index 0000000..1dcf960
--- /dev/null
+++ b/src/test/cord-api/Tests/xos-migration-tests/migration-test2.patch
@@ -0,0 +1,16 @@
+*** xos/synchronizer/models/simpleexampleservice.orig	Fri Mar 29 09:26:15 2019
+--- xos/synchronizer/models/simpleexampleservice.xproto	Fri Mar 29 09:41:09 2019
+***************
+*** 10,15 ****
+--- 10,20 ----
+          max_length = 254];
+      optional string service_secret = 2 [
+          help_text = "Service Secret to place in a file"];
++ 
++     required string new_field = 3 [
++         help_text = "New field to test data migration",
++         db_index = False,
++         default = "new_stuff"];
+  }
+  
+  // Note: Named ColorNew to prevent name collision with ExampleService's Color.
diff --git a/src/test/cord-api/Tests/xos-migration-tests/migration-test3.patch b/src/test/cord-api/Tests/xos-migration-tests/migration-test3.patch
new file mode 100644
index 0000000..556af9a
--- /dev/null
+++ b/src/test/cord-api/Tests/xos-migration-tests/migration-test3.patch
@@ -0,0 +1,16 @@
+*** xos/synchronizer/models/simpleexampleservice.orig	Fri Mar 29 09:26:15 2019
+--- xos/synchronizer/models/simpleexampleservice.xproto	Fri Mar 29 09:50:10 2019
+***************
+*** 10,15 ****
+--- 10,20 ----
+          max_length = 254];
+      optional string service_secret = 2 [
+          help_text = "Service Secret to place in a file"];
++ 
++     required string renamed_new_field = 3 [
++         help_text = "New field to test data migration",
++         db_index = False,
++         default = "renamed_new_stuff"];
+  }
+  
+  // Note: Named ColorNew to prevent name collision with ExampleService's Color.
diff --git a/src/test/cord-api/Tests/xos-migration-tests/push-images.sh b/src/test/cord-api/Tests/xos-migration-tests/push-images.sh
new file mode 100644
index 0000000..fd1e840
--- /dev/null
+++ b/src/test/cord-api/Tests/xos-migration-tests/push-images.sh
@@ -0,0 +1,20 @@
+#!/usr/bin/env bash
+
+# Copyright 2017-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.
+
+docker push xosproject/simpleexampleservice-synchronizer:migration-test1
+docker push xosproject/simpleexampleservice-synchronizer:migration-test2
+docker push xosproject/simpleexampleservice-synchronizer:migration-test3
+docker push xosproject/simpleexampleservice-synchronizer:migration-test4
diff --git a/src/test/cord-api/Tests/xos-migration-tests/rebuild-images.sh b/src/test/cord-api/Tests/xos-migration-tests/rebuild-images.sh
new file mode 100644
index 0000000..bd90091
--- /dev/null
+++ b/src/test/cord-api/Tests/xos-migration-tests/rebuild-images.sh
@@ -0,0 +1,73 @@
+#!/usr/bin/env bash
+
+# Copyright 2017-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.
+
+set -e
+
+# to generate a patch:
+#    pushd $SES_DIR && diff -c xos/synchronizer/models/simpleexampleservice.orig xos/synchronizer/models/simpleexampleservice.xproto > /tmp/migration-test.patch && popd
+
+export PATCH_DIR=`pwd`
+export REPO_DIR=/home/smbaker/projects/opencord/
+export SES_DIR=$REPO_DIR/orchestration/xos-services/simpleexampleservice
+export SES_MODELS_DIR=$SES_DIR/xos/synchronizer/models
+export SES_MIG_DIR=$SES_DIR/xos/synchronizer/migrations
+
+if [ ! -f $SES_MODELS_DIR/simpleexampleservice.orig ]; then
+    cp $SES_MODELS_DIR/simpleexampleservice.xproto $SES_MODELS_DIR/simpleexampleservice.orig
+fi
+
+cd $SES_DIR
+
+# migration-test1: initial image with no new files
+rm -rf $SES_MIG_DIR/*.py
+rm -rf $SES_MIG_DIR/*.pyc
+xos-migrate -r $REPO_DIR -s simpleexampleservice --verbose
+
+# In case we're pointing to a migration that's in an xos core that isn't released
+sed -i 's/0009_auto_20190313_1442/0002_initial_data/g' $SES_MIG_DIR/0001_initial.py
+
+docker build -t xosproject/simpleexampleservice-synchronizer:migration-test1 -f Dockerfile.synchronizer .
+
+# migration-test2: new field added
+#    required string new_field = 3 [
+#        help_text = "New field to test data migration",
+#        db_index = False,
+#        default = "new_stuff"];
+
+patch -d $SES_DIR -i $PATCH_DIR/migration-test2.patch -p0 -o xos/synchronizer/models/simpleexampleservice.xproto
+xos-migrate -r $REPO_DIR -s simpleexampleservice --verbose
+docker build -t xosproject/simpleexampleservice-synchronizer:migration-test2 -f Dockerfile.synchronizer .
+
+# migration-test3: new field renamed
+#    required string renamed_new_field = 3 [
+#        help_text = "New field to test data migration",
+#        db_index = False,
+#        default = "renamed_new_stuff"];
+
+patch -d $SES_DIR -i $PATCH_DIR/migration-test3.patch -p0 -o xos/synchronizer/models/simpleexampleservice.xproto
+xos-migrate -r $REPO_DIR -s simpleexampleservice --verbose
+
+echo "Generated migration script is likely incorrect -- manually edit and change it to a rename"
+echo "   migrations.RenameField(model_name='simpleexampleservice', old_name='new_field', new_name='renamed_new_field'),"
+read -n1 -r -p "Press any key and I will launch an editor..." key
+emacs $SES_MIG_DIR/0003*.py
+
+docker build -t xosproject/simpleexampleservice-synchronizer:migration-test3 -f Dockerfile.synchronizer .
+
+# migration-test4: revert back to original models
+cp $SES_MODELS_DIR/simpleexampleservice.orig $SES_MODELS_DIR/simpleexampleservice.xproto
+xos-migrate -r $REPO_DIR -s simpleexampleservice --verbose
+docker build -t xosproject/simpleexampleservice-synchronizer:migration-test4 -f Dockerfile.synchronizer .
diff --git a/src/test/cord-api/Tests/xos-migration-tests/xos-remove-service.robot b/src/test/cord-api/Tests/xos-migration-tests/xos-remove-service.robot
index 4864366..01a9bd0 100644
--- a/src/test/cord-api/Tests/xos-migration-tests/xos-remove-service.robot
+++ b/src/test/cord-api/Tests/xos-migration-tests/xos-remove-service.robot
@@ -110,8 +110,8 @@
     ${auth} =    Create List    ${XOS_USER}    ${XOS_PASSWD}
     ${HEADERS}    Create Dictionary    Content-Type=application/json    allow_modify_feedback=True
     Create Session    ${server_ip}    http://${server_ip}:${server_port}    auth=${AUTH}    headers=${HEADERS}
-    @{model_A_fields}=    Create List    service_message    service_secret
-    @{model_B_fields}=    Create List    service_message    service_secret    new_field
+    @{model_A_fields}=    Create List    service_message
+    @{model_B_fields}=    Create List    service_message    new_field
     @{model_B_only_fields}=    Create List    new_field
 
     ${db_addr}=    Run    kubectl get services | grep -i xos-db | awk '{print $3}'
diff --git a/src/test/cord-api/Tests/xos-migration-tests/xos-service-migrations.robot b/src/test/cord-api/Tests/xos-migration-tests/xos-service-migrations.robot
index 227703b..0d042ca 100644
--- a/src/test/cord-api/Tests/xos-migration-tests/xos-service-migrations.robot
+++ b/src/test/cord-api/Tests/xos-migration-tests/xos-service-migrations.robot
@@ -152,12 +152,12 @@
     ${auth} =    Create List    ${XOS_USER}    ${XOS_PASSWD}
     ${HEADERS}    Create Dictionary    Content-Type=application/json    allow_modify_feedback=True
     Create Session    ${server_ip}    http://${server_ip}:${server_port}    auth=${AUTH}    headers=${HEADERS}
-    @{model_A_fields}=    Create List    service_message    service_secret
-    @{model_B_fields}=    Create List    service_message    service_secret    new_field
+    @{model_A_fields}=    Create List    service_message
+    @{model_B_fields}=    Create List    service_message    new_field
     @{model_B_only_fields}=    Create List    new_field
-    @{model_C_fields}=    Create List    service_message    service_secret    renamed_new_field
+    @{model_C_fields}=    Create List    service_message    renamed_new_field
     @{model_C_only_fields}=    Create List    renamed_new_field
-    @{model_D_fields}=    Create List    service_message    service_secret
+    @{model_D_fields}=    Create List    service_message
 
     Set Suite Variable    @{model_A_fields}