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

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

There is a newer version: 4.0.0
Show newest version
/**
 *  Copyright 2005-2016 Red Hat, Inc.
 *
 *  Red Hat licenses this file to you under the Apache License, version
 *  2.0 (the "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 *  implied.  See the License for the specific language governing
 *  permissions and limitations under the License.
 */
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