github.com/redhat-appstudio/e2e-tests@v0.0.0-20230619105049-9a422b2094d7/tests/release/release.go (about)

     1  package release
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	ecp "github.com/enterprise-contract/enterprise-contract-controller/api/v1alpha1"
     8  	"github.com/google/uuid"
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  	applicationapiv1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1"
    12  	"github.com/redhat-appstudio/e2e-tests/pkg/framework"
    13  
    14  	"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
    15  	"knative.dev/pkg/apis"
    16  )
    17  
    18  const (
    19  	DEFAULT_RELEASE_SERVICE_USER = "release-service-e2e"
    20  )
    21  
    22  var snapshotComponents = []applicationapiv1alpha1.SnapshotComponent{
    23  	{Name: "component-1", ContainerImage: "quay.io/redhat-appstudio/component1@sha256:d5e85e49c89df42b221d972f5b96c6507a8124717a6e42e83fd3caae1031d514"},
    24  	{Name: "component-2", ContainerImage: "quay.io/redhat-appstudio/component2@sha256:a01dfd18cf8ca8b68770b09a9b6af0fd7c6d1f8644c7ab97f0e06c34dfc5860e"},
    25  	{Name: "component-3", ContainerImage: "quay.io/redhat-appstudio/component3@sha256:d90a0a33e4c5a1daf5877f8dd989a570bfae4f94211a8143599245e503775b1f"},
    26  }
    27  
    28  var _ = framework.ReleaseSuiteDescribe("[HACBS-1108]test-release-service-happy-path", Label("release", "happy-path", "HACBS"), func() {
    29  	defer GinkgoRecover()
    30  
    31  	var fw *framework.Framework
    32  	var err error
    33  
    34  	var devNamespace = uuid.New().String()
    35  	var managedNamespace = uuid.New().String()
    36  	var ecPolicy ecp.EnterpriseContractPolicySpec
    37  
    38  	BeforeAll(func() {
    39  		// Initialize the tests controllers
    40  		fw, err = framework.NewFramework(DEFAULT_RELEASE_SERVICE_USER)
    41  		Expect(err).NotTo(HaveOccurred())
    42  		// Create the dev namespace
    43  		_, err := fw.AsKubeAdmin.CommonController.CreateTestNamespace(devNamespace)
    44  		Expect(err).NotTo(HaveOccurred(), "Error when creating namespace '%s': %v", devNamespace, err)
    45  		GinkgoWriter.Println("Dev Namespace :", devNamespace)
    46  
    47  		// Create the managed namespace
    48  		_, err = fw.AsKubeAdmin.CommonController.CreateTestNamespace(managedNamespace)
    49  		Expect(err).NotTo(HaveOccurred(), "Error when creating namespace '%s': %v", managedNamespace, err)
    50  
    51  		GinkgoWriter.Println("Managed Namespace :", managedNamespace)
    52  
    53  		// get the ec configmap to configure the policy and data sources
    54  		cm, err := fw.AsKubeAdmin.CommonController.GetConfigMap("ec-defaults", "enterprise-contract-service")
    55  		Expect(err).ToNot(HaveOccurred())
    56  		// the default policy source
    57  		ecPolicy = ecp.EnterpriseContractPolicySpec{
    58  			Description: "Red Hat's enterprise requirements",
    59  			Sources: []ecp.Source{
    60  				{
    61  					Name:   "ec-policies",
    62  					Policy: []string{cm.Data["ec_policy_source"]},
    63  					Data:   []string{cm.Data["ec_data_source"]},
    64  				},
    65  			},
    66  			Configuration: &ecp.EnterpriseContractPolicyConfiguration{
    67  				Exclude: []string{"tasks", "attestation_task_bundle", "java", "test", "not_useful"},
    68  			},
    69  		}
    70  	})
    71  
    72  	AfterAll(func() {
    73  		if !CurrentSpecReport().Failed() {
    74  			// Delete the dev and managed namespaces with all the resources created in them
    75  			Expect(fw.AsKubeAdmin.TektonController.DeleteAllPipelineRunsInASpecificNamespace(devNamespace)).NotTo(HaveOccurred())
    76  			Expect(fw.AsKubeAdmin.TektonController.DeleteAllPipelineRunsInASpecificNamespace(managedNamespace)).NotTo(HaveOccurred())
    77  			Expect(fw.AsKubeAdmin.CommonController.DeleteNamespace(devNamespace)).NotTo(HaveOccurred())
    78  			Expect(fw.AsKubeAdmin.CommonController.DeleteNamespace(managedNamespace)).NotTo(HaveOccurred())
    79  			Expect(fw.SandboxController.DeleteUserSignup(fw.UserName)).NotTo(BeFalse())
    80  		}
    81  	})
    82  
    83  	var _ = Describe("Creation of the 'Happy path' resources", func() {
    84  
    85  		It("creates a Snapshot in dev namespace.", func(ctx SpecContext) {
    86  			_, err := fw.AsKubeAdmin.ReleaseController.CreateSnapshot(snapshotName, devNamespace, applicationName, snapshotComponents)
    87  			Expect(err).NotTo(HaveOccurred())
    88  			// We add the namespace creation timeout as this is the first test so must also take into account the code in BeforeAll
    89  		}, SpecTimeout(snapshotCreationTimeout+namespaceCreationTimeout*2))
    90  
    91  		It("creates Release Strategy in managed namespace.", func(ctx SpecContext) {
    92  			_, err := fw.AsKubeAdmin.ReleaseController.CreateReleaseStrategy(releaseStrategyName, managedNamespace, releasePipelineName, releasePipelineBundle, releaseStrategyPolicy, serviceAccount, paramsReleaseStrategyM6)
    93  			Expect(err).NotTo(HaveOccurred())
    94  		}, SpecTimeout(releaseStrategyCreationTimeout))
    95  
    96  		It("creates ReleasePlan in dev namespace.", func(ctx SpecContext) {
    97  			_, err := fw.AsKubeAdmin.ReleaseController.CreateReleasePlan(sourceReleasePlanName, devNamespace, applicationName, managedNamespace, "")
    98  			Expect(err).NotTo(HaveOccurred())
    99  		}, SpecTimeout(releasePlanCreationTimeout))
   100  
   101  		It("creates EnterpriseContractPolicy in managed namespace.", func(ctx SpecContext) {
   102  			_, err := fw.AsKubeAdmin.TektonController.CreateEnterpriseContractPolicy(releaseStrategyPolicy, managedNamespace, ecPolicy)
   103  			Expect(err).NotTo(HaveOccurred())
   104  		}, SpecTimeout(EnterpriseContractPolicyTimeout))
   105  
   106  		It("creates ReleasePlanAdmission in managed namespace.", func(ctx SpecContext) {
   107  			_, err := fw.AsKubeAdmin.ReleaseController.CreateReleasePlanAdmission(destinationReleasePlanAdmissionName, devNamespace, applicationName, managedNamespace, "", "", releaseStrategyName)
   108  			Expect(err).NotTo(HaveOccurred())
   109  		}, SpecTimeout(releasePlanAdmissionCreationTimeout))
   110  
   111  		It("creates a Release in dev namespace.", func(ctx SpecContext) {
   112  			_, err := fw.AsKubeAdmin.ReleaseController.CreateRelease(releaseName, devNamespace, snapshotName, sourceReleasePlanName)
   113  			Expect(err).NotTo(HaveOccurred())
   114  		}, SpecTimeout(releaseCreationTimeout))
   115  	})
   116  
   117  	var _ = Describe("post-release verification.", func() {
   118  
   119  		It("makes sure a PipelineRun should have been created in the managed namespace.", func() {
   120  			Eventually(func() bool {
   121  				prList, err := fw.AsKubeAdmin.TektonController.ListAllPipelineRuns(managedNamespace)
   122  				if err != nil || prList == nil || len(prList.Items) < 1 {
   123  					GinkgoWriter.Println(err)
   124  					return false
   125  				}
   126  
   127  				return strings.Contains(prList.Items[0].Name, releaseName)
   128  			}, releasePipelineRunCreationTimeout, defaultInterval).Should(BeTrue())
   129  		})
   130  
   131  		It("makes sure the PipelineRun exists and succeeded", func() {
   132  			Eventually(func() bool {
   133  				prList, err := fw.AsKubeAdmin.TektonController.ListAllPipelineRuns(managedNamespace)
   134  				if prList == nil || err != nil || len(prList.Items) < 1 {
   135  					GinkgoWriter.Println(err)
   136  					return false
   137  				}
   138  
   139  				return prList.Items[0].HasStarted() && prList.Items[0].IsDone() && prList.Items[0].Status.GetCondition(apis.ConditionSucceeded).IsTrue()
   140  			}, releasePipelineRunCompletionTimeout, defaultInterval).Should(BeTrue())
   141  		})
   142  
   143  		It("makes sure that the Release should have succeeded.", func() {
   144  			Eventually(func() bool {
   145  				release, err := fw.AsKubeAdmin.ReleaseController.GetRelease(releaseName, "", devNamespace)
   146  				if err != nil || release == nil {
   147  					return false
   148  				}
   149  
   150  				return release.IsReleased()
   151  			}, releaseCreationTimeout, defaultInterval).Should(BeTrue())
   152  		})
   153  
   154  		It("makes sure the Release references the release PipelineRun.", func(ctx SpecContext) {
   155  			var pipelineRunList *v1beta1.PipelineRunList
   156  
   157  			Eventually(func() bool {
   158  				pipelineRunList, err = fw.AsKubeAdmin.TektonController.ListAllPipelineRuns(managedNamespace)
   159  				if err != nil || pipelineRunList == nil {
   160  					return false
   161  				}
   162  
   163  				return len(pipelineRunList.Items) > 0 && err == nil
   164  			}, avgControllerQueryTimeout, defaultInterval).Should(BeTrue())
   165  
   166  			release, err := fw.AsKubeAdmin.ReleaseController.GetRelease(releaseName, "", devNamespace)
   167  			if err != nil {
   168  				GinkgoWriter.Println(err)
   169  			}
   170  			Expect(release.Status.Processing.PipelineRun == (fmt.Sprintf("%s/%s", pipelineRunList.Items[0].Namespace, pipelineRunList.Items[0].Name))).Should(BeTrue())
   171  			// We add the namespace deletion timeout as this is the last test so must also take into account the code in AfterAll
   172  		}, SpecTimeout(avgControllerQueryTimeout*2+namespaceDeletionTimeout*2))
   173  	})
   174  })