Skip to content

Commit

Permalink
Fix CI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olivere committed Jul 30, 2021
1 parent 4a09914 commit 175d39e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 60 deletions.
68 changes: 68 additions & 0 deletions cat_snapshots_integration_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
50 changes: 5 additions & 45 deletions cat_snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
10 changes: 5 additions & 5 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
10 changes: 2 additions & 8 deletions setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down
4 changes: 2 additions & 2 deletions termvectors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestTermVectorsWithDoc(t *testing.T) {
client := setupTestClientAndCreateIndex(t)

// Travis lags sometimes
if isTravis() {
if isCI() {
time.Sleep(2 * time.Second)
}

Expand Down Expand Up @@ -136,7 +136,7 @@ func TestTermVectorsWithFilter(t *testing.T) {
client := setupTestClientAndCreateIndex(t)

// Travis lags sometimes
if isTravis() {
if isCI() {
time.Sleep(2 * time.Second)
}

Expand Down

0 comments on commit 175d39e

Please sign in to comment.