Skip to content

Commit 97f0a71

Browse files
committed
Merge remote-tracking branch 'origin/main' into gh_pages
2 parents d1db364 + 6c46de7 commit 97f0a71

File tree

15 files changed

+249
-68
lines changed

15 files changed

+249
-68
lines changed

internal/apiclient/httpclient.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ func HttpClient(params ...string) (respBody []byte, err error) {
6262

6363
switch paramLen := len(params); paramLen {
6464
case 1:
65+
clilog.Debug.Println("Method: GET")
6566
req, err = http.NewRequestWithContext(ctx, http.MethodGet, params[0], nil)
6667
case 2:
6768
// some POST functions don't have a body
69+
clilog.Debug.Println("Method: POST")
6870
if len([]byte(params[1])) > 0 {
6971
payload, _ := PrettifyJson([]byte(params[1]))
7072
clilog.Debug.Println("Payload: ", string(payload))
@@ -121,6 +123,7 @@ func PrettyPrint(body []byte) error {
121123

122124
clilog.HTTPResponse.Println(prettyJSON.String())
123125
}
126+
clilog.Debug.Println(string(body))
124127
return nil
125128
}
126129

@@ -137,14 +140,18 @@ func PrettifyJson(body []byte) (prettyJson []byte, err error) {
137140
func getRequest(params []string) (req *http.Request, err error) {
138141
ctx := context.Background()
139142
if params[2] == "DELETE" {
143+
clilog.Debug.Println("Method: DELETE")
140144
req, err = http.NewRequestWithContext(ctx, http.MethodDelete, params[0], nil)
141145
} else if params[2] == "PUT" {
146+
clilog.Debug.Println("Method: PUT")
142147
clilog.Debug.Println("Payload: ", params[1])
143148
req, err = http.NewRequestWithContext(ctx, http.MethodPut, params[0], bytes.NewBuffer([]byte(params[1])))
144149
} else if params[2] == "PATCH" {
150+
clilog.Debug.Println("Method: PATCH")
145151
clilog.Debug.Println("Payload: ", params[1])
146152
req, err = http.NewRequestWithContext(ctx, http.MethodPatch, params[0], bytes.NewBuffer([]byte(params[1])))
147153
} else if params[2] == "POST" {
154+
clilog.Debug.Println("Method: POST")
148155
clilog.Debug.Println("Payload: ", params[1])
149156
req, err = http.NewRequestWithContext(ctx, http.MethodPost, params[0], bytes.NewBuffer([]byte(params[1])))
150157
} else {

internal/client/integrations/integrations.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,49 @@ func GetConnectionsWithRegion(integration []byte) (connections []integrationConn
943943
return connections, err
944944
}
945945

946+
// GetVersion
947+
func GetVersion(name string, userLabel string, snapshot string) (version string, err error) {
948+
var integrationBody []byte
949+
950+
apiclient.DisableCmdPrintHttpResponse()
951+
defer apiclient.EnableCmdPrintHttpResponse()
952+
953+
if userLabel != "" {
954+
integrationBody, err = GetByUserlabel(name, userLabel, true, false, false)
955+
if err != nil {
956+
return "", err
957+
}
958+
} else {
959+
integrationBody, err = GetBySnapshot(name, snapshot, true, false, false)
960+
if err != nil {
961+
return "", err
962+
}
963+
}
964+
apiclient.ClientPrintHttpResponse.Set(true)
965+
return GetIntegrationVersion(integrationBody)
966+
}
967+
968+
func GetIntegrationVersion(respBody []byte) (string, error) {
969+
var data map[string]interface{}
970+
err := json.Unmarshal(respBody, &data)
971+
if err != nil {
972+
return "", err
973+
}
974+
if data["integrationVersions"] == nil {
975+
if data["version"] == nil {
976+
return "", fmt.Errorf("no integration versions were found")
977+
} else {
978+
return data["version"].(string), nil
979+
}
980+
}
981+
integrationVersions := data["integrationVersions"].([]interface{})
982+
firstIntegrationVersion := integrationVersions[0].(map[string]interface{})
983+
if firstIntegrationVersion["version"].(string) == "" {
984+
return "", fmt.Errorf("unable to extract version id from integration")
985+
}
986+
return firstIntegrationVersion["version"].(string), nil
987+
}
988+
946989
// changeState
947990
func changeState(name string, version string, filter string, configVars []byte, action string) (respBody []byte, err error) {
948991
// if a version is sent, use it, else try the filter
@@ -1518,3 +1561,46 @@ func isCustomConnection(connectionVersion eventparameter) bool {
15181561
return false
15191562
}
15201563
}
1564+
1565+
// GetInputParameters
1566+
func GetInputParameters(integrationBody []byte) (execConfig []byte, err error) {
1567+
iversion := integrationVersionExternal{}
1568+
1569+
inputParameters := []string{}
1570+
1571+
const emptyTestConfig = `{
1572+
"inputParameters": {}
1573+
}`
1574+
1575+
err = json.Unmarshal(integrationBody, &iversion)
1576+
if err != nil {
1577+
return []byte(emptyTestConfig), err
1578+
}
1579+
1580+
for _, p := range iversion.IntegrationParameters {
1581+
if p.InputOutputType == "IN" {
1582+
switch p.DataType {
1583+
case "STRING_VALUE":
1584+
inputParameters = append(inputParameters, fmt.Sprintf("\"%s\": {\"stringValue\": \"\"}", p.Key))
1585+
case "INT_VALUE":
1586+
inputParameters = append(inputParameters, fmt.Sprintf("\"%s\": {\"intValue\": 0}", p.Key))
1587+
case "BOOLEAN_VALUE":
1588+
inputParameters = append(inputParameters, fmt.Sprintf("\"%s\": {\"booleanValue\": false}", p.Key))
1589+
case "JSON_VALUE":
1590+
inputParameters = append(inputParameters, fmt.Sprintf("\"%s\": {\"jsonValue\": {}}", p.Key))
1591+
case "DOUBLE_TPYE":
1592+
inputParameters = append(inputParameters, fmt.Sprintf("\"%s\": {\"doubleValue\": 0.0}", p.Key))
1593+
case "INT_ARRAY":
1594+
inputParameters = append(inputParameters, fmt.Sprintf("\"%s\": {\"intArray\": {\"intValues\": [0]}}", p.Key))
1595+
case "STRING_ARRAY":
1596+
inputParameters = append(inputParameters, fmt.Sprintf("\"%s\": {\"stringArray\": {\"stringValues\":[\"\"]}}", p.Key))
1597+
}
1598+
}
1599+
}
1600+
1601+
if len(inputParameters) == 0 {
1602+
return []byte(emptyTestConfig), nil
1603+
}
1604+
1605+
return apiclient.PrettifyJson([]byte("{\"inputParameters\": {" + strings.Join(inputParameters, ",") + "}}"))
1606+
}

internal/client/integrations/testcases.go

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ type testTaskConfig struct {
4949
}
5050

5151
type assertion struct {
52-
AssertionStrategy string `json:"assertionStrategy,omitempty"`
53-
Parameter eventparameter `json:"parameter,omitempty"`
54-
Condition string `json:"condition,omitempty"`
55-
RetryCount int `json:"retryCount,omitempty"`
52+
AssertionStrategy string `json:"assertionStrategy,omitempty"`
53+
Parameter *eventparameter `json:"parameter,omitempty"`
54+
Condition string `json:"condition,omitempty"`
55+
RetryCount int `json:"retryCount,omitempty"`
5656
}
5757

5858
type mockConfig struct {
@@ -61,6 +61,21 @@ type mockConfig struct {
6161
FailedExecutions string `json:"failedExecutions,omitempty"`
6262
}
6363

64+
type testCaseResponse struct {
65+
ExecutionId string `json:"executionId,omitempty`
66+
OutputParameters interface{} `json:"outputParameters,omitempty`
67+
AssertionResults []assertionResult `json:"assertionResults,omitempty`
68+
TestExecutionState string `json:"testExecutionState,omitempty`
69+
}
70+
71+
type assertionResult struct {
72+
TaskNumber string `json:"taskNumber,omitempty"`
73+
Assertion interface{} `json:"assertion,omitempty"`
74+
TaskName string `json:"taskName,omitempty"`
75+
Status string `json:"status,omitempty"`
76+
FailureMessage string `json:"failureMessage,omitempty"`
77+
}
78+
6479
func CreateTestCase(name string, version string, content string) (respBody []byte, err error) {
6580
u, _ := url.Parse(apiclient.GetBaseIntegrationURL())
6681
u.Path = path.Join(u.Path, "integrations", name, "versions", version, "testCases")
@@ -132,6 +147,18 @@ func ExecuteTestCase(name string, version string, testCaseID string, content str
132147
return respBody, err
133148
}
134149

150+
func AssertTestExecutionResult(testBody []byte) error {
151+
tr := testCaseResponse{}
152+
err := json.Unmarshal(testBody, &tr)
153+
if err != nil {
154+
return err
155+
}
156+
if tr.TestExecutionState == "PASSED" {
157+
return nil
158+
}
159+
return fmt.Errorf("test failed with %d assertions", len(tr.AssertionResults))
160+
}
161+
135162
func ListTestCasesByUserlabel(name string, userLabel string, full bool, filter string,
136163
pageSize int, pageToken string, orderBy string) (respBody []byte, err error) {
137164

@@ -185,12 +212,58 @@ func FindTestCase(name string, integrationVersion string, displayName string, pa
185212
return "", fmt.Errorf("testCase not found")
186213
}
187214

215+
func ListAllTestCases(name string, version string) (respBody []byte, err error) {
216+
l := listTestCases{}
217+
for {
218+
newltc := listTestCases{}
219+
respBody, err = ListTestCases(name, version, true, "", -1, "", "")
220+
if err != nil {
221+
return nil, err
222+
}
223+
err = json.Unmarshal(respBody, &newltc)
224+
if err != nil {
225+
return nil, err
226+
}
227+
228+
l.TestCases = append(l.TestCases, newltc.TestCases...)
229+
230+
if newltc.NextPageToken == "" {
231+
break
232+
}
233+
}
234+
235+
respBody, err = json.Marshal(l)
236+
237+
return respBody, err
238+
}
239+
240+
func DeleteAllTestCases(name string, version string) (err error) {
241+
respBody, err := ListAllTestCases(name, version)
242+
if err != nil {
243+
return err
244+
}
245+
246+
l := listTestCases{}
247+
err = json.Unmarshal(respBody, &l)
248+
if err != nil {
249+
return err
250+
}
251+
252+
for _, tc := range l.TestCases {
253+
_, err = DeleteTestCase(name, version, filepath.Base(tc.Name))
254+
if err != nil {
255+
return err
256+
}
257+
}
258+
259+
return nil
260+
}
261+
188262
func getTestCaseIntegrationVersion(name string, snapshot string, userLabel string) (version string, err error) {
189263

190264
var iversionBytes []byte
191265

192-
tmp := apiclient.GetCmdPrintHttpResponseSetting()
193-
apiclient.DisableCmdPrintHttpResponse()
266+
apiclient.ClientPrintHttpResponse.Set(false)
194267

195268
if snapshot != "" {
196269
iversionBytes, err = GetBySnapshot(name, snapshot, false, false, false)
@@ -212,8 +285,7 @@ func getTestCaseIntegrationVersion(name string, snapshot string, userLabel strin
212285

213286
version = getVersion(iversion.Name)
214287

215-
apiclient.ClientPrintHttpResponse.Set(tmp)
216-
apiclient.EnableCmdPrintHttpResponse()
288+
apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting())
217289

218290
return version, nil
219291

internal/cmd/integrations/apply.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ var ApplyCmd = &cobra.Command{
9393
grantPermission, _ := strconv.ParseBool(utils.GetStringParam(cmd.Flag("grant-permission")))
9494
userLabel := utils.GetStringParam(cmd.Flag("user-label"))
9595
wait, _ := strconv.ParseBool(utils.GetStringParam(cmd.Flag("wait")))
96+
runTests, _ := strconv.ParseBool(utils.GetStringParam(cmd.Flag("run-tests")))
9697

9798
integrationFolder := path.Join(srcFolder, "src")
9899
testsFolder := path.Join(folder, "tests")
100+
testsConfigFolder := path.Join(folder, "test-configs")
99101
authconfigFolder := path.Join(folder, "authconfigs")
100102
connectorsFolder := path.Join(folder, "connectors")
101103
customConnectorsFolder := path.Join(folder, "custom-connectors")
@@ -145,7 +147,7 @@ var ApplyCmd = &cobra.Command{
145147
}
146148

147149
if err = processIntegration(overridesFile, integrationFolder, testsFolder,
148-
configVarsFolder, testsConfigFolder, pipeline, userLabel, grantPermission); err != nil {
150+
configVarsFolder, testsConfigFolder, pipeline, userLabel, grantPermission, runTests); err != nil {
149151
return err
150152
}
151153

@@ -159,11 +161,10 @@ Apply scaffold configuration and run functional tests: ` + GetExample(18),
159161
}
160162

161163
var serviceAccountName, serviceAccountProject, encryptionKey, pipeline, release, outputGCSPath string
162-
var testsConfigFolder string
163164

164165
func init() {
165166
var userLabel string
166-
grantPermission, createSecret, wait := false, false, false
167+
grantPermission, createSecret, wait, runTests := false, false, false, false
167168

168169
ApplyCmd.Flags().StringVarP(&folder, "folder", "f",
169170
"", "Folder containing scaffolding configuration")
@@ -195,8 +196,8 @@ func init() {
195196
false, "Skip applying authconfigs configuration; default is false")
196197
ApplyCmd.Flags().BoolVarP(&useUnderscore, "use-underscore", "",
197198
false, "Use underscore as a file splitter; default is __")
198-
ApplyCmd.Flags().StringVarP(&testsConfigFolder, "tests-folder", "",
199-
"", "Path to a folder containing files for test case execution. File names MUST match display names. See ./samples/test-config.json for an example")
199+
ApplyCmd.Flags().BoolVarP(&runTests, "run-tests", "",
200+
false, "Runs unit tests from config files in test-configs folder. See ./samples/test-config.json for an example config")
200201
}
201202

202203
func getFilenameWithoutExtension(filname string) string {
@@ -543,6 +544,7 @@ func processSfdcChannels(sfdcchannelsFolder string) (err error) {
543544

544545
func processIntegration(overridesFile string, integrationFolder string, testsFolder string,
545546
configVarsFolder string, testConfigFolder string, pipeline string, userLabel string, grantPermission bool,
547+
runTests bool,
546548
) (err error) {
547549
rJSONFiles := regexp.MustCompile(`(\S*)\.json$`)
548550

@@ -631,7 +633,7 @@ func processIntegration(overridesFile string, integrationFolder string, testsFol
631633
}
632634

633635
// Execute test cases
634-
if testConfigFolder != "" {
636+
if runTests {
635637
err = executeAllTestCases(testConfigFolder, getFilenameWithoutExtension(integrationNames[0]), version)
636638
if err != nil {
637639
return err
@@ -706,12 +708,12 @@ func processCodeFolders(javascriptFolder string, jsonnetFolder string) (codeMap
706708
return codeMap, nil
707709
}
708710

709-
func processTestCases(testCasesFolder string, integrationName string, version string) (err error) {
711+
func processTestCases(testsFolder string, integrationName string, version string) (err error) {
710712
rJSONFiles := regexp.MustCompile(`(\S*)\.json`)
711713

712714
var testCaseFiles []string
713715

714-
_ = filepath.Walk(testCasesFolder, func(path string, info os.FileInfo, err error) error {
716+
_ = filepath.Walk(testsFolder, func(path string, info os.FileInfo, err error) error {
715717
if err != nil {
716718
return err
717719
}
@@ -726,8 +728,15 @@ func processTestCases(testCasesFolder string, integrationName string, version st
726728
})
727729

728730
if len(testCaseFiles) > 0 {
731+
732+
// delete any old test cases
733+
err = integrations.DeleteAllTestCases(integrationName, version)
734+
if err != nil {
735+
return err
736+
}
737+
729738
for _, testCaseFile := range testCaseFiles {
730-
testCaseBytes, err := utils.ReadFile(path.Join(testCasesFolder, testCaseFile))
739+
testCaseBytes, err := utils.ReadFile(path.Join(testsFolder, testCaseFile))
731740
if err != nil {
732741
return err
733742
}

internal/cmd/integrations/archive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ var ArchiveVerCmd = &cobra.Command{
7272
return fmt.Errorf("unable to list versions: %v", err)
7373
}
7474
}
75-
version, err = getIntegrationVersion(respBody)
75+
version, err = integrations.GetIntegrationVersion(respBody)
7676
if err != nil {
7777
return err
7878
}

internal/cmd/integrations/download.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ var DownloadVerCmd = &cobra.Command{
7474
return fmt.Errorf("unable to list versions: %v", err)
7575
}
7676
}
77-
version, err = getIntegrationVersion(respBody)
77+
version, err = integrations.GetIntegrationVersion(respBody)
7878
if err != nil {
7979
return err
8080
}

0 commit comments

Comments
 (0)