Skip to content

Commit 175d39e

Browse files
committed
Fix CI tests
1 parent 4a09914 commit 175d39e

5 files changed

+82
-60
lines changed

cat_snapshots_integration_test.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2012-present Oliver Eilhard. All rights reserved.
2+
// Use of this source code is governed by a MIT-license.
3+
// See http://olivere.mit-license.org/license.txt for details.
4+
5+
package elastic
6+
7+
import (
8+
"context"
9+
"testing"
10+
"time"
11+
)
12+
13+
func TestCatSnapshotsIntegration(t *testing.T) {
14+
if isCI() {
15+
t.Skip("this test requires local directories")
16+
}
17+
18+
client := setupTestClientAndCreateIndexAndAddDocs(t, SetDecoder(&strictDecoder{})) // , SetTraceLog(log.New(os.Stdout, "", 0)))
19+
20+
{
21+
// Create a repository for this test
22+
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
23+
defer cancel()
24+
_, err := client.SnapshotCreateRepository("my_backup").
25+
Type("fs").
26+
Settings(map[string]interface{}{
27+
// Notice the path is configured as path.repo in docker-compose.yml
28+
"location": "/usr/share/elasticsearch/backup",
29+
}).
30+
Do(ctx)
31+
if err != nil {
32+
t.Fatal(err)
33+
}
34+
35+
// Make a snapshot
36+
ctx, cancel = context.WithTimeout(context.Background(), 15*time.Second)
37+
defer cancel()
38+
_, err = client.SnapshotCreate("my_backup", "snapshot_1").
39+
WaitForCompletion(true).
40+
Do(ctx)
41+
if err != nil {
42+
t.Fatal(err)
43+
}
44+
45+
defer func() {
46+
// Remove snapshot
47+
_, _ = client.SnapshotDelete("my_backup", "snapshot_1").Do(context.Background())
48+
// Remove repository
49+
_, _ = client.SnapshotDeleteRepository("my_backup").Do(context.Background())
50+
}()
51+
}
52+
53+
// List snapshots of repository
54+
ctx := context.Background()
55+
res, err := client.CatSnapshots().Repository("my_backup").Columns("*").Do(ctx)
56+
if err != nil {
57+
t.Fatal(err)
58+
}
59+
if res == nil {
60+
t.Fatal("want response, have nil")
61+
}
62+
if want, have := 1, len(res); want != have {
63+
t.Fatalf("want %d snapshot, have %d", want, have)
64+
}
65+
if want, have := "snapshot_1", res[0].ID; want != have {
66+
t.Fatalf("want ID=%q, have %q", want, have)
67+
}
68+
}

cat_snapshots_test.go

+5-45
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,20 @@
55
package elastic
66

77
import (
8-
"context"
98
"testing"
10-
"time"
119
)
1210

1311
func TestCatSnapshots(t *testing.T) {
1412
client := setupTestClientAndCreateIndexAndAddDocs(t, SetDecoder(&strictDecoder{})) // , SetTraceLog(log.New(os.Stdout, "", 0)))
1513

16-
{
17-
// Create a repository for this test
18-
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
19-
defer cancel()
20-
_, err := client.SnapshotCreateRepository("my_backup").
21-
Type("fs").
22-
Settings(map[string]interface{}{
23-
// Notice the path is configured as path.repo in docker-compose.yml
24-
"location": "/usr/share/elasticsearch/backup",
25-
}).
26-
Do(ctx)
27-
if err != nil {
28-
t.Fatal(err)
29-
}
30-
31-
// Make a snapshot
32-
ctx, cancel = context.WithTimeout(context.Background(), 15*time.Second)
33-
defer cancel()
34-
_, err = client.SnapshotCreate("my_backup", "snapshot_1").
35-
WaitForCompletion(true).
36-
Do(ctx)
37-
if err != nil {
38-
t.Fatal(err)
39-
}
40-
41-
defer func() {
42-
// Remove snapshot
43-
_, _ = client.SnapshotDelete("my_backup", "snapshot_1").Do(context.Background())
44-
// Remove repository
45-
_, _ = client.SnapshotDeleteRepository("my_backup").Do(context.Background())
46-
}()
47-
}
48-
49-
// List snapshots of repository
50-
ctx := context.Background()
51-
res, err := client.CatSnapshots().Repository("my_backup").Columns("*").Do(ctx)
14+
urls, params, err := client.CatSnapshots().Repository("my_repo").Columns("*").buildURL()
5215
if err != nil {
5316
t.Fatal(err)
5417
}
55-
if res == nil {
56-
t.Fatal("want response, have nil")
57-
}
58-
if want, have := 1, len(res); want != have {
59-
t.Fatalf("want %d snapshot, have %d", want, have)
18+
if want, have := "/_cat/snapshots/my_repo", urls; want != have {
19+
t.Fatalf("want URL=%q, have %q", want, have)
6020
}
61-
if want, have := "snapshot_1", res[0].ID; want != have {
62-
t.Fatalf("want ID=%q, have %q", want, have)
21+
if want, have := "format=json&h=%2A", params.Encode(); want != have {
22+
t.Fatalf("want Params=%q, have %q", want, have)
6323
}
6424
}

client_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestClientWithoutURL(t *testing.T) {
9090
if len(client.conns) == 0 {
9191
t.Fatalf("expected at least 1 node in the cluster, got: %d (%v)", len(client.conns), client.conns)
9292
}
93-
if !isTravis() {
93+
if !isCI() {
9494
if _, found := findConn(DefaultURL, client.conns...); !found {
9595
t.Errorf("expected to find node with default URL of %s in %v", DefaultURL, client.conns)
9696
}
@@ -108,7 +108,7 @@ func TestClientWithSingleURL(t *testing.T) {
108108
if len(client.conns) == 0 {
109109
t.Fatalf("expected at least 1 node in the cluster, got: %d (%v)", len(client.conns), client.conns)
110110
}
111-
if !isTravis() {
111+
if !isCI() {
112112
if _, found := findConn(DefaultURL, client.conns...); !found {
113113
t.Errorf("expected to find node with default URL of %s in %v", DefaultURL, client.conns)
114114
}
@@ -124,7 +124,7 @@ func TestClientWithMultipleURLs(t *testing.T) {
124124
if len(client.conns) != 1 {
125125
t.Fatalf("expected exactly 1 node in the local cluster, got: %d (%v)", len(client.conns), client.conns)
126126
}
127-
if !isTravis() {
127+
if !isCI() {
128128
if client.conns[0].URL() != DefaultURL {
129129
t.Errorf("expected to find node with default URL of %s in %v", DefaultURL, client.conns)
130130
}
@@ -269,7 +269,7 @@ func TestClientFromConfig(t *testing.T) {
269269
if len(client.conns) == 0 {
270270
t.Fatalf("expected at least 1 node in the cluster, got: %d (%v)", len(client.conns), client.conns)
271271
}
272-
if !isTravis() {
272+
if !isCI() {
273273
if _, found := findConn(DefaultURL, client.conns...); !found {
274274
t.Errorf("expected to find node with default URL of %s in %v", DefaultURL, client.conns)
275275
}
@@ -291,7 +291,7 @@ func TestClientDialFromConfig(t *testing.T) {
291291
if len(client.conns) == 0 {
292292
t.Fatalf("expected at least 1 node in the cluster, got: %d (%v)", len(client.conns), client.conns)
293293
}
294-
if !isTravis() {
294+
if !isCI() {
295295
if _, found := findConn(DefaultURL, client.conns...); !found {
296296
t.Errorf("expected to find node with default URL of %s in %v", DefaultURL, client.conns)
297297
}

setup_test.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -265,16 +265,10 @@ type doctype struct {
265265
Message string `json:"message"`
266266
}
267267

268-
func isTravis() bool {
269-
return os.Getenv("TRAVIS") != ""
268+
func isCI() bool {
269+
return os.Getenv("TRAVIS") != "" || os.Getenv("CI") != "" || os.Getenv("GITHUB_ACTIONS") != ""
270270
}
271271

272-
func travisGoVersion() string {
273-
return os.Getenv("TRAVIS_GO_VERSION")
274-
}
275-
276-
var _ = travisGoVersion // remove unused warning in staticcheck
277-
278272
type logger interface {
279273
Error(args ...interface{})
280274
Errorf(format string, args ...interface{})

termvectors_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func TestTermVectorsWithDoc(t *testing.T) {
102102
client := setupTestClientAndCreateIndex(t)
103103

104104
// Travis lags sometimes
105-
if isTravis() {
105+
if isCI() {
106106
time.Sleep(2 * time.Second)
107107
}
108108

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

138138
// Travis lags sometimes
139-
if isTravis() {
139+
if isCI() {
140140
time.Sleep(2 * time.Second)
141141
}
142142

0 commit comments

Comments
 (0)