github.com/redhat-appstudio/e2e-tests@v0.0.0-20230619105049-9a422b2094d7/tests/spi/token-upload-k8s.go (about)

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