Skip to content

Commit

Permalink
opensearch: increase test coverage (#517)
Browse files Browse the repository at this point in the history
* opensearch: remove unused structs and functions

Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>

* opensearch: use assert and require for tests

Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>

* opensearch: add more client config tests

Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>

* opensearchtransport: check for emtpy body to prevent nil panic

Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>

* opensearch: Do() use error vars

Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>

* opensearch: add io read error test, adjust Perform test

Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>

* add changelog

Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>

* github/workflow: bump codecov action version to v4

Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>

---------

Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
  • Loading branch information
Jakob3xD authored Apr 10, 2024
1 parent 17956a2 commit 38b48ff
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 196 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
if curl -s localhost:9200; \
then echo '=====> ready'; break; fi; if [ $attempt == 25 ]; then exit 1; fi; echo '=====> waiting...'; done
- run: make test-integ race=true coverage=true
- uses: codecov/codecov-action@v3
- uses: codecov/codecov-action@v4
with:
file: tmp/integ.cov
flags: integration
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
if: matrix.os != 'ubuntu-latest'
- run: make test-unit race=true coverage=true
if: matrix.os == 'ubuntu-latest'
- uses: codecov/codecov-action@v3
- uses: codecov/codecov-action@v4
with:
file: tmp/unit.cov
flags: unit
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Moved Error structs from opensearchapi to opensearch package ([#512](https://github.com/opensearch-project/opensearch-go/pull/506))
- Move parseError function from opensearchapi to opensearch package as ParseError ([#512](https://github.com/opensearch-project/opensearch-go/pull/506))
- Change ParseError function to do type assertion to determine error type ([#512](https://github.com/opensearch-project/opensearch-go/pull/506))
- Removed unused structs and functions from opensearch ([#517](https://github.com/opensearch-project/opensearch-go/pull/517))
- Adjust and extent opensearch tests for better coverage ([#517](https://github.com/opensearch-project/opensearch-go/pull/517))
- Bump codecov action version to v4 ([#517](https://github.com/opensearch-project/opensearch-go/pull/517))

### Deprecated

### Removed

### Fixed
- Fix search request missing a slash when no indices are given ([#470](https://github.com/opensearch-project/opensearch-go/pull/469))
- Fix opensearchtransport check for nil response body ([#517](https://github.com/opensearch-project/opensearch-go/pull/517))

### Security

Expand Down Expand Up @@ -185,4 +189,4 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
[2.1.0]: https://github.com/opensearch-project/opensearch-go/compare/v2.0.1...v2.1.0
[2.0.1]: https://github.com/opensearch-project/opensearch-go/compare/v2.0.0...v2.0.1
[2.0.0]: https://github.com/opensearch-project/opensearch-go/compare/v1.1.0...v2.0.0
[1.0.0]: https://github.com/opensearch-project/opensearch-go/compare/v1.0.0...v1.1.0
[1.0.0]: https://github.com/opensearch-project/opensearch-go/compare/v1.0.0...v1.1.0
36 changes: 2 additions & 34 deletions opensearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ const (
// Version returns the package version as a string.
const Version = version.Client

// SupportedElasticVersion defines the supported major elasticsearch version
const SupportedElasticVersion = 7

// Error vars
var (
ErrCreateClient = errors.New("cannot create client")
Expand Down Expand Up @@ -112,17 +109,6 @@ type Client struct {
Transport opensearchtransport.Interface
}

type esVersion struct {
Number string `json:"number"`
BuildFlavor string `json:"build_flavor"`
Distribution string `json:"distribution"`
}

type info struct {
Version esVersion `json:"version"`
Tagline string `json:"tagline"`
}

// NewDefaultClient creates a new client with default options.
//
// It will use http://localhost:9200 as the default address.
Expand Down Expand Up @@ -217,24 +203,6 @@ func getAddressFromEnvironment() []string {
return addrsFromEnvironment(envOpenSearchURL)
}

// checkCompatibleInfo validates the information given by OpenSearch
func checkCompatibleInfo(info info) error {
major, _, _, err := ParseVersion(info.Version.Number)
if err != nil {
return err
}

if info.Version.Distribution == openSearch {
return nil
}

if major != SupportedElasticVersion {
return errors.New(unsupportedProduct)
}

return nil
}

// ParseVersion returns an int64 representation of version.
func ParseVersion(version string) (int64, int64, int64, error) {
reVersion := regexp.MustCompile(`^([0-9]+)\.([0-9]+)\.([0-9]+)`)
Expand Down Expand Up @@ -294,13 +262,13 @@ func (c *Client) Do(ctx context.Context, req Request, dataPointer interface{}) (
if dataPointer != nil && resp.Body != nil && !response.IsError() {
data, err := io.ReadAll(resp.Body)
if err != nil {
return response, fmt.Errorf("failed to read the response body, status: %d, err: %w", resp.StatusCode, err)
return response, fmt.Errorf("%w, status: %d, err: %w", ErrReadBody, resp.StatusCode, err)
}

response.Body = io.NopCloser(bytes.NewReader(data))

if err := json.Unmarshal(data, dataPointer); err != nil {
return response, fmt.Errorf("failed to parse body into the pointer, status: %d, body: %s, err: %w", resp.StatusCode, data, err)
return response, fmt.Errorf("%w, status: %d, body: %s, err: %w", ErrJSONUnmarshalBody, resp.StatusCode, data, err)
}
}

Expand Down
Loading

0 comments on commit 38b48ff

Please sign in to comment.