Skip to content

Commit

Permalink
Close when no error
Browse files Browse the repository at this point in the history
  • Loading branch information
csenol committed Jul 25, 2019
1 parent 1b3fa89 commit 5ed698f
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ type TestCase struct {

func LoadTestCaseSetups(filepath string) ([]TestCaseSetup, error) {
file, err := os.Open(filepath)
if err == nil {
defer file.Close()
}
if err != nil {
os.Stderr.WriteString("error while opening test case setup file")
return nil, err
}
byteValue, _ := ioutil.ReadAll(file)
var testCaseSetups = make([]TestCaseSetup, 0)
defer file.Close()

err = json.Unmarshal(byteValue, &testCaseSetups)
if err != nil {
Expand All @@ -54,12 +56,15 @@ func LoadTestCaseSetups(filepath string) ([]TestCaseSetup, error) {

func LoadTestCaseSetup(filepath string) (*TestCaseSetup, error) {
file, err := os.Open(filepath)
if err == nil {
defer file.Close()
}
if err != nil {
os.Stderr.WriteString("error while opening test case setup file")
return nil, err
}
byteValue, _ := ioutil.ReadAll(file)
defer file.Close()

var testCaseSetup TestCaseSetup

err = json.Unmarshal(byteValue, &testCaseSetup)
Expand Down Expand Up @@ -116,9 +121,10 @@ func ExecuteQuery(script string, setup TestCaseSetup, elasticsearhEndpoint strin
resp, err := http.Post(elasticsearhEndpoint, "application/json", bytes.NewBuffer(jsonValue))
if err != nil {
return nil, err
} else {
defer resp.Body.Close()
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
Expand Down Expand Up @@ -258,14 +264,15 @@ func RunPerf(queryFilePath string, script string, contextFilePath string, esEndp
if err != nil {
os.Stderr.WriteString("error while opening query file")
return nil, err
} else {
defer queryFile.Close()
}

byteValue, _ := ioutil.ReadAll(queryFile)
var query map[string]interface{}

err = json.Unmarshal(byteValue, &query)

defer queryFile.Close()
var params map[string]interface{} = make(map[string]interface{})
var indexName string
if contextFilePath != "" {
Expand Down Expand Up @@ -333,7 +340,9 @@ func QueryES(query map[string]interface{}, script string, index string, params m

req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
defer resp.Body.Close()
if err == nil {
defer resp.Body.Close()
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
Expand Down

0 comments on commit 5ed698f

Please sign in to comment.