def test_cleanup(): api = get_core_api() # take just namespaces that are not in terminating state number_of_namespaces = len( [item for item in api.list_namespace().items if item.status.phase != "Terminating"]) with K8sBackend(cleanup=[K8sCleanupPolicy.NAMESPACES]) as k8s_backend: # create two namespaces _ = k8s_backend.create_namespace() _ = k8s_backend.create_namespace() # cleanup should delete two namespaces created with k8s backend assert len( [item for item in api.list_namespace().items if item.status.phase != "Terminating"]) == number_of_namespaces with K8sBackend() as k8s_backend: # create two namespaces _ = k8s_backend.create_namespace() _ = k8s_backend.create_namespace() # no cleanup - namespaces are not deleted after work with backend is finished assert len( [item for item in api.list_namespace().items if item.status.phase != "Terminating"]) == number_of_namespaces + 2
def test_database_deployment(): with K8sBackend() 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)
def test_list_deployments(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_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() assert any(db_deployment.name == d.name for d in k8s_backend.list_deployments()) finally: db_deployment.delete() k8s_backend.delete_namespace(namespace)
def test_list_services(): with K8sBackend() 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)
def test_list_pods(): with K8sBackend() as k8s_backend: namespace = k8s_backend.create_namespace() with DockerBackend() as backend: image = backend.ImageClass(FEDORA_MINIMAL_REPOSITORY, tag=FEDORA_MINIMAL_REPOSITORY_TAG) 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)
def test_pod(): with K8sBackend() as k8s_backend: namespace = k8s_backend.create_namespace() with DockerBackend() as backend: image = backend.ImageClass(FEDORA_MINIMAL_REPOSITORY, tag=FEDORA_MINIMAL_REPOSITORY_TAG) pod = image.run_in_pod(namespace=namespace) try: pod.wait(200) assert pod.get_phase() == PodPhase.RUNNING finally: pod.delete() assert pod.get_phase() == PodPhase.TERMINATING k8s_backend.delete_namespace(namespace)
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)
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)
def test_deployment_from_template(): with K8sBackend() as k8s_backend: namespace = k8s_backend.create_namespace() template = """ apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80 """ 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)
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)
# """ Create deployment using template and check if all pods are ready """ import logging from conu.backend.k8s.backend import K8sBackend from conu.backend.k8s.deployment import Deployment from conu.utils import get_oc_api_token # obtain API key from OpenShift cluster. If you are not using OpenShift cluster for kubernetes tests # you need to replace `get_oc_api_token()` with your Bearer token. More information here: # https://kubernetes.io/docs/reference/access-authn-authz/authentication/ api_key = get_oc_api_token() with K8sBackend(api_key=api_key, logging_level=logging.DEBUG) 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
# This program is distributed in the hope that it will be useful, # 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/>. # """ Create deployment using template and check if all pods are ready """ from conu.backend.k8s.backend import K8sBackend from conu.backend.k8s.deployment import Deployment with K8sBackend() as k8s_backend: namespace = k8s_backend.create_namespace() template = """ apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 3 selector: matchLabels: app: nginx template: