github.com/redhat-appstudio/e2e-tests@v0.0.0-20230619105049-9a422b2094d7/tests/spi/get-file-content.go (about)

     1  package spi
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"time"
     7  
     8  	. "github.com/onsi/ginkgo/v2"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/redhat-appstudio/e2e-tests/pkg/constants"
    11  	"github.com/redhat-appstudio/e2e-tests/pkg/framework"
    12  	"github.com/redhat-appstudio/e2e-tests/pkg/utils"
    13  	"github.com/redhat-appstudio/service-provider-integration-operator/api/v1beta1"
    14  )
    15  
    16  /*
    17   * Component: spi
    18   * Description: SVPI-402 - Get file content from a private Github repository
    19   */
    20  
    21  var _ = framework.SPISuiteDescribe(Label("spi-suite", "get-file-content"), func() {
    22  
    23  	defer GinkgoRecover()
    24  
    25  	var fw *framework.Framework
    26  	var err error
    27  	var namespace string
    28  
    29  	Describe("SVPI-402 - Get file content from a private Github repository", Ordered, func() {
    30  		BeforeAll(func() {
    31  			if os.Getenv("CI") != "true" {
    32  				Skip(fmt.Sprintln("test skipped on local execution"))
    33  			}
    34  			// Initialize the tests controllers
    35  			fw, err = framework.NewFramework(utils.GetGeneratedNamespace("spi-demos"))
    36  			Expect(err).NotTo(HaveOccurred())
    37  			namespace = fw.UserNamespace
    38  			Expect(namespace).NotTo(BeEmpty())
    39  
    40  			// collect SPI ResourceQuota metrics (temporary)
    41  			err := fw.AsKubeAdmin.CommonController.GetResourceQuotaInfo("token-upload-rest-endpoint", namespace, "appstudio-crds-spi")
    42  			Expect(err).NotTo(HaveOccurred())
    43  		})
    44  
    45  		// Clean up after running these tests and before the next tests block: can't have multiple AccessTokens in Injected phase
    46  		AfterAll(func() {
    47  			// collect SPI ResourceQuota metrics (temporary)
    48  			err := fw.AsKubeAdmin.CommonController.GetResourceQuotaInfo("get-file-content", namespace, "appstudio-crds-spi")
    49  			Expect(err).NotTo(HaveOccurred())
    50  
    51  			if !CurrentSpecReport().Failed() {
    52  				Expect(fw.AsKubeAdmin.SPIController.DeleteAllBindingTokensInASpecificNamespace(namespace)).To(Succeed())
    53  				Expect(fw.AsKubeAdmin.SPIController.DeleteAllAccessTokensInASpecificNamespace(namespace)).To(Succeed())
    54  				Expect(fw.AsKubeAdmin.SPIController.DeleteAllAccessTokenDataInASpecificNamespace(namespace)).To(Succeed())
    55  				Expect(fw.AsKubeAdmin.CommonController.DeleteAllServiceAccountsInASpecificNamespace(namespace)).To(Succeed())
    56  			}
    57  		})
    58  
    59  		var SPIFcr *v1beta1.SPIFileContentRequest
    60  
    61  		It("creates SPIFileContentRequest", func() {
    62  			SPIFcr, err = fw.AsKubeDeveloper.SPIController.CreateSPIFileContentRequest("gh-spi-filecontent-request", namespace, GithubPrivateRepoURL, GithubPrivateRepoFilePath)
    63  			Expect(err).NotTo(HaveOccurred())
    64  
    65  			SPIFcr, err = fw.AsKubeDeveloper.SPIController.GetSPIFileContentRequest(SPIFcr.Name, namespace)
    66  			Expect(err).NotTo(HaveOccurred())
    67  		})
    68  
    69  		It("SPIFileContentRequest should be in AwaitingTokenData phase", func() {
    70  			Eventually(func() bool {
    71  				SPIFcr, err = fw.AsKubeDeveloper.SPIController.GetSPIFileContentRequest(SPIFcr.Name, namespace)
    72  				Expect(err).NotTo(HaveOccurred())
    73  
    74  				return SPIFcr.Status.Phase == v1beta1.SPIFileContentRequestPhaseAwaitingTokenData
    75  			}, 2*time.Minute, 10*time.Second).Should(BeTrue(), "")
    76  
    77  		})
    78  
    79  		It("uploads username and token using rest endpoint", func() {
    80  			// the UploadUrl in SPITokenBinding should be available before uploading the token
    81  			Eventually(func() bool {
    82  				SPIFcr, err = fw.AsKubeDeveloper.SPIController.GetSPIFileContentRequest(SPIFcr.Name, namespace)
    83  				Expect(err).NotTo(HaveOccurred())
    84  
    85  				return SPIFcr.Status.TokenUploadUrl != ""
    86  			}, 1*time.Minute, 10*time.Second).Should(BeTrue(), "uploadUrl not set")
    87  			Expect(err).NotTo(HaveOccurred())
    88  
    89  			// get the url to manually upload the token
    90  			uploadURL := SPIFcr.Status.TokenUploadUrl
    91  			Expect(uploadURL).NotTo(BeEmpty())
    92  
    93  			// Get the token for the current openshift user
    94  			bearerToken, err := utils.GetOpenshiftToken()
    95  			Expect(err).NotTo(HaveOccurred())
    96  
    97  			// build and upload the payload using the uploadURL. it should return 204
    98  			oauthCredentials := `{"access_token":"` + utils.GetEnv(constants.GITHUB_TOKEN_ENV, "") + `"}`
    99  			statusCode, err := fw.AsKubeDeveloper.SPIController.UploadWithRestEndpoint(uploadURL, oauthCredentials, bearerToken)
   100  			Expect(err).NotTo(HaveOccurred())
   101  			Expect(statusCode).Should(Equal(204))
   102  		})
   103  
   104  		It("SPIFileContentRequest should be in Delivered phase and content should be provided", func() {
   105  			Eventually(func() bool {
   106  				SPIFcr, err = fw.AsKubeDeveloper.SPIController.GetSPIFileContentRequest(SPIFcr.Name, namespace)
   107  				Expect(err).NotTo(HaveOccurred())
   108  
   109  				return SPIFcr.Status.Phase == v1beta1.SPIFileContentRequestPhaseDelivered && SPIFcr.Status.Content != ""
   110  			}, 2*time.Minute, 10*time.Second).Should(BeTrue(), "content not provided by SPIFileContentRequest")
   111  
   112  		})
   113  
   114  	})
   115  })