diff --git a/tests/examples/source/devfiles/nodejs/devfile-deploy-with-multiple-resources.yaml b/tests/examples/source/devfiles/nodejs/devfile-deploy-with-multiple-resources.yaml new file mode 100644 index 0000000..870a49e --- /dev/null +++ b/tests/examples/source/devfiles/nodejs/devfile-deploy-with-multiple-resources.yaml @@ -0,0 +1,136 @@ +commands: + - exec: + commandLine: npm install + component: runtime + group: + isDefault: true + kind: build + workingDir: /project + id: install + - exec: + commandLine: npm start + component: runtime + group: + isDefault: true + kind: run + workingDir: /project + id: run + - exec: + commandLine: npm run debug + component: runtime + group: + isDefault: true + kind: debug + workingDir: /project + id: debug + - exec: + commandLine: npm test + component: runtime + group: + isDefault: true + kind: test + workingDir: /project + id: test + - id: build-image + apply: + component: outerloop-build + - id: deployk8s + apply: + component: outerloop-deploy + - id: deployservice + apply: + component: outerloop-deploy-2 + - id: deploy + composite: + commands: + - build-image + - deployk8s + - deployservice + group: + kind: deploy + isDefault: true +components: + - container: + endpoints: + - name: http-3000 + targetPort: 3000 + image: registry.access.redhat.com/ubi8/nodejs-14:latest + memoryLimit: 1024Mi + mountSources: true + sourceMapping: /project + name: runtime + - name: outerloop-build + image: + imageName: "{{CONTAINER_IMAGE}}" + dockerfile: + uri: ./Dockerfile + buildContext: ${PROJECTS_ROOT} + rootRequired: false + + - name: outerloop-deploy + kubernetes: + inlined: | + kind: Deployment + apiVersion: apps/v1 + metadata: + name: my-component + spec: + replicas: 1 + selector: + matchLabels: + app: node-app + template: + metadata: + labels: + app: node-app + spec: + containers: + - name: main + image: {{CONTAINER_IMAGE}} + resources: + limits: + memory: "128Mi" + cpu: "500m" + - name: outerloop-deploy-2 + kubernetes: + inlined: | + apiVersion: v1 + kind: Service + metadata: + creationTimestamp: null + labels: + app: my-cs + name: my-cs + spec: + ports: + - name: 5678-8080 + port: 5678 + protocol: TCP + targetPort: 8080 + selector: + app: my-cs + type: ClusterIP + status: + loadBalancer: {} + + +metadata: + description: Stack with Node.js 14 + displayName: Node.js Runtime + icon: https://nodejs.org/static/images/logos/nodejs-new-pantone-black.svg + language: javascript + name: mynodejs + projectType: nodejs + tags: + - NodeJS + - Express + - ubi8 + version: 1.0.1 +schemaVersion: 2.2.0 +starterProjects: + - git: + remotes: + origin: https://github.com/odo-devfiles/nodejs-ex.git + name: nodejs-starter +variables: + CONTAINER_IMAGE: quay.io/unknown-account/myimage diff --git a/tests/odo/test_create_cmd.py b/tests/odo/test_create_cmd.py index 4bf957d..c9c531d 100644 --- a/tests/odo/test_create_cmd.py +++ b/tests/odo/test_create_cmd.py @@ -60,10 +60,16 @@ def test_create_component_with_project_flag(self): os.chdir(tmp_workspace) component_namespace = random_string() subprocess.run(["odo", "create", "java-openliberty", "--project", component_namespace]) - time.sleep(5) - envfile_path = os.path.abspath(os.path.join(tmp_workspace, '.odo/env/env.yaml')) + time_out = 30 + time_counter = 0 + while not os.path.exists(envfile_path): + time.sleep(1) + time_counter += 1 + if time_counter > time_out: + break + if os.path.isfile(envfile_path): assert query_yaml(envfile_path, "ComponentSettings", "Project", -1) == component_namespace else: diff --git a/tests/odo_300/test_delete_cmd.py b/tests/odo_300/test_delete_cmd.py new file mode 100644 index 0000000..7a9b4e0 --- /dev/null +++ b/tests/odo_300/test_delete_cmd.py @@ -0,0 +1,188 @@ +import sys +import tempfile +from utils.config import * +from utils.util import * + +@pytest.mark.usefixtures("use_test_registry_v300") +class TestDeleteCmd: + + CONTEXT = "test-context" + COMPONENT = "acomponent" + PROJECT = "intg-test-project" + + tmp_project_name = None + + @classmethod + def setup_class(cls): + # Runs once per class + cls.tmp_project_name = create_test_project() + + @classmethod + def teardown_class(cls): + '''Runs at end of class''' + subprocess.run(["odo", "project", "delete", cls.tmp_project_name, "-f", "-w"]) + + def test_delete(self): + print("Test case : the component is deployed in DEV mode and it is deleted using its name and namespace") + + with tempfile.TemporaryDirectory() as tmp_workspace: + os.chdir(tmp_workspace) + + # get test devfile path + source_devfile_path = get_source_devfile_path("nodejs/devfile-deploy-with-multiple-resources.yaml") + copy_example("nodejs/project", tmp_workspace, self.CONTEXT) + + os.environ['PODMAN_CMD'] = "echo" + os.chdir(self.CONTEXT) + + result = subprocess.run(["odo", "init", "--name", self.COMPONENT, "--devfile-path", source_devfile_path], + capture_output=True, text=True, check=True) + + assert contains(result.stdout, "Your new component '{}' is ready in the current directory.".format(self.COMPONENT)) + + cmd_odo_dev = str('odo dev --random-ports') + + try: + # starting dev mode with random ports should work + cmd_proc = subprocess.Popen(cmd_odo_dev, shell=True, bufsize=-1) + # sleep until odo dev is started + time.sleep(3) + result = subprocess.run(["minikube", "kubectl", "--", "get", "deployment", "-n", self.PROJECT], + capture_output=True, text=True, check=True) + + list_expected = [ + self.COMPONENT + "-app", + "NAME", + "AVAILABLE", + ] + assert match_all(result.stdout, list_expected) + + result = subprocess.run(["odo", "delete", "component", "--name", self.COMPONENT, "--namespace", self.PROJECT, "-f"], + capture_output=True, text=True, check=True) + str_deleted = "The component \"{}\" is successfully deleted from namespace \"{}\"".format(self.COMPONENT, self.PROJECT) + + assert contains(result.stdout, str_deleted) + cmd_proc.terminate() + + except Exception as e: + raise e + + def test_delete_from_other_directory(self): + print("Test case : the component is deployed in DEV mode and it is deleted using its name and namespace from another directory") + + with tempfile.TemporaryDirectory() as tmp_workspace: + os.chdir(tmp_workspace) + + # get test devfile path + source_devfile_path = get_source_devfile_path("nodejs/devfile-deploy-with-multiple-resources.yaml") + copy_example("nodejs/project", tmp_workspace, self.CONTEXT) + + os.environ['PODMAN_CMD'] = "echo" + os.chdir(self.CONTEXT) + + # should work without --starter flag + result = subprocess.run(["odo", "init", "--name", self.COMPONENT, "--devfile-path", source_devfile_path], + capture_output=True, text=True, check=True) + + assert contains(result.stdout, "Your new component '{}' is ready in the current directory.".format(self.COMPONENT)) + + cmd_odo_dev = str('odo dev --random-ports') + + try: + # starting dev mode with random ports should work + cmd_proc = subprocess.Popen(cmd_odo_dev, shell=True, bufsize=-1) + # sleep while odo dev is started + time.sleep(3) + result = subprocess.run(["minikube", "kubectl", "--", "get", "deployment", "-n", self.PROJECT], + capture_output=True, text=True, check=True) + + list_expected = [ + self.COMPONENT + "-app", + "NAME", + "AVAILABLE", + ] + assert match_all(result.stdout, list_expected) + + os.chdir(tmp_workspace) + os.mkdir("test-dir") + os.chdir("test-dir") + result = subprocess.run(["odo", "delete", "component", "--name", self.COMPONENT, "--namespace", self.PROJECT, "-f"], + capture_output=True, text=True, check=True) + str_deleted = "The component \"{}\" is successfully deleted from namespace \"{}\"".format(self.COMPONENT, self.PROJECT) + + assert contains(result.stdout, str_deleted) + + result = subprocess.run(["minikube", "kubectl", "--", "get", "deployment", "-n", self.PROJECT], + capture_output=True, text=True, check=True) + + assert not contains(result.stdout, self.COMPONENT + "-app") + assert contains(result.stderr, "No resources found in " + self.PROJECT + " namespace.") + + # run delete command again after deletion + result = subprocess.run( + ["odo", "delete", "component", "--name", self.COMPONENT, "--namespace", self.PROJECT, "-f"], + capture_output=True, text=True, check=True) + str_no_resource_found = "No resource found for component \"{}\" in namespace \"{}\"".format(self.COMPONENT, self.PROJECT) + assert contains(result.stdout, str_no_resource_found) + + cmd_proc.terminate() + except Exception as e: + raise e + + def test_delete_with_devfile_present(self): + print("Test case : the component is deployed in DEV mode and it is deleted when devfile present") + + with tempfile.TemporaryDirectory() as tmp_workspace: + os.chdir(tmp_workspace) + + # get test devfile path + source_devfile_path = get_source_devfile_path("nodejs/devfile-deploy-with-multiple-resources.yaml") + copy_example("nodejs/project", tmp_workspace, self.CONTEXT) + + os.environ['PODMAN_CMD'] = "echo" + os.chdir(self.CONTEXT) + + # should work without --starter flag + result = subprocess.run(["odo", "init", "--name", self.COMPONENT, "--devfile-path", source_devfile_path], + capture_output=True, text=True, check=True) + + assert contains(result.stdout, "Your new component '{}' is ready in the current directory.".format(self.COMPONENT)) + + cmd_odo_dev = str('odo dev --random-ports') + + try: + # starting dev mode with random ports should work + cmd_proc = subprocess.Popen(cmd_odo_dev, shell=True, bufsize=-1) + # sleep while odo dev is started + time.sleep(3) + result = subprocess.run(["minikube", "kubectl", "--", "get", "deployment", "-n", self.PROJECT], + capture_output=True, text=True, check=True) + + list_expected = [ + self.COMPONENT + "-app", + "NAME", + "AVAILABLE", + ] + assert match_all(result.stdout, list_expected) + + str_deleted = "The component \"{}\" is successfully deleted from namespace \"{}\"".format(self.COMPONENT, self.PROJECT) + result = subprocess.run(["odo", "delete", "component", "-f"], + capture_output=True, text=True, check=True) + + assert contains(result.stdout, str_deleted) + + result = subprocess.run(["minikube", "kubectl", "--", "get", "deployment", "-n", self.PROJECT], + capture_output=True, text=True, check=True) + + assert not contains(result.stdout, self.COMPONENT + "-app") + assert contains(result.stderr, "No resources found in " + self.PROJECT + " namespace.") + + # run delete command again after deletion + result = subprocess.run(["odo", "delete", "component", "-f"], + capture_output=True, text=True, check=True) + str_no_resource_found = "No resource found for component \"{}\" in namespace \"{}\"".format(self.COMPONENT, self.PROJECT) + assert contains(result.stdout, str_no_resource_found) + + cmd_proc.terminate() + except Exception as e: + raise e