From 7ad8229a67fcab648d71aabd43b9da4cd17e39f0 Mon Sep 17 00:00:00 2001 From: David Sabeti Date: Mon, 3 Oct 2022 15:49:25 +0000 Subject: [PATCH] Reorganize credhub tests so that test setup is not run when tests are skipped Because 'AssistedCredhubDescribe' and 'NonAssistedCredhubDescribe' were nested under the top-level 'CredhubDescribe', the BeforeEach of the top-level describe block would be run even when Assisted or NonAssisted tests were skipped. This removes the top-level CredhubDescribe and uses helper functions for the set up functionality shared by the two types of tests. --- credhub/service_bindings.go | 543 ++++++++++++++++++++---------------- 1 file changed, 306 insertions(+), 237 deletions(-) diff --git a/credhub/service_bindings.go b/credhub/service_bindings.go index e4c26ad3c..9dd51f46f 100644 --- a/credhub/service_bindings.go +++ b/credhub/service_bindings.go @@ -1,6 +1,7 @@ package credhub import ( + "encoding/json" "io/ioutil" "os" "path" @@ -14,8 +15,6 @@ import ( . "github.com/onsi/gomega/gbytes" . "github.com/onsi/gomega/gexec" - "encoding/json" - "strings" archive_helpers "code.cloudfoundry.org/archiver/extractor/test_helper" @@ -27,7 +26,7 @@ import ( "github.com/cloudfoundry/cf-test-helpers/v2/workflowhelpers" ) -var _ = CredhubDescribe("service bindings", func() { +var _ = NonAssistedCredhubDescribe("service bindings", func() { var ( chBrokerAppName string chServiceName string @@ -36,95 +35,118 @@ var _ = CredhubDescribe("service bindings", func() { ) BeforeEach(func() { - TestSetup.RegularUserContext().TargetSpace() - cf.Cf("target", "-o", TestSetup.RegularUserContext().Org, "-s", TestSetup.RegularUserContext().Space) + chBrokerAppName, chServiceName, instanceName = sharedCredhubSetup() + }) - chBrokerAppName = random_name.CATSRandomName("BRKR-CH") + AfterEach(func() { + sharedCredhubTeardown(chBrokerAppName, instanceName) + }) - Expect(cf.Cf( - "push", chBrokerAppName, - "-b", Config.GetGoBuildpackName(), - "-m", DEFAULT_MEMORY_LIMIT, - "-p", assets.NewAssets().CredHubServiceBroker, - "-f", assets.NewAssets().CredHubServiceBroker+"/manifest.yml", - ).Wait(Config.CfPushTimeoutDuration())).To(Exit(0), "failed pushing credhub-enabled service broker") - - existingEnvVar := string(cf.Cf("running-environment-variable-group").Wait().Out.Contents()) - - if !strings.Contains(existingEnvVar, "CREDHUB_API") { - Expect(cf.Cf( - "set-env", chBrokerAppName, - "CREDHUB_API", Config.GetCredHubLocation(), - ).Wait()).To(Exit(0), "failed setting CREDHUB_API env var on credhub-enabled service broker") - } - - chServiceName = random_name.CATSRandomName("SERVICE-NAME") - Expect(cf.Cf( - "set-env", chBrokerAppName, - "SERVICE_NAME", chServiceName, - ).Wait()).To(Exit(0), "failed setting SERVICE_NAME env var on credhub-enabled service broker") + Context("during staging", func() { + var ( + buildpackName string + appName string + appPath string - Expect(cf.Cf( - "set-env", chBrokerAppName, - "CREDHUB_CLIENT", Config.GetCredHubBrokerClientCredential(), - ).Wait()).To(Exit(0), "failed setting CREDHUB_CLIENT env var on credhub-enabled service broker") + tmpdir string + ) - Expect(cf.CfRedact( - Config.GetCredHubBrokerClientSecret(), "set-env", chBrokerAppName, - "CREDHUB_SECRET", Config.GetCredHubBrokerClientSecret(), - ).Wait()).To(Exit(0), "failed setting CREDHUB_SECRET env var on credhub-enabled service broker") + SkipOnK8s("Custom buildpacks not yet supported") - Expect(cf.Cf( - "restart", chBrokerAppName, - ).Wait(Config.CfPushTimeoutDuration())).To(Exit(0), "failed restarting credhub-enabled service broker") + BeforeEach(func() { + buildpackName, appName, appPath, tmpdir = createBuildpack() + Expect(cf.Cf("push", appName, + "--no-start", + "-b", buildpackName, + "-m", DEFAULT_MEMORY_LIMIT, + "-p", appPath, + ).Wait()).To(Exit(0)) - workflowhelpers.AsUser(TestSetup.AdminUserContext(), Config.DefaultTimeoutDuration(), func() { - serviceUrl := "https://" + chBrokerAppName + "." + Config.GetAppsDomain() - createServiceBroker := cf.Cf("create-service-broker", chBrokerAppName, Config.GetAdminUser(), Config.GetAdminPassword(), serviceUrl).Wait() - Expect(createServiceBroker).To(Exit(0), "failed creating credhub-enabled service broker") + appStartSession = bindServiceAndStartApp(appName, chServiceName, instanceName) + }) + + AfterEach(func() { + app_helpers.AppReport(appName) - enableAccess := cf.Cf("enable-service-access", chServiceName, "-o", TestSetup.RegularUserContext().Org).Wait() - Expect(enableAccess).To(Exit(0), "failed to enable service access for credhub-enabled broker") + Expect(cf.Cf("delete", appName, "-f", "-r").Wait()).To(Exit(0)) + deleteBuildpack(buildpackName, tmpdir) + }) - TestSetup.RegularUserContext().TargetSpace() - instanceName = random_name.CATSRandomName("SVIN-CH") - createService := cf.Cf("create-service", chServiceName, "credhub-read-plan", instanceName).Wait() - Expect(createService).To(Exit(0), "failed creating credhub enabled service") + It("still contains CredHub references in VCAP_SERVICES", func() { + Expect(appStartSession).NotTo(Say("pinkyPie")) + Expect(appStartSession).NotTo(Say("rainbowDash")) + Expect(appStartSession).To(Say("credhub-ref")) }) }) - AfterEach(func() { - workflowhelpers.AsUser(TestSetup.AdminUserContext(), Config.DefaultTimeoutDuration(), func() { - TestSetup.RegularUserContext().TargetSpace() + Context("during runtime", func() { + var appName, appURL string + BeforeEach(func() { + appName = random_name.CATSRandomName("APP-CH") + appURL = "https://" + appName + "." + Config.GetAppsDomain() - Expect(cf.Cf("purge-service-instance", instanceName, "-f").Wait()).To(Exit(0)) - Expect(cf.Cf("delete-service-broker", chBrokerAppName, "-f").Wait()).To(Exit(0)) + createApp := cf.Cf( + "push", appName, + "--no-start", + "-b", Config.GetJavaBuildpackName(), + "-m", "1024M", + "-p", assets.NewAssets().CredHubEnabledApp, + ).Wait(Config.CfPushTimeoutDuration()) + Expect(createApp).To(Exit(0), "failed creating credhub-enabled app") + appStartSession = bindServiceAndStartApp(appName, chServiceName, instanceName) }) - }) - bindServiceAndStartApp := func(appName string) { - Expect(chServiceName).ToNot(Equal("")) - setServiceName := cf.Cf("set-env", appName, "SERVICE_NAME", chServiceName).Wait() - Expect(setServiceName).To(Exit(0), "failed setting SERVICE_NAME env var on app") + AfterEach(func() { + app_helpers.AppReport(appName) + app_helpers.AppReport(chBrokerAppName) - existingEnvVar := string(cf.Cf("running-environment-variable-group").Wait().Out.Contents()) + workflowhelpers.AsUser(TestSetup.AdminUserContext(), Config.DefaultTimeoutDuration(), func() { + TestSetup.RegularUserContext().TargetSpace() + unbindService := cf.Cf("unbind-service", appName, instanceName).Wait() + Expect(unbindService).To(Exit(0), "failed unbinding app and service") + + Expect(cf.Cf("delete", appName, "-f", "-r").Wait(Config.CfPushTimeoutDuration())).To(Exit(0)) + }) + }) + + It("the broker returns credhub-ref in the credentials block", func() { + appEnv := string(cf.Cf("env", appName).Wait().Out.Contents()) + Expect(appEnv).To(ContainSubstring("credentials"), "credential block missing from service") + Expect(appEnv).To(ContainSubstring("credhub-ref"), "credhub-ref not found") + }) - if !strings.Contains(existingEnvVar, "CREDHUB_API") { - Expect(cf.Cf( - "set-env", appName, - "CREDHUB_API", Config.GetCredHubLocation(), - ).Wait()).To(Exit(0), "failed setting CREDHUB_API env var on app") - } + It("the bound app retrieves the credentials for the ref from CredHub", func() { + curlCmd := helpers.CurlSkipSSL(true, appURL+"/test").Wait() + Expect(curlCmd).To(Exit(0)) - workflowhelpers.AsUser(TestSetup.AdminUserContext(), Config.DefaultTimeoutDuration(), func() { - TestSetup.RegularUserContext().TargetSpace() + bytes := curlCmd.Out.Contents() + var response struct { + UserName string `json:"user-name"` + Password string `json:"password"` + } - bindService := cf.Cf("bind-service", appName, instanceName).Wait() - Expect(bindService).To(Exit(0), "failed binding app to service") + json.Unmarshal(bytes, &response) + Expect(response.UserName).To(Equal("pinkyPie")) + Expect(response.Password).To(Equal("rainbowDash")) }) - appStartSession = cf.Cf("start", appName).Wait(Config.CfPushTimeoutDuration()) - Expect(appStartSession).To(Exit(0)) - } + + }) +}) + +var _ = AssistedCredhubDescribe("service bindings", func() { + var ( + chBrokerAppName string + chServiceName string + instanceName string + ) + + BeforeEach(func() { + chBrokerAppName, chServiceName, instanceName = sharedCredhubSetup() + }) + + AfterEach(func() { + sharedCredhubTeardown(chBrokerAppName, instanceName) + }) Context("during staging", func() { var ( @@ -132,71 +154,13 @@ var _ = CredhubDescribe("service bindings", func() { appName string appPath string - buildpackPath string - buildpackArchivePath string - tmpdir string ) SkipOnK8s("Custom buildpacks not yet supported") BeforeEach(func() { - workflowhelpers.AsUser(TestSetup.AdminUserContext(), Config.DefaultTimeoutDuration(), func() { - buildpackName = random_name.CATSRandomName("BPK") - appName = random_name.CATSRandomName("APP") - - var err error - tmpdir, err = ioutil.TempDir("", "buildpack_env") - Expect(err).ToNot(HaveOccurred()) - appPath, err = ioutil.TempDir(tmpdir, "matching-app") - Expect(err).ToNot(HaveOccurred()) - - buildpackPath, err = ioutil.TempDir(tmpdir, "matching-buildpack") - Expect(err).ToNot(HaveOccurred()) - - buildpackArchivePath = path.Join(buildpackPath, "buildpack.zip") - - archive_helpers.CreateZipArchive(buildpackArchivePath, []archive_helpers.ArchiveFile{ - { - Name: "bin/compile", - Body: `#!/usr/bin/env bash -echo COMPILING... really just dumping env... -env -`, - }, - { - Name: "bin/detect", - Body: `#!/bin/bash - -exit 1 -`, - }, - { - Name: "bin/release", - Body: `#!/usr/bin/env bash - -cat <