From da7e96c038c0d5833a9885af792a0be5ed7897a1 Mon Sep 17 00:00:00 2001 From: Samantha Jayasinghe Date: Tue, 5 Dec 2023 22:13:51 +1300 Subject: [PATCH] OSD-19188: Get backplane URL via ocm environment Fetch Backplane URL via OCM environment --- cmd/ocm-backplane/console/console_test.go | 5 + cmd/ocm-backplane/login/login_test.go | 26 +++++ cmd/ocm-backplane/logout/logout_test.go | 4 + .../managedJob/createManagedJob_test.go | 14 ++- .../managedJob/deleteManagedJob_test.go | 9 +- .../managedJob/getManagedJob_test.go | 13 ++- .../script/describeScript_test.go | 19 +++- cmd/ocm-backplane/script/listScripts_test.go | 18 ++- .../testJob/createTestJob_test.go | 16 ++- cmd/ocm-backplane/testJob/getTestJob_test.go | 9 +- pkg/cli/config/config.go | 50 +++++++-- pkg/cli/config/config_test.go | 104 ------------------ pkg/cli/session/session_test.go | 9 ++ pkg/monitoring/monitoring_test.go | 5 + pkg/ocm/mocks/ocmWrapperMock.go | 15 +++ pkg/ocm/ocmWrapper.go | 18 +++ pkg/utils/cluster_test.go | 4 + 17 files changed, 210 insertions(+), 128 deletions(-) delete mode 100644 pkg/cli/config/config_test.go diff --git a/cmd/ocm-backplane/console/console_test.go b/cmd/ocm-backplane/console/console_test.go index d06f0c5d..2d16254a 100644 --- a/cmd/ocm-backplane/console/console_test.go +++ b/cmd/ocm-backplane/console/console_test.go @@ -39,6 +39,7 @@ var _ = Describe("console command", func() { proxyURL string clusterInfo *cmv1.Cluster testKubeCfg api.Config + ocmEnv *cmv1.Environment ) BeforeEach(func() { @@ -125,6 +126,7 @@ var _ = Describe("console command", func() { dirName, _ := os.MkdirTemp("", ".kube") pullSecretConfigDirectory = dirName + ocmEnv, _ = cmv1.NewEnvironment().BackplaneURL("https://dummy.api").Build() }) AfterEach(func() { @@ -178,6 +180,7 @@ var _ = Describe("console command", func() { It("should start console server", func() { setupConfig() + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() mockOcmInterface.EXPECT().GetPullSecret().Return(pullSecret, nil).AnyTimes() mockOcmInterface.EXPECT().GetClusterInfoByID(clusterID).Return(clusterInfo, nil).AnyTimes() @@ -196,6 +199,7 @@ var _ = Describe("console command", func() { testKubeCfg.CurrentContext = "custom-context" setupConfig() + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() mockOcmInterface.EXPECT().GetPullSecret().Return(pullSecret, nil).AnyTimes() mockOcmInterface.EXPECT().GetClusterInfoByID(clusterID).Return(clusterInfo, nil).AnyTimes() @@ -213,6 +217,7 @@ var _ = Describe("console command", func() { testKubeCfg.CurrentContext = "undefined-context" setupConfig() + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() mockOcmInterface.EXPECT().GetPullSecret().Return(pullSecret, nil).AnyTimes() mockOcmInterface.EXPECT().GetClusterInfoByID(clusterID).Return(clusterInfo, nil).AnyTimes() diff --git a/cmd/ocm-backplane/login/login_test.go b/cmd/ocm-backplane/login/login_test.go index a9873026..51455af3 100644 --- a/cmd/ocm-backplane/login/login_test.go +++ b/cmd/ocm-backplane/login/login_test.go @@ -14,6 +14,7 @@ import ( "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd/api" + cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" "github.com/openshift/backplane-cli/pkg/backplaneapi" backplaneapiMock "github.com/openshift/backplane-cli/pkg/backplaneapi/mocks" "github.com/openshift/backplane-cli/pkg/client/mocks" @@ -45,6 +46,7 @@ var _ = Describe("Login command", func() { serviceClusterID string serviceClusterName string fakeResp *http.Response + ocmEnv *cmv1.Environment ) BeforeEach(func() { @@ -80,6 +82,8 @@ var _ = Describe("Login command", func() { clientcmd.UseModifyConfigLock = false globalOpts.BackplaneURL = backplaneAPIURI + + ocmEnv, _ = cmv1.NewEnvironment().BackplaneURL("https://dummy.api").Build() }) AfterEach(func() { @@ -95,6 +99,7 @@ var _ = Describe("Login command", func() { Context("check ocm token", func() { It("Should fail when failed to get OCM token", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().GetOCMAccessToken().Return(nil, errors.New("err")).AnyTimes() mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() @@ -108,6 +113,7 @@ var _ = Describe("Login command", func() { err := utils.CreateTempKubeConfig(nil) Expect(err).To(BeNil()) globalOpts.ProxyURL = "https://squid.myproxy.com" + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockClientUtil.EXPECT().SetClientProxyURL(globalOpts.ProxyURL).Return(nil) mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() @@ -132,6 +138,7 @@ var _ = Describe("Login command", func() { err := utils.CreateTempKubeConfig(nil) Expect(err).To(BeNil()) globalOpts.BackplaneURL = "https://sadge.app" + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil) @@ -154,6 +161,7 @@ var _ = Describe("Login command", func() { err := utils.CreateTempKubeConfig(nil) Expect(err).To(BeNil()) globalOpts.ProxyURL = "https://squid.myproxy.com" + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockClientUtil.EXPECT().SetClientProxyURL(globalOpts.ProxyURL).Return(nil) mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() @@ -176,6 +184,7 @@ var _ = Describe("Login command", func() { }) It("should fail if unable to create api client", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil) @@ -186,6 +195,15 @@ var _ = Describe("Login command", func() { Expect(err).ToNot(BeNil()) }) + It("should fail if ocm env backplane url is empty", func() { + ocmEnv, _ = cmv1.NewEnvironment().Build() + + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() + err := runLogin(nil, []string{testClusterID}) + + Expect(err).ToNot(BeNil()) + }) + }) Context("check cluster login", func() { @@ -196,6 +214,7 @@ var _ = Describe("Login command", func() { It("when running with a simple case should work as expected", func() { err := utils.CreateTempKubeConfig(nil) Expect(err).To(BeNil()) + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil) @@ -215,6 +234,7 @@ var _ = Describe("Login command", func() { }) It("Should fail when trying to find a non existent cluster", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() @@ -227,6 +247,7 @@ var _ = Describe("Login command", func() { It("should return the managing cluster if one is requested", func() { globalOpts.Manager = true + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().GetManagingCluster(trueClusterID).Return(managingClusterID, managingClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(managingClusterID)).Return(false, nil).AnyTimes() @@ -241,6 +262,7 @@ var _ = Describe("Login command", func() { It("should failed if managing cluster not exist in same env", func() { globalOpts.Manager = true + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().GetManagingCluster(trueClusterID).Return( managingClusterID, @@ -261,6 +283,7 @@ var _ = Describe("Login command", func() { It("should return the service cluster if hosted cluster is given", func() { globalOpts.Service = true + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().GetServiceCluster(trueClusterID).Return(serviceClusterID, serviceClusterName, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(serviceClusterID)).Return(false, nil).AnyTimes() @@ -277,6 +300,7 @@ var _ = Describe("Login command", func() { err := utils.CreateTempKubeConfig(nil) Expect(err).To(BeNil()) globalOpts.ProxyURL = "https://squid.myproxy.com" + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockClientUtil.EXPECT().SetClientProxyURL(globalOpts.ProxyURL).Return(nil) mockOcmInterface.EXPECT().GetTargetCluster("configcluster").Return(testClusterID, "dummy_cluster", nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(testClusterID)).Return(false, nil).AnyTimes() @@ -299,6 +323,7 @@ var _ = Describe("Login command", func() { err := utils.CreateTempKubeConfig(nil) Expect(err).To(BeNil()) + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil) @@ -326,6 +351,7 @@ var _ = Describe("Login command", func() { _, err = login.CreateClusterKubeConfig(trueClusterID, utils.GetDefaultKubeConfig()) Expect(err).To(BeNil()) + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil) diff --git a/cmd/ocm-backplane/logout/logout_test.go b/cmd/ocm-backplane/logout/logout_test.go index 515de613..f77235ae 100644 --- a/cmd/ocm-backplane/logout/logout_test.go +++ b/cmd/ocm-backplane/logout/logout_test.go @@ -13,6 +13,7 @@ import ( "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd/api" + cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" "github.com/openshift/backplane-cli/cmd/ocm-backplane/login" "github.com/openshift/backplane-cli/pkg/backplaneapi" backplaneapiMock "github.com/openshift/backplane-cli/pkg/backplaneapi/mocks" @@ -48,6 +49,7 @@ var _ = Describe("Logout command", func() { kubeConfig api.Config loggedInNotBackplaneConfig api.Config + ocmEnv *cmv1.Environment ) BeforeEach(func() { @@ -114,6 +116,7 @@ var _ = Describe("Logout command", func() { } os.Setenv(info.BackplaneURLEnvName, backplaneAPIURI) + ocmEnv, _ = cmv1.NewEnvironment().BackplaneURL("https://dummy.api").Build() }) AfterEach(func() { @@ -128,6 +131,7 @@ var _ = Describe("Logout command", func() { err := utils.CreateTempKubeConfig(&kubeConfig) Expect(err).To(BeNil()) + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil).AnyTimes() mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() diff --git a/cmd/ocm-backplane/managedJob/createManagedJob_test.go b/cmd/ocm-backplane/managedJob/createManagedJob_test.go index e63439a7..d5e2af25 100644 --- a/cmd/ocm-backplane/managedJob/createManagedJob_test.go +++ b/cmd/ocm-backplane/managedJob/createManagedJob_test.go @@ -15,6 +15,7 @@ import ( "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd/api" + cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" "github.com/openshift/backplane-cli/pkg/backplaneapi" backplaneapiMock "github.com/openshift/backplane-cli/pkg/backplaneapi/mocks" "github.com/openshift/backplane-cli/pkg/client/mocks" @@ -40,7 +41,8 @@ var _ = Describe("managedJob create command", func() { fakeJobResp *http.Response jobResponseBody string - sut *cobra.Command + sut *cobra.Command + ocmEnv *cmv1.Environment ) BeforeEach(func() { @@ -95,6 +97,7 @@ var _ = Describe("managedJob create command", func() { _ = clientcmd.ModifyConfig(clientcmd.NewDefaultPathOptions(), api.Config{}, true) os.Setenv(info.BackplaneURLEnvName, proxyURI) + ocmEnv, _ = cmv1.NewEnvironment().BackplaneURL("https://dummy.api").Build() }) AfterEach(func() { @@ -104,6 +107,7 @@ var _ = Describe("managedJob create command", func() { Context("create managed job", func() { It("when running with a simple case should work as expected", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() // It should query for the internal cluster id first mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) // Then it will look for the backplane shard @@ -119,6 +123,7 @@ var _ = Describe("managedJob create command", func() { }) It("should respect url flag", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() // It should query for the internal cluster id first mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) // Then it will look for the backplane shard @@ -134,6 +139,7 @@ var _ = Describe("managedJob create command", func() { }) It("should fail when backplane did not return a 200", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -147,6 +153,7 @@ var _ = Describe("managedJob create command", func() { }) It("should fail when backplane returns a non parsable response", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -161,6 +168,7 @@ var _ = Describe("managedJob create command", func() { }) It("should wait for job to be finished", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -188,6 +196,7 @@ var _ = Describe("managedJob create command", func() { } fakeJobResp.Header.Add("Content-Type", "json") + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -209,6 +218,7 @@ var _ = Describe("managedJob create command", func() { } fakeJobResp.Header.Add("Content-Type", "json") + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -229,6 +239,7 @@ var _ = Describe("managedJob create command", func() { }) It("should stream the log of the job if it is in running status", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -256,6 +267,7 @@ var _ = Describe("managedJob create command", func() { StatusCode: http.StatusOK, } fakeJobResp.Header.Add("Content-Type", "json") + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() diff --git a/cmd/ocm-backplane/managedJob/deleteManagedJob_test.go b/cmd/ocm-backplane/managedJob/deleteManagedJob_test.go index d6d8cc13..0934eada 100644 --- a/cmd/ocm-backplane/managedJob/deleteManagedJob_test.go +++ b/cmd/ocm-backplane/managedJob/deleteManagedJob_test.go @@ -12,6 +12,7 @@ import ( "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd/api" + cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" "github.com/openshift/backplane-cli/pkg/backplaneapi" backplaneapiMock "github.com/openshift/backplane-cli/pkg/backplaneapi/mocks" "github.com/openshift/backplane-cli/pkg/client/mocks" @@ -36,7 +37,8 @@ var _ = Describe("managedJob delete command", func() { fakeResp *http.Response - sut *cobra.Command + sut *cobra.Command + ocmEnv *cmv1.Environment ) BeforeEach(func() { @@ -67,6 +69,7 @@ var _ = Describe("managedJob delete command", func() { _ = clientcmd.ModifyConfig(clientcmd.NewDefaultPathOptions(), api.Config{}, true) os.Setenv(info.BackplaneURLEnvName, proxyURI) + ocmEnv, _ = cmv1.NewEnvironment().BackplaneURL("https://dummy.api").Build() }) AfterEach(func() { @@ -76,6 +79,7 @@ var _ = Describe("managedJob delete command", func() { Context("delete managed job", func() { It("when running with a simple case should work as expected", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() // It should query for the internal cluster id first mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) // Then it will look for the backplane shard @@ -92,6 +96,7 @@ var _ = Describe("managedJob delete command", func() { }) It("should respect url flag", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) // Then it will look for the backplane shard mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() @@ -106,6 +111,7 @@ var _ = Describe("managedJob delete command", func() { }) It("should fail when backplane did not return a 200", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -119,6 +125,7 @@ var _ = Describe("managedJob delete command", func() { }) It("should not work when backplane returns a non parsable response with 200 return", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() diff --git a/cmd/ocm-backplane/managedJob/getManagedJob_test.go b/cmd/ocm-backplane/managedJob/getManagedJob_test.go index 6117b6b3..f74f3390 100644 --- a/cmd/ocm-backplane/managedJob/getManagedJob_test.go +++ b/cmd/ocm-backplane/managedJob/getManagedJob_test.go @@ -12,6 +12,7 @@ import ( "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd/api" + cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" "github.com/openshift/backplane-cli/pkg/backplaneapi" backplaneapiMock "github.com/openshift/backplane-cli/pkg/backplaneapi/mocks" "github.com/openshift/backplane-cli/pkg/client/mocks" @@ -37,7 +38,8 @@ var _ = Describe("managedJob get command", func() { fakeResp *http.Response fakeRespMultiple *http.Response - sut *cobra.Command + sut *cobra.Command + ocmEnv *cmv1.Environment ) BeforeEach(func() { @@ -122,6 +124,7 @@ var _ = Describe("managedJob get command", func() { _ = clientcmd.ModifyConfig(clientcmd.NewDefaultPathOptions(), api.Config{}, true) os.Setenv(info.BackplaneURLEnvName, proxyURI) + ocmEnv, _ = cmv1.NewEnvironment().BackplaneURL("https://dummy.api").Build() }) AfterEach(func() { @@ -131,6 +134,7 @@ var _ = Describe("managedJob get command", func() { Context("get a single managed job", func() { It("when running with a simple case should work as expected for single job", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() // It should query for the internal cluster id first mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) // Then it will look for the backplane shard @@ -146,6 +150,7 @@ var _ = Describe("managedJob get command", func() { }) It("should respect url flag", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) // Then it will look for the backplane shard mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() @@ -160,6 +165,7 @@ var _ = Describe("managedJob get command", func() { }) It("should fail when backplane did not return a 200", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -173,6 +179,7 @@ var _ = Describe("managedJob get command", func() { }) It("should not work when backplane returns a non parsable response", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -189,6 +196,7 @@ var _ = Describe("managedJob get command", func() { Context("get a all managed jobs", func() { It("when running with a simple case should work as expected", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() // It should query for the internal cluster id first mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) // Then it will look for the backplane shard @@ -204,6 +212,7 @@ var _ = Describe("managedJob get command", func() { }) It("should respect url flag", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) // Then it will look for the backplane shard mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() @@ -218,6 +227,7 @@ var _ = Describe("managedJob get command", func() { }) It("should fail when backplane did not return a 200", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -231,6 +241,7 @@ var _ = Describe("managedJob get command", func() { }) It("should not work when backplane returns a non parsable response", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() diff --git a/cmd/ocm-backplane/script/describeScript_test.go b/cmd/ocm-backplane/script/describeScript_test.go index 13e1c923..fb3980d3 100644 --- a/cmd/ocm-backplane/script/describeScript_test.go +++ b/cmd/ocm-backplane/script/describeScript_test.go @@ -14,6 +14,7 @@ import ( bpclient "github.com/openshift/backplane-api/pkg/client" + cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" "github.com/openshift/backplane-cli/pkg/backplaneapi" backplaneapiMock "github.com/openshift/backplane-cli/pkg/backplaneapi/mocks" "github.com/openshift/backplane-cli/pkg/client/mocks" @@ -35,12 +36,10 @@ var _ = Describe("describe script command", func() { testToken string trueClusterID string testScriptName string - - proxyURI string - - fakeResp *http.Response - - sut *cobra.Command + proxyURI string + fakeResp *http.Response + sut *cobra.Command + ocmEnv *cmv1.Environment ) BeforeEach(func() { @@ -86,6 +85,7 @@ var _ = Describe("describe script command", func() { _ = clientcmd.ModifyConfig(clientcmd.NewDefaultPathOptions(), api.Config{}, true) os.Setenv(info.BackplaneURLEnvName, proxyURI) + ocmEnv, _ = cmv1.NewEnvironment().BackplaneURL("https://dummy.api").Build() }) AfterEach(func() { @@ -95,6 +95,7 @@ var _ = Describe("describe script command", func() { Context("describe script", func() { It("when running with a simple case should work as expected", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() // It should query for the internal cluster id first mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) // Then it will look for the backplane shard @@ -111,6 +112,7 @@ var _ = Describe("describe script command", func() { }) It("should respect url flag", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() mockClientUtil.EXPECT().MakeRawBackplaneAPIClient("https://newbackplane.url").Return(mockClient, nil) @@ -126,6 +128,7 @@ var _ = Describe("describe script command", func() { It("Should able use the current logged in cluster if non specified and retrieve from config file", func() { err := utils.CreateTempKubeConfig(nil) Expect(err).To(BeNil()) + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq("configcluster")).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() mockClientUtil.EXPECT().MakeRawBackplaneAPIClient("https://api-backplane.apps.something.com").Return(mockClient, nil) @@ -139,6 +142,7 @@ var _ = Describe("describe script command", func() { }) It("should fail when backplane did not return a 200", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -160,6 +164,7 @@ var _ = Describe("describe script command", func() { } fakeRespBlank.Header.Add("Content-Type", "json") + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -192,6 +197,7 @@ var _ = Describe("describe script command", func() { } fakeRespNoEnv.Header.Add("Content-Type", "json") + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -206,6 +212,7 @@ var _ = Describe("describe script command", func() { }) It("should not work when backplane returns a non parsable response with 200 return", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() diff --git a/cmd/ocm-backplane/script/listScripts_test.go b/cmd/ocm-backplane/script/listScripts_test.go index 7e8a319a..4b860419 100644 --- a/cmd/ocm-backplane/script/listScripts_test.go +++ b/cmd/ocm-backplane/script/listScripts_test.go @@ -12,6 +12,7 @@ import ( bpclient "github.com/openshift/backplane-api/pkg/client" "github.com/spf13/cobra" + cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" "github.com/openshift/backplane-cli/pkg/backplaneapi" backplaneapiMock "github.com/openshift/backplane-cli/pkg/backplaneapi/mocks" "github.com/openshift/backplane-cli/pkg/client/mocks" @@ -34,12 +35,10 @@ var _ = Describe("list script command", func() { trueClusterID string //testKubeCfg api.Config testJobID string - - proxyURI string - - fakeResp *http.Response - - sut *cobra.Command + proxyURI string + fakeResp *http.Response + sut *cobra.Command + ocmEnv *cmv1.Environment ) BeforeEach(func() { @@ -82,6 +81,7 @@ var _ = Describe("list script command", func() { fakeResp.Header.Add("Content-Type", "json") os.Setenv(info.BackplaneURLEnvName, proxyURI) + ocmEnv, _ = cmv1.NewEnvironment().BackplaneURL("https://dummy.api").Build() }) AfterEach(func() { @@ -92,6 +92,7 @@ var _ = Describe("list script command", func() { Context("list scripts", func() { It("when running with a simple case should work as expected", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() // It should query for the internal cluster id first mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) // Then it will look for the backplane shard @@ -108,6 +109,7 @@ var _ = Describe("list script command", func() { }) It("should respect url flag", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() // Then it will look for the backplane shard mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -124,6 +126,7 @@ var _ = Describe("list script command", func() { It("Should able use the current logged in cluster if non specified and retrieve from config file", func() { err := utils.CreateTempKubeConfig(nil) Expect(err).To(BeNil()) + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq("configcluster")).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() mockClientUtil.EXPECT().MakeRawBackplaneAPIClient("https://api-backplane.apps.something.com").Return(mockClient, nil) @@ -137,6 +140,7 @@ var _ = Describe("list script command", func() { }) It("should fail when backplane did not return a 200", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -151,6 +155,7 @@ var _ = Describe("list script command", func() { }) It("should not work when backplane returns a non parsable response with 200 return", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -166,6 +171,7 @@ var _ = Describe("list script command", func() { }) It("should handle an empty list of scripts without errors", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() diff --git a/cmd/ocm-backplane/testJob/createTestJob_test.go b/cmd/ocm-backplane/testJob/createTestJob_test.go index f0db6937..dd859ddc 100644 --- a/cmd/ocm-backplane/testJob/createTestJob_test.go +++ b/cmd/ocm-backplane/testJob/createTestJob_test.go @@ -13,6 +13,7 @@ import ( . "github.com/onsi/gomega" "github.com/spf13/cobra" + cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" "github.com/openshift/backplane-cli/pkg/backplaneapi" backplaneapiMock "github.com/openshift/backplane-cli/pkg/backplaneapi/mocks" "github.com/openshift/backplane-cli/pkg/client/mocks" @@ -71,7 +72,8 @@ var _ = Describe("testJob create command", func() { fakeResp *http.Response - sut *cobra.Command + sut *cobra.Command + ocmEnv *cmv1.Environment ) BeforeEach(func() { @@ -111,6 +113,7 @@ var _ = Describe("testJob create command", func() { } fakeResp.Header.Add("Content-Type", "json") os.Setenv(info.BackplaneURLEnvName, proxyURI) + ocmEnv, _ = cmv1.NewEnvironment().BackplaneURL("https://dummy.api").Build() }) AfterEach(func() { @@ -123,6 +126,7 @@ var _ = Describe("testJob create command", func() { Context("create test job", func() { It("when running with a simple case should work as expected", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().IsProduction().Return(false, nil) // It should query for the internal cluster id first mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) @@ -138,6 +142,7 @@ var _ = Describe("testJob create command", func() { }) It("should respect url flag", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().IsProduction().Return(false, nil) // It should query for the internal cluster id first mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) @@ -153,9 +158,11 @@ var _ = Describe("testJob create command", func() { }) It("should respect the base image when supplied as a flag", func() { + baseImgOverride := "quay.io/foo/bar" mockOcmInterface.EXPECT().IsProduction().Return(false, nil) // It should query for the internal cluster id first + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockClientUtil.EXPECT().MakeRawBackplaneAPIClient(proxyURI).Return(mockClient, nil) @@ -170,6 +177,7 @@ var _ = Describe("testJob create command", func() { It("Should able use the current logged in cluster if non specified and retrieve from config file", func() { os.Setenv(info.BackplaneURLEnvName, "https://api-backplane.apps.something.com") + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().IsProduction().Return(false, nil) err := utils.CreateTempKubeConfig(nil) Expect(err).To(BeNil()) @@ -185,6 +193,7 @@ var _ = Describe("testJob create command", func() { }) It("should fail when backplane did not return a 200", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().IsProduction().Return(false, nil) mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() @@ -199,6 +208,7 @@ var _ = Describe("testJob create command", func() { }) It("should fail when backplane returns a non parsable response", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().IsProduction().Return(false, nil) mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() @@ -214,6 +224,7 @@ var _ = Describe("testJob create command", func() { }) It("should fail when metadata is not found/invalid", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().IsProduction().Return(false, nil) mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() @@ -229,6 +240,7 @@ var _ = Describe("testJob create command", func() { }) It("should fail when script file is not found/invalid", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().IsProduction().Return(false, nil) mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() @@ -244,6 +256,7 @@ var _ = Describe("testJob create command", func() { }) It("should not run in production environment", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().IsProduction().Return(true, nil) _ = os.Remove(path.Join(tempDir, "script.sh")) @@ -283,6 +296,7 @@ echo_touch "Hello" _ = os.WriteFile(path.Join(tempDir, "script.sh"), []byte(script), 0600) _ = os.Mkdir(path.Join(tempDir, "scripts"), 0755) _ = os.WriteFile(path.Join(tempDir, "scripts", "lib.sh"), []byte(lib), 0600) + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().IsProduction().Return(false, nil) // It should query for the internal cluster id first mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) diff --git a/cmd/ocm-backplane/testJob/getTestJob_test.go b/cmd/ocm-backplane/testJob/getTestJob_test.go index f6296f5c..6cedd320 100644 --- a/cmd/ocm-backplane/testJob/getTestJob_test.go +++ b/cmd/ocm-backplane/testJob/getTestJob_test.go @@ -11,6 +11,7 @@ import ( . "github.com/onsi/gomega" "github.com/spf13/cobra" + cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" "github.com/openshift/backplane-cli/pkg/backplaneapi" backplaneapiMock "github.com/openshift/backplane-cli/pkg/backplaneapi/mocks" "github.com/openshift/backplane-cli/pkg/client/mocks" @@ -35,7 +36,8 @@ var _ = Describe("testJob get command", func() { fakeResp *http.Response - sut *cobra.Command + sut *cobra.Command + ocmEnv *cmv1.Environment ) BeforeEach(func() { @@ -75,6 +77,7 @@ var _ = Describe("testJob get command", func() { } fakeResp.Header.Add("Content-Type", "json") os.Setenv(info.BackplaneURLEnvName, proxyURI) + ocmEnv, _ = cmv1.NewEnvironment().BackplaneURL("https://dummy.api").Build() }) AfterEach(func() { @@ -86,6 +89,7 @@ var _ = Describe("testJob get command", func() { Context("get test job", func() { It("when running with a simple case should work as expected", func() { // It should query for the internal cluster id first + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -99,6 +103,7 @@ var _ = Describe("testJob get command", func() { }) It("should respect url flag", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() // It should query for the internal cluster id first mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) // Then it will look for the backplane shard @@ -114,6 +119,7 @@ var _ = Describe("testJob get command", func() { }) It("should fail when backplane did not return a 200", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() @@ -127,6 +133,7 @@ var _ = Describe("testJob get command", func() { }) It("should fail when backplane returns a non parsable response", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil) mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes() mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes() diff --git a/pkg/cli/config/config.go b/pkg/cli/config/config.go index 1f414d01..028f2c02 100644 --- a/pkg/cli/config/config.go +++ b/pkg/cli/config/config.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "net/http" "net/url" "os" @@ -11,6 +12,7 @@ import ( "github.com/spf13/viper" "github.com/openshift/backplane-cli/pkg/info" + "github.com/openshift/backplane-cli/pkg/ocm" ) type BackplaneConfiguration struct { @@ -58,19 +60,28 @@ func GetBackplaneConfiguration() (bpConfig BackplaneConfiguration, err error) { } } - // Check if user has explicitly defined backplane URL; it has higher precedence over the config file - err = viper.BindEnv("url", info.BackplaneURLEnvName) - if err != nil { - return bpConfig, err - } - // Check if user has explicitly defined proxy; it has higher precedence over the config file err = viper.BindEnv("proxy-url", info.BackplaneProxyEnvName) if err != nil { return bpConfig, err } - bpConfig.URL = viper.GetString("url") + // Warn user if url defined in the config file + if viper.GetString("url") != "" { + logger.Warn("Manual URL configuration is deprecated, please remove URL key from Backplane configuration") + } + + // Check if user has explicitly defined backplane URL via env; it has higher precedence over the ocm env URL + url, ok := getBackplaneEnv(info.BackplaneURLEnvName) + if ok { + bpConfig.URL = url + } else { + // Fetch backplane URL from ocm env + if bpConfig.URL, err = bpConfig.GetBackplaneURL(); err != nil { + return bpConfig, err + } + } + bpConfig.SessionDirectory = viper.GetString("session-dir") bpConfig.AssumeInitialArn = viper.GetString("assume-initial-arn") @@ -85,6 +96,31 @@ func GetBackplaneConfiguration() (bpConfig BackplaneConfiguration, err error) { return bpConfig, nil } +// GetBackplaneURL returns API URL +func (config *BackplaneConfiguration) GetBackplaneURL() (string, error) { + + ocmEnv, err := ocm.DefaultOCMInterface.GetOCMEnvironment() + if err != nil { + return "", err + } + url, ok := ocmEnv.GetBackplaneURL() + if !ok { + return "", fmt.Errorf("the requested API endpoint is not available for the OCM environment: %v", ocmEnv.Name()) + } + logger.Infof("Backplane URL retrieved via OCM environment: %s", url) + return url, nil +} + +// getBackplaneEnv retrieves the value of the environment variable named by the key +func getBackplaneEnv(key string) (string, bool) { + val, ok := os.LookupEnv(key) + if ok { + logger.Infof("Backplane key %s set via env vars: %s", key, val) + return val, ok + } + return "", false +} + // CheckAPIConnection validate API connection via configured proxy and VPN func (config BackplaneConfiguration) CheckAPIConnection() error { diff --git a/pkg/cli/config/config_test.go b/pkg/cli/config/config_test.go deleted file mode 100644 index 07a3f44b..00000000 --- a/pkg/cli/config/config_test.go +++ /dev/null @@ -1,104 +0,0 @@ -package config - -import ( - "net/http" - "net/http/httptest" - "testing" -) - -func TestGetBackplaneConfig(t *testing.T) { - t.Run("it returns the user defined proxy instead of the configuration variable", func(t *testing.T) { - userDefinedProxy := "example-proxy" - t.Setenv("HTTPS_PROXY", userDefinedProxy) - config, err := GetBackplaneConfiguration() - if err != nil { - t.Error(err) - } - - if config.ProxyURL != nil && *config.ProxyURL != userDefinedProxy { - t.Errorf("expected to return the explicitly defined proxy %v instead of the default one %v", userDefinedProxy, config.ProxyURL) - } - }) - - t.Run("it returns the user defined backplane URL instead of the configuration variable", func(t *testing.T) { - userDefinedURL := "example-url" - t.Setenv("BACKPLANE_URL", userDefinedURL) - config, err := GetBackplaneConfiguration() - if err != nil { - t.Error(err) - } - - if config.URL != userDefinedURL { - t.Errorf("expected to return the explicitly defined url %v instead of the default one %v", userDefinedURL, config.URL) - } - }) -} - -func TestGetBackplaneConfiguration(t *testing.T) { - - for name, tc := range map[string]struct { - envNeedToSet bool - backplaneURLEnvVar string - proxyURL string - expectedBackplaneURL string - expectedError bool - }{ - "backplane url set via env vars": { - envNeedToSet: true, - backplaneURLEnvVar: "https://api-backplane.apps.openshiftapps.com", - proxyURL: "http://squid.myproxy.com", - expectedBackplaneURL: "https://api-backplane.apps.openshiftapps.com", - expectedError: false, - }, - } { - tc := tc - - t.Run(name, func(t *testing.T) { - if tc.envNeedToSet { - t.Setenv("BACKPLANE_URL", tc.backplaneURLEnvVar) - t.Setenv("HTTPS_PROXY", tc.proxyURL) - } - - bpConfig, err := GetBackplaneConfiguration() - - if tc.expectedError && err == nil { - t.Errorf("expected err to be %v", err) - } - if bpConfig.URL != tc.expectedBackplaneURL { - t.Errorf("expected res to be %s got %s", tc.expectedBackplaneURL, bpConfig.URL) - } - }) - } -} - -func TestGetBackplaneConnection(t *testing.T) { - t.Run("should fail if backplane API return connection errors", func(t *testing.T) { - - svr := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - _, _ = w.Write([]byte("dummy data")) - })) - - proxyURL := "http://squid.myproxy.com" - t.Setenv("BACKPLANE_URL", svr.URL) - t.Setenv("HTTPS_PROXY", proxyURL) - config, err := GetBackplaneConfiguration() - if err != nil { - t.Error(err) - } - - err = config.CheckAPIConnection() - if err != nil { - t.Failed() - } - - }) - - t.Run("should fail for empty proxy url", func(t *testing.T) { - config := BackplaneConfiguration{URL: "https://api-backplane.apps.openshiftapps.com", ProxyURL: nil} - err := config.CheckAPIConnection() - - if err != nil { - t.Failed() - } - }) -} diff --git a/pkg/cli/session/session_test.go b/pkg/cli/session/session_test.go index 9a17c48e..3ae389f7 100644 --- a/pkg/cli/session/session_test.go +++ b/pkg/cli/session/session_test.go @@ -13,6 +13,7 @@ import ( . "github.com/onsi/gomega" "github.com/spf13/cobra" + cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" "github.com/openshift/backplane-cli/pkg/backplaneapi" backplaneapiMock "github.com/openshift/backplane-cli/pkg/backplaneapi/mocks" "github.com/openshift/backplane-cli/pkg/cli/globalflags" @@ -40,6 +41,7 @@ var _ = Describe("Backplane Session Unit test", func() { backplaneAPIUri string fakeResp *http.Response + ocmEnv *cmv1.Environment ) BeforeEach(func() { @@ -83,6 +85,7 @@ var _ = Describe("Backplane Session Unit test", func() { fakeResp.Header.Add("Content-Type", "json") os.Setenv(info.BackplaneURLEnvName, backplaneAPIUri) + ocmEnv, _ = cmv1.NewEnvironment().BackplaneURL("https://dummy.api").Build() }) AfterEach(func() { @@ -91,6 +94,7 @@ var _ = Describe("Backplane Session Unit test", func() { Context("check Backplane session setup", func() { It("Check backplane session default files", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(options.Alias).Return("", "", errors.New("err")).AnyTimes() err := bpSession.Setup() @@ -113,6 +117,7 @@ var _ = Describe("Backplane Session Unit test", func() { }) It("Check backplane session folder permissions", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(options.Alias).Return("", "", errors.New("err")).AnyTimes() err := bpSession.Setup() @@ -135,6 +140,7 @@ var _ = Describe("Backplane Session Unit test", func() { It("should fail for invalid cluster alias name ", func() { options.Alias = "my-session" + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(options.Alias).Return("", "", errors.New("err")).AnyTimes() err := bpSession.RunCommand(cmd, []string{}) @@ -155,6 +161,7 @@ var _ = Describe("Backplane Session Unit test", func() { options.Alias = "" options.ClusterID = testClusterID + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockClientWithResp.EXPECT().LoginClusterWithResponse(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(options.ClusterID).Return(trueClusterID, testClusterID, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(trueClusterID).Return(trueClusterID, testClusterID, nil).AnyTimes() @@ -172,6 +179,7 @@ var _ = Describe("Backplane Session Unit test", func() { options.Alias = "test-env" options.ClusterID = testClusterID + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockClientWithResp.EXPECT().LoginClusterWithResponse(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(options.ClusterID).Return(trueClusterID, testClusterID, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(trueClusterID).Return(trueClusterID, testClusterID, nil).AnyTimes() @@ -211,6 +219,7 @@ var _ = Describe("Backplane Session Unit test", func() { options.Alias = "my-session" options.ClusterID = testClusterID + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockClientWithResp.EXPECT().LoginClusterWithResponse(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(options.ClusterID).Return(trueClusterID, testClusterID, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(trueClusterID).Return(trueClusterID, testClusterID, nil).AnyTimes() diff --git a/pkg/monitoring/monitoring_test.go b/pkg/monitoring/monitoring_test.go index a28be05d..8ba4b654 100644 --- a/pkg/monitoring/monitoring_test.go +++ b/pkg/monitoring/monitoring_test.go @@ -40,6 +40,7 @@ var _ = Describe("Backplane Monitoring Unit test", func() { clusterVersion412 *cmv1.Cluster clusterVersion410 *cmv1.Cluster client Client + ocmEnv *cmv1.Environment ) BeforeEach(func() { @@ -116,6 +117,7 @@ var _ = Describe("Backplane Monitoring Unit test", func() { Expect(err).To(BeNil()) os.Setenv(info.BackplaneURLEnvName, backplaneAPIUri) + ocmEnv, _ = cmv1.NewEnvironment().BackplaneURL("https://dummy.api").Build() }) AfterEach(func() { @@ -134,6 +136,7 @@ var _ = Describe("Backplane Monitoring Unit test", func() { It("should fail for cluster version greater than 4.11 and openshift monitoring namespace ", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockClientWithResp.EXPECT().LoginClusterWithResponse(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(trueClusterID).Return(trueClusterID, testClusterID, nil).AnyTimes() mockOcmInterface.EXPECT().GetClusterInfoByID(testClusterID).Return(clusterVersion412, nil).AnyTimes() @@ -168,6 +171,7 @@ var _ = Describe("Backplane Monitoring Unit test", func() { }) It("should serve thanos monitoring dashboard for cluster version greater than 4.11", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockClientWithResp.EXPECT().LoginClusterWithResponse(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(trueClusterID).Return(trueClusterID, testClusterID, nil).AnyTimes() mockOcmInterface.EXPECT().GetClusterInfoByID(testClusterID).Return(clusterVersion412, nil).AnyTimes() @@ -191,6 +195,7 @@ var _ = Describe("Backplane Monitoring Unit test", func() { It("should serve monitoring dashboard for cluster version lower than 4.11 and openshift monitoring namespace ", func() { + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() mockClientWithResp.EXPECT().LoginClusterWithResponse(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes() mockOcmInterface.EXPECT().GetTargetCluster(trueClusterID).Return(trueClusterID, testClusterID, nil).AnyTimes() mockOcmInterface.EXPECT().GetClusterInfoByID(testClusterID).Return(clusterVersion410, nil).AnyTimes() diff --git a/pkg/ocm/mocks/ocmWrapperMock.go b/pkg/ocm/mocks/ocmWrapperMock.go index 6c223bca..7471a6f0 100644 --- a/pkg/ocm/mocks/ocmWrapperMock.go +++ b/pkg/ocm/mocks/ocmWrapperMock.go @@ -81,6 +81,21 @@ func (mr *MockOCMInterfaceMockRecorder) GetOCMAccessToken() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetOCMAccessToken", reflect.TypeOf((*MockOCMInterface)(nil).GetOCMAccessToken)) } +// GetOCMEnvironment mocks base method. +func (m *MockOCMInterface) GetOCMEnvironment() (*v1.Environment, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetOCMEnvironment") + ret0, _ := ret[0].(*v1.Environment) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetOCMEnvironment indicates an expected call of GetOCMEnvironment. +func (mr *MockOCMInterfaceMockRecorder) GetOCMEnvironment() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetOCMEnvironment", reflect.TypeOf((*MockOCMInterface)(nil).GetOCMEnvironment)) +} + // GetPullSecret mocks base method. func (m *MockOCMInterface) GetPullSecret() (string, error) { m.ctrl.T.Helper() diff --git a/pkg/ocm/ocmWrapper.go b/pkg/ocm/ocmWrapper.go index 3f364d4a..17b248fc 100644 --- a/pkg/ocm/ocmWrapper.go +++ b/pkg/ocm/ocmWrapper.go @@ -25,6 +25,7 @@ type OCMInterface interface { IsProduction() (bool, error) GetPullSecret() (string, error) GetStsSupportJumpRoleARN(ocmConnection *ocmsdk.Connection, clusterID string) (string, error) + GetOCMEnvironment() (*cmv1.Environment, error) } const ( @@ -275,6 +276,23 @@ func (*DefaultOCMInterfaceImpl) GetStsSupportJumpRoleARN(ocmConnection *ocmsdk.C return response.Body().RoleArn(), nil } +// GetBackplaneURL returns the Backplane API URL based on the OCM env +func (*DefaultOCMInterfaceImpl) GetOCMEnvironment() (*cmv1.Environment, error) { + // Create the client for the OCM API + connection, err := ocm.NewConnection().Build() + if err != nil { + return nil, fmt.Errorf("failed to create OCM connection: %v", err) + } + defer connection.Close() + + responseEnv, err := connection.ClustersMgmt().V1().Environment().Get().Send() + if err != nil { + return nil, fmt.Errorf("failed to get cluster environment %w", err) + } + + return responseEnv.Body(), nil +} + func getClusters(client *cmv1.ClustersClient, clusterKey string) ([]*cmv1.Cluster, error) { var clusters []*cmv1.Cluster diff --git a/pkg/utils/cluster_test.go b/pkg/utils/cluster_test.go index fbd82a4d..4ebfb92e 100644 --- a/pkg/utils/cluster_test.go +++ b/pkg/utils/cluster_test.go @@ -10,6 +10,7 @@ import ( "github.com/golang/mock/gomock" "k8s.io/client-go/tools/clientcmd" + cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" "github.com/openshift/backplane-cli/pkg/info" "github.com/openshift/backplane-cli/pkg/ocm" ocmMock "github.com/openshift/backplane-cli/pkg/ocm/mocks" @@ -199,6 +200,7 @@ func TestGetClusterIDAndHostFromClusterURL(t *testing.T) { } func TestGetBackplaneClusterFromClusterKey(t *testing.T) { + mockCtrl := gomock.NewController(t) mockOcmInterface := ocmMock.NewMockOCMInterface(mockCtrl) @@ -209,6 +211,8 @@ func TestGetBackplaneClusterFromClusterKey(t *testing.T) { ocm.DefaultOCMInterface = mockOcmInterface t.Run("it returns a cluster struct from a valid cluster key", func(_ *testing.T) { + ocmEnv, _ := cmv1.NewEnvironment().BackplaneURL("https://dummy.api").Build() + mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes() os.Setenv(info.BackplaneURLEnvName, "https://backplane-url.cluster-key.redhat.com") mockOcmInterface.EXPECT().GetTargetCluster("cluster-key").Return("1234", "cluster-key", nil)