code.gitea.io/gitea@v1.22.3/modules/indexer/issues/elasticsearch/elasticsearch_test.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package elasticsearch
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"os"
    10  	"testing"
    11  	"time"
    12  
    13  	"code.gitea.io/gitea/modules/indexer/issues/internal/tests"
    14  )
    15  
    16  func TestElasticsearchIndexer(t *testing.T) {
    17  	// The elasticsearch instance started by pull-db-tests.yml > test-unit > services > elasticsearch
    18  	url := "http://elastic:changeme@elasticsearch:9200"
    19  
    20  	if os.Getenv("CI") == "" {
    21  		// Make it possible to run tests against a local elasticsearch instance
    22  		url = os.Getenv("TEST_ELASTICSEARCH_URL")
    23  		if url == "" {
    24  			t.Skip("TEST_ELASTICSEARCH_URL not set and not running in CI")
    25  			return
    26  		}
    27  	}
    28  
    29  	ok := false
    30  	for i := 0; i < 60; i++ {
    31  		resp, err := http.Get(url)
    32  		if err == nil && resp.StatusCode == http.StatusOK {
    33  			ok = true
    34  			break
    35  		}
    36  		t.Logf("Waiting for elasticsearch to be up: %v", err)
    37  		time.Sleep(time.Second)
    38  	}
    39  	if !ok {
    40  		t.Fatalf("Failed to wait for elasticsearch to be up")
    41  		return
    42  	}
    43  
    44  	indexer := NewIndexer(url, fmt.Sprintf("test_elasticsearch_indexer_%d", time.Now().Unix()))
    45  	defer indexer.Close()
    46  
    47  	tests.TestIndexer(t, indexer)
    48  }