This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelper.go
116 lines (106 loc) · 3.02 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package it
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"odfe-cli/client"
"odfe-cli/entity"
"os"
"path/filepath"
"github.com/hashicorp/go-retryablehttp"
"github.com/stretchr/testify/suite"
)
type ODFECLISuite struct {
suite.Suite
Client *client.Client
Profile *entity.Profile
}
//HelperLoadBytes loads file from testdata and stream contents
func HelperLoadBytes(name string) []byte {
path := filepath.Join("testdata", name) // relative path
contents, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return contents
}
// DeleteIndex deletes index by name
func (a *ODFECLISuite) DeleteIndex(indexName string) {
_, err := a.callRequest(http.MethodDelete, []byte(""), fmt.Sprintf("%s/%s", a.Profile.Endpoint, indexName))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func (a *ODFECLISuite) ValidateProfile() error {
if a.Profile.Endpoint == "" {
return fmt.Errorf("odfe endpoint cannot be empty. set env ODFE_ENDPOINT")
}
if a.Profile.UserName == "" {
return fmt.Errorf("odfe user name cannot be empty. set env ODFE_USER")
}
if a.Profile.Password == "" {
return fmt.Errorf("odfe endpoint cannot be empty. set env ODFE_PASSWORD")
}
return nil
}
//CreateIndex creates test data for plugin processing
func (a *ODFECLISuite) CreateIndex(indexFileName string, mappingFileName string) {
if mappingFileName != "" {
mapping, err := a.callRequest(
http.MethodPut, HelperLoadBytes(mappingFileName), fmt.Sprintf("%s/%s", a.Profile.Endpoint, indexFileName))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(mapping))
}
res, err := a.callRequest(
http.MethodPost, HelperLoadBytes(indexFileName), fmt.Sprintf("%s/_bulk?refresh", a.Profile.Endpoint))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(res))
}
func (a *ODFECLISuite) callRequest(method string, reqBytes []byte, url string) ([]byte, error) {
var reqReader *bytes.Reader
if reqBytes != nil {
reqReader = bytes.NewReader(reqBytes)
}
r, err := retryablehttp.NewRequest(method, url, reqReader)
if err != nil {
return nil, err
}
req := r.WithContext(context.Background())
req.SetBasicAuth(a.Profile.UserName, a.Profile.Password)
req.Header.Set("Content-Type", "application/x-ndjson")
response, err := a.Client.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer func() {
err := response.Body.Close()
if err != nil {
return
}
}()
return ioutil.ReadAll(response.Body)
}