diff --git a/cat_snapshots_integration_test.go b/cat_snapshots_integration_test.go new file mode 100644 index 00000000..a4b3a88f --- /dev/null +++ b/cat_snapshots_integration_test.go @@ -0,0 +1,68 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "testing" + "time" +) + +func TestCatSnapshotsIntegration(t *testing.T) { + if isCI() { + t.Skip("this test requires local directories") + } + + client := setupTestClientAndCreateIndexAndAddDocs(t, SetDecoder(&strictDecoder{})) // , SetTraceLog(log.New(os.Stdout, "", 0))) + + { + // Create a repository for this test + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + _, err := client.SnapshotCreateRepository("my_backup"). + Type("fs"). + Settings(map[string]interface{}{ + // Notice the path is configured as path.repo in docker-compose.yml + "location": "/usr/share/elasticsearch/backup", + }). + Do(ctx) + if err != nil { + t.Fatal(err) + } + + // Make a snapshot + ctx, cancel = context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + _, err = client.SnapshotCreate("my_backup", "snapshot_1"). + WaitForCompletion(true). + Do(ctx) + if err != nil { + t.Fatal(err) + } + + defer func() { + // Remove snapshot + _, _ = client.SnapshotDelete("my_backup", "snapshot_1").Do(context.Background()) + // Remove repository + _, _ = client.SnapshotDeleteRepository("my_backup").Do(context.Background()) + }() + } + + // List snapshots of repository + ctx := context.Background() + res, err := client.CatSnapshots().Repository("my_backup").Columns("*").Do(ctx) + if err != nil { + t.Fatal(err) + } + if res == nil { + t.Fatal("want response, have nil") + } + if want, have := 1, len(res); want != have { + t.Fatalf("want %d snapshot, have %d", want, have) + } + if want, have := "snapshot_1", res[0].ID; want != have { + t.Fatalf("want ID=%q, have %q", want, have) + } +} diff --git a/cat_snapshots_test.go b/cat_snapshots_test.go index 61e9caff..816d8f4e 100644 --- a/cat_snapshots_test.go +++ b/cat_snapshots_test.go @@ -5,60 +5,20 @@ package elastic import ( - "context" "testing" - "time" ) func TestCatSnapshots(t *testing.T) { client := setupTestClientAndCreateIndexAndAddDocs(t, SetDecoder(&strictDecoder{})) // , SetTraceLog(log.New(os.Stdout, "", 0))) - { - // Create a repository for this test - ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) - defer cancel() - _, err := client.SnapshotCreateRepository("my_backup"). - Type("fs"). - Settings(map[string]interface{}{ - // Notice the path is configured as path.repo in docker-compose.yml - "location": "/usr/share/elasticsearch/backup", - }). - Do(ctx) - if err != nil { - t.Fatal(err) - } - - // Make a snapshot - ctx, cancel = context.WithTimeout(context.Background(), 15*time.Second) - defer cancel() - _, err = client.SnapshotCreate("my_backup", "snapshot_1"). - WaitForCompletion(true). - Do(ctx) - if err != nil { - t.Fatal(err) - } - - defer func() { - // Remove snapshot - _, _ = client.SnapshotDelete("my_backup", "snapshot_1").Do(context.Background()) - // Remove repository - _, _ = client.SnapshotDeleteRepository("my_backup").Do(context.Background()) - }() - } - - // List snapshots of repository - ctx := context.Background() - res, err := client.CatSnapshots().Repository("my_backup").Columns("*").Do(ctx) + urls, params, err := client.CatSnapshots().Repository("my_repo").Columns("*").buildURL() if err != nil { t.Fatal(err) } - if res == nil { - t.Fatal("want response, have nil") - } - if want, have := 1, len(res); want != have { - t.Fatalf("want %d snapshot, have %d", want, have) + if want, have := "/_cat/snapshots/my_repo", urls; want != have { + t.Fatalf("want URL=%q, have %q", want, have) } - if want, have := "snapshot_1", res[0].ID; want != have { - t.Fatalf("want ID=%q, have %q", want, have) + if want, have := "format=json&h=%2A", params.Encode(); want != have { + t.Fatalf("want Params=%q, have %q", want, have) } } diff --git a/client_test.go b/client_test.go index 0a59ad30..095745b1 100644 --- a/client_test.go +++ b/client_test.go @@ -90,7 +90,7 @@ func TestClientWithoutURL(t *testing.T) { if len(client.conns) == 0 { t.Fatalf("expected at least 1 node in the cluster, got: %d (%v)", len(client.conns), client.conns) } - if !isTravis() { + if !isCI() { if _, found := findConn(DefaultURL, client.conns...); !found { t.Errorf("expected to find node with default URL of %s in %v", DefaultURL, client.conns) } @@ -108,7 +108,7 @@ func TestClientWithSingleURL(t *testing.T) { if len(client.conns) == 0 { t.Fatalf("expected at least 1 node in the cluster, got: %d (%v)", len(client.conns), client.conns) } - if !isTravis() { + if !isCI() { if _, found := findConn(DefaultURL, client.conns...); !found { t.Errorf("expected to find node with default URL of %s in %v", DefaultURL, client.conns) } @@ -124,7 +124,7 @@ func TestClientWithMultipleURLs(t *testing.T) { if len(client.conns) != 1 { t.Fatalf("expected exactly 1 node in the local cluster, got: %d (%v)", len(client.conns), client.conns) } - if !isTravis() { + if !isCI() { if client.conns[0].URL() != DefaultURL { t.Errorf("expected to find node with default URL of %s in %v", DefaultURL, client.conns) } @@ -269,7 +269,7 @@ func TestClientFromConfig(t *testing.T) { if len(client.conns) == 0 { t.Fatalf("expected at least 1 node in the cluster, got: %d (%v)", len(client.conns), client.conns) } - if !isTravis() { + if !isCI() { if _, found := findConn(DefaultURL, client.conns...); !found { t.Errorf("expected to find node with default URL of %s in %v", DefaultURL, client.conns) } @@ -291,7 +291,7 @@ func TestClientDialFromConfig(t *testing.T) { if len(client.conns) == 0 { t.Fatalf("expected at least 1 node in the cluster, got: %d (%v)", len(client.conns), client.conns) } - if !isTravis() { + if !isCI() { if _, found := findConn(DefaultURL, client.conns...); !found { t.Errorf("expected to find node with default URL of %s in %v", DefaultURL, client.conns) } diff --git a/setup_test.go b/setup_test.go index 38964b89..10614575 100644 --- a/setup_test.go +++ b/setup_test.go @@ -265,16 +265,10 @@ type doctype struct { Message string `json:"message"` } -func isTravis() bool { - return os.Getenv("TRAVIS") != "" +func isCI() bool { + return os.Getenv("TRAVIS") != "" || os.Getenv("CI") != "" || os.Getenv("GITHUB_ACTIONS") != "" } -func travisGoVersion() string { - return os.Getenv("TRAVIS_GO_VERSION") -} - -var _ = travisGoVersion // remove unused warning in staticcheck - type logger interface { Error(args ...interface{}) Errorf(format string, args ...interface{}) diff --git a/termvectors_test.go b/termvectors_test.go index 7e088704..8f31f485 100644 --- a/termvectors_test.go +++ b/termvectors_test.go @@ -102,7 +102,7 @@ func TestTermVectorsWithDoc(t *testing.T) { client := setupTestClientAndCreateIndex(t) // Travis lags sometimes - if isTravis() { + if isCI() { time.Sleep(2 * time.Second) } @@ -136,7 +136,7 @@ func TestTermVectorsWithFilter(t *testing.T) { client := setupTestClientAndCreateIndex(t) // Travis lags sometimes - if isTravis() { + if isCI() { time.Sleep(2 * time.Second) }