forked from opensearch-project/opensearch-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
151 lines (137 loc) · 3.69 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package it
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"opensearch-cli/client"
"opensearch-cli/entity"
"opensearch-cli/environment"
"os"
"path/filepath"
"strings"
"github.com/hashicorp/go-retryablehttp"
"github.com/stretchr/testify/suite"
)
const (
newLine = "\n"
getPluginNamesURL = "_cat/plugins?h=c"
)
type CLISuite struct {
suite.Suite
Client *client.Client
Profile *entity.Profile
Plugins []string
}
//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 *CLISuite) 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 *CLISuite) ValidateProfile() error {
if a.Profile.Endpoint == "" {
return fmt.Errorf("endpoint cannot be empty. set env %s", environment.OPENSEARCH_ENDPOINT)
}
if len(a.Profile.UserName) == 0 {
return nil
}
if a.Profile.Password == "" {
return fmt.Errorf("password cannot be empty. set env %s", environment.OPENSEARCH_PASSWORD)
}
return nil
}
//CreateIndex creates test data for plugin processing
func (a *CLISuite) 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 *CLISuite) 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)
}
// isPluginInstalled checks whether dependent plugins are insalled or not
func (a *CLISuite) IsPluginInstalled() bool {
return a.IsPluginFromInputInstalled(a.Plugins)
}
// IsPluginFromInputInstalled checks whether input plugins are insalled or not
func (a *CLISuite) IsPluginFromInputInstalled(plugins []string) bool {
pluginListsInBytes, err := a.callRequest(http.MethodGet, []byte(""), fmt.Sprintf("%s/%s", a.Profile.Endpoint, getPluginNamesURL))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
pluginListsAsString := string(pluginListsInBytes[:])
pluginArray := strings.Split(pluginListsAsString, newLine)
for _, plugin := range plugins {
if !contains(pluginArray, plugin) {
return false
}
}
return true
}
func contains(container []string, value string) bool {
for _, item := range container {
if item == value {
return true
}
}
return false
}