Add structure tests for gerrit-init container

Change-Id: Icffac7cb7baef42457ebbe0851dd45377b2130ad
diff --git a/tests/conftest.py b/tests/conftest.py
index 011e760..073414b 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -151,6 +151,11 @@
   return docker_build(container_images["gerrit-slave"], "gerrit-slave")
 
 @pytest.fixture(scope="session")
+def gerrit_init_image(container_images, docker_build, base_image,
+                      gerrit_base_image):
+  return docker_build(container_images["gerrit-init"], "gerrit-init")
+
+@pytest.fixture(scope="session")
 def mysql_replication_init_image(container_images, docker_build):
   return docker_build(
     container_images["mysql-replication-init"],
diff --git a/tests/container-images/gerrit-init/test_container_build_gerrit_init.py b/tests/container-images/gerrit-init/test_container_build_gerrit_init.py
new file mode 100644
index 0000000..85d4996
--- /dev/null
+++ b/tests/container-images/gerrit-init/test_container_build_gerrit_init.py
@@ -0,0 +1,20 @@
+# Copyright (C) 2018 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.
+
+import pytest
+
+def test_build_gerrit_init(gerrit_init_image, tag_of_cached_container):
+  if tag_of_cached_container:
+    pytest.skip("Cached image used for testing. Build will not be tested.")
+  assert gerrit_init_image.id is not None
diff --git a/tests/container-images/gerrit-init/test_container_structure_gerrit_init.py b/tests/container-images/gerrit-init/test_container_structure_gerrit_init.py
new file mode 100755
index 0000000..c50b543
--- /dev/null
+++ b/tests/container-images/gerrit-init/test_container_structure_gerrit_init.py
@@ -0,0 +1,78 @@
+# Copyright (C) 2018 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.
+
+import pytest
+
+@pytest.fixture(scope="module")
+def container_run(request, docker_client, gerrit_init_image):
+  container_run = docker_client.containers.run(
+    image=gerrit_init_image.id,
+    entrypoint="/bin/bash",
+    command=["-c", "tail -f /dev/null"],
+    user="gerrit",
+    detach=True,
+    auto_remove=True
+  )
+
+  def stop_container():
+    container_run.stop(timeout=1)
+
+  request.addfinalizer(stop_container)
+
+  return container_run
+
+@pytest.fixture(scope="function",
+                params=["/var/tools/gerrit_init.py",
+                        "/var/tools/git_config_parser.py",
+                        "/var/tools/validate_db.py"])
+def expected_script(request):
+  return request.param
+
+@pytest.fixture(scope="function",
+                params=["python3",
+                        "pip3",
+                        "mysql"])
+def expected_tool(request):
+  return request.param
+
+@pytest.fixture(scope="function",
+                params=["pymysql",
+                        "sqlalchemy"])
+def expected_pip_package(request):
+  return request.param
+
+def test_gerrit_init_inherits_from_gerrit_base(gerrit_init_image):
+  contains_tag = False
+  for layer in gerrit_init_image.history():
+    contains_tag = layer['Tags'] is not None and "gerrit-base:latest" in layer['Tags']
+    if contains_tag:
+      break
+  assert contains_tag
+
+def test_gerrit_init_contains_expected_scripts(container_run, expected_script):
+  exit_code, _ = container_run.exec_run("test -f %s" % expected_script)
+  assert exit_code == 0
+
+def test_gerrit_init_contains_expected_tools(container_run, expected_tool):
+  exit_code, _ = container_run.exec_run("which %s" % expected_tool)
+  assert exit_code == 0
+
+def test_gerrit_init_contains_expected_pip_packages(container_run, expected_pip_package):
+  exit_code, _ = container_run.exec_run("pip3 show %s" % expected_pip_package)
+  assert exit_code == 0
+
+def test_gerrit_init_has_entrypoint(gerrit_init_image):
+  entrypoint = gerrit_init_image.attrs["ContainerConfig"]["Entrypoint"]
+  assert len(entrypoint) >= 1
+  assert entrypoint == ["/var/tools/gerrit_init.py", "-s", "/var/gerrit"]