Skip to content

Commit 6690146

Browse files
committed
opt export
1 parent 0df9132 commit 6690146

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

Diff for: sceneTest/export/export_test.go

+1-35
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ package export
22

33
import (
44
"context"
5-
"encoding/xml"
65
"flag"
7-
"fmt"
86
"io"
97
"net/http"
108
"os"
119
"testing"
1210
"time"
1311

14-
"github.com/go-resty/resty/v2"
1512
"github.com/shiyuhang0/serverless-scene-test/config"
1613
"github.com/shiyuhang0/serverless-scene-test/util"
1714
"github.com/stretchr/testify/assert"
@@ -59,7 +56,7 @@ func TestExportToLocalAndDownload(t *testing.T) {
5956
t.Log("start to download files")
6057
for _, exportFile := range exportFilesRes.Files {
6158
// download file
62-
downloadRes, err := GetResponse(*exportFile.Url)
59+
downloadRes, err := util.GetResponse(*exportFile.Url)
6360
if err != nil {
6461
t.Fatal(err)
6562
}
@@ -378,34 +375,3 @@ func CreateExport(ctx context.Context, c *export.APIClient, clusterId string, bo
378375
res, h, err := r.Execute()
379376
return res, util.ParseError(err, h)
380377
}
381-
382-
func GetResponse(url string) (*http.Response, error) {
383-
httpClient := resty.New()
384-
resp, err := httpClient.GetClient().Get(url)
385-
if err != nil {
386-
return nil, err
387-
}
388-
if resp.StatusCode != http.StatusOK {
389-
// read the body to get the error message
390-
body, err := io.ReadAll(resp.Body)
391-
resp.Body.Close()
392-
if err != nil {
393-
return nil, fmt.Errorf("receiving status of %d", resp.StatusCode)
394-
}
395-
type AwsError struct {
396-
Code string `xml:"Code"`
397-
Message string `xml:"Message"`
398-
}
399-
v := AwsError{}
400-
err = xml.Unmarshal(body, &v)
401-
if err != nil {
402-
return nil, fmt.Errorf("receiving status of %d", resp.StatusCode)
403-
}
404-
return nil, fmt.Errorf("receiving status of %d. code: %s, message: %s", resp.StatusCode, v.Code, v.Message)
405-
}
406-
if resp.ContentLength <= 0 {
407-
resp.Body.Close()
408-
return nil, fmt.Errorf("file is empty")
409-
}
410-
return resp, nil
411-
}

Diff for: util/util.go

+33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package util
22

33
import (
4+
"encoding/xml"
45
"errors"
56
"fmt"
67
"io"
@@ -9,6 +10,7 @@ import (
910
"net/url"
1011
"os"
1112

13+
"github.com/go-resty/resty/v2"
1214
"github.com/icholy/digest"
1315
)
1416

@@ -89,3 +91,34 @@ func ValidateApiUrl(value string) (*url.URL, error) {
8991
}
9092
return u, nil
9193
}
94+
95+
func GetResponse(url string) (*http.Response, error) {
96+
httpClient := resty.New()
97+
resp, err := httpClient.GetClient().Get(url)
98+
if err != nil {
99+
return nil, err
100+
}
101+
if resp.StatusCode != http.StatusOK {
102+
// read the body to get the error message
103+
body, err := io.ReadAll(resp.Body)
104+
resp.Body.Close()
105+
if err != nil {
106+
return nil, fmt.Errorf("receiving status of %d", resp.StatusCode)
107+
}
108+
type AwsError struct {
109+
Code string `xml:"Code"`
110+
Message string `xml:"Message"`
111+
}
112+
v := AwsError{}
113+
err = xml.Unmarshal(body, &v)
114+
if err != nil {
115+
return nil, fmt.Errorf("receiving status of %d", resp.StatusCode)
116+
}
117+
return nil, fmt.Errorf("receiving status of %d. code: %s, message: %s", resp.StatusCode, v.Code, v.Message)
118+
}
119+
if resp.ContentLength <= 0 {
120+
resp.Body.Close()
121+
return nil, fmt.Errorf("file is empty")
122+
}
123+
return resp, nil
124+
}

0 commit comments

Comments
 (0)