-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathhelm_charts_mock_test.go
127 lines (105 loc) · 3.58 KB
/
helm_charts_mock_test.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
// © Broadcom. All Rights Reserved.
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: MPL-2.0
package helmcharts
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"testing"
"github.com/go-test/deep"
"github.com/jarcoal/httpmock"
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/helper"
helmchartsmodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/helmcharts"
objectmetamodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/objectmeta"
)
// nolint: unused
func bodyInspectingResponder(t *testing.T, expectedContent interface{}, successResponse int, successResponseBody interface{}) httpmock.Responder {
return func(r *http.Request) (*http.Response, error) {
successFunc := func() (*http.Response, error) {
return httpmock.NewJsonResponse(successResponse, successResponseBody)
}
if expectedContent == nil {
return successFunc()
}
// Compare to expected content.
expectedBytes, err := json.Marshal(expectedContent)
if err != nil {
t.Fail()
return nil, err
}
if r.Body == nil {
t.Fail()
return nil, fmt.Errorf("expected body on request")
}
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
t.Fail()
return nil, err
}
var bodyInterface map[string]interface{}
if err = json.Unmarshal(bodyBytes, &bodyInterface); err == nil {
var expectedInterface map[string]interface{}
err = json.Unmarshal(expectedBytes, &expectedInterface)
if err != nil {
return nil, err
}
diff := deep.Equal(bodyInterface, expectedInterface)
if diff == nil {
return successFunc()
}
} else {
return nil, err
}
return successFunc()
}
}
// nolint: unused
func (testConfig *testAcceptanceConfig) setupHTTPMocks(t *testing.T) {
httpmock.Activate()
t.Cleanup(httpmock.Deactivate)
endpoint := os.Getenv("TMC_ENDPOINT")
OrgID := os.Getenv("ORG_ID")
reference := objectmetamodel.VmwareTanzuCoreV1alpha1ObjectReference{
Rid: "test_rid",
UID: "test_uid",
}
referenceArray := make([]*objectmetamodel.VmwareTanzuCoreV1alpha1ObjectReference, 0)
referenceArray = append(referenceArray, &reference)
// cluster level helm chart resource.
getResponse := &helmchartsmodel.VmwareTanzuManageV1alpha1OrganizationFluxcdHelmRepositoryChartmetadataChartListResponse{
TotalCount: "1",
Charts: []*helmchartsmodel.VmwareTanzuManageV1alpha1OrganizationFluxcdHelmRepositoryChartmetadataChart{
{
FullName: &helmchartsmodel.VmwareTanzuManageV1alpha1OrganizationFluxcdHelmRepositoryChartmetadataChartFullName{
OrgID: OrgID,
Name: "*",
RepositoryName: "bitnami",
ChartMetadataName: "zookeeper",
},
Meta: &objectmetamodel.VmwareTanzuCoreV1alpha1ObjectMeta{
ParentReferences: referenceArray,
Description: "resource with description",
Labels: map[string]string{
"key1": "value1",
"key2": "value2",
},
UID: "helmchart1",
ResourceVersion: "v1",
},
Spec: &helmchartsmodel.VmwareTanzuManageV1alpha1OrganizationFluxcdHelmRepositoryChartmetadataChartSpec{},
},
},
}
const (
https = "https:/"
clAPIVersionAndGroup = "v1alpha1/organization/fluxcd/helm/repositories"
apiSubGroup = "chartmetadatas"
apiKind = "charts"
)
getPkgEndpoint := (helper.ConstructRequestURL(https, endpoint, clAPIVersionAndGroup, "*", apiSubGroup, testConfig.ChartMetadataName, apiKind)).String()
httpmock.RegisterResponder("GET", getPkgEndpoint,
bodyInspectingResponder(t, nil, 200, getResponse))
}