All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.fabric8.kubernetes.assertions.Conditions Maven / Gradle / Ivy

Go to download

This library provides a bunch of helpful assertj assertions for working with the kubernetes-api.

The newest version!
package io.fabric8.kubernetes.assertions;

import io.fabric8.kubernetes.api.KubernetesHelper;
import io.fabric8.kubernetes.api.PodStatusType;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.ReplicationController;
import io.fabric8.kubernetes.api.model.Service;
import org.assertj.core.api.Condition;

import java.util.Map;
import java.util.Objects;

/**
 *
 */
public class Conditions
{

    public static  Condition hasLabel(final String key, final String value)
    {
        return new Condition()
        {
            @Override
            public String toString()
            {
                return "hasLabel(" + key + " = " + value + ")";
            }

            @Override
            public boolean matches(T resource)
            {
                return matchesLabel(resource.getMetadata().getLabels(), key, value);
            }
        };
    }

    public static  Condition hasName(final String name)
    {
        return new Condition()
        {
            @Override
            public String toString()
            {
                return "hasName(" + name + ")";
            }

            @Override
            public boolean matches(T resource)
            {
                return Objects.equals(name, resource.getMetadata().getName());
            }
        };
    }

    public static  Condition hasNamespace(final String namespace)
    {
        return new Condition()
        {
            @Override
            public String toString()
            {
                return "hasNamespace(" + namespace + ")";
            }

            @Override
            public boolean matches(T resource)
            {
                return Objects.equals(namespace, resource.getMetadata().getNamespace());
            }
        };
    }

    public static Condition status(final PodStatusType status)
    {
        return new Condition()
        {
            @Override
            public String toString()
            {
                return "podStatus(" + status + ")";
            }

            @Override
            public boolean matches(Pod pod)
            {
                return Objects.equals(status, KubernetesHelper.getPodStatus(pod));
            }
        };
    }

    public static Condition runningStatus()
    {
        return status(PodStatusType.OK);
    }

    public static Condition waitingStatus()
    {
        return status(PodStatusType.WAIT);
    }

    public static Condition errorStatus()
    {
        return status(PodStatusType.ERROR);
    }

    public static Condition podLabel(final String key, final String value)
    {
        return new Condition()
        {
            @Override
            public String toString()
            {
                return "podLabel(" + key + " = " + value + ")";
            }

            @Override
            public boolean matches(Pod pod)
            {
                return matchesLabel(pod.getMetadata().getLabels(), key, value);
            }
        };
    }

    public static Condition podNamespace(final String namespace)
    {
        return new Condition()
        {
            @Override
            public String toString()
            {
                return "podNamespace(" + namespace + ")";
            }

            @Override
            public boolean matches(Pod pod)
            {
                return Objects.equals(namespace, pod.getMetadata().getNamespace());
            }
        };
    }

    public static Condition replicationControllerLabel(final String key, final String value)
    {
        return new Condition()
        {
            @Override
            public String toString()
            {
                return "replicationControllerLabel(" + key + " = " + value + ")";
            }

            @Override
            public boolean matches(ReplicationController replicationControllerSchema)
            {
                return matchesLabel(replicationControllerSchema.getMetadata().getLabels(), key, value);
            }
        };
    }

    public static Condition serviceLabel(final String key, final String value)
    {
        return new Condition()
        {
            @Override
            public String toString()
            {
                return "serviceLabel(" + key + " = " + value + ")";
            }

            @Override
            public boolean matches(Service service)
            {
                return matchesLabel(service.getMetadata().getLabels(), key, value);
            }
        };
    }

    public static boolean matchesLabel(Map labels, String key, String value)
    {
        if (labels != null) {
            String actual = labels.get(key);
            return Objects.equals(value, actual);
        }
        else {
            return false;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy