Skip to content

Commit

Permalink
opensearchapi: Add Point in Time integration test
Browse files Browse the repository at this point in the history
Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
  • Loading branch information
Jakob3xD committed Mar 23, 2023
1 parent d494087 commit f9f678b
Showing 1 changed file with 66 additions and 13 deletions.
79 changes: 66 additions & 13 deletions opensearchapi/opensearchapi_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ import (
"github.com/opensearch-project/opensearch-go/v2/opensearchapi"
)

func createTestIndex(client *opensearch.Client, index string) error {
var buf bytes.Buffer
// Index data
//
for j := 1; j <= 1000; j++ {
meta := []byte(fmt.Sprintf(`{ "index" : { "_id" : "%d" } }%s`, j, "\n"))
data := []byte(`{"content":"` + strings.Repeat("ABC", 100) + `"}`)
data = append(data, "\n"...)

buf.Grow(len(meta) + len(data))
buf.Write(meta)
buf.Write(data)
}
_, err := client.Bulk(bytes.NewReader(buf.Bytes()), client.Bulk.WithIndex(index), client.Bulk.WithRefresh("true"))
return err
}

func TestAPI(t *testing.T) {
t.Run("Search", func(t *testing.T) {
client, err := opensearch.NewDefaultClient()
Expand Down Expand Up @@ -82,8 +99,6 @@ func TestAPI(t *testing.T) {

t.Run("OpaqueID", func(t *testing.T) {
var (
buf bytes.Buffer

res *opensearchapi.Response
err error

Expand All @@ -101,20 +116,10 @@ func TestAPI(t *testing.T) {

// Index data
//
for j := 1; j <= 1000; j++ {
meta := []byte(fmt.Sprintf(`{ "index" : { "_id" : "%d" } }%s`, j, "\n"))
data := []byte(`{"content":"` + strings.Repeat("ABC", 100) + `"}`)
data = append(data, "\n"...)

buf.Grow(len(meta) + len(data))
buf.Write(meta)
buf.Write(data)
}
res, err = client.Bulk(bytes.NewReader(buf.Bytes()), client.Bulk.WithIndex("test"), client.Bulk.WithRefresh("true"))
err = createTestIndex(client, "test")
if err != nil {
t.Fatalf("Failed to index data: %s", err)
}
defer res.Body.Close()

// Launch reindexing task with wait_for_completion=false
//
Expand Down Expand Up @@ -192,4 +197,52 @@ func TestAPI(t *testing.T) {
}
}
})
t.Run("Point_in_Time", func(t *testing.T) {
var err error
index := "test"
// Create Client
//
client, err := opensearch.NewDefaultClient()
if err != nil {
t.Fatalf("Error creating the client: %s\n", err)
}
resp, _, err := client.PointInTime.Delete(client.PointInTime.Delete.WithAllPits(true))
if err != nil {
if resp != nil && resp.StatusCode != 404 {
t.Fatalf("Failed to Delete all Pits: %s", err)
}
}

// Index data
//
err = createTestIndex(client, index)
if err != nil {
t.Fatalf("Failed to index data: %s", err)
}

// Create a Pit
//
keepAlive, _ := time.ParseDuration("5m")
_, pitCreateResp, err := client.PointInTime.Create(client.PointInTime.Create.WithKeepAlive(keepAlive), client.PointInTime.Create.WithIndex(index))
if err != nil {
t.Fatalf("Failed to create Pit: %s", err)
}

// Get all Pits
//
_, pitGetResp, err := client.PointInTime.Get()
if err != nil {
t.Fatalf("Failed to get Pits: %s", err)
}

// Delete the create Pit
//
_, pitDeleteResp, err := client.PointInTime.Delete(client.PointInTime.Delete.WithPitID(pitCreateResp.PitID))
if err != nil {
t.Fatalf("Failed to delete Pit: %s", err)
}
if (pitCreateResp.PitID != pitGetResp.Pits[0].PitID) || (pitCreateResp.PitID != pitDeleteResp.Pits[0].PitID) {
t.Fatalf("The create Pit does not match the Get Pit or Deleted Pit")
}
})
}

0 comments on commit f9f678b

Please sign in to comment.