Skip to content

Commit

Permalink
Mock all release data without mixing with real data (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
divolgin authored Dec 23, 2024
1 parent 2a305d5 commit 28a6f55
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 19 deletions.
34 changes: 24 additions & 10 deletions pkg/handlers/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,40 +89,47 @@ func GetCurrentAppInfo(w http.ResponseWriter, r *http.Request) {
}

if isIntegrationModeEnabled {
response := GetCurrentAppInfoResponse{
InstanceID: store.GetStore().GetAppID(),
AppSlug: store.GetStore().GetAppSlug(),
AppName: store.GetStore().GetAppName(),
ChannelID: store.GetStore().GetChannelID(),
ChannelName: store.GetStore().GetChannelName(),
ChannelSequence: store.GetStore().GetChannelSequence(),
ReleaseSequence: store.GetStore().GetReleaseSequence(),
}

mockData, err := integration.GetMockData(r.Context(), clientset, store.GetStore().GetNamespace())
if err != nil {
logger.Errorf("failed to get mock data: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}

response := GetCurrentAppInfoResponse{
InstanceID: store.GetStore().GetAppID(),
AppSlug: store.GetStore().GetAppSlug(),
AppName: store.GetStore().GetAppName(),
}

switch mockData := mockData.(type) {
case *integrationtypes.MockDataV1:
response.AppStatus = mockData.AppStatus
response.HelmChartURL = mockData.HelmChartURL
if mockData.CurrentRelease != nil {
response.CurrentRelease = mockReleaseToAppRelease(*mockData.CurrentRelease)
}
response.ChannelID = store.GetStore().GetChannelID()
response.ChannelName = store.GetStore().GetChannelName()
response.ChannelSequence = store.GetStore().GetChannelSequence()
response.ReleaseSequence = store.GetStore().GetReleaseSequence()

case *integrationtypes.MockDataV2:
response.AppStatus = mockData.AppStatus.State
response.HelmChartURL = mockData.HelmChartURL
if mockData.CurrentRelease != nil {
response.CurrentRelease = mockReleaseToAppRelease(*mockData.CurrentRelease)
response.ChannelID = mockData.CurrentRelease.ChannelID
response.ChannelName = mockData.CurrentRelease.ChannelName
response.ChannelSequence = mockData.CurrentRelease.ChannelSequence
response.ReleaseSequence = mockData.CurrentRelease.ReleaseSequence
}
default:
logger.Errorf("unknown mock data type: %T", mockData)
}

w.Header().Set(MockDataHeader, "true")

JSON(w, http.StatusOK, response)
return
}
Expand Down Expand Up @@ -199,6 +206,8 @@ func GetCurrentAppStatus(w http.ResponseWriter, r *http.Request) {
logger.Errorf("unknown mock data type: %T", mockData)
}

w.Header().Set(MockDataHeader, "true")

JSON(w, http.StatusOK, response)
return
}
Expand Down Expand Up @@ -258,6 +267,8 @@ func GetAppUpdates(w http.ResponseWriter, r *http.Request) {
})
}

w.Header().Set(MockDataHeader, "true")

JSON(w, http.StatusOK, response)
return
}
Expand Down Expand Up @@ -334,6 +345,8 @@ func GetAppHistory(w http.ResponseWriter, r *http.Request) {
response.Releases = append(response.Releases, mockReleaseToAppRelease(mockRelease))
}

w.Header().Set(MockDataHeader, "true")

JSON(w, http.StatusOK, response)
return
}
Expand Down Expand Up @@ -511,6 +524,7 @@ func DeleteCustomAppMetricsKey(w http.ResponseWriter, r *http.Request) {

JSON(w, http.StatusNoContent, "")
}

func validateCustomAppMetricsData(data CustomAppMetricsData) error {
if len(data) == 0 {
return errors.New("no data provided")
Expand Down
4 changes: 4 additions & 0 deletions pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"github.com/replicatedhq/replicated-sdk/pkg/logger"
)

const (
MockDataHeader = "X-Replicated-Mock-Data"
)

func JSON(w http.ResponseWriter, code int, payload interface{}) {
response, err := json.Marshal(payload)
if err != nil {
Expand Down
18 changes: 11 additions & 7 deletions pkg/integration/data/default_mock_data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,45 @@ appStatus:
state: ready
helmChartURL: oci://registry.replicated.com/dev-app/dev-channel/dev-parent-chart
currentRelease:
versionLabel: 0.1.3
releaseNotes: "release notes 0.1.3"
versionLabel: 0.1.3-mock-release
releaseNotes: "mock release notes for 0.1.3"
createdAt: 2023-05-23T20:58:07Z
deployedAt: 2023-05-23T21:58:07Z
helmReleaseName: dev-parent-chart
helmReleaseRevision: 3
helmReleaseNamespace: default
channelID: dev-channel
channelName: Dev Channel
channelSequence: 13
releaseSequence: 113
deployedReleases:
- versionLabel: 0.1.1
releaseNotes: "release notes 0.1.1"
releaseNotes: "mock release release notes for 0.1.1"
createdAt: 2023-05-21T20:58:07Z
deployedAt: 2023-05-21T21:58:07Z
helmReleaseName: dev-parent-chart
helmReleaseRevision: 1
helmReleaseNamespace: default
- versionLabel: 0.1.2
releaseNotes: "release notes 0.1.2"
releaseNotes: "mock release release notes for 0.1.2"
createdAt: 2023-05-22T20:58:07Z
deployedAt: 2023-05-22T21:58:07Z
helmReleaseName: dev-parent-chart
helmReleaseRevision: 2
helmReleaseNamespace: default
- versionLabel: 0.1.3
releaseNotes: "release notes 0.1.3"
releaseNotes: "mock release release notes for 0.1.3"
createdAt: 2023-05-23T20:58:07Z
deployedAt: 2023-05-23T21:58:07Z
helmReleaseName: dev-parent-chart
helmReleaseRevision: 3
helmReleaseNamespace: default
availableReleases:
- versionLabel: 0.1.4
releaseNotes: "release notes 0.1.4"
releaseNotes: "mock release release notes for 0.1.4"
createdAt: 2023-05-24T20:58:07Z
deployedAt: 2023-05-24T21:58:07Z
- versionLabel: 0.1.5
releaseNotes: "release notes 0.1.5"
releaseNotes: "mock release release notes for 0.1.5"
createdAt: 2023-06-01T20:58:07Z
deployedAt: 2023-06-01T21:58:07Z
4 changes: 4 additions & 0 deletions pkg/integration/data/test_mock_data_v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ currentRelease:
helmReleaseName: custom-helm-release-name
helmReleaseRevision: 4
helmReleaseNamespace: default
channelID: dev-channel
channelName: Dev Channel
channelSequence: 13
releaseSequence: 113
deployedReleases:
- versionLabel: custom-version-label-1
releaseNotes: "custom release notes 1"
Expand Down
2 changes: 0 additions & 2 deletions pkg/integration/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ func TestMock_SetMockData(t *testing.T) {
t.Errorf("SetMockData() \n\n%q", fmtJSONDiff(gotV2, testMockDataV2))
}
},
// want: testMockDataV2,
// wantErr: false,
},
}
for _, tt := range tests {
Expand Down
4 changes: 4 additions & 0 deletions pkg/integration/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ type MockRelease struct {
HelmReleaseName string `json:"helmReleaseName" yaml:"helmReleaseName"`
HelmReleaseRevision int `json:"helmReleaseRevision" yaml:"helmReleaseRevision"`
HelmReleaseNamespace string `json:"helmReleaseNamespace" yaml:"helmReleaseNamespace"`
ChannelID string `json:"channelID" yaml:"channelID"`
ChannelName string `json:"channelName" yaml:"channelName"`
ChannelSequence int64 `json:"channelSequence" yaml:"channelSequence"`
ReleaseSequence int64 `json:"releaseSequence" yaml:"releaseSequence"`
}

0 comments on commit 28a6f55

Please sign in to comment.