Fixing up integration tests.

Updates OS/packages to get absolute paths (this allows
the tests DB to have relative paths and not break).

BUG: https://b/28718555, https://b/28723347
Change-Id: Ibe292b9a7c179607d428a01fb381b0695bcc93d6
TEST: sh cli/bdk_tests.sh passes, unit tests pass.
diff --git a/cli/lib/bsp/operating_system.py b/cli/lib/bsp/operating_system.py
index 1549620..9afa18c 100644
--- a/cli/lib/bsp/operating_system.py
+++ b/cli/lib/bsp/operating_system.py
@@ -56,7 +56,7 @@
     """
     def __init__(self, name, root):
         self._name = name
-        self._root = root
+        self._root = os.path.abspath(root)
         # For now, we read version from the VERSION file
         # at <root>/tools/bdk/VERSION. Eventually this will
         # just be a property like in Device.
diff --git a/cli/lib/bsp/package.py b/cli/lib/bsp/package.py
index 4723833..2f287a7 100644
--- a/cli/lib/bsp/package.py
+++ b/cli/lib/bsp/package.py
@@ -123,9 +123,9 @@
 
     @property
     def directory(self):
-        return os.path.join(user_config.USER_CONFIG.bsp_dir,
-                            BSP_PACKAGE_DIR_FORMAT.format(
-                                name=self.name, source_version=self.version))
+        return os.path.abspath(os.path.join(
+            user_config.USER_CONFIG.bsp_dir, BSP_PACKAGE_DIR_FORMAT.format(
+                name=self.name, source_version=self.version)))
 
     def _get_subpackage(self, name):
         """Gets a subpackage of the given name.
@@ -177,6 +177,10 @@
                 result = status.LINKED
             else:
                 result = status.UNRECOGNIZED
+        elif os.path.islink(path):
+            # Is link but doesn't exist = broken link.
+            # For now, this is also "unrecognized".
+            result = status.UNRECOGNIZED
         elif not self.is_downloaded():
             result = status.NOT_INSTALLED
         elif not os.path.isdir(subdir):
@@ -287,7 +291,6 @@
         """
         if not self.is_downloaded():
             raise NotDownloadedError()
-
         subpackage_ = self._get_subpackage(name)
         subdir = os.path.join(self.directory, subpackage_.subdir)
         if not os.path.isdir(subdir):
diff --git a/cli/lib/bsp/package_unittest.py b/cli/lib/bsp/package_unittest.py
index 8a96b33..3b2fb10 100644
--- a/cli/lib/bsp/package_unittest.py
+++ b/cli/lib/bsp/package_unittest.py
@@ -151,7 +151,7 @@
                                    self.pkg.version))
         # If we update the user config, bsp dir should update accordingly;
         # it shouldn't be cached.
-        self.stub_user_config.USER_CONFIG.bsp_dir = 'who/knows/where'
+        self.stub_user_config.USER_CONFIG.bsp_dir = '/who/knows/where'
         self.assertEqual(
             self.pkg.directory,
             self.stub_os.path.join(self.stub_user_config.USER_CONFIG.bsp_dir,
diff --git a/cli/lib/core/user_config_stub.py b/cli/lib/core/user_config_stub.py
index 7feada6..c693c2d 100644
--- a/cli/lib/core/user_config_stub.py
+++ b/cli/lib/core/user_config_stub.py
@@ -35,8 +35,8 @@
             self.table = table
             self.metrics_opt_in = '0'
             self.uid = 'user_id'
-            self.bsp_dir = 'somewhere/bsps'
-            self.platform_cache = 'elsewhere/pc'
+            self.bsp_dir = '/somewhere/bsps'
+            self.platform_cache = '/elsewhere/pc'
             self.is_complete = True
             self.os_root = '/where/the/tree/is'
 
diff --git a/cli/tests/bsp_download.sh b/cli/tests/bsp_download.sh
index f0b80cb..db0fd39 100755
--- a/cli/tests/bsp_download.sh
+++ b/cli/tests/bsp_download.sh
@@ -37,9 +37,9 @@
 # Success message expected.
 grep "Successfully installed all packages for Test Board." output.txt
 # Make sure download happened.
-grep "git subpackage license" ${TEST_TMP_DATA}/BSPs/git_package/source_versions\
+grep "git subpackage license" ${TEST_BSPs}/git_package/source_versions\
 /branch\:deadbeef/git_subdir/git_license.txt
-tar_source=${TEST_TMP_DATA}/BSPs/tar_package/source_versions\
+tar_source=${TEST_BSPs}/tar_package/source_versions\
 /e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
 grep "tar license 1" ${tar_source}/tar/sub1/tar_license1.txt
 grep "tar license 2" ${tar_source}/tar/sub2/deeper/tar_license2.txt
diff --git a/cli/tests/bsp_install.sh b/cli/tests/bsp_install.sh
index 33dab03..22ddac4 100755
--- a/cli/tests/bsp_install.sh
+++ b/cli/tests/bsp_install.sh
@@ -38,9 +38,9 @@
 # Success message expected.
 grep "Successfully installed all packages for Test Board." output.txt
 # Make sure download happened.
-grep "git subpackage license" ${TEST_TMP_DATA}/BSPs/git_package/source_versions\
+grep "git subpackage license" ${TEST_BSPs}/git_package/source_versions\
 /branch\:deadbeef/git_subdir/git_license.txt
-tar_source=${TEST_TMP_DATA}/BSPs/tar_package/source_versions\
+tar_source=${TEST_BSPs}/tar_package/source_versions\
 /e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
 grep "tar license 1" ${tar_source}/tar/sub1/tar_license1.txt
 grep "tar license 2" ${tar_source}/tar/sub2/deeper/tar_license2.txt
diff --git a/cli/tests/bsp_link.sh b/cli/tests/bsp_link.sh
new file mode 100644
index 0000000..521fe2d
--- /dev/null
+++ b/cli/tests/bsp_link.sh
@@ -0,0 +1,38 @@
+#!/bin/bash
+
+# Copyright (C) 2016 The Android Open Source Project
+#
+# 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.
+
+. tests/test_helpers
+
+setup_working_path
+
+stub_git
+stub_tar
+stub_curl
+
+echo "Testing bdk bsp download with linkage."
+
+${BDK} bsp download test_board -a -l -m "${DATA_DIR}/test_manifest.json"
+# All checks are obviously dependent upon how test_bard is defined
+# in test_manifest.
+
+# Check that the links exist. For ease of writing this code we assume
+# the source versions are correct.
+readlink "${TEST_TREE}/bsp/git/git_subpackage" |\
+ grep "${TEST_BSPs}/git_package/source_versions/.*/git_subdir"
+readlink "${TEST_TREE}/bsp/tar1" |\
+ grep "${TEST_BSPs}/tar_package/source_versions/.*/tar/sub1"
+readlink "${TEST_TREE}/bsp/tar2" |\
+ grep "${TEST_BSPs}/tar_package/source_versions/.*/tar/sub2"
diff --git a/cli/tests/bsp_no_overwrite.sh b/cli/tests/bsp_no_overwrite.sh
index 630d4d4..dccbf99 100755
--- a/cli/tests/bsp_no_overwrite.sh
+++ b/cli/tests/bsp_no_overwrite.sh
@@ -25,7 +25,9 @@
 echo "Testing that bdk bsp update doesn't override existing files."
 
 # Start with a tree where the user has existing work at a destination.
-: 'DISABLED until b/28297966 implemented
+${BDK} config set os_root "${TEST_TREE}"
+mkdir -p ${TEST_TREE}/bsp/tar1
+
 common_args=("-m" "${DATA_DIR}/test_manifest.json")
 
 ${BDK} bsp status test_board "${common_args[@]}" |\
@@ -34,4 +36,3 @@
 # Install should complete, but still consider the status "Unrecognized".
 ${BDK} bsp status test_board "${common_args[@]}" |\
 grep "Test Board 0.0.0 - Unrecognized"
-'
diff --git a/cli/tests/bsp_status.sh b/cli/tests/bsp_status.sh
index 12ac07c..4e39ce4 100755
--- a/cli/tests/bsp_status.sh
+++ b/cli/tests/bsp_status.sh
@@ -24,6 +24,8 @@
 
 common_args=("-m" "${DATA_DIR}/test_manifest.json")
 
+${BDK} config check
+
 echo "Testing bsp status"
 
 # A trivial bsp is "Installed".
@@ -37,14 +39,17 @@
 ${BDK} bsp install test_board --accept_licenses "${common_args[@]}"
 ${BDK} bsp status test_board "${common_args[@]}" |\
  grep "Test Board 0.0.0 - Installed"
-: 'DISABLED until b/28297966 implemented
+
+# Linked.
+${BDK} bsp install test_board --accept_licenses --link "${common_args[@]}"
+${BDK} bsp status test_board "${common_args[@]}" |\
+ grep "Test Board 0.0.0 - Linked"
+
 # Something unexpected in tree.
-rm brillo/bsp/tar1
-touch brillo/bsp/tar1
+rm ${TEST_TREE}/bsp/tar1
+touch ${TEST_TREE}/bsp/tar1
 ${BDK} bsp status test_board "${common_args[@]}" |\
  grep "Test Board 0.0.0 - Unrecognized"
-# Linked.
-TODO
+
 # Missing.
-TODO
-'
+#rm ${TODO
diff --git a/cli/tests/bsp_update.sh b/cli/tests/bsp_update.sh
index 264494d..8319f02 100755
--- a/cli/tests/bsp_update.sh
+++ b/cli/tests/bsp_update.sh
@@ -29,15 +29,24 @@
 # already present tarball.
 
 # Start with a screwed up tree with a broken link.
-: 'DISABLED until b/28297966 implemented
-mkdir -p brillo/bsp/git
+TEST_DIR="${TEST_TREE}/bsp/git"
+mkdir -p ${TEST_DIR}
 touch target
-ln -s target brillo/bsp/git/git_subpackage
+ln -s target ${TEST_DIR}/git_subpackage
 rm target
+touch ${TEST_TREE}/bsp/tar2
 
-common_args=("-m" "${DATA_DIR}/test_manifest.json" "-o" "brillo")
+common_args=("-m" "${DATA_DIR}/test_manifest.json")
 
-${BDK} bsp status test_board "${common_args[@]}" | grep "Test Board - Invalid"
+${BDK} bsp status test_board "${common_args[@]}" |\
+ grep "Test Board 0.0.0 - Not Installed"
 ${BDK} bsp update test_board --accept_licenses "${common_args[@]}"
-${BDK} bsp status test_board "${common_args[@]}" | grep "Test Board - Installed"
-'
+# Update will not actually touch anything that already exists.
+${BDK} bsp status test_board "${common_args[@]}" |\
+ grep "Test Board 0.0.0 - Unrecognized"
+# Definitely don't remove an unrecognized file.
+${BDK} bsp status test_board "${common_args[@]}" |\
+ grep "subpackage2 - Unrecognized"
+# Even broken links should remain in place.
+${BDK} bsp status test_board "${common_args[@]}" |\
+ grep "subpackage - Unrecognized"
diff --git a/cli/tests/data/config.db b/cli/tests/data/config.db
index ca3cabd..c10fd3b 100644
--- a/cli/tests/data/config.db
+++ b/cli/tests/data/config.db
Binary files differ
diff --git a/cli/tests/data/test_tree/tools/bdk/VERSION b/cli/tests/data/test_tree/tools/bdk/VERSION
new file mode 100644
index 0000000..1b88eb5
--- /dev/null
+++ b/cli/tests/data/test_tree/tools/bdk/VERSION
@@ -0,0 +1 @@
+00.00.00
diff --git a/cli/tests/shared_bsps.sh b/cli/tests/shared_bsps.sh
index e133d78..6ceae20 100755
--- a/cli/tests/shared_bsps.sh
+++ b/cli/tests/shared_bsps.sh
@@ -22,10 +22,6 @@
 stub_tar
 stub_curl
 
-# Set up isolation for testing.
-export HOME=${PWD} # bsps live in "~/.brillo/.BSPs".
-mkdir brillo
-
 common_args=("-m" "${DATA_DIR}/test_manifest.json")
 
 echo "Testing that bsp packages are shared among devices"
@@ -40,7 +36,7 @@
  grep "Test Board 0.0.0 - Installed"
 ${BDK} bsp status sharing_board "${common_args[@]}" |\
  grep "Sharing Board 0.0.0 - Installed"
-rm -r ${TEST_TMP_DATA}/BSPs/tar_package
+rm -r ${TEST_BSPs}/tar_package
 ${BDK} bsp status test_board "${common_args[@]}" |\
  grep "Test Board 0.0.0 - Not Installed"
 ${BDK} bsp status sharing_board "${common_args[@]}" |\
diff --git a/cli/tests/test_helpers b/cli/tests/test_helpers
index 6b496f7..1f9ff6f 100644
--- a/cli/tests/test_helpers
+++ b/cli/tests/test_helpers
@@ -30,21 +30,23 @@
   trap "rm -rf $tmp" exit
   cd $tmp
 
-  # Unlike tmp, TEST_TMP_DATA is in a predictable location;
-  # all the paths in the DB below point there.
-  if [ -e ${TEST_TMP_DATA} ]; then
-      rm -r ${TEST_TMP_DATA}
-  fi
-  mkdir ${TEST_TMP_DATA}
-
   # Set config values, setting aside old values.
+  # Note: this DB uses relative paths to PWD. In a real user's config,
+  # all paths will be absolute.
   mv ${BDK_USER_DATA}/config.db ${BDK_USER_DATA}/config_old.db
   cp ${DATA_DIR}/config.db ${BDK_USER_DATA}/config.db
 
   # Update our trap.
   trap "rm -rf $tmp && \
-rm -r ${TEST_TMP_DATA} && \
 mv ${BDK_USER_DATA}/config_old.db ${BDK_USER_DATA}/config.db" exit
+
+  # Set some values. Note: these should be kept in sync with the DB.
+  TEST_TREE=${tmp}/OS
+  TEST_BSPs=${tmp}/BSPs
+  TEST_PLATFORM_CACHE=${tmp}/platform_cache
+
+  # Initialize the test_tree.
+  cp -r ${DATA_DIR}/test_tree ${TEST_TREE}
 }
 
 # Places make in stubs/ and updates PATH.