Skip to content

Commit

Permalink
Added Integration test
Browse files Browse the repository at this point in the history
Added Integration test for owned resource deletion.
  • Loading branch information
adisky committed Mar 19, 2020
1 parent b8a0f55 commit 7d97ff0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 53 deletions.
85 changes: 46 additions & 39 deletions pkg/occlient/occlient.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ type CreateArgs struct {
}

const (
failedEventCount = 5
OcUpdateTimeout = 5 * time.Minute
OcBuildTimeout = 5 * time.Minute
OpenShiftNameSpace = "openshift"
failedEventCount = 5
OcUpdateTimeout = 5 * time.Minute
OcBuildTimeout = 5 * time.Minute
OpenShiftNameSpace = "openshift"
waitForComponentDeletionTimeout = 120 * time.Second

// The length of the string to be generated for names of resources
nameLength = 5
Expand Down Expand Up @@ -2077,38 +2078,6 @@ func (c *Client) Delete(labels map[string]string, wait bool) error {
if err != nil {
errorList = append(errorList, "unable to delete deploymentconfig")
}

// watch the component to recieve deleted event.
if wait {
// check later about selector.
fmt.Println("In watch")
watcher, err := c.appsClient.DeploymentConfigs(c.Namespace).Watch(metav1.ListOptions{LabelSelector: selector})
if err != nil {
errorList = append(errorList, "unable to Watch deploymentconfig")
}
defer watcher.Stop()
eventCh := watcher.ResultChan()

Loop:
for event := range eventCh {
fmt.Println("In for loop")
_, ok := event.Object.(*appsv1.DeploymentConfig)
if !ok {
errorList = append(errorList, "Unexpected type unable to Watch deploymentconfig")
}
switch event.Type {
case watch.Deleted:
fmt.Println("in case deleted")
break Loop
case watch.Error:
errorList = append(errorList, "unable to Watch deploymentconfig")
fmt.Println("in case error")
break Loop

}

}
}
// Delete BuildConfig
glog.V(4).Info("Deleting BuildConfigs")
err = c.buildClient.BuildConfigs(c.Namespace).DeleteCollection(&metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: selector})
Expand All @@ -2122,6 +2091,16 @@ func (c *Client) Delete(labels map[string]string, wait bool) error {
errorList = append(errorList, "unable to delete imagestream")
}

// for --wait it waits for component to be deleted
// TODO: Need to modify for `odo app delete`, currently wait flag is added only in `odo component delete`
// so only one component gets passed in selector
if wait {
err = c.WaitForComponentDeletion(selector)
if err != nil {
errorList = append(errorList, err.Error())
}
}

// Error string
errString := strings.Join(errorList, ",")
if len(errString) != 0 {
Expand All @@ -2131,6 +2110,37 @@ func (c *Client) Delete(labels map[string]string, wait bool) error {

}

// WaitForComponentDeletion waits for component to be deleted
func (c *Client) WaitForComponentDeletion(selector string) error {

glog.V(4).Infof("Waiting for component to get deleted")

watcher, err := c.appsClient.DeploymentConfigs(c.Namespace).Watch(metav1.ListOptions{LabelSelector: selector})
if err != nil {
return err
}
defer watcher.Stop()
eventCh := watcher.ResultChan()

for {
select {
case event, ok := <-eventCh:
_, typeOk := event.Object.(*appsv1.DeploymentConfig)
if !ok || !typeOk {
return errors.New("Unable to watch deployment config")
}
if event.Type == watch.Deleted {
fmt.Println("deleted")
return nil
} else if event.Type == watch.Error {
return errors.New("Unable to watch deployment config")
}
case <-time.After(waitForComponentDeletionTimeout):
return errors.New("Time out waiting for component to get deleted")
}
}
}

// DeleteServiceInstance takes labels as a input and based on it, deletes respective service instance
func (c *Client) DeleteServiceInstance(labels map[string]string) error {
glog.V(4).Infof("Deleting Service Instance")
Expand Down Expand Up @@ -3196,14 +3206,11 @@ func isSubDir(baseDir, otherDir string) bool {
// OpenShift garbage collector
func generateOwnerReference(dc *appsv1.DeploymentConfig) metav1.OwnerReference {

blockOwnerDeletion := true

ownerReference := metav1.OwnerReference{
APIVersion: "apps.openshift.io/v1",
Kind: "DeploymentConfig",
Name: dc.Name,
UID: dc.UID,
BlockOwnerDeletion: &blockOwnerDeletion,
}

return ownerReference
Expand Down
6 changes: 3 additions & 3 deletions tests/helper/helper_oc.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,10 @@ func (oc *OcRunner) WaitForDCRollout(dcName string, project string, timeout time
session.Wait(timeout)
}

// CheckForExistence checks if the given resource type exists on the cluster
func (oc *OcRunner) CheckForExistence(resourceType, namespace string) {
// VerifyResourceDeleted verifies if the given resource is deleted from cluster
func (oc *OcRunner) VerifyResourceDeleted(resourceType, resourceName, namespace string) {
session := CmdRunner(oc.path, "get", resourceType, "--namespace", namespace)
Eventually(session).Should(gexec.Exit(0))
output := string(session.Wait().Out.Contents())
Expect(output).To(ContainSubstring(""))
Expect(output).NotTo(ContainSubstring(resourceName))
}
21 changes: 10 additions & 11 deletions tests/integration/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,22 +835,21 @@ func componentTests(args ...string) {
helper.CopyExample(filepath.Join("source", "nodejs"), context)
helper.CmdShouldPass("odo", append(args, "create", "nodejs", cmpName, "--app", appName, "--project", project, "--context", context)...)
helper.CmdShouldPass("odo", "url", "create", "example", "--context", context)
helper.CmdShouldPass("odo", "storage", "create", "storage-name", "--size", "1Gi", "--path", "/data", "--context", context)

// TODO: Add this When the issue #2710 gets fixed
// helper.CmdShouldPass("odo", "storage", "create", "storage-name", "--size", "1Gi", "--path", "/data", "--context", context)
helper.ValidateLocalCmpExist(context, "Type,nodejs", "Name,"+cmpName, "Application,"+appName, "URL,0,Name,example")
helper.CmdShouldPass("odo", append(args, "push", "--context", context)...)

helper.CmdShouldPass("odo", "url", "create", "example-1", "--context", context)
helper.CmdShouldPass("odo", "storage", "create", "storage-name-1", "--size", "1Gi", "--path", "/data-1", "--context", context)
helper.CmdShouldPass("odo", append(args, "push", "--context", context)...)
// delete with --wait flag
helper.CmdShouldPass("odo", append(args, "delete", "-f", "-w", "--context", context)...)

helper.CmdShouldPass("odo", append(args, "delete", "-f", "--context", context)...)
oc.VerifyResourceDeleted("routes", "example", project)
oc.VerifyResourceDeleted("service", cmpName, project)
// verify s2i pvc is delete
oc.VerifyResourceDeleted("pvc", "s2idata", project)
oc.VerifyResourceDeleted("dc", cmpName, project)

oc.CheckForExistence("routes", project)
oc.CheckForExistence("dc", project)
oc.CheckForExistence("pvc", project)
oc.CheckForExistence("bc", project)
oc.CheckForExistence("is", project)
oc.CheckForExistence("service", project)
})
})
}

0 comments on commit 7d97ff0

Please sign in to comment.