Skip to content

Commit

Permalink
adjust tests to run on secure and unsecure clusters depending on env
Browse files Browse the repository at this point in the history
Signed-off-by: Jakob Hahn <[email protected]>
  • Loading branch information
Jakob3xD committed Mar 13, 2024
1 parent eceac12 commit 909ec5f
Show file tree
Hide file tree
Showing 41 changed files with 326 additions and 237 deletions.
48 changes: 37 additions & 11 deletions opensearch_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,27 @@ package opensearch_test
import (
"context"
"crypto/tls"
"github.com/stretchr/testify/require"
"log"
"net"
"net/http"
"net/url"
"os"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/opensearch-project/opensearch-go/v3"
osapitest "github.com/opensearch-project/opensearch-go/v3/internal/test"
"github.com/opensearch-project/opensearch-go/v3/opensearchapi"
"github.com/opensearch-project/opensearch-go/v3/opensearchtransport"
)

func TestClientTransport(t *testing.T) {
/*
t.Run("Persistent", func(t *testing.T) {
client, err := opensearchapi.NewDefaultClient()
client, err := osapitest.NewClient()
if err != nil {
t.Fatalf("Error creating the client: %s", err)
}
Expand Down Expand Up @@ -87,7 +90,7 @@ func TestClientTransport(t *testing.T) {
t.Run("Concurrent", func(t *testing.T) {
var wg sync.WaitGroup

client, err := opensearchapi.NewDefaultClient()
client, err := osapitest.NewClient()
if err != nil {
t.Fatalf("Error creating the client: %s", err)
}
Expand All @@ -108,7 +111,7 @@ func TestClientTransport(t *testing.T) {
})

t.Run("WithContext", func(t *testing.T) {
client, err := opensearchapi.NewDefaultClient()
client, err := osapitest.NewClient()
if err != nil {
t.Fatalf("Error creating the client: %s", err)
}
Expand Down Expand Up @@ -166,15 +169,18 @@ func (t *CustomTransport) RoundTrip(req *http.Request) (*http.Response, error) {

func TestClientCustomTransport(t *testing.T) {
t.Run("Customized", func(t *testing.T) {
cfg := opensearchapi.Config{
Client: opensearch.Config{
Transport: &CustomTransport{
client: http.DefaultClient,
cfg, err := osapitest.ClientConfig()
require.Nil(t, err)

cfg.Client.Transport = &CustomTransport{
client: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
},
}

client, err := opensearchapi.NewClient(cfg)
client, err := opensearchapi.NewClient(*cfg)
if err != nil {
t.Fatalf("Error creating the client: %s", err)
}
Expand All @@ -194,6 +200,19 @@ func TestClientCustomTransport(t *testing.T) {
},
Transport: http.DefaultTransport,
})
if os.Getenv("SECURE_INTEGRATION") == "true" {
password, _ := osapitest.GetPassword()
tp, _ = opensearchtransport.New(opensearchtransport.Config{
URLs: []*url.URL{
{Scheme: "https", Host: "localhost:9200"},
},
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Username: "admin",
Password: password,
})
}

client := opensearchapi.Client{
Client: &opensearch.Client{
Expand All @@ -217,10 +236,17 @@ type ReplacedTransport struct {
func (t *ReplacedTransport) Perform(req *http.Request) (*http.Response, error) {
req.URL.Scheme = "http"
req.URL.Host = "localhost:9200"
password, _ := osapitest.GetPassword()
if os.Getenv("SECURE_INTEGRATION") == "true" {
req.URL.Scheme = "https"
req.SetBasicAuth("admin", password)
}

atomic.AddUint64(&t.counter, 1)

return http.DefaultTransport.RoundTrip(req)
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
return transport.RoundTrip(req)
}

func (t *ReplacedTransport) Count() uint64 {
Expand Down Expand Up @@ -251,7 +277,7 @@ func TestClientReplaceTransport(t *testing.T) {

func TestClientAPI(t *testing.T) {
t.Run("Info", func(t *testing.T) {
client, err := opensearchapi.NewDefaultClient()
client, err := osapitest.NewClient()
if err != nil {
log.Fatalf("Error creating the client: %s\n", err)
}
Expand Down
37 changes: 4 additions & 33 deletions opensearch_secure_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build integration && secure
//go:build integration

package opensearch_test

import (
"context"
"crypto/tls"
"errors"
"log"
"net/http"
"testing"

"github.com/stretchr/testify/assert"

"github.com/opensearch-project/opensearch-go/v3"
"github.com/opensearch-project/opensearch-go/v3/opensearchapi"
ostest "github.com/opensearch-project/opensearch-go/v3/internal/test"
)

func getSecuredClient() (*opensearchapi.Client, error) {
errs := make([]error, 0)
for _, password := range []string{"admin", "myStrongPassword123!"} {
client, _ := opensearchapi.NewClient(
opensearchapi.Config{
Client: opensearch.Config{
Username: "admin",
Password: password,
Addresses: []string{"https://localhost:9200"},
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
},
},
)
_, err := client.Info(nil, nil)
if err != nil {
errs = append(errs, err)
continue
}
return client, nil
}
return nil, errors.Join(errs...)

}

func TestSecuredClientAPI(t *testing.T) {
t.Run("Check Info", func(t *testing.T) {
ostest.SkipIfNotSecure(t)
ctx := context.Background()
client, err := getSecuredClient()
client, err := ostest.NewClient()
if err != nil {
log.Fatalf("Error creating the client: %s\n", err)
}
Expand Down
5 changes: 3 additions & 2 deletions opensearchapi/api_aliases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (

"github.com/stretchr/testify/require"

ostest "github.com/opensearch-project/opensearch-go/v3/internal/test"
"github.com/opensearch-project/opensearch-go/v3/opensearchapi"
osapitest "github.com/opensearch-project/opensearch-go/v3/opensearchapi/internal/test"
)

func TestAliases(t *testing.T) {
t.Run("Aliases", func(t *testing.T) {
client, err := opensearchapi.NewDefaultClient()
client, err := ostest.NewClient()
require.Nil(t, err)

index := "test-aliases"
Expand All @@ -41,7 +42,7 @@ func TestAliases(t *testing.T) {
require.Nil(t, err)
require.NotEmpty(t, resp)
require.NotEmpty(t, resp.Inspect().Response)
osapitest.CompareRawJSONwithParsedJSON(t, resp, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp, resp.Inspect().Response)
})

t.Run("inspect", func(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions opensearchapi/api_bulk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

ostest "github.com/opensearch-project/opensearch-go/v3/internal/test"
"github.com/opensearch-project/opensearch-go/v3/opensearchapi"
osapitest "github.com/opensearch-project/opensearch-go/v3/opensearchapi/internal/test"
)

func TestBulkClient(t *testing.T) {
client, err := opensearchapi.NewDefaultClient()
client, err := ostest.NewClient()
require.Nil(t, err)

index := "test-bulk"
Expand Down Expand Up @@ -58,7 +59,7 @@ func TestBulkClient(t *testing.T) {
)
require.Nil(t, err)
assert.NotEmpty(t, res)
osapitest.CompareRawJSONwithParsedJSON(t, res, res.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, res, res.Inspect().Response)
})
}
t.Run("inspect", func(t *testing.T) {
Expand Down
47 changes: 24 additions & 23 deletions opensearchapi/api_cat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

ostest "github.com/opensearch-project/opensearch-go/v3/internal/test"
"github.com/opensearch-project/opensearch-go/v3/opensearchapi"
osapitest "github.com/opensearch-project/opensearch-go/v3/opensearchapi/internal/test"
)

func TestCatClient(t *testing.T) {
client, err := opensearchapi.NewDefaultClient()
client, err := ostest.NewClient()
require.Nil(t, err)
failingClient, err := osapitest.CreateFailingClient()
require.Nil(t, err)
Expand Down Expand Up @@ -365,7 +366,7 @@ func TestCatClient(t *testing.T) {
for catType, value := range testCases {
t.Run(catType, func(t *testing.T) {
if catType == "ClusterManager" {
osapitest.SkipIfBelowVersion(t, client, 1, 3, catType)
ostest.SkipIfBelowVersion(t, client, 1, 3, catType)
}
for _, testCase := range value {
t.Run(testCase.Name, func(t *testing.T) {
Expand All @@ -389,124 +390,124 @@ func TestCatClient(t *testing.T) {
resp, err := client.Cat.Aliases(nil, nil)
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.Aliases, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.Aliases, resp.Inspect().Response)
})
t.Run("Allocation", func(t *testing.T) {
resp, err := client.Cat.Allocation(nil, nil)
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.Allocations, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.Allocations, resp.Inspect().Response)
})
t.Run("ClusterManager", func(t *testing.T) {
osapitest.SkipIfBelowVersion(t, client, 1, 3, "ClusterManager")
ostest.SkipIfBelowVersion(t, client, 1, 3, "ClusterManager")
resp, err := client.Cat.ClusterManager(nil, nil)
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.ClusterManagers, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.ClusterManagers, resp.Inspect().Response)
})
t.Run("Count", func(t *testing.T) {
resp, err := client.Cat.Count(nil, nil)
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.Counts, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.Counts, resp.Inspect().Response)
})
t.Run("FieldData", func(t *testing.T) {
resp, err := client.Cat.FieldData(nil, nil)
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.FieldData, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.FieldData, resp.Inspect().Response)
})
t.Run("Health", func(t *testing.T) {
resp, err := client.Cat.Health(nil, nil)
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.Health, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.Health, resp.Inspect().Response)
})
t.Run("Indices", func(t *testing.T) {
resp, err := client.Cat.Indices(nil, &opensearchapi.CatIndicesReq{Params: opensearchapi.CatIndicesParams{H: []string{"*"}}})
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.Indices, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.Indices, resp.Inspect().Response)
})
t.Run("Master", func(t *testing.T) {
resp, err := client.Cat.Master(nil, nil)
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.Master, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.Master, resp.Inspect().Response)
})
t.Run("NodeAttrs", func(t *testing.T) {
resp, err := client.Cat.NodeAttrs(nil, &opensearchapi.CatNodeAttrsReq{Params: opensearchapi.CatNodeAttrsParams{H: []string{"*"}}})
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.NodeAttrs, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.NodeAttrs, resp.Inspect().Response)
})
t.Run("Nodes", func(t *testing.T) {
resp, err := client.Cat.Nodes(nil, &opensearchapi.CatNodesReq{Params: opensearchapi.CatNodesParams{H: []string{"*"}}})
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.Nodes, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.Nodes, resp.Inspect().Response)
})
t.Run("PendingTasks", func(t *testing.T) {
resp, err := client.Cat.PendingTasks(nil, nil)
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.PendingTasks, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.PendingTasks, resp.Inspect().Response)
})
t.Run("Plugins", func(t *testing.T) {
resp, err := client.Cat.Plugins(nil, nil)
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.Plugins, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.Plugins, resp.Inspect().Response)
})
t.Run("Recovery", func(t *testing.T) {
resp, err := client.Cat.Recovery(nil, &opensearchapi.CatRecoveryReq{Params: opensearchapi.CatRecoveryParams{H: []string{"*"}}})
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.Recovery, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.Recovery, resp.Inspect().Response)
})
t.Run("Repositories", func(t *testing.T) {
resp, err := client.Cat.Repositories(nil, nil)
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.Repositories, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.Repositories, resp.Inspect().Response)
})
t.Run("Segments", func(t *testing.T) {
resp, err := client.Cat.Segments(nil, &opensearchapi.CatSegmentsReq{Params: opensearchapi.CatSegmentsParams{H: []string{"*"}}})
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.Segments, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.Segments, resp.Inspect().Response)
})
t.Run("Shards", func(t *testing.T) {
resp, err := client.Cat.Shards(nil, &opensearchapi.CatShardsReq{Params: opensearchapi.CatShardsParams{H: []string{"*"}}})
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.Shards, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.Shards, resp.Inspect().Response)
})
/* Need to create Snapshot + Repo
t.Run("Snapshots", func(t *testing.T) {
resp, err := client.Cat.Snapshots(nil, &opensearchapi.CatSnapshotsReq{Repository: snapshotRepo, Params: opensearchapi.CatSnapshotsParams{H: []string{"*"}}})
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.Snapshots, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.Snapshots, resp.Inspect().Response)
})
*/
t.Run("Tasks", func(t *testing.T) {
resp, err := client.Cat.Tasks(nil, &opensearchapi.CatTasksReq{Params: opensearchapi.CatTasksParams{H: []string{"*"}}})
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.Tasks, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.Tasks, resp.Inspect().Response)
})
t.Run("Templates", func(t *testing.T) {
resp, err := client.Cat.Templates(nil, nil)
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.Templates, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.Templates, resp.Inspect().Response)
})
t.Run("ThreadPool", func(t *testing.T) {
resp, err := client.Cat.ThreadPool(nil, &opensearchapi.CatThreadPoolReq{Params: opensearchapi.CatThreadPoolParams{H: []string{"*"}}})
assert.Nil(t, err)
assert.NotNil(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp.ThreadPool, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp.ThreadPool, resp.Inspect().Response)
})
})
}
Loading

0 comments on commit 909ec5f

Please sign in to comment.