Skip to content

Commit

Permalink
OCM-6318 | ci: Build up deprovision step
Browse files Browse the repository at this point in the history
  • Loading branch information
jameszwang committed May 11, 2024
1 parent 1a4be1a commit 31d6b85
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 46 deletions.
2 changes: 1 addition & 1 deletion tests/e2e/e2e_tear_down_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var _ = Describe("ROSA CLI Test", func() {
func() {
client := rosacli.NewClient()
profile := ph.LoadProfileYamlFileByENV()
var errs = ph.DestroyClusterByProfile(profile, client, true)
var errs = ph.DestroyResourceByProfile(profile, client)
Expect(len(errs)).To(Equal(0))
})
})
16 changes: 0 additions & 16 deletions tests/utils/exec/rosacli/report_portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,7 @@ func GenerateReportXMLFile() (int, int, map[string][]Testcase, map[string][]Test
passedNum := 0
issuedNum := 0

<<<<<<< HEAD
xmlFileList := ListFiles(config.Test.ArtifactDir, ".xml")
=======
xmlFileList := ListFiles(config.Test.OutputDir, ".xml")
>>>>>>> d221817a (OCM-6318 | ci: Build up deprovision step)
for _, xmlFile := range xmlFileList {
xmlFilename := path.Base(xmlFile)
xmlFilePrefix := strings.TrimSuffix(xmlFilename, ".xml")
Expand Down Expand Up @@ -187,30 +183,18 @@ func GenerateReportXMLFile() (int, int, map[string][]Testcase, map[string][]Test
return passedNum, issuedNum, issuedTCList, successedTCList
}

<<<<<<< HEAD
func ListFiles(dir string, subfix string) []string {
var Files []string
fs, err := os.ReadDir(dir)
if err != nil {
panic(fmt.Sprintf("Failed to open the directory %s: %v\n", dir, err))
=======
func ListFiles(outputDir string, subfix string) []string {
var Files []string
fs, err := os.ReadDir(outputDir)
if err != nil {
panic(fmt.Sprintf("Failed to open the directory %s: %v\n", outputDir, err))
>>>>>>> d221817a (OCM-6318 | ci: Build up deprovision step)
}
for _, f := range fs {
if path.Ext(f.Name()) != subfix {
continue
}

<<<<<<< HEAD
filename := path.Join(dir, f.Name())
=======
filename := path.Join(outputDir, f.Name())
>>>>>>> d221817a (OCM-6318 | ci: Build up deprovision step)
Files = append(Files, filename)
}

Expand Down
85 changes: 56 additions & 29 deletions tests/utils/profilehandler/profile_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,21 +709,17 @@ func WaitForClusterUninstalled(client *rosacli.Client, cluster string, timeoutMi
return fmt.Errorf("timeout for waiting for cluster deletion finished after %d mins", timeoutMin)
}

func DestroyClusterByProfile(profile *Profile, client *rosacli.Client, waitForClusterUninstall bool) (errors []error) {
func DestroyCluster(client *rosacli.Client) (*ClusterDetail, []error) {
var (
ud *UserData
cd *ClusterDetail
clusterService rosacli.ClusterService
ocmResourceService rosacli.OCMResourceService
cd *ClusterDetail
clusterService rosacli.ClusterService
errors []error
)
region := profile.Region
ocmResourceService = client.OCMResource

// get cluster info from cluster detail file
cd, err := ParserClusterDetail()
if err != nil {
errors = append(errors, err)
return
return nil, errors
}

// delete cluster
Expand All @@ -736,29 +732,37 @@ func DestroyClusterByProfile(profile *Profile, client *rosacli.Client, waitForCl
} else {
log.Logger.Errorf("Error happened when delete cluster: %s", output.String())
errors = append(errors, errDeleteCluster)
return
return nil, errors
}
} else {
if waitForClusterUninstall {
log.Logger.Infof("Waiting for the cluster %s to be uninstalled", cd.ClusterID)

err = WaitForClusterUninstalled(client, cd.ClusterID, config.Test.GlobalENV.ClusterWaitingTime)
if err != nil {
log.Logger.Errorf("Error happened when waiting cluster uninstall: %s", err.Error())
errors = append(errors, err)
return
} else {
log.Logger.Infof("Delete cluster %s successfully.", cd.ClusterID)
}
log.Logger.Infof("Waiting for the cluster %s to be uninstalled", cd.ClusterID)

err = WaitForClusterUninstalled(client, cd.ClusterID, config.Test.GlobalENV.ClusterWaitingTime)
if err != nil {
log.Logger.Errorf("Error happened when waiting cluster uninstall: %s", err.Error())
errors = append(errors, err)
return nil, errors
} else {
log.Logger.Infof("Delete cluster %s successfully.", cd.ClusterID)
}
}
}
return cd, errors
}

func DestroyPreparedUserData(client *rosacli.Client, clusterID string, region string, isSTS bool) []error {
var (
ud *UserData
ocmResourceService rosacli.OCMResourceService
errors []error
)
ocmResourceService = client.OCMResource

// get user data from resource file
ud, err = ParseUserData()
ud, err := ParseUserData()
if err != nil {
errors = append(errors, err)
return
return errors
}
defer func() {
log.Logger.Info("Rewrite User data file")
Expand Down Expand Up @@ -788,7 +792,7 @@ func DestroyClusterByProfile(profile *Profile, client *rosacli.Client, waitForCl
errors = append(errors, err)
} else {
ud.EtcdKMSKey = ""
log.Logger.Infof("Schedule etcd kms key deletion successfully for cluster: %s", cd.ClusterID)
log.Logger.Infof("Schedule etcd kms key deletion successfully for cluster: %s", clusterID)
}
}
// delete audit log arn
Expand All @@ -801,7 +805,7 @@ func DestroyClusterByProfile(profile *Profile, client *rosacli.Client, waitForCl
errors = append(errors, err)
} else {
ud.AuditLogArn = ""
log.Logger.Infof("Delete audit log arn successfully for cluster: %s", cd.ClusterID)
log.Logger.Infof("Delete audit log arn successfully for cluster: %s", clusterID)
}
}
// delete vpc chain
Expand All @@ -813,7 +817,7 @@ func DestroyClusterByProfile(profile *Profile, client *rosacli.Client, waitForCl
errors = append(errors, err)
} else {
ud.VpcID = ""
log.Logger.Infof("Delete vpc chain successfully for cluster: %s", cd.ClusterID)
log.Logger.Infof("Delete vpc chain successfully for cluster: %s", clusterID)
}
}
// delete operator roles
Expand Down Expand Up @@ -841,8 +845,8 @@ func DestroyClusterByProfile(profile *Profile, client *rosacli.Client, waitForCl
}
}
// delete oidc provider of sts cluster
if cd != nil && cd.ClusterID != "" && profile.ClusterConfig.STS && ud.OIDCConfigID == "" {
_, err = ocmResourceService.DeleteOIDCProvider("-c", cd.ClusterID, "-y", "--mode", "auto")
if clusterID != "" && isSTS && ud.OIDCConfigID == "" {
_, err = ocmResourceService.DeleteOIDCProvider("-c", clusterID, "-y", "--mode", "auto")
if err != nil {
log.Logger.Errorf("Error happened when delete oidc provider: %s", err.Error())
errors = append(errors, err)
Expand All @@ -863,5 +867,28 @@ func DestroyClusterByProfile(profile *Profile, client *rosacli.Client, waitForCl
}
}
}
return
return errors
}

func DestroyResourceByProfile(profile *Profile, client *rosacli.Client) (errors [][]error) {
// destroy cluster
cd, errDestroyCluster := DestroyCluster(client)
if len(errDestroyCluster) > 0 {
errors = append(errors, errDestroyCluster)
return errors
}

// destroy prepared user data
clusterId := ""
if cd != nil {
clusterId = cd.ClusterID
}
region := profile.Region
isSTS := profile.ClusterConfig.STS
errDestroyUserData := DestroyPreparedUserData(client, clusterId, region, isSTS)
if len(errDestroyUserData) > 0 {
errors = append(errors, errDestroyUserData)
}

return errors
}

0 comments on commit 31d6b85

Please sign in to comment.