Beispiel #1
0
    def test_list_services(self):
        api_key = get_oc_api_token()
        with K8sBackend(api_key=api_key) as k8s_backend:

            namespace = k8s_backend.create_namespace()

            labels = {"app": "postgres"}

            service = Service(name="database", ports=["5432"], selector=labels, namespace=namespace,
                              create_in_cluster=True)

            try:
                assert any(service.name == s.name for s in k8s_backend.list_services())
            finally:
                service.delete()
                k8s_backend.delete_namespace(namespace)
Beispiel #2
0
    def test_list_pods(self):
        api_key = get_oc_api_token()
        with K8sBackend(api_key=api_key) as k8s_backend:

            namespace = k8s_backend.create_namespace()

            with DockerBackend() as backend:

                image = backend.ImageClass("openshift/hello-openshift")

                pod = image.run_in_pod(namespace=namespace)

                try:
                    pod.wait(200)
                    assert any(pod.name == p.name for p in k8s_backend.list_pods())
                finally:
                    pod.delete()
                    k8s_backend.delete_namespace(namespace)
Beispiel #3
0
    def test_database_deployment(self):
        api_key = get_oc_api_token()
        with K8sBackend(api_key=api_key) as k8s_backend:

            namespace = k8s_backend.create_namespace()

            with DockerBackend() as backend:
                postgres_image = backend.ImageClass(
                    "centos/postgresql-10-centos7")

                postgres_image_metadata = postgres_image.get_metadata()

                # set up env variables
                db_env_variables = {
                    "POSTGRESQL_USER": "******",
                    "POSTGRESQL_PASSWORD": "******",
                    "POSTGRESQL_DATABASE": "db"
                }

                postgres_image_metadata.env_variables.update(db_env_variables)

                db_labels = {"app": "postgres"}

                db_service = Service(name="database",
                                     ports=["5432"],
                                     selector=db_labels,
                                     namespace=namespace,
                                     create_in_cluster=True)

                db_deployment = Deployment(
                    name="database",
                    selector=db_labels,
                    labels=db_labels,
                    image_metadata=postgres_image_metadata,
                    namespace=namespace,
                    create_in_cluster=True)

                try:
                    db_deployment.wait(200)
                    assert db_deployment.all_pods_ready()
                finally:
                    db_deployment.delete()
                    db_service.delete()
                    k8s_backend.delete_namespace(namespace)
Beispiel #4
0
    def test_pod(self):
        api_key = get_oc_api_token()
        with K8sBackend(api_key=api_key) as k8s_backend:

            namespace = k8s_backend.create_namespace()

            with DockerBackend() as backend:
                image = backend.ImageClass("openshift/hello-openshift")

                pod = image.run_in_pod(namespace=namespace)

                try:
                    pod.wait(200)
                    assert pod.is_ready()
                    assert pod.get_phase() == PodPhase.RUNNING
                finally:
                    pod.delete()
                    assert pod.get_phase() == PodPhase.TERMINATING
                    k8s_backend.delete_namespace(namespace)
Beispiel #5
0
    def test_deploy_image(self):
        api_key = get_oc_api_token()
        with OpenshiftBackend(api_key=api_key, logging_level=logging.DEBUG) as openshift_backend:
            # create new app from remote source in OpenShift cluster
            app_name = openshift_backend.deploy_image(CENTOS_MARIADB_10_2,
                                                      oc_new_app_args=[
                                                          "--env", "MYSQL_ROOT_PASSWORD=test"],
                                                      project=MY_PROJECT)

            try:
                # wait until service is ready to accept requests
                openshift_backend.wait_for_service(
                    app_name=app_name,
                    port=3306,
                    timeout=300)
                assert openshift_backend.all_pods_are_ready(app_name)
            finally:
                openshift_backend.get_logs(app_name)
                openshift_backend.clean_project(app_name)
Beispiel #6
0
    def test_oc_s2i_remote(self):
        api_key = get_oc_api_token()
        with OpenshiftBackend(api_key=api_key, logging_level=logging.DEBUG) as openshift_backend:

            openshift_backend.get_status()

            app_name = openshift_backend.create_new_app_from_source(
                CENTOS_PYTHON_3,
                source="https://github.com/openshift/django-ex.git",
                project=MY_PROJECT)

            try:
                openshift_backend.wait_for_service(
                    app_name=app_name,
                    port=8080,
                    expected_output='Welcome to your Django application on OpenShift',
                    timeout=300)
            finally:
                openshift_backend.get_logs(app_name)
                openshift_backend.clean_project(app_name)
Beispiel #7
0
    def test_oc_s2i_local(self):
        api_key = get_oc_api_token()
        with OpenshiftBackend(api_key=api_key, logging_level=logging.DEBUG) as openshift_backend:

            openshift_backend.get_status()

            app_name = openshift_backend.create_new_app_from_source(
                CENTOS_PYTHON_3,
                source="examples/openshift/standalone-test-app",
                project=MY_PROJECT)

            try:
                openshift_backend.wait_for_service(
                    app_name=app_name,
                    port=8080,
                    expected_output="Hello World from standalone WSGI application!",
                    timeout=300)
            finally:
                openshift_backend.get_logs(app_name)
                openshift_backend.clean_project(app_name)
Beispiel #8
0
    def test_oc_s2i_local(self):
        api_key = get_oc_api_token()
        with OpenshiftBackend(api_key=api_key, logging_level=logging.DEBUG) as openshift_backend:

            with DockerBackend(logging_level=logging.DEBUG) as backend:
                python_image = backend.ImageClass("centos/python-36-centos7")

                OpenshiftBackend.login_to_registry('developer')

                app_name = openshift_backend.new_app(
                    python_image,
                    source="examples/openshift/standalone-test-app",
                    project='myproject')

                try:
                    openshift_backend.wait_for_service(
                        app_name=app_name,
                        expected_output="Hello World from standalone WSGI application!",
                        timeout=300)
                finally:
                    openshift_backend.clean_project(app_name)
Beispiel #9
0
    def test_oc_s2i_remote(self):
        api_key = get_oc_api_token()
        with OpenshiftBackend(api_key=api_key, logging_level=logging.DEBUG) as openshift_backend:

            with DockerBackend(logging_level=logging.DEBUG) as backend:
                python_image = backend.ImageClass("centos/python-36-centos7")

                OpenshiftBackend.login_to_registry('developer')

                app_name = openshift_backend.new_app(
                    python_image,
                    source="https://github.com/openshift/django-ex.git",
                    project='myproject')

                try:
                    openshift_backend.wait_for_service(
                        app_name=app_name,
                        expected_output='Welcome to your Django application on OpenShift',
                        timeout=300)
                finally:
                    openshift_backend.clean_project(app_name)
Beispiel #10
0
    def test_pod_from_template(self):

        template = {
          "apiVersion": "v1",
          "kind": "Pod",
          "metadata": {
            "name": "myapp-pod",
            "labels": {
              "app": "myapp"
            }
          },
          "spec": {
            "containers": [
              {
                "name": "myapp-container",
                "image": "busybox",
                "command": [
                  "sh",
                  "-c",
                  "echo Hello Kubernetes! && sleep 3600"
                ]
              }
            ]
          }
        }

        api_key = get_oc_api_token()
        with K8sBackend(api_key=api_key) as k8s_backend:
            namespace = k8s_backend.create_namespace()

            pod = Pod(namespace=namespace, from_template=template)

            try:
                pod.wait(200)
                assert pod.is_ready()
                assert pod.get_phase() == PodPhase.RUNNING
            finally:
                pod.delete()
                assert pod.get_phase() == PodPhase.TERMINATING
                k8s_backend.delete_namespace(namespace)
Beispiel #11
0
    def test_oc_s2i_template(self):
        api_key = get_oc_api_token()
        with OpenshiftBackend(
                api_key=api_key,
                logging_level=logging.DEBUG) as openshift_backend:

            openshift_backend.get_status()

            openshift_backend.create_app_from_template(
                image_name=CENTOS_PYTHON_3,
                name=DJANGO_POSTGRES_TEMPLATE,
                template="https://raw.githubusercontent.com/sclorg/django-ex"
                "/master/openshift/templates/django-postgresql.json",
                oc_new_app_args=[
                    "-p",
                    "NAMESPACE=%s" % MY_PROJECT, "-p",
                    "NAME=%s" % DJANGO_POSTGRES_TEMPLATE, "-p",
                    "SOURCE_REPOSITORY_REF=master", "-p", "PYTHON_VERSION=3.6",
                    "-p",
                    "POSTGRESQL_VERSION=%s" % CENTOS_POSTGRES_9_6_TAG
                ],
                name_in_template={"python": "3.6"},
                other_images=[{
                    "%s:%s" % (CENTOS_POSTGRES_9_6, CENTOS_POSTGRES_9_6_TAG):
                    "postgresql:%s" % CENTOS_POSTGRES_9_6_TAG
                }],
                project=MY_PROJECT)

            try:
                openshift_backend.wait_for_service(
                    app_name=DJANGO_POSTGRES_TEMPLATE,
                    port=8080,
                    expected_output=
                    'Welcome to your Django application on OpenShift',
                    timeout=300)
            finally:
                openshift_backend.get_logs(DJANGO_POSTGRES_TEMPLATE)
                # pass name from template as argument
                openshift_backend.clean_project(DJANGO_POSTGRES_TEMPLATE)
Beispiel #12
0
    def test_deployment_from_template(self):
        api_key = get_oc_api_token()
        with K8sBackend(api_key=api_key) as k8s_backend:

            namespace = k8s_backend.create_namespace()

            template = """
            apiVersion: apps/v1
            kind: Deployment
            metadata:
              name: hello-world
              labels:
                app: hello-world
            spec:
              replicas: 3
              selector:
                matchLabels:
                  app: hello-world
              template:
                metadata:
                  labels:
                    app: hello-world
                spec:
                  containers:
                  - name: hello-openshift
                    image: openshift/hello-openshift
            """

            test_deployment = Deployment(namespace=namespace,
                                         from_template=template,
                                         create_in_cluster=True)

            try:
                test_deployment.wait(200)
                assert test_deployment.all_pods_ready()
            finally:
                test_deployment.delete()
                k8s_backend.delete_namespace(namespace)
Beispiel #13
0
 def test_get_project_name(self):
     api_key = get_oc_api_token()
     with OpenshiftBackend(
             api_key=api_key,
             logging_level=logging.DEBUG) as openshift_backend:
         assert openshift_backend.get_current_project() == MY_PROJECT
Beispiel #14
0
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import logging

from conu import OpenshiftBackend, \
                 DockerBackend
from conu.backend.origin.registry import login_to_registry
from conu.utils import get_oc_api_token

api_key = get_oc_api_token()
with OpenshiftBackend(api_key=api_key,
                      logging_level=logging.DEBUG) as openshift_backend:

    openshift_backend.get_status()

    with DockerBackend(logging_level=logging.DEBUG) as backend:
        # builder image
        python_image = backend.ImageClass("centos/python-36-centos7")

        # docker login inside OpenShift internal registry
        login_to_registry('developer')

        # create new app from remote source in OpenShift cluster
        app_name = openshift_backend.create_new_app_from_source(
            python_image,