github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/tests/spi/token-upload-k8s.go (about)

     1  package spi
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/redhat-appstudio/e2e-tests/pkg/constants"
     8  	"github.com/redhat-appstudio/e2e-tests/pkg/utils"
     9  	"github.com/redhat-appstudio/service-provider-integration-operator/api/v1beta1"
    10  	v1 "k8s.io/api/core/v1"
    11  
    12  	. "github.com/onsi/ginkgo/v2"
    13  	. "github.com/onsi/gomega"
    14  	"github.com/redhat-appstudio/e2e-tests/pkg/framework"
    15  	k8sErrors "k8s.io/apimachinery/pkg/api/errors"
    16  )
    17  
    18  /*
    19   * Component: spi
    20   * Description: SVPI-399 - Upload token with k8s secret
    21  
    22   * Test Scenario 1: Upload token with k8s secret (associate it to existing SPIAccessToken)
    23   * Test Scenario 2: Upload token with k8s secret (create new SPIAccessToken automatically)
    24  
    25    * Flow of Test Scenario 1:
    26  	* 1º - creates SPITokenBinding
    27  	* 2º - creates secret with access token and associate it to an existing SPIAccessToken
    28  	* 3º - SPITokenBinding should be in Injected phase
    29  	* 4º - upload secret should be automatically be removed
    30  	* 5º - SPIAccessToken exists and is in Read phase
    31  
    32    * Flow of Test Scenario 2:
    33  	* 1º - creates secret with access token and associate it to an existing SPIAccessToken
    34  	* 2º - upload secret should be automatically be removed
    35  	* 3º - SPIAccessToken exists and is in Read phase
    36  */
    37  
    38  var _ = framework.SPISuiteDescribe(Label("spi-suite", "token-upload-k8s"), func() {
    39  
    40  	defer GinkgoRecover()
    41  
    42  	var fw *framework.Framework
    43  	var err error
    44  	var namespace string
    45  	AfterEach(framework.ReportFailure(&fw))
    46  
    47  	Describe("SVPI-399 - Upload token with k8s secret (associate it to existing SPIAccessToken)", Ordered, func() {
    48  		BeforeAll(func() {
    49  			// Initialize the tests controllers
    50  			fw, err = framework.NewFramework(utils.GetGeneratedNamespace("spi-demos"))
    51  			Expect(err).NotTo(HaveOccurred())
    52  			namespace = fw.UserNamespace
    53  			Expect(namespace).NotTo(BeEmpty())
    54  
    55  			// collect SPI ResourceQuota metrics (temporary)
    56  			err := fw.AsKubeAdmin.CommonController.GetResourceQuotaInfo("token-upload-rest-endpoint", namespace, "appstudio-crds-spi")
    57  			Expect(err).NotTo(HaveOccurred())
    58  		})
    59  
    60  		// Clean up after running these tests and before the next tests block: can't have multiple AccessTokens in Injected phase
    61  		AfterAll(func() {
    62  			// collect SPI ResourceQuota metrics (temporary)
    63  			err := fw.AsKubeAdmin.CommonController.GetResourceQuotaInfo("token-upload-k8s", namespace, "appstudio-crds-spi")
    64  			Expect(err).NotTo(HaveOccurred())
    65  
    66  			if !CurrentSpecReport().Failed() {
    67  				Expect(fw.AsKubeAdmin.SPIController.DeleteAllBindingTokensInASpecificNamespace(namespace)).To(Succeed())
    68  				Expect(fw.AsKubeAdmin.SPIController.DeleteAllAccessTokensInASpecificNamespace(namespace)).To(Succeed())
    69  				Expect(fw.AsKubeAdmin.SPIController.DeleteAllAccessTokenDataInASpecificNamespace(namespace)).To(Succeed())
    70  			}
    71  		})
    72  
    73  		// create a new SPITokenBinding and get the generated SPIAccessToken; we will associate the secret to it
    74  		var SPITokenBinding *v1beta1.SPIAccessTokenBinding
    75  		var SPIAccessToken *v1beta1.SPIAccessToken
    76  		var K8sSecret *v1.Secret
    77  		secretName := "access-token-binding-k8s-secret"
    78  		tokenBindingName := "spi-token-binding-k8s-"
    79  
    80  		It("creates SPITokenBinding", func() {
    81  			SPITokenBinding, err = fw.AsKubeDeveloper.SPIController.CreateSPIAccessTokenBinding(tokenBindingName, namespace, RepoURL, "", "kubernetes.io/basic-auth")
    82  			Expect(err).NotTo(HaveOccurred())
    83  
    84  			SPITokenBinding, err = fw.AsKubeDeveloper.SPIController.GetSPIAccessTokenBinding(SPITokenBinding.Name, namespace)
    85  			Expect(err).NotTo(HaveOccurred())
    86  		})
    87  
    88  		It("creates secret with access token and associate it to an existing SPIAccessToken", func() {
    89  			Eventually(func() string {
    90  				SPITokenBinding, err = fw.AsKubeDeveloper.SPIController.GetSPIAccessTokenBinding(SPITokenBinding.Name, namespace)
    91  				Expect(err).NotTo(HaveOccurred())
    92  
    93  				return SPITokenBinding.Status.LinkedAccessTokenName
    94  			}, 1*time.Minute, 5*time.Second).ShouldNot(BeEmpty(), fmt.Sprintf("SPITokenBinding %s/%s '.Status.LinkedAccessTokenName' field should not be empty", SPITokenBinding.GetNamespace(), SPITokenBinding.GetName()))
    95  
    96  			linkedAccessTokenName := SPITokenBinding.Status.LinkedAccessTokenName
    97  			tokenData := utils.GetEnv(constants.GITHUB_TOKEN_ENV, "")
    98  			Expect(tokenData).NotTo(BeEmpty())
    99  
   100  			K8sSecret, err = fw.AsKubeDeveloper.SPIController.UploadWithK8sSecret(secretName, namespace, linkedAccessTokenName, RepoURL, "", tokenData)
   101  			Expect(err).NotTo(HaveOccurred())
   102  		})
   103  
   104  		It("SPITokenBinding should be in Injected phase", func() {
   105  			Eventually(func() v1beta1.SPIAccessTokenBindingPhase {
   106  				SPITokenBinding, err = fw.AsKubeDeveloper.SPIController.GetSPIAccessTokenBinding(SPITokenBinding.Name, namespace)
   107  				Expect(err).NotTo(HaveOccurred())
   108  
   109  				return SPITokenBinding.Status.Phase
   110  			}, 2*time.Minute, 5*time.Second).Should(Equal(v1beta1.SPIAccessTokenBindingPhaseInjected), fmt.Sprintf("SPIAccessTokenBinding %s/%s is not in %s phase", SPITokenBinding.GetNamespace(), SPITokenBinding.GetName(), v1beta1.SPIAccessTokenBindingPhaseInjected))
   111  		})
   112  
   113  		It("upload secret should be automatically be removed", func() {
   114  			Eventually(func() bool {
   115  				_, err := fw.AsKubeDeveloper.CommonController.GetSecret(namespace, K8sSecret.Name)
   116  				if err == nil {
   117  					return false
   118  				}
   119  				return k8sErrors.IsNotFound(err)
   120  			}, 2*time.Minute, 10*time.Second).Should(BeTrue(), fmt.Sprintf("timed out waiting for upload secret %s/%s to be removed", K8sSecret.GetNamespace(), K8sSecret.GetName()))
   121  		})
   122  
   123  		It("SPIAccessToken exists and is in Ready phase", func() {
   124  			Eventually(func() (v1beta1.SPIAccessTokenPhase, error) {
   125  				SPIAccessToken, err = fw.AsKubeDeveloper.SPIController.GetSPIAccessToken(SPITokenBinding.Status.LinkedAccessTokenName, namespace)
   126  				if err != nil {
   127  					return "", err
   128  				}
   129  				return SPIAccessToken.Status.Phase, nil
   130  			}, 1*time.Minute, 5*time.Second).Should(Equal(v1beta1.SPIAccessTokenPhaseReady), fmt.Sprintf("SPIAccessToken for SPITokenBinding %s/%s should be in %s phase", SPITokenBinding.GetNamespace(), SPITokenBinding.GetName(), v1beta1.SPIAccessTokenPhaseReady))
   131  
   132  		})
   133  	})
   134  
   135  	Describe("SVPI-399 - Upload token with k8s secret (create new SPIAccessToken automatically)", Ordered, func() {
   136  		BeforeAll(func() {
   137  			// Initialize the tests controllers
   138  			fw, err = framework.NewFramework(utils.GetGeneratedNamespace("spi-demos"))
   139  			Expect(err).NotTo(HaveOccurred())
   140  			namespace = fw.UserNamespace
   141  			Expect(namespace).NotTo(BeEmpty())
   142  
   143  			// collect SPI ResourceQuota metrics (temporary)
   144  			err := fw.AsKubeAdmin.CommonController.GetResourceQuotaInfo("token-upload-rest-endpoint", namespace, "appstudio-crds-spi")
   145  			Expect(err).NotTo(HaveOccurred())
   146  		})
   147  
   148  		// Clean up after running these tests and before the next tests block: can't have multiple AccessTokens in Injected phase
   149  		AfterAll(func() {
   150  			// collect SPI ResourceQuota metrics (temporary)
   151  			err := fw.AsKubeAdmin.CommonController.GetResourceQuotaInfo("token-upload-k8s", namespace, "appstudio-crds-spi")
   152  			Expect(err).NotTo(HaveOccurred())
   153  
   154  			if !CurrentSpecReport().Failed() {
   155  				Expect(fw.AsKubeAdmin.SPIController.DeleteAllBindingTokensInASpecificNamespace(namespace)).To(Succeed())
   156  				Expect(fw.AsKubeAdmin.SPIController.DeleteAllAccessTokensInASpecificNamespace(namespace)).To(Succeed())
   157  				Expect(fw.AsKubeAdmin.SPIController.DeleteAllAccessTokenDataInASpecificNamespace(namespace)).To(Succeed())
   158  			}
   159  		})
   160  
   161  		// we create a secret specifying a non-existing SPIAccessToken name: it should be created automatically by SPI
   162  		var K8sSecret *v1.Secret
   163  		var SPIAccessToken *v1beta1.SPIAccessToken
   164  		secretName := "access-token-k8s-secret"
   165  		nonExistingAccessTokenName := "new-access-token-k8s"
   166  
   167  		It("creates secret with access token and associate it to an existing SPIAccessToken", func() {
   168  			tokenData := utils.GetEnv(constants.GITHUB_TOKEN_ENV, "")
   169  			Expect(tokenData).NotTo(BeEmpty())
   170  
   171  			K8sSecret, err = fw.AsKubeDeveloper.SPIController.UploadWithK8sSecret(secretName, namespace, nonExistingAccessTokenName, RepoURL, "", tokenData)
   172  			Expect(err).NotTo(HaveOccurred())
   173  		})
   174  
   175  		It("upload secret should be automatically be removed", func() {
   176  			Eventually(func() bool {
   177  				_, err := fw.AsKubeDeveloper.CommonController.GetSecret(namespace, K8sSecret.Name)
   178  				if err == nil {
   179  					return false
   180  				}
   181  				return k8sErrors.IsNotFound(err)
   182  			}, 2*time.Minute, 10*time.Second).Should(BeTrue(), fmt.Sprintf("timed out waiting for upload secret %s/%s to be removed", K8sSecret.GetNamespace(), K8sSecret.GetName()))
   183  		})
   184  
   185  		It("SPIAccessToken exists and is in Ready phase", func() {
   186  			Eventually(func() (v1beta1.SPIAccessTokenPhase, error) {
   187  				SPIAccessToken, err = fw.AsKubeDeveloper.SPIController.GetSPIAccessToken(nonExistingAccessTokenName, namespace)
   188  				if err != nil {
   189  					return "", err
   190  				}
   191  				return SPIAccessToken.Status.Phase, nil
   192  			}, 2*time.Minute, 5*time.Second).Should(Equal(v1beta1.SPIAccessTokenPhaseReady), fmt.Sprintf("SPIAccessToken for access token %s/%s should be in %s phase", namespace, nonExistingAccessTokenName, v1beta1.SPIAccessTokenPhaseReady))
   193  
   194  		})
   195  	})
   196  })