/
opensearchs3backupmanagedmock.go
131 lines (108 loc) · 5.12 KB
/
opensearchs3backupmanagedmock.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
package opensearchbackupservice
import (
"net/http"
"net/http/httptest"
"github.com/chef/automate/components/automate-cli/pkg/verifyserver/models"
"github.com/gofiber/fiber/v2"
elastic "github.com/olivere/elastic/v7"
"github.com/pkg/errors"
)
type TestMockFunc struct {
TestCase bool
Err error
}
type MockOSS3BackupService struct {
OSS3BackupVerifyFunc func(models.S3BackupDetails, *fiber.Ctx) (models.S3BackupManagedResponse, error)
}
func (mos *MockOSS3BackupService) OSS3BackupVerify(request models.S3BackupDetails, ctx *fiber.Ctx) (models.S3BackupManagedResponse, error) {
return mos.OSS3BackupVerifyFunc(request, ctx)
}
type MockOpensearchClient struct {
CreateAWSClientFunc func(models.S3BackupDetails, string) (*elastic.Client, error)
}
func (osc *MockOpensearchClient) CreateAWSClient(req models.S3BackupDetails, url string) (*elastic.Client, error) {
return osc.CreateAWSClientFunc(req, url)
}
type IMockHttpClient struct {
responseMock string
}
func (c *IMockHttpClient) Do(r *http.Request) (*http.Response, error) {
recorder := httptest.NewRecorder()
recorder.Write([]byte(c.responseMock))
recorder.Header().Set("Content-Type", "application/json")
return recorder.Result(), nil
}
func MockHttpClient(responseMock string) *IMockHttpClient {
return &IMockHttpClient{responseMock}
}
func SetupMockOpensearchClient(responseMock string) IOpenSearchclient {
return &MockOpensearchClient{
CreateAWSClientFunc: func(req models.S3BackupDetails, url string) (*elastic.Client, error) {
httpClient := MockHttpClient(responseMock)
var err error
createRepoClient, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetHttpClient(httpClient), elastic.SetHealthcheck(false))
if err != nil {
return nil, errors.Wrap(err, "Failed to create ES client with aws signing")
}
return createRepoClient, nil
},
}
}
type MockOSOperations struct {
CreateTestIndexFunc func(*elastic.Client, *fiber.Ctx, string) (bool, error)
CreateSnapshotRepoFunc func(*elastic.Client, *fiber.Ctx, SnapshotRepoRequestS3, string) (bool, error)
CreateSnapshotFunc func(*elastic.Client, *fiber.Ctx, string, string, string) (bool, error)
GetSnapshotStatusFunc func(*elastic.Client, *fiber.Ctx, string, string) (string, error)
DeleteTestSnapshotFunc func(*elastic.Client, *fiber.Ctx, string, string) (bool, error)
DeleteTestSnapshotRepoFunc func(*elastic.Client, *fiber.Ctx, string) (bool, error)
DeleteTestIndexFunc func(*elastic.Client, *fiber.Ctx, string) (bool, error)
}
func (mco *MockOSOperations) CreateTestIndex(client *elastic.Client, ctx *fiber.Ctx, index string) (bool, error) {
return mco.CreateTestIndexFunc(client, ctx, index)
}
func (mco *MockOSOperations) CreateSnapshotRepo(client *elastic.Client, ctx *fiber.Ctx, req SnapshotRepoRequestS3, repo string) (bool, error) {
return mco.CreateSnapshotRepoFunc(client, ctx, req, repo)
}
func (mco *MockOSOperations) CreateSnapshot(client *elastic.Client, ctx *fiber.Ctx, repoName string, snapshotName string, index string) (bool, error) {
return mco.CreateSnapshotFunc(client, ctx, repoName, snapshotName, index)
}
func (mco *MockOSOperations) GetSnapshotStatus(client *elastic.Client, ctx *fiber.Ctx, repoName string, snapshotName string) (string, error) {
return mco.GetSnapshotStatusFunc(client, ctx, repoName, snapshotName)
}
func (mco *MockOSOperations) DeleteTestSnapshot(client *elastic.Client, ctx *fiber.Ctx, repoName string, snapshotName string) (bool, error) {
return mco.DeleteTestSnapshotFunc(client, ctx, repoName, snapshotName)
}
func (mco *MockOSOperations) DeleteTestSnapshotRepo(client *elastic.Client, ctx *fiber.Ctx, repoName string) (bool, error) {
return mco.DeleteTestSnapshotRepoFunc(client, ctx, repoName)
}
func (mco *MockOSOperations) DeleteTestIndex(client *elastic.Client, ctx *fiber.Ctx, index string) (bool, error) {
return mco.DeleteTestIndexFunc(client, ctx, index)
}
func SetupMockOSOperations(resp []TestMockFunc) IOpensearchOperations {
return &MockOSOperations{
CreateTestIndexFunc: func(client *elastic.Client, ctx *fiber.Ctx, index string) (bool, error) {
return resp[0].TestCase, resp[0].Err
},
CreateSnapshotRepoFunc: func(client *elastic.Client, ctx *fiber.Ctx, req SnapshotRepoRequestS3, repo string) (bool, error) {
return resp[1].TestCase, resp[1].Err
},
CreateSnapshotFunc: func(client *elastic.Client, ctx *fiber.Ctx, repoName string, snapshotName string, index string) (bool, error) {
return resp[2].TestCase, resp[2].Err
},
GetSnapshotStatusFunc: func(client *elastic.Client, ctx *fiber.Ctx, repoName string, snapshotName string) (string, error) {
if resp[3].TestCase {
return "SUCCESS", resp[3].Err
}
return "FAILED", resp[3].Err
},
DeleteTestSnapshotFunc: func(client *elastic.Client, ctx *fiber.Ctx, repoName string, snapshotName string) (bool, error) {
return resp[4].TestCase, resp[4].Err
},
DeleteTestSnapshotRepoFunc: func(client *elastic.Client, ctx *fiber.Ctx, repoName string) (bool, error) {
return resp[5].TestCase, resp[5].Err
},
DeleteTestIndexFunc: func(client *elastic.Client, ctx *fiber.Ctx, index string) (bool, error) {
return resp[6].TestCase, resp[6].Err
},
}
}