Skip to content

Commit

Permalink
Automate Qase 2,6, 305, 306, 109, 155 (#245)
Browse files Browse the repository at this point in the history
* Automate Qase 2,6, 305, 109, 155

Signed-off-by: Parthvi Vala <parthvi.vala@suse.com>

* Add focus

Signed-off-by: Parthvi Vala <parthvi.vala@suse.com>

* Add env to workflow

Signed-off-by: Parthvi Vala <parthvi.vala@suse.com>

* Remove focus

Signed-off-by: Parthvi Vala <parthvi.vala@suse.com>

* Automate Qase 306

Signed-off-by: Parthvi Vala <parthvi.vala@suse.com>

* Add focus

Signed-off-by: Parthvi Vala <parthvi.vala@suse.com>

* Remove focus

Signed-off-by: Parthvi Vala <parthvi.vala@suse.com>

* Review changes: make invalid and expired cred checks in a common function

Signed-off-by: Parthvi Vala <parthvi.vala@suse.com>

* Add focus and add indentation imported tests

Signed-off-by: Parthvi Vala <parthvi.vala@suse.com>

* Remove focus

Signed-off-by: Parthvi Vala <parthvi.vala@suse.com>

---------

Signed-off-by: Parthvi Vala <parthvi.vala@suse.com>
  • Loading branch information
valaparthvi authored Jan 28, 2025
1 parent 90dafc7 commit 8c78a5e
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 65 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ env:
GCP_RUNNER_ZONE: asia-south2-c
DOWNSTREAM_CLUSTER_CLEANUP: ${{ inputs.downstream_cluster_cleanup }}
QASE_HELPER: ${{ github.workspace }}/hosted/helpers/qase/helper_qase.go
SECONDARY_GCP_CREDENTIALS: ${{ secrets.SECONDARY_GOOGLE_APPLICATION_CREDENTIALS }}
jobs:
create-runner:
runs-on: ubuntu-latest
Expand Down
5 changes: 5 additions & 0 deletions hosted/eks/p1/p1_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ var _ = Describe("P1Import", func() {
Expect(cluster.EKSStatus.UpstreamSpec.NodeGroups).To(HaveLen(nodepoolcount + 1))
})

It("Update the cloud creds", func() {
testCaseID = 155
updateCloudCredentialsCheck(cluster, ctx.RancherAdminClient)
})

Context("Reimporting/Editing a cluster with invalid config", func() {
It("Reimport a cluster to Rancher should fail", func() {
testCaseID = 101
Expand Down
5 changes: 5 additions & 0 deletions hosted/eks/p1/p1_provisioning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,5 +310,10 @@ var _ = Describe("P1Provisioning", func() {
testCaseID = 131
updateTagsAndLabels(cluster, ctx.RancherAdminClient)
})

It("Update the cloud creds", func() {
testCaseID = 109
updateCloudCredentialsCheck(cluster, ctx.RancherAdminClient)
})
})
})
20 changes: 20 additions & 0 deletions hosted/eks/p1/p1_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,3 +679,23 @@ func updateLoggingCheck(cluster *management.Cluster, client *rancher.Client) {
Expect(err).To(BeNil())
})
}

// Automates Qase: 109 and 155
func updateCloudCredentialsCheck(cluster *management.Cluster, client *rancher.Client) {
newCCID, err := helpers.CreateCloudCredentials(client)
Expect(err).To(BeNil())
updateFunc := func(cluster *management.Cluster) {
cluster.EKSConfig.AmazonCredentialSecret = newCCID
}
cluster, err = helper.UpdateCluster(cluster, client, updateFunc)
Expect(err).To(BeNil())
Expect(cluster.EKSConfig.AmazonCredentialSecret).To(Equal(newCCID))
Eventually(func() bool {
cluster, err = client.Management.Cluster.ByID(cluster.ID)
Expect(err).NotTo(HaveOccurred())
return cluster.EKSStatus.UpstreamSpec.AmazonCredentialSecret == newCCID
}, "5m", "5s").Should(BeTrue(), "Failed while upstream cloud credentials update")

cluster, err = helper.ScaleNodeGroup(cluster, client, 3, true, true)
Expect(err).To(BeNil())
}
18 changes: 18 additions & 0 deletions hosted/gke/helper/helper_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,24 @@ func DeleteGKEClusterOnGCloud(zone, project, clusterName string) error {
return nil
}

// EnableDisableServiceAccountOnGCloud can enable/disable a service account via gcloud cli
func EnableDisableServiceAccountOnGCloud(clientID, project, op string) error {
if !(op == "enable" || op == "disable") {
return fmt.Errorf("unknown operation: %s", op)
}
fmt.Printf("%s service account on GKE cluster...\n", op)
var args = []string{"iam", "service-accounts", op, fmt.Sprintf("%s@%s.iam.gserviceaccount.com", clientID, project), "--project", project}
fmt.Printf("Running command: gcloud %v\n", args)
out, err := proc.RunW("gcloud", args...)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed to %s service-account: %s", op, out))
}

fmt.Printf("%sd service account on GKE cluster...\n", op)
return nil

}

// <==============================================================================GCLOUD CLI (end)==============================>

// GetK8sVersion returns the k8s version to be used by the test;
Expand Down
144 changes: 79 additions & 65 deletions hosted/gke/p1/p1_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,89 +33,103 @@ var _ = Describe("P1Import", func() {
}
})

When("a cluster is created and imported", func() {

When("a cluster is created on cloud console", func() {
BeforeEach(func() {
var err error
err = helper.CreateGKEClusterOnGCloud(zone, clusterName, project, k8sVersion)
err := helper.CreateGKEClusterOnGCloud(zone, clusterName, project, k8sVersion)
Expect(err).To(BeNil())
})

cluster, err = helper.ImportGKEHostedCluster(ctx.RancherAdminClient, clusterName, ctx.CloudCredID, zone, project)
Expect(err).To(BeNil())
cluster, err = helpers.WaitUntilClusterIsReady(cluster, ctx.RancherAdminClient)
Expect(err).To(BeNil())
It("User should not be able to import a cluster using an expired GKE creds", func() {
testCaseID = 305
expiredCredCheck(cluster, ctx.RancherAdminClient)
})

It("should fail to reimport an imported cluster", func() {
testCaseID = 49
_, err := helper.ImportGKEHostedCluster(ctx.RancherAdminClient, clusterName, ctx.CloudCredID, zone, project)
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("cluster already exists for GKE cluster [%s] in zone [%s]", clusterName, zone)))
It("User should not be able to import cluster with invalid GKE creds in Rancher", func() {
testCaseID = 306
invalidCredCheck(cluster, ctx.RancherAdminClient)
})

It("should be able to update mutable parameter", func() {
testCaseID = 52
By("disabling the services", func() {
updateLoggingAndMonitoringServiceCheck(cluster, ctx.RancherAdminClient, "none", "none")
})
By("enabling the services", func() {
updateLoggingAndMonitoringServiceCheck(cluster, ctx.RancherAdminClient, "monitoring.googleapis.com/kubernetes", "logging.googleapis.com/kubernetes")
When("the cluster is imported", func() {

BeforeEach(func() {
var err error
cluster, err = helper.ImportGKEHostedCluster(ctx.RancherAdminClient, clusterName, ctx.CloudCredID, zone, project)
Expect(err).To(BeNil())
cluster, err = helpers.WaitUntilClusterIsReady(cluster, ctx.RancherAdminClient)
Expect(err).To(BeNil())
})
})

It("should be able to update autoscaling", func() {
testCaseID = 53
By("enabling autoscaling", func() {
updateAutoScaling(cluster, ctx.RancherAdminClient, true)
It("should fail to reimport an imported cluster", func() {
testCaseID = 49
_, err := helper.ImportGKEHostedCluster(ctx.RancherAdminClient, clusterName, ctx.CloudCredID, zone, project)
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("cluster already exists for GKE cluster [%s] in zone [%s]", clusterName, zone)))
})
By("disabling autoscalling", func() {
updateAutoScaling(cluster, ctx.RancherAdminClient, false)

It("should be able to update mutable parameter", func() {
testCaseID = 52
By("disabling the services", func() {
updateLoggingAndMonitoringServiceCheck(cluster, ctx.RancherAdminClient, "none", "none")
})
By("enabling the services", func() {
updateLoggingAndMonitoringServiceCheck(cluster, ctx.RancherAdminClient, "monitoring.googleapis.com/kubernetes", "logging.googleapis.com/kubernetes")
})
})
})

It("should be able to reimport a deleted cluster", func() {
testCaseID = 57
err := helper.DeleteGKEHostCluster(cluster, ctx.RancherAdminClient)
Expect(err).To(BeNil())
clusterID := cluster.ID
Eventually(func() error {
_, err := ctx.RancherAdminClient.Management.Cluster.ByID(clusterID)
return err
}, "10s", "1s").ShouldNot(BeNil())
cluster, err = helper.ImportGKEHostedCluster(ctx.RancherAdminClient, clusterName, ctx.CloudCredID, zone, project)
Expect(err).To(BeNil())
cluster, err = helpers.WaitUntilClusterIsReady(cluster, ctx.RancherAdminClient)
Expect(err).To(BeNil())
})
It("should be able to update autoscaling", func() {
testCaseID = 53
By("enabling autoscaling", func() {
updateAutoScaling(cluster, ctx.RancherAdminClient, true)
})
By("disabling autoscalling", func() {
updateAutoScaling(cluster, ctx.RancherAdminClient, false)
})
})

It("should successfully add a windows nodepool", func() {
testCaseID = 54
var err error
_, err = helper.AddNodePool(cluster, ctx.RancherAdminClient, 1, "WINDOWS_LTSC_CONTAINERD", true, true)
Expect(err).To(BeNil())
})
It("should be able to reimport a deleted cluster", func() {
testCaseID = 57
err := helper.DeleteGKEHostCluster(cluster, ctx.RancherAdminClient)
Expect(err).To(BeNil())
clusterID := cluster.ID
Eventually(func() error {
_, err := ctx.RancherAdminClient.Management.Cluster.ByID(clusterID)
return err
}, "10s", "1s").ShouldNot(BeNil())
cluster, err = helper.ImportGKEHostedCluster(ctx.RancherAdminClient, clusterName, ctx.CloudCredID, zone, project)
Expect(err).To(BeNil())
cluster, err = helpers.WaitUntilClusterIsReady(cluster, ctx.RancherAdminClient)
Expect(err).To(BeNil())
})

It("updating a cluster to all windows nodepool should fail", func() {
testCaseID = 264
_, err := helper.UpdateCluster(cluster, ctx.RancherAdminClient, func(upgradedCluster *management.Cluster) {
updateNodePoolsList := cluster.GKEConfig.NodePools
for i := 0; i < len(updateNodePoolsList); i++ {
updateNodePoolsList[i].Config.ImageType = "WINDOWS_LTSC_CONTAINERD"
}
It("should successfully add a windows nodepool", func() {
testCaseID = 54
var err error
_, err = helper.AddNodePool(cluster, ctx.RancherAdminClient, 1, "WINDOWS_LTSC_CONTAINERD", true, true)
Expect(err).To(BeNil())
})

upgradedCluster.GKEConfig.NodePools = updateNodePoolsList
It("updating a cluster to all windows nodepool should fail", func() {
testCaseID = 264
_, err := helper.UpdateCluster(cluster, ctx.RancherAdminClient, func(upgradedCluster *management.Cluster) {
updateNodePoolsList := cluster.GKEConfig.NodePools
for i := 0; i < len(updateNodePoolsList); i++ {
updateNodePoolsList[i].Config.ImageType = "WINDOWS_LTSC_CONTAINERD"
}

upgradedCluster.GKEConfig.NodePools = updateNodePoolsList
})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("at least 1 Linux node pool is required"))
})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("at least 1 Linux node pool is required"))
})

It("should be able to update combination mutable parameter", func() {
testCaseID = 56
combinationMutableParameterUpdate(cluster, ctx.RancherAdminClient)
})
It("should be able to update combination mutable parameter", func() {
testCaseID = 56
combinationMutableParameterUpdate(cluster, ctx.RancherAdminClient)
})

It("should successfully update with new cloud credentials", func() {
updateCloudCredentialsCheck(cluster, ctx.RancherAdminClient)
It("should successfully update with new cloud credentials", func() {
updateCloudCredentialsCheck(cluster, ctx.RancherAdminClient)
})
})
})

Expand Down
10 changes: 10 additions & 0 deletions hosted/gke/p1/p1_provisioning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ var _ = Describe("P1Provisioning", func() {
Expect(err.Error()).To(ContainSubstring("InvalidFormat"))
})

It("User should not be able to add cluster with invalid GKE creds in Rancher", func() {
testCaseID = 2
invalidCredCheck(cluster, ctx.RancherAdminClient)
})

It("User should not be able to add a cluster using an expired GKE creds", func() {
testCaseID = 6
expiredCredCheck(cluster, ctx.RancherAdminClient)
})

It("should fail to provision a cluster with invalid nodepool name", func() {
testCaseID = 37

Expand Down
63 changes: 63 additions & 0 deletions hosted/gke/p1/p1_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package p1_test

import (
"fmt"
"os"
"strings"
"testing"
"time"
Expand All @@ -26,6 +27,8 @@ import (
. "github.com/rancher-sandbox/qase-ginkgo"
"github.com/rancher/shepherd/clients/rancher"
management "github.com/rancher/shepherd/clients/rancher/generated/management/v3"
"github.com/rancher/shepherd/extensions/cloudcredentials"
"github.com/rancher/shepherd/extensions/cloudcredentials/google"
"github.com/rancher/shepherd/extensions/clusters"
namegen "github.com/rancher/shepherd/pkg/namegenerator"
"k8s.io/utils/pointer"
Expand Down Expand Up @@ -364,3 +367,63 @@ func upgradeK8sVersionChecks(cluster *management.Cluster, client *rancher.Client
Expect(err).To(BeNil())
})
}

// Automates Qase 2 and 306
func invalidCredCheck(cluster *management.Cluster, client *rancher.Client) {
By("creating invalid creds")
cloudCredentialConfig := cloudcredentials.CloudCredential{GoogleCredentialConfig: &cloudcredentials.GoogleCredentialConfig{AuthEncodedJSON: "{\"invalid-key\":\"invalid-value\"}"}}
cloudCredential, err := google.CreateGoogleCloudCredentials(client, cloudCredentialConfig)
Expect(err).To(BeNil())
cloudCredentialID := fmt.Sprintf("%s:%s", cloudCredential.Namespace, cloudCredential.Name)

if helpers.IsImport {
By("importing the cluster")
cluster, err = helper.ImportGKEHostedCluster(client, clusterName, cloudCredentialID, zone, project)
Expect(err).To(BeNil())
} else {
By("provisioning the cluster")
cluster, err = helper.CreateGKEHostedCluster(client, clusterName, cloudCredentialID, k8sVersion, zone, "", project, nil)
Expect(err).To(BeNil())
}

Eventually(func() bool {
cluster, err = client.Management.Cluster.ByID(cluster.ID)
Expect(err).To(BeNil())
return cluster.Transitioning == "error"
}, "2m", "3s").Should(BeTrue())
}

// Automates Qase 6 and 305
func expiredCredCheck(cluster *management.Cluster, client *rancher.Client) {
By("adding the creds")
cloudCredentialConfig := cloudcredentials.CloudCredential{GoogleCredentialConfig: &cloudcredentials.GoogleCredentialConfig{AuthEncodedJSON: os.Getenv("SECONDARY_GCP_CREDENTIALS")}}
cloudCredential, err := google.CreateGoogleCloudCredentials(client, cloudCredentialConfig)
Expect(err).To(BeNil())
cloudCredentialID := fmt.Sprintf("%s:%s", cloudCredential.Namespace, cloudCredential.Name)

By("disabling the service account")
const clientID = "hosted-providers-ci-creds-test"
err = helper.EnableDisableServiceAccountOnGCloud(clientID, project, "disable")
Expect(err).To(BeNil())
defer func() {
By("cleanup: enabling the service account")
err = helper.EnableDisableServiceAccountOnGCloud(clientID, project, "enable")
Expect(err).To(BeNil())
}()

if helpers.IsImport {
By("importing the cluster")
cluster, err = helper.ImportGKEHostedCluster(client, clusterName, cloudCredentialID, zone, project)
Expect(err).To(BeNil())
} else {
By("provisioning the cluster")
cluster, err = helper.CreateGKEHostedCluster(client, clusterName, cloudCredentialID, k8sVersion, zone, "", project, nil)
Expect(err).To(BeNil())
}

Eventually(func() bool {
cluster, err = client.Management.Cluster.ByID(cluster.ID)
Expect(err).To(BeNil())
return cluster.Transitioning == "error" && strings.Contains(cluster.TransitioningMessage, "cannot fetch token")
}, "2m", "3s").Should(BeTrue())
}

0 comments on commit 8c78a5e

Please sign in to comment.