From 28a6f552c2a495671f16967615fae779c8017b00 Mon Sep 17 00:00:00 2001 From: Dmitriy Ivolgin Date: Mon, 23 Dec 2024 13:05:55 -0800 Subject: [PATCH] Mock all release data without mixing with real data (#232) --- pkg/handlers/app.go | 34 +++++++++++++++------ pkg/handlers/handlers.go | 4 +++ pkg/integration/data/default_mock_data.yaml | 18 ++++++----- pkg/integration/data/test_mock_data_v2.yaml | 4 +++ pkg/integration/mock_test.go | 2 -- pkg/integration/types/types.go | 4 +++ 6 files changed, 47 insertions(+), 19 deletions(-) diff --git a/pkg/handlers/app.go b/pkg/handlers/app.go index 0ea3d5d9..2fc49ec2 100644 --- a/pkg/handlers/app.go +++ b/pkg/handlers/app.go @@ -89,16 +89,6 @@ 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) @@ -106,6 +96,12 @@ func GetCurrentAppInfo(w http.ResponseWriter, r *http.Request) { 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 @@ -113,16 +109,27 @@ func GetCurrentAppInfo(w http.ResponseWriter, r *http.Request) { 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 } @@ -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 } @@ -258,6 +267,8 @@ func GetAppUpdates(w http.ResponseWriter, r *http.Request) { }) } + w.Header().Set(MockDataHeader, "true") + JSON(w, http.StatusOK, response) return } @@ -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 } @@ -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") diff --git a/pkg/handlers/handlers.go b/pkg/handlers/handlers.go index cf129cf4..68078f56 100644 --- a/pkg/handlers/handlers.go +++ b/pkg/handlers/handlers.go @@ -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 { diff --git a/pkg/integration/data/default_mock_data.yaml b/pkg/integration/data/default_mock_data.yaml index 8959ebfe..0df7dbc7 100644 --- a/pkg/integration/data/default_mock_data.yaml +++ b/pkg/integration/data/default_mock_data.yaml @@ -8,30 +8,34 @@ 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 @@ -39,10 +43,10 @@ deployedReleases: 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 diff --git a/pkg/integration/data/test_mock_data_v2.yaml b/pkg/integration/data/test_mock_data_v2.yaml index 44341462..a21cacde 100644 --- a/pkg/integration/data/test_mock_data_v2.yaml +++ b/pkg/integration/data/test_mock_data_v2.yaml @@ -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" diff --git a/pkg/integration/mock_test.go b/pkg/integration/mock_test.go index 7f4acb9f..78a59931 100644 --- a/pkg/integration/mock_test.go +++ b/pkg/integration/mock_test.go @@ -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 { diff --git a/pkg/integration/types/types.go b/pkg/integration/types/types.go index 6b2e5386..4381599d 100644 --- a/pkg/integration/types/types.go +++ b/pkg/integration/types/types.go @@ -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"` }