From 5ec59c9322dea43de3b90d40a4494e63ee6a0bf9 Mon Sep 17 00:00:00 2001 From: Yuri Shkuro Date: Sun, 15 Jan 2023 13:43:59 -0500 Subject: [PATCH 1/2] Refactor scripts/es-integration-test.sh Signed-off-by: Yuri Shkuro --- .github/workflows/ci-opensearch.yml | 2 +- scripts/es-integration-test.sh | 72 ++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/.github/workflows/ci-opensearch.yml b/.github/workflows/ci-opensearch.yml index fb109f8f17c..d4e0176a3f8 100644 --- a/.github/workflows/ci-opensearch.yml +++ b/.github/workflows/ci-opensearch.yml @@ -18,7 +18,7 @@ jobs: matrix: version: - major: 1.x - image: 1.0.0 + image: 1.3.7 distribution: opensearch - major: 2.x image: 2.3.0 diff --git a/scripts/es-integration-test.sh b/scripts/es-integration-test.sh index dff2f392dc2..96bf4445467 100755 --- a/scripts/es-integration-test.sh +++ b/scripts/es-integration-test.sh @@ -3,6 +3,10 @@ PS4='T$(date "+%H:%M:%S") ' set -euxf -o pipefail +# use global variables to reflect status of db +db_is_up= +db_cid= + usage() { echo $"Usage: $0 " exit 1 @@ -19,7 +23,6 @@ setup_es() { local tag=$1 local image=docker.elastic.co/elasticsearch/elasticsearch local params=( - --rm --detach --publish 9200:9200 --env "http.host=0.0.0.0" @@ -35,7 +38,6 @@ setup_opensearch() { local image=opensearchproject/opensearch local tag=$1 local params=( - --rm --detach --publish 9200:9200 --env "http.host=0.0.0.0" @@ -59,7 +61,7 @@ setup_query() { SPAN_STORAGE_TYPE=${distro} ./cmd/query/query-${os}-${arch} ${params[@]} } -wait_for_it() { +wait_for_storage() { local url=$1 local cid=$2 local params=( @@ -72,18 +74,49 @@ wait_for_it() { local counter=0 local max_counter=60 while [[ "$(curl ${params[@]} ${url})" != "200" && ${counter} -le ${max_counter} ]]; do - sleep 5 - counter=$((counter+1)) + docker inspect ${cid} | jq '.[].State' echo "waiting for ${url} to be up..." - if [ ${counter} -eq ${max_counter} ]; then - echo "ERROR: elasticsearch/opensearch is down" - docker logs ${cid} + sleep 10 + counter=$((counter+1)) + done + # after the loop, do final verification and set status as global var + if [[ "$(curl ${params[@]} ${url})" != "200" ]]; then + echo "ERROR: elasticsearch/opensearch is not reachable" + docker logs ${cid} + docker kill ${cid} + db_is_up=0 + else + echo "SUCCESS: elasticsearch/opensearch is reachable" + db_is_up=1 + fi +} + +bring_up_storage() { + local distro=$1 + local version=$2 + local cid + for retry in 1 2 3 + do + if [ ${distro} = "elasticsearch" ]; then + cid=$(setup_es ${version}) + elif [ ${distro} == "opensearch" ]; then + cid=$(setup_opensearch ${version}) + else + echo "Unknown distribution $distro. Valid options are opensearch or elasticsearch" + usage + fi + wait_for_storage "http://localhost:9200" ${cid} + if [ ${db_is_up} = "1" ]; then + break + else + echo "ERROR: unable to start elasticsearch/opensearch" exit 1 fi done + db_cid=${cid} } -teardown_es() { +teardown_storage() { local cid=$1 docker kill ${cid} } @@ -100,21 +133,9 @@ build_query() { run_integration_test() { local distro=$1 - local version=$2 - local cid - if [ ${distro} = "elasticsearch" ]; then - cid=$(setup_es ${version}) - elif [ ${distro} == "opensearch" ]; then - cid=$(setup_opensearch ${version}) - else - echo "Unknown distribution $distro. Valid options are opensearch or elasticsearch" - usage - fi - wait_for_it "http://localhost:9200" ${cid} STORAGE=${distro} make storage-integration-test make index-cleaner-integration-test make index-rollover-integration-test - teardown_es ${cid} } run_token_propagation_test() { @@ -129,8 +150,13 @@ run_token_propagation_test() { main() { check_arg "$@" - echo "Executing integration test for $1 $2" - run_integration_test "$1" "$2" + echo "Preparing $1 $2" + bring_up_storage "$1" "$2" + trap "teardown_storage ${db_cid}" EXIT + + echo "Executing main integration tests" + run_integration_test "$1" + echo "Executing token propagation test" run_token_propagation_test "$1" } From 10cbe9200d1459365e68bff7d63d62ec103c6919 Mon Sep 17 00:00:00 2001 From: Yuri Shkuro Date: Sun, 15 Jan 2023 17:36:09 -0500 Subject: [PATCH 2/2] Use different ES address for token test Signed-off-by: Yuri Shkuro --- .../integration/token_propagation_test.go | 26 +++++++++------- scripts/es-integration-test.sh | 31 +++++++++++++------ 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/plugin/storage/integration/token_propagation_test.go b/plugin/storage/integration/token_propagation_test.go index 070379c2ee7..d1ced9fea9e 100644 --- a/plugin/storage/integration/token_propagation_test.go +++ b/plugin/storage/integration/token_propagation_test.go @@ -88,23 +88,25 @@ func TestBearTokenPropagation(t *testing.T) { // Run elastic search mocked server in background.. // is not a full server, just mocked the necessary stuff for this test. - srv := &http.Server{Addr: ":9200"} + srv := &http.Server{Addr: ":19200"} defer srv.Shutdown(context.Background()) go createElasticSearchMock(srv, t) // Test cases. for _, testCase := range testCases { - // Ask for services query, this should return 200 - req, err := http.NewRequest("GET", queryUrl, nil) - require.NoError(t, err) - req.Header.Add(testCase.headerName, testCase.headerValue) - client := &http.Client{} - resp, err := client.Do(req) - assert.NoError(t, err) - assert.NotNil(t, resp) - if err == nil && resp != nil { - assert.Equal(t, resp.StatusCode, http.StatusOK) - } + t.Run(testCase.name, func(t *testing.T) { + // Ask for services query, this should return 200 + req, err := http.NewRequest("GET", queryUrl, nil) + require.NoError(t, err) + req.Header.Add(testCase.headerName, testCase.headerValue) + client := &http.Client{} + resp, err := client.Do(req) + assert.NoError(t, err) + assert.NotNil(t, resp) + if err == nil && resp != nil { + assert.Equal(t, resp.StatusCode, http.StatusOK) + } + }) } } diff --git a/scripts/es-integration-test.sh b/scripts/es-integration-test.sh index 96bf4445467..a60370dee3e 100755 --- a/scripts/es-integration-test.sh +++ b/scripts/es-integration-test.sh @@ -7,6 +7,17 @@ set -euxf -o pipefail db_is_up= db_cid= +exit_trap_command="echo executing exit traps" +function cleanup { + eval "$exit_trap_command" +} +trap cleanup EXIT + +function add_exit_trap { + local to_add=$1 + exit_trap_command="$exit_trap_command; $to_add" +} + usage() { echo $"Usage: $0 " exit 1 @@ -48,6 +59,7 @@ setup_opensearch() { echo ${cid} } +# bearer token propagaton test uses real query service, but starts a fake DB at 19200 setup_query() { local distro=$1 local os=$(go env GOOS) @@ -55,15 +67,16 @@ setup_query() { local params=( --es.tls.enabled=false --es.version=7 - --es.server-urls=http://127.0.0.1:9200 + --es.server-urls=http://127.0.0.1:19200 --query.bearer-token-propagation=true ) SPAN_STORAGE_TYPE=${distro} ./cmd/query/query-${os}-${arch} ${params[@]} } wait_for_storage() { - local url=$1 - local cid=$2 + local distro=$1 + local url=$2 + local cid=$3 local params=( --silent --output @@ -81,12 +94,12 @@ wait_for_storage() { done # after the loop, do final verification and set status as global var if [[ "$(curl ${params[@]} ${url})" != "200" ]]; then - echo "ERROR: elasticsearch/opensearch is not reachable" + echo "ERROR: ${distro} is not ready" docker logs ${cid} docker kill ${cid} db_is_up=0 else - echo "SUCCESS: elasticsearch/opensearch is reachable" + echo "SUCCESS: ${distro} is ready" db_is_up=1 fi } @@ -105,11 +118,11 @@ bring_up_storage() { echo "Unknown distribution $distro. Valid options are opensearch or elasticsearch" usage fi - wait_for_storage "http://localhost:9200" ${cid} + wait_for_storage ${distro} "http://localhost:9200" ${cid} if [ ${db_is_up} = "1" ]; then break else - echo "ERROR: unable to start elasticsearch/opensearch" + echo "ERROR: unable to start ${distro}" exit 1 fi done @@ -143,8 +156,8 @@ run_token_propagation_test() { build_query setup_query ${distro} & local pid=$! + add_exit_trap "teardown_query ${pid}" make token-propagation-integration-test - teardown_query ${pid} } main() { @@ -152,7 +165,7 @@ main() { echo "Preparing $1 $2" bring_up_storage "$1" "$2" - trap "teardown_storage ${db_cid}" EXIT + add_exit_trap "teardown_storage ${db_cid}" echo "Executing main integration tests" run_integration_test "$1"