Skip to content
Snippets Groups Projects
Commit 6057d2de authored by Nick Thomas's avatar Nick Thomas
Browse files

Improve (non-integration) test coverage in elastic

parent 39b434b0
No related branches found
No related tags found
1 merge request!1Initial implementation of an elasticsearch indexer in Go
Pipeline #
Loading
Loading
@@ -3,6 +3,7 @@ package elastic
import (
"context"
"fmt"
"net/http"
"os"
"strings"
 
Loading
Loading
@@ -60,7 +61,7 @@ func NewClient(config *Config) (*Client, error) {
if config.AWS {
credentials := credentials.NewStaticCredentials(config.AccessKey, config.SecretKey, "")
signer := v4.NewSigner(credentials)
awsClient, err := aws_signing_client.New(signer, nil, "es", config.Region)
awsClient, err := aws_signing_client.New(signer, &http.Client{}, "es", config.Region)
if err != nil {
return nil, err
}
Loading
Loading
@@ -108,6 +109,10 @@ func (c *Client) Flush() error {
return c.bulk.Flush()
}
 
func (c *Client) Close() {
c.Client.Stop()
}
func (c *Client) Index(id string, thing interface{}) {
req := elastic.NewBulkIndexRequest().
Index(c.IndexName).
Loading
Loading
Loading
Loading
@@ -2,21 +2,30 @@ package elastic_test
 
import (
"crypto/tls"
"fmt"
"net/http"
"net/http/httptest"
"os"
"regexp"
"strings"
"testing"
"time"
 
"github.com/stretchr/testify/assert"
 
"gitlab.com/gitlab-org/es-git-go/elastic"
)
 
const (
projectID = "667"
)
func TestAWSConfiguration(t *testing.T) {
var req *http.Request
 
// httptest certificate is unsigned
transport := http.DefaultTransport
defer func() { http.DefaultTransport = transport }()
http.DefaultTransport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
 
f := func(w http.ResponseWriter, r *http.Request) {
Loading
Loading
@@ -40,8 +49,9 @@ func TestAWSConfiguration(t *testing.T) {
))
assert.NoError(t, err)
 
_, err = elastic.NewClient(config)
client, err := elastic.NewClient(config)
assert.NoError(t, err)
defer client.Close()
 
if assert.NotNil(t, req) {
authRE := regexp.MustCompile(`\AAWS4-HMAC-SHA256 Credential=0/\d{8}/us-east-1/es/aws4_request, SignedHeaders=accept;date;host;x-amz-date, Signature=[a-f0-9]{64}\z`)
Loading
Loading
@@ -49,3 +59,44 @@ func TestAWSConfiguration(t *testing.T) {
assert.NotEqual(t, "", req.Header.Get("X-Amz-Date"))
}
}
func TestElasticClientIndexAndRetrieval(t *testing.T) {
config := os.Getenv("ELASTIC_CONNECTION_INFO")
if config == "" {
t.Log("ELASTIC_CONNECTION_INFO not set")
t.SkipNow()
}
os.Setenv("RAILS_ENV", fmt.Sprintf("test-%d", time.Now().Unix()))
client, err := elastic.FromEnv(projectID)
assert.NoError(t, err)
assert.Equal(t, projectID, client.ParentID())
assert.NoError(t, client.CreateIndex())
blobDoc := map[string]interface{}{}
client.Index(projectID+"_foo", blobDoc)
commitDoc := map[string]interface{}{}
client.Index(projectID+"_0000", commitDoc)
assert.NoError(t, client.Flush())
blob, err := client.GetBlob("foo")
assert.NoError(t, err)
assert.Equal(t, true, blob.Found)
commit, err := client.GetCommit("0000")
assert.NoError(t, err)
assert.Equal(t, true, commit.Found)
client.Remove(projectID + "_foo")
assert.NoError(t, client.Flush())
_, err = client.GetBlob("foo")
assert.Error(t, err)
assert.NoError(t, client.DeleteIndex())
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment