#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.

# A simple test of gosu. We create a myvarsolr, and chown it

if [[ "$OSTYPE" == "darwin"* ]]; then
  # TODO: Fix this test on Mac
  echo "WARNING: Ignoring test 'gosu' on macOS"
  exit 0
fi

set -euo pipefail

TEST_DIR="${TEST_DIR:-$(dirname -- "${BASH_SOURCE[0]}")}"
source "${TEST_DIR}/../../shared.sh"

myvarsolr="${BUILD_DIR}/myvarsolr-${container_name}"
prepare_dir_to_mount 8983 "$myvarsolr"

echo "Running $container_name"
docker run --user 0:0 --name "$container_name" -d -e VERBOSE=yes \
  -v "$myvarsolr:/var/solr" "$tag" \
  bash -c "chown -R solr:solr /var/solr; touch /var/solr/root_was_here; exec gosu solr:solr solr-precreate gettingstarted"

wait_for_container_and_solr "$container_name"

echo "Loading data"
docker exec --user=solr "$container_name" bin/solr post -c gettingstarted example/exampledocs/manufacturers.xml
sleep 1
echo "Checking data"
data=$(docker exec --user=solr "$container_name" wget -q -O - 'http://localhost:8983/solr/gettingstarted/select?q=id%3Adell')
if ! grep -E -q 'One Dell Way Round Rock, Texas 78682' <<<"$data"; then
  echo "Test $TEST_NAME $tag failed; data did not load"
  exit 1
fi

# check test file was created by root
data=$(docker exec --user=root "$container_name" stat -c %U /var/solr/root_was_here )
if [[ "$data" == *'No such file or directory' ]]; then
  echo "Missing /var/solr/root_was_here"
  exit 1
fi
if [[ "$data" != root ]]; then
  echo "/var/solr/root_was_here is owned by $data"
  exit 1
fi

# check core is created by solr
data=$(docker exec --user=root "$container_name" stat -c %U /var/solr/data/gettingstarted/core.properties )
if [[ "$data" == *'No such file or directory' ]]; then
  echo "Missing /var/solr/data/gettingstarted/core.properties"
  exit 1
fi
if [[ "$data" != solr ]]; then
  echo "/var/solr/data/gettingstarted/core.properties is owned by $data"
  exit 1
fi

container_cleanup "$container_name"

# chown it back
docker run --rm --user 0:0 -e VERBOSE=yes \
  -v "$myvarsolr:/myvarsolr" "$tag" \
  bash -c "chown -R $(id -u):$(id -g) /myvarsolr; ls -ld /myvarsolr"

rm -fr "$myvarsolr"

echo "Test $TEST_NAME $tag succeeded"
