├── .circleci └── config.yml ├── .gitignore ├── .mvn └── wrapper │ ├── MavenWrapperDownloader.java │ └── maven-wrapper.properties ├── ReadMe.md ├── license.txt ├── mvnw ├── mvnw.cmd ├── pom.xml └── src ├── main ├── assertj-templates │ ├── custom_abstract_assertion_class_template.txt │ ├── has_elements_assertion_template_for_iterable.txt │ └── navigation_template.txt └── java │ └── io │ └── fabric8 │ └── kubernetes │ └── assertions │ ├── AbstractPodSelectionAssert.java │ ├── Assertions.java │ ├── Conditions.java │ ├── DeploymentConfigPodsAssert.java │ ├── DeploymentPodsAssert.java │ ├── HasMetadatasAssert.java │ ├── HasPodSelectionAssert.java │ ├── KubernetesAssert.java │ ├── KubernetesNamespaceAssert.java │ ├── MetadatasListAssert.java │ ├── MultiHasPodSelectionAssert.java │ ├── NavigationListAssert.java │ ├── PodLogsAssert.java │ ├── PodSelectionAssert.java │ ├── PodsAssert.java │ ├── ReplicaSetPodsAssert.java │ ├── ReplicationControllerPodsAssert.java │ ├── ServicePodsAssert.java │ ├── ServicesAssert.java │ └── support │ ├── LogHelpers.java │ ├── PodAsserter.java │ ├── PodLogWatcher.java │ └── PodWatcher.java └── test ├── java └── io │ └── fabric8 │ └── kubernetes │ └── assertions │ ├── AssertionTests.java │ ├── ConditionsTests.java │ ├── DeploymentConfigPodsAssertTests.java │ ├── DeploymentPodsAssertTests.java │ ├── Example.java │ ├── HasMetadatasAssertTests.java │ ├── KubernetesAssertTests.java │ ├── KubernetesNamespaceAssertTests.java │ ├── MetadatasListAssertTests.java │ ├── NavigationAssertionTests.java │ ├── PodSelectionAssertTests.java │ ├── PodsAssertTests.java │ ├── ReplicaSetPodsAssertTests.java │ ├── ServicePodsAssertTests.java │ ├── ServicesAssertTests.java │ ├── TestBase.java │ └── support │ └── LogHelpersTests.java └── resources └── log4j.properties /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2016 Red Hat, Inc. 3 | # 4 | # Red Hat licenses this file to you under the Apache License, version 5 | # 2.0 (the "License"); you may not use this file except in compliance 6 | # with the License. You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 13 | # implied. See the License for the specific language governing 14 | # permissions and limitations under the License. 15 | # 16 | 17 | version: 2 18 | 19 | jobs: 20 | build: 21 | working_directory: ~/fabric8io/kubernetes-assertions 22 | machine: true 23 | steps: 24 | - checkout 25 | - run: 26 | command: | 27 | mvn clean install 28 | - save_cache: 29 | key: f8-{{ checksum "pom.xml" }} 30 | paths: 31 | - ~/.m2 32 | 33 | workflows: 34 | version: 2 35 | all: 36 | jobs: 37 | - build 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | .idea 3 | *.iml 4 | *.im 5 | *.ipr 6 | *.iws 7 | overlays 8 | .DS_Store 9 | .settings 10 | *.swp 11 | *.log 12 | .project 13 | .classpath 14 | *.fmd 15 | .cache 16 | dependency-reduced-pom.xml 17 | apps/modifiedFabric8.json 18 | bin/openshift*/ 19 | .vagrant/ 20 | *.jar 21 | -------------------------------------------------------------------------------- /.mvn/wrapper/MavenWrapperDownloader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2007-present the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | import java.net.*; 17 | import java.io.*; 18 | import java.nio.channels.*; 19 | import java.util.Properties; 20 | 21 | public class MavenWrapperDownloader { 22 | 23 | private static final String WRAPPER_VERSION = "0.5.6"; 24 | /** 25 | * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. 26 | */ 27 | private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" 28 | + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; 29 | 30 | /** 31 | * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to 32 | * use instead of the default one. 33 | */ 34 | private static final String MAVEN_WRAPPER_PROPERTIES_PATH = 35 | ".mvn/wrapper/maven-wrapper.properties"; 36 | 37 | /** 38 | * Path where the maven-wrapper.jar will be saved to. 39 | */ 40 | private static final String MAVEN_WRAPPER_JAR_PATH = 41 | ".mvn/wrapper/maven-wrapper.jar"; 42 | 43 | /** 44 | * Name of the property which should be used to override the default download url for the wrapper. 45 | */ 46 | private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; 47 | 48 | public static void main(String args[]) { 49 | System.out.println("- Downloader started"); 50 | File baseDirectory = new File(args[0]); 51 | System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); 52 | 53 | // If the maven-wrapper.properties exists, read it and check if it contains a custom 54 | // wrapperUrl parameter. 55 | File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); 56 | String url = DEFAULT_DOWNLOAD_URL; 57 | if(mavenWrapperPropertyFile.exists()) { 58 | FileInputStream mavenWrapperPropertyFileInputStream = null; 59 | try { 60 | mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); 61 | Properties mavenWrapperProperties = new Properties(); 62 | mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); 63 | url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); 64 | } catch (IOException e) { 65 | System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); 66 | } finally { 67 | try { 68 | if(mavenWrapperPropertyFileInputStream != null) { 69 | mavenWrapperPropertyFileInputStream.close(); 70 | } 71 | } catch (IOException e) { 72 | // Ignore ... 73 | } 74 | } 75 | } 76 | System.out.println("- Downloading from: " + url); 77 | 78 | File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); 79 | if(!outputFile.getParentFile().exists()) { 80 | if(!outputFile.getParentFile().mkdirs()) { 81 | System.out.println( 82 | "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); 83 | } 84 | } 85 | System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); 86 | try { 87 | downloadFileFromURL(url, outputFile); 88 | System.out.println("Done"); 89 | System.exit(0); 90 | } catch (Throwable e) { 91 | System.out.println("- Error downloading"); 92 | e.printStackTrace(); 93 | System.exit(1); 94 | } 95 | } 96 | 97 | private static void downloadFileFromURL(String urlString, File destination) throws Exception { 98 | if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { 99 | String username = System.getenv("MVNW_USERNAME"); 100 | char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); 101 | Authenticator.setDefault(new Authenticator() { 102 | @Override 103 | protected PasswordAuthentication getPasswordAuthentication() { 104 | return new PasswordAuthentication(username, password); 105 | } 106 | }); 107 | } 108 | URL website = new URL(urlString); 109 | ReadableByteChannel rbc; 110 | rbc = Channels.newChannel(website.openStream()); 111 | FileOutputStream fos = new FileOutputStream(destination); 112 | fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); 113 | fos.close(); 114 | rbc.close(); 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar 3 | -------------------------------------------------------------------------------- /ReadMe.md: -------------------------------------------------------------------------------- 1 | ## Kubernetes Assertions 2 | 3 | [![CircleCI](https://circleci.com/gh/fabric8io/kubernetes-assertions.svg?style=svg)](https://circleci.com/gh/fabric8io/kubernetes-assertions) 4 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.fabric8/kubernetes-assertions/badge.svg?style=flat-square)](https://maven-badges.herokuapp.com/maven-central/io.fabric8/kubernetes-assertions/) 5 | [![Javadocs](http://www.javadoc.io/badge/io.fabric8/kubernetes-assertions.svg?color=blue)](http://www.javadoc.io/doc/io.fabric8/kubernetes-assertions) 6 | 7 | This library provides a bunch of helpful [assertj](http://joel-costigliola.github.io/assertj/) assertions for working with the [kubernetes-api](https://github.com/fabric8io/fabric8/tree/master/components/kubernetes-api). 8 | 9 | ### Default system test 10 | 11 | The following code provides a default system test: 12 | 13 | ``` 14 | assertThat(client).deployments().pods().isPodReadyForPeriod(); 15 | ``` 16 | 17 | This will assert that the current project's `Deployment` creates at least one pod; that it becomes `Ready` within a time period (30 seconds by default), then that the pod keeps being `Ready` for a period (defaults to 10 seconds). 18 | 19 | This may seem a fairly simple test case; but it catches most errors with the `Deployment` being invalid or failing to start; the pod starting then failing due to some configuration issue etc. 20 | 21 | If your application uses [liveness checks](http://kubernetes.io/docs/user-guide/liveness/) (which are used by default with Spring Boot apps) then this test also asserts that the liveness checks keep valid for the period too. So if your application fails to connect to a database or your Camel route fails to start a route or whatever; then the test fails! 22 | 23 | This means to improve your system tests you can just improve your liveness checks; which also helps Kubernetes manage your production environment too! 24 | 25 | ### Other examples 26 | 27 | Some quick examples: 28 | 29 | * [assertThat(KubernetesClient)](https://github.com/fabric8io/fabric8/blob/master/components/kubernetes-assertions/src/test/java/io/fabric8/kubernetes/assertions/Example.java#L38) helper code that is available if you add the **kubernetes-assertions** dependency. 30 | * [assertThat(Pod)](https://github.com/fabric8io/fabric8/blob/master/components/kubernetes-assertions/src/test/java/io/fabric8/kubernetes/assertions/ExampleTest.java#L49-L50) and navigating the model 31 | * [assertThat(PodList)](https://github.com/fabric8io/fabric8/blob/master/components/kubernetes-assertions/src/test/java/io/fabric8/kubernetes/assertions/ExampleTest.java#L96-L102) using list navigations 32 | 33 | ### Navigating and asserting around resources 34 | 35 | When working with Kubernetes and OpenShift resources in test cases you'll find that objects can be massively nested. 36 | 37 | For example even using something as simple as a Pod you need to navigate to compare its name: 38 | 39 | ```java 40 | Pod pod = kubernetesClient.pods().inNamespace(ns).withName("foo").get(); 41 | assertThat(pod).metadata().name().isEqualTo("foo"); 42 | ``` 43 | 44 | Things get even more complex when asserting a ReplicationController 45 | 46 | ```java 47 | ReplicationController rc = kubernetesClient.replicationControllers().inNamespace(ns).withName("foo").get(); 48 | assertThat(rc).spec().template().spec().containers().first().image().isEqualTo("someDockerImageName"); 49 | ``` 50 | 51 | Whats great about Kubernetes Assertions is that you can chain methods together to navigate the model; if any navigation fails you get meaninful errors in the test failure telling you exactly which object was null or list was empty or other assertion failed (e.g. a list index was out of range) etc. 52 | 53 | For example here's some example error messages from assertj when navigation or assertions fail in [these tests](https://github.com/fabric8io/fabric8/blob/master/components/kubernetes-assertions/src/test/java/io/fabric8/kubernetes/assertions/ExampleTest.java#L111-L123): 54 | 55 | ``` 56 | org.junit.ComparisonFailure: [podListWith2Items.items.first().metadata.name] expected:<"[shouldNotMatch]"> but was:<"[abc]"> 57 | 58 | java.lang.AssertionError: [podListWith2Items.items.index] 59 | Expecting: 60 | <-1> 61 | to be greater than or equal to: 62 | <0> 63 | ``` 64 | 65 | ### Add it to your Maven pom.xml 66 | 67 | To be able to use the Java code in your [Apache Maven](http://maven.apache.org/) based project add this into your pom.xml 68 | 69 | 70 | io.fabric8 71 | kubernetes-assertions 72 | 4.0.0 73 | test 74 | 75 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Mingw, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | fi 118 | 119 | if [ -z "$JAVA_HOME" ]; then 120 | javaExecutable="`which javac`" 121 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 122 | # readlink(1) is not available as standard on Solaris 10. 123 | readLink=`which readlink` 124 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 125 | if $darwin ; then 126 | javaHome="`dirname \"$javaExecutable\"`" 127 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 128 | else 129 | javaExecutable="`readlink -f \"$javaExecutable\"`" 130 | fi 131 | javaHome="`dirname \"$javaExecutable\"`" 132 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 133 | JAVA_HOME="$javaHome" 134 | export JAVA_HOME 135 | fi 136 | fi 137 | fi 138 | 139 | if [ -z "$JAVACMD" ] ; then 140 | if [ -n "$JAVA_HOME" ] ; then 141 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 142 | # IBM's JDK on AIX uses strange locations for the executables 143 | JAVACMD="$JAVA_HOME/jre/sh/java" 144 | else 145 | JAVACMD="$JAVA_HOME/bin/java" 146 | fi 147 | else 148 | JAVACMD="`which java`" 149 | fi 150 | fi 151 | 152 | if [ ! -x "$JAVACMD" ] ; then 153 | echo "Error: JAVA_HOME is not defined correctly." >&2 154 | echo " We cannot execute $JAVACMD" >&2 155 | exit 1 156 | fi 157 | 158 | if [ -z "$JAVA_HOME" ] ; then 159 | echo "Warning: JAVA_HOME environment variable is not set." 160 | fi 161 | 162 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 163 | 164 | # traverses directory structure from process work directory to filesystem root 165 | # first directory with .mvn subdirectory is considered project base directory 166 | find_maven_basedir() { 167 | 168 | if [ -z "$1" ] 169 | then 170 | echo "Path not specified to find_maven_basedir" 171 | return 1 172 | fi 173 | 174 | basedir="$1" 175 | wdir="$1" 176 | while [ "$wdir" != '/' ] ; do 177 | if [ -d "$wdir"/.mvn ] ; then 178 | basedir=$wdir 179 | break 180 | fi 181 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 182 | if [ -d "${wdir}" ]; then 183 | wdir=`cd "$wdir/.."; pwd` 184 | fi 185 | # end of workaround 186 | done 187 | echo "${basedir}" 188 | } 189 | 190 | # concatenates all lines of a file 191 | concat_lines() { 192 | if [ -f "$1" ]; then 193 | echo "$(tr -s '\n' ' ' < "$1")" 194 | fi 195 | } 196 | 197 | BASE_DIR=`find_maven_basedir "$(pwd)"` 198 | if [ -z "$BASE_DIR" ]; then 199 | exit 1; 200 | fi 201 | 202 | ########################################################################################## 203 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 204 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 205 | ########################################################################################## 206 | if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then 207 | if [ "$MVNW_VERBOSE" = true ]; then 208 | echo "Found .mvn/wrapper/maven-wrapper.jar" 209 | fi 210 | else 211 | if [ "$MVNW_VERBOSE" = true ]; then 212 | echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." 213 | fi 214 | if [ -n "$MVNW_REPOURL" ]; then 215 | jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" 216 | else 217 | jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" 218 | fi 219 | while IFS="=" read key value; do 220 | case "$key" in (wrapperUrl) jarUrl="$value"; break ;; 221 | esac 222 | done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" 223 | if [ "$MVNW_VERBOSE" = true ]; then 224 | echo "Downloading from: $jarUrl" 225 | fi 226 | wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" 227 | if $cygwin; then 228 | wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` 229 | fi 230 | 231 | if command -v wget > /dev/null; then 232 | if [ "$MVNW_VERBOSE" = true ]; then 233 | echo "Found wget ... using wget" 234 | fi 235 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 236 | wget "$jarUrl" -O "$wrapperJarPath" 237 | else 238 | wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" 239 | fi 240 | elif command -v curl > /dev/null; then 241 | if [ "$MVNW_VERBOSE" = true ]; then 242 | echo "Found curl ... using curl" 243 | fi 244 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 245 | curl -o "$wrapperJarPath" "$jarUrl" -f 246 | else 247 | curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f 248 | fi 249 | 250 | else 251 | if [ "$MVNW_VERBOSE" = true ]; then 252 | echo "Falling back to using Java to download" 253 | fi 254 | javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" 255 | # For Cygwin, switch paths to Windows format before running javac 256 | if $cygwin; then 257 | javaClass=`cygpath --path --windows "$javaClass"` 258 | fi 259 | if [ -e "$javaClass" ]; then 260 | if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 261 | if [ "$MVNW_VERBOSE" = true ]; then 262 | echo " - Compiling MavenWrapperDownloader.java ..." 263 | fi 264 | # Compiling the Java class 265 | ("$JAVA_HOME/bin/javac" "$javaClass") 266 | fi 267 | if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 268 | # Running the downloader 269 | if [ "$MVNW_VERBOSE" = true ]; then 270 | echo " - Running MavenWrapperDownloader.java ..." 271 | fi 272 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") 273 | fi 274 | fi 275 | fi 276 | fi 277 | ########################################################################################## 278 | # End of extension 279 | ########################################################################################## 280 | 281 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 282 | if [ "$MVNW_VERBOSE" = true ]; then 283 | echo $MAVEN_PROJECTBASEDIR 284 | fi 285 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 286 | 287 | # For Cygwin, switch paths to Windows format before running java 288 | if $cygwin; then 289 | [ -n "$M2_HOME" ] && 290 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 291 | [ -n "$JAVA_HOME" ] && 292 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 293 | [ -n "$CLASSPATH" ] && 294 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 295 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 296 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 297 | fi 298 | 299 | # Provide a "standardized" way to retrieve the CLI args that will 300 | # work with both Windows and non-Windows executions. 301 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" 302 | export MAVEN_CMD_LINE_ARGS 303 | 304 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 305 | 306 | exec "$JAVACMD" \ 307 | $MAVEN_OPTS \ 308 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 309 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 310 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 311 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM set title of command window 39 | title %0 40 | @REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' 41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 42 | 43 | @REM set %HOME% to equivalent of $HOME 44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 45 | 46 | @REM Execute a user defined script before this one 47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 49 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 50 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 51 | :skipRcPre 52 | 53 | @setlocal 54 | 55 | set ERROR_CODE=0 56 | 57 | @REM To isolate internal variables from possible post scripts, we use another setlocal 58 | @setlocal 59 | 60 | @REM ==== START VALIDATION ==== 61 | if not "%JAVA_HOME%" == "" goto OkJHome 62 | 63 | echo. 64 | echo Error: JAVA_HOME not found in your environment. >&2 65 | echo Please set the JAVA_HOME variable in your environment to match the >&2 66 | echo location of your Java installation. >&2 67 | echo. 68 | goto error 69 | 70 | :OkJHome 71 | if exist "%JAVA_HOME%\bin\java.exe" goto init 72 | 73 | echo. 74 | echo Error: JAVA_HOME is set to an invalid directory. >&2 75 | echo JAVA_HOME = "%JAVA_HOME%" >&2 76 | echo Please set the JAVA_HOME variable in your environment to match the >&2 77 | echo location of your Java installation. >&2 78 | echo. 79 | goto error 80 | 81 | @REM ==== END VALIDATION ==== 82 | 83 | :init 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 122 | 123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" 124 | 125 | FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( 126 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B 127 | ) 128 | 129 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 130 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 131 | if exist %WRAPPER_JAR% ( 132 | if "%MVNW_VERBOSE%" == "true" ( 133 | echo Found %WRAPPER_JAR% 134 | ) 135 | ) else ( 136 | if not "%MVNW_REPOURL%" == "" ( 137 | SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" 138 | ) 139 | if "%MVNW_VERBOSE%" == "true" ( 140 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 141 | echo Downloading from: %DOWNLOAD_URL% 142 | ) 143 | 144 | powershell -Command "&{"^ 145 | "$webclient = new-object System.Net.WebClient;"^ 146 | "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ 147 | "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ 148 | "}"^ 149 | "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ 150 | "}" 151 | if "%MVNW_VERBOSE%" == "true" ( 152 | echo Finished downloading %WRAPPER_JAR% 153 | ) 154 | ) 155 | @REM End of extension 156 | 157 | @REM Provide a "standardized" way to retrieve the CLI args that will 158 | @REM work with both Windows and non-Windows executions. 159 | set MAVEN_CMD_LINE_ARGS=%* 160 | 161 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 162 | if ERRORLEVEL 1 goto error 163 | goto end 164 | 165 | :error 166 | set ERROR_CODE=1 167 | 168 | :end 169 | @endlocal & set ERROR_CODE=%ERROR_CODE% 170 | 171 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 172 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 173 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 174 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 175 | :skipRcPost 176 | 177 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 178 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 179 | 180 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 181 | 182 | exit /B %ERROR_CODE% 183 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | io.fabric8 8 | kubernetes-assertions 9 | 4.0-SNAPSHOT 10 | jar 11 | Fabric8 Kubernetes :: Assertions 12 | This library provides a bunch of helpful assertj assertions for working with the kubernetes-api. 13 | 14 | http://github.com/fabric8io/kubernetes-assertions 15 | 16 | 17 | 18 | Apache License, Version 2.0 19 | http://www.apache.org/licenses/LICENSE-2.0.txt 20 | A business-friendly OSS license 21 | repo 22 | 23 | 24 | 25 | 26 | 27 | geeks 28 | Fabric8 Development Team 29 | fabric8@googlegroups.com 30 | fabric8 31 | http://fabric8.io/ 32 | 33 | 34 | 35 | 36 | scm:git:git://github.com/fabric8io/kubernetes-assertions.git 37 | scm:git:ssh://git@github.com/fabric8io/kubernetes-assertions.git 38 | HEAD 39 | git://github.com/fabric8io/kubernetes-assertions.git 40 | 41 | 42 | 43 | 44 | maven-central-plugin-snapshots 45 | Maven Central Plugin Snapshots 46 | https://oss.sonatype.org/content/repositories/snapshots 47 | 48 | false 49 | 50 | 51 | true 52 | 53 | 54 | 55 | 56 | 57 | 58 | sonatype-nexus-snapshots 59 | Sonatype Nexus Snapshots 60 | https://oss.sonatype.org/content/repositories/snapshots/ 61 | 62 | 63 | sonatype-nexus-staging 64 | Nexus Release Repository 65 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 66 | 67 | 68 | 69 | 70 | 71 | io.fabric8 72 | kubernetes-api 73 | ${fabric8-utils.version} 74 | 75 | 76 | 77 | io.fabric8 78 | kubernetes-client 79 | ${kubernetes-client.version} 80 | 81 | 82 | 83 | io.fabric8 84 | openshift-client 85 | ${kubernetes-client.version} 86 | 87 | 88 | 89 | io.fabric8 90 | fabric8-utils 91 | ${fabric8-utils.version} 92 | 93 | 94 | 95 | org.fusesource.jansi 96 | jansi 97 | ${jansi.version} 98 | 99 | 100 | 101 | org.assertj 102 | assertj-core 103 | ${assertj.core.version} 104 | 105 | 106 | 107 | org.slf4j 108 | slf4j-log4j12 109 | ${slf4j-log4j12.version} 110 | test 111 | 112 | 113 | org.junit.jupiter 114 | junit-jupiter-api 115 | ${junit-jupiter-api.version} 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | org.assertj 124 | assertj-assertions-generator-maven-plugin 125 | 2.0.0 126 | 127 | 128 | org.assertj 129 | assertj-core 130 | ${assertj.core.version} 131 | 132 | 133 | 134 | io.fabric8.assertj 135 | assertj-assertions-generator 136 | 2.0.1.2 137 | 138 | 139 | 140 | 141 | io.fabric8.kubernetes.api.model 142 | io.fabric8.openshift.api.model 143 | 144 | 145 | 146 | true 147 | 148 | io.fabric8.kubernetes.assertions.internal 149 | 150 | 151 | 152 | .*Doneable.* 153 | .*Editable.* 154 | .*Builder 155 | .*Fluent 156 | .*FluentImpl 157 | 158 | 159 | ${basedir}/target/generated-sources 160 | 161 | true 162 | false 163 | false 164 | false 165 | 166 | 167 | ${basedir}/src/main/assertj-templates 168 | 169 | navigation_template.txt 170 | has_elements_assertion_template_for_iterable.txt 171 | custom_abstract_assertion_class_template.txt 172 | 173 | 174 | 175 | 176 | 177 | generate-tests 178 | generate-sources 179 | 180 | generate-assertions 181 | 182 | 183 | 184 | 185 | 186 | 187 | org.codehaus.mojo 188 | build-helper-maven-plugin 189 | 3.0.0 190 | 191 | 192 | add-source 193 | generate-sources 194 | 195 | add-source 196 | 197 | 198 | 199 | ${basedir}/target/generated-sources 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | org.apache.maven.plugins 208 | maven-failsafe-plugin 209 | 2.20.1 210 | 211 | 212 | org.junit.jupiter 213 | junit-jupiter-api 214 | RELEASE 215 | 216 | 217 | 218 | 219 | 220 | org.apache.maven.plugins 221 | maven-compiler-plugin 222 | 3.7.0 223 | 224 | 1.8 225 | 1.8 226 | 227 | 228 | 229 | 230 | 231 | 232 | 3.15.0 233 | 5.6.0 234 | 1.7.25 235 | 1.11 236 | 3.0.8 237 | 4.9.1 238 | 239 | 240 | 241 | 242 | release 243 | 244 | 245 | 246 | org.apache.maven.plugins 247 | maven-gpg-plugin 248 | 249 | ${env.GPG_PASSPHRASE} 250 | true 251 | 252 | 253 | 254 | 255 | sign-artifacts 256 | verify 257 | 258 | sign 259 | 260 | 261 | 262 | 263 | 264 | org.apache.maven.plugins 265 | maven-enforcer-plugin 266 | 267 | 268 | enforce-no-snapshots-maven-v3 269 | 270 | enforce 271 | 272 | 273 | 274 | 275 | No Snapshots Allowed! 276 | 277 | 278 | 3.0 279 | Maven minimum required version is 3.0! 280 | 281 | 282 | true 283 | 284 | 285 | 286 | 287 | 288 | org.apache.maven.plugins 289 | maven-javadoc-plugin 290 | 291 | 292 | attach-javadocs 293 | 294 | jar 295 | 296 | 297 | 298 | -Xdoclint:none 299 | 300 | 301 | 302 | 303 | 304 | org.apache.maven.plugins 305 | maven-source-plugin 306 | 307 | 308 | attach-sources 309 | 310 | jar 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | -------------------------------------------------------------------------------- /src/main/assertj-templates/custom_abstract_assertion_class_template.txt: -------------------------------------------------------------------------------- 1 | package ${package}; 2 | 3 | ${imports} 4 | 5 | import org.assertj.core.api.AssertFactory; 6 | import io.fabric8.kubernetes.assertions.NavigationListAssert; 7 | 8 | import static org.assertj.core.api.Assertions.assertThat; 9 | import static io.fabric8.kubernetes.assertions.internal.Assertions.assertThat; 10 | 11 | /** 12 | * Abstract base class for {@link ${class_to_assert}} specific assertions - Generated by CustomAssertionGenerator. 13 | */ 14 | public abstract class ${custom_assertion_class}, A extends ${class_to_assert}> extends ${super_assertion_class} { 15 | 16 | /** 17 | * Creates a new {@link ${custom_assertion_class}} to make assertions on actual ${class_to_assert}. 18 | * @param actual the ${class_to_assert} we want to make assertions on. 19 | */ 20 | protected ${custom_assertion_class}(A actual, Class selfType) { 21 | super(actual, selfType); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /src/main/assertj-templates/has_elements_assertion_template_for_iterable.txt: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Navigates to the property ${property} so that assertions can be done on it 4 | */ 5 | public NavigationListAssert<${elementType}, ${elementAssertType}> ${property_safe}() ${throws} { 6 | isNotNull(); 7 | 8 | // return the assertion for the property 9 | AssertFactory<${elementType}, ${elementAssertType}> assertFactory = new AssertFactory<${elementType}, ${elementAssertType}>() { 10 | public ${elementAssertType} createAssert(${elementType} t) { 11 | return (${elementAssertType}) assertThat(t); 12 | } 13 | }; 14 | NavigationListAssert<${elementType}, ${elementAssertType}> answer = new NavigationListAssert<${elementType}, ${elementAssertType}>(actual.get${Property}(), assertFactory); 15 | answer.describedAs(io.fabric8.kubernetes.assertions.Assertions.joinDescription(this, "${property}")); 16 | return answer; 17 | } 18 | -------------------------------------------------------------------------------- /src/main/assertj-templates/navigation_template.txt: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Navigates to the property ${property} so that assertions can be done on it 4 | */ 5 | public ${propertyAssertType} ${property_safe}() ${throws} { 6 | // check that actual ${class_to_assert} we want to make assertions on is not null. 7 | isNotNull(); 8 | 9 | // return the assertion for the property 10 | return (${propertyAssertType}) assertThat(actual.get${Property}()).describedAs(io.fabric8.kubernetes.assertions.Assertions.joinDescription(this, "${property}")); 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/AbstractPodSelectionAssert.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.model.Pod; 4 | import io.fabric8.utils.Strings; 5 | import io.fabric8.utils.Systems; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | 9 | import java.util.List; 10 | 11 | public abstract class AbstractPodSelectionAssert { 12 | private static final transient Logger LOG = LoggerFactory.getLogger(AbstractPodSelectionAssert.class); 13 | 14 | public static final String PROPERTY_ASSERT_NOT_READY_TIMEOUT_MILLIS = "fabric8.assert.notReadyTimeoutMills"; 15 | public static final String PROPERTY_ASSERT_READY_PERIOD_MILLIS = "fabric8.assert.readyPeriodMills"; 16 | 17 | public static final long DEFAULT_NOT_READY_TIMEOUT_MS = 5 * 60 * 1000; 18 | public static final long DEFAULT_READY_PERIOD_MS = 10 * 1000; 19 | 20 | public static long getDefaultReadyPeriodMs() { 21 | return parseLongValue(Systems.getEnvVarOrSystemProperty(PROPERTY_ASSERT_READY_PERIOD_MILLIS), DEFAULT_READY_PERIOD_MS); 22 | } 23 | 24 | public static long getDefaultNotReadyTimeoutMs() { 25 | return parseLongValue(Systems.getEnvVarOrSystemProperty(PROPERTY_ASSERT_NOT_READY_TIMEOUT_MILLIS), DEFAULT_NOT_READY_TIMEOUT_MS); 26 | } 27 | 28 | private static long parseLongValue(String text, long defaultValue) { 29 | if (Strings.isNotBlank(text)) { 30 | try { 31 | return Long.parseLong(text); 32 | } catch (NumberFormatException e) { 33 | LOG.warn("Could not parse long value " + text + ": " + e, e); 34 | } 35 | } 36 | return defaultValue; 37 | } 38 | 39 | /** 40 | * Asserts that a pod is ready for this deployment all become ready within the given time and that each one keeps being ready for the given time 41 | */ 42 | public AbstractPodSelectionAssert isPodReadyForPeriod() { 43 | return isPodReadyForPeriod(getDefaultNotReadyTimeoutMs(), getDefaultReadyPeriodMs()); 44 | } 45 | 46 | abstract public AbstractPodSelectionAssert isPodReadyForPeriod(long notReadyTimeoutMS, long readyPeriodMS); 47 | 48 | abstract public List getPods(); 49 | } -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/Assertions.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.client.KubernetesClient; 4 | import org.assertj.core.api.AbstractAssert; 5 | 6 | public class Assertions extends io.fabric8.kubernetes.assertions.internal.Assertions { 7 | 8 | public static KubernetesAssert assertThat(KubernetesClient kubernetesClient) { 9 | return new KubernetesAssert(kubernetesClient); 10 | } 11 | 12 | public static KubernetesNamespaceAssert assertThat(KubernetesClient kubernetesClient, String namespace) { 13 | return assertThat(kubernetesClient).namespace(namespace); 14 | } 15 | 16 | // TODO remove and replace with Descriptions.navigateDescription() when this issue is resolved and released: 17 | // https://github.com/joel-costigliola/assertj-core/issues/641 18 | public static String joinDescription(AbstractAssert asserter, String propertyName) { 19 | String text = asserter.descriptionText(); 20 | if (text == null || text.length() == 0) { 21 | text = asserter.getClass().getSimpleName(); 22 | String postfix = "Assert"; 23 | if (text.endsWith(postfix)) { 24 | text = text.substring(0, text.length() - postfix.length()); 25 | } 26 | } 27 | return text + "." + propertyName; 28 | } 29 | } -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/Conditions.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.KubernetesHelper; 4 | import io.fabric8.kubernetes.api.PodStatusType; 5 | import io.fabric8.kubernetes.api.model.HasMetadata; 6 | import io.fabric8.kubernetes.api.model.Pod; 7 | import io.fabric8.kubernetes.api.model.ReplicationController; 8 | import io.fabric8.kubernetes.api.model.Service; 9 | import org.assertj.core.api.Condition; 10 | 11 | import java.util.Map; 12 | import java.util.Objects; 13 | 14 | /** 15 | */ 16 | public class Conditions { 17 | 18 | public static Condition hasLabel(final String key, final String value) { 19 | return new Condition() { 20 | @Override 21 | public String toString() { 22 | return "hasLabel(" + key + " = " + value + ")"; 23 | } 24 | 25 | @Override 26 | public boolean matches(T resource) { 27 | return matchesLabel(resource.getMetadata().getLabels(), key, value); 28 | } 29 | }; 30 | } 31 | 32 | public static Condition hasName(final String name) { 33 | return new Condition() { 34 | @Override 35 | public String toString() { 36 | return "hasName(" + name + ")"; 37 | } 38 | 39 | @Override 40 | public boolean matches(T resource) { 41 | return Objects.equals(name, resource.getMetadata().getName()); 42 | } 43 | }; 44 | } 45 | 46 | public static Condition hasNamespace(final String namespace) { 47 | return new Condition() { 48 | @Override 49 | public String toString() { 50 | return "hasNamespace(" + namespace + ")"; 51 | } 52 | 53 | @Override 54 | public boolean matches(T resource) { 55 | return Objects.equals(namespace, resource.getMetadata().getNamespace()); 56 | } 57 | }; 58 | } 59 | 60 | 61 | 62 | public static Condition status(final PodStatusType status) { 63 | return new Condition() { 64 | @Override 65 | public String toString() { 66 | return "podStatus(" + status + ")"; 67 | } 68 | 69 | @Override 70 | public boolean matches(Pod pod) { 71 | return Objects.equals(status, KubernetesHelper.getPodStatus(pod)); 72 | } 73 | }; 74 | } 75 | 76 | public static Condition runningStatus() { 77 | return status(PodStatusType.OK); 78 | } 79 | 80 | public static Condition waitingStatus() { 81 | return status(PodStatusType.WAIT); 82 | } 83 | 84 | public static Condition errorStatus() { 85 | return status(PodStatusType.ERROR); 86 | } 87 | 88 | 89 | public static Condition podLabel(final String key, final String value) { 90 | return new Condition() { 91 | @Override 92 | public String toString() { 93 | return "podLabel(" + key + " = " + value + ")"; 94 | } 95 | 96 | @Override 97 | public boolean matches(Pod pod) { 98 | return matchesLabel(pod.getMetadata().getLabels(), key, value); 99 | } 100 | }; 101 | } 102 | 103 | public static Condition podNamespace(final String namespace) { 104 | return new Condition() { 105 | @Override 106 | public String toString() { 107 | return "podNamespace(" + namespace + ")"; 108 | } 109 | 110 | @Override 111 | public boolean matches(Pod pod) { 112 | return Objects.equals(namespace, pod.getMetadata().getNamespace()); 113 | } 114 | }; 115 | } 116 | 117 | 118 | public static Condition replicationControllerLabel(final String key, final String value) { 119 | return new Condition() { 120 | @Override 121 | public String toString() { 122 | return "replicationControllerLabel(" + key + " = " + value + ")"; 123 | } 124 | 125 | @Override 126 | public boolean matches(ReplicationController replicationControllerSchema) { 127 | return matchesLabel(replicationControllerSchema.getMetadata().getLabels(), key, value); 128 | } 129 | }; 130 | } 131 | 132 | 133 | public static Condition serviceLabel(final String key, final String value) { 134 | return new Condition() { 135 | @Override 136 | public String toString() { 137 | return "serviceLabel(" + key + " = " + value + ")"; 138 | } 139 | 140 | @Override 141 | public boolean matches(Service service) { 142 | return matchesLabel(service.getMetadata().getLabels(), key, value); 143 | } 144 | }; 145 | } 146 | 147 | public static boolean matchesLabel(Map labels, String key, String value) { 148 | if (labels != null) { 149 | String actual = labels.get(key); 150 | return Objects.equals(value, actual); 151 | } else { 152 | return false; 153 | } 154 | } 155 | 156 | } -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/DeploymentConfigPodsAssert.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.KubernetesHelper; 4 | import io.fabric8.kubernetes.api.model.LabelSelectorRequirement; 5 | import io.fabric8.kubernetes.client.KubernetesClient; 6 | import io.fabric8.openshift.api.model.DeploymentConfig; 7 | import io.fabric8.openshift.api.model.DeploymentConfigAssert; 8 | import io.fabric8.openshift.api.model.DeploymentConfigSpec; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | /** 15 | * Adds assertions for asserting that a Deployment starts up correctly etc 16 | */ 17 | public class DeploymentConfigPodsAssert extends DeploymentConfigAssert implements HasPodSelectionAssert { 18 | private final KubernetesClient client; 19 | 20 | public DeploymentConfigPodsAssert(KubernetesClient client, DeploymentConfig deployment) { 21 | super(deployment); 22 | this.client = client; 23 | } 24 | 25 | public PodSelectionAssert pods() { 26 | spec().isNotNull().selector().isNotNull(); 27 | DeploymentConfigSpec spec = this.actual.getSpec(); 28 | Integer replicas = spec.getReplicas(); 29 | Map matchLabels = spec.getSelector(); 30 | List matchExpressions = new ArrayList<>(); 31 | return new PodSelectionAssert(client, replicas, matchLabels, matchExpressions, "DeploymentConfig " + KubernetesHelper.getName(actual)); 32 | } 33 | } -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/DeploymentPodsAssert.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.KubernetesHelper; 4 | import io.fabric8.kubernetes.api.model.LabelSelector; 5 | import io.fabric8.kubernetes.api.model.LabelSelectorRequirement; 6 | import io.fabric8.kubernetes.api.model.apps.Deployment; 7 | import io.fabric8.kubernetes.api.model.apps.DeploymentAssert; 8 | import io.fabric8.kubernetes.api.model.apps.DeploymentSpec; 9 | import io.fabric8.kubernetes.client.KubernetesClient; 10 | 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | /** 15 | * Adds assertions for asserting that a Deployment starts up correctly etc 16 | */ 17 | public class DeploymentPodsAssert extends DeploymentAssert implements HasPodSelectionAssert { 18 | private final KubernetesClient client; 19 | 20 | public DeploymentPodsAssert(KubernetesClient client, Deployment deployment) { 21 | super(deployment); 22 | this.client = client; 23 | } 24 | 25 | @Override 26 | public PodSelectionAssert pods() { 27 | spec().isNotNull().selector().isNotNull(); 28 | DeploymentSpec spec = this.actual.getSpec(); 29 | Integer replicas = spec.getReplicas(); 30 | LabelSelector selector = spec.getSelector(); 31 | Map matchLabels = selector.getMatchLabels(); 32 | List matchExpressions = selector.getMatchExpressions(); 33 | return new PodSelectionAssert(client, replicas, matchLabels, matchExpressions, "DeploymentConfig " + KubernetesHelper.getName(actual)); 34 | } 35 | } -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/HasMetadatasAssert.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.model.HasMetadata; 4 | import org.assertj.core.api.Condition; 5 | import org.assertj.core.api.IntegerAssert; 6 | import org.assertj.core.api.ObjectAssert; 7 | import org.assertj.core.api.filter.Filters; 8 | import org.assertj.core.util.Lists; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | */ 14 | public abstract class HasMetadatasAssert extends MetadatasListAssert { 15 | public HasMetadatasAssert(List actual) { 16 | super(actual); 17 | } 18 | 19 | protected abstract AI createListAssert(List list); 20 | 21 | protected AI assertThat(Iterable result) { 22 | List list = Lists.newArrayList(result); 23 | return createListAssert(list); 24 | } 25 | 26 | public AI filter(Condition condition) { 27 | return assertThat((Iterable) Filters.filter(actual).having(condition).get()); 28 | } 29 | 30 | /** 31 | * Returns an assertion on the size of the list 32 | */ 33 | public IntegerAssert assertSize() { 34 | return (IntegerAssert) org.assertj.core.api.Assertions.assertThat(get().size()).as("size"); 35 | } 36 | 37 | /** 38 | * Returns the underlying actual value 39 | */ 40 | public List get() { 41 | return (List) actual; 42 | } 43 | 44 | /** 45 | * Asserts that this collection has at least one element and returns the first one 46 | */ 47 | public ObjectAssert first() { 48 | assertSize().isGreaterThan(0); 49 | return new ObjectAssert<>(get().get(0)); 50 | } 51 | 52 | /** 53 | * Asserts that this collection has at least one element and returns the last one 54 | */ 55 | public ObjectAssert last() { 56 | assertSize().isGreaterThan(0); 57 | List list = get(); 58 | return new ObjectAssert<>(list.get(list.size() - 1)); 59 | } 60 | 61 | 62 | /** 63 | * Asserts that this collection has a resource with the given name and return it 64 | * 65 | * @return returns the matching resource 66 | */ 67 | public R hasName(String name) { 68 | return (R) filterName(name).first(); 69 | } 70 | 71 | /** 72 | * Filters the resources by name 73 | */ 74 | public AI filterName(String name) { 75 | return filter((Condition) Conditions.hasName(name)); 76 | } 77 | 78 | /** 79 | * Filters the resources using the given label key and value 80 | */ 81 | public AI filterLabel(String key, String value) { 82 | return filter((Condition) Conditions.hasLabel(key, value)); 83 | } 84 | 85 | /** 86 | * Filters the resources using the given namespace 87 | */ 88 | public AI filterNamespace(String namespace) { 89 | return filter((Condition) Conditions.hasNamespace(namespace)); 90 | } 91 | } -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/HasPodSelectionAssert.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | /** 4 | */ 5 | public interface HasPodSelectionAssert { 6 | AbstractPodSelectionAssert pods(); 7 | } -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/KubernetesAssert.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.Controller; 4 | import io.fabric8.kubernetes.api.KubernetesHelper; 5 | import io.fabric8.kubernetes.api.model.HasMetadata; 6 | import io.fabric8.kubernetes.api.model.Pod; 7 | import io.fabric8.kubernetes.api.model.PodAssert; 8 | import io.fabric8.kubernetes.api.model.PodList; 9 | import io.fabric8.kubernetes.api.model.ReplicationController; 10 | import io.fabric8.kubernetes.api.model.ReplicationControllerAssert; 11 | import io.fabric8.kubernetes.api.model.ReplicationControllerList; 12 | import io.fabric8.kubernetes.api.model.ReplicationControllerListAssert; 13 | import io.fabric8.kubernetes.api.model.Service; 14 | import io.fabric8.kubernetes.api.model.ServiceAssert; 15 | import io.fabric8.kubernetes.api.model.ServiceList; 16 | import io.fabric8.kubernetes.api.model.ServiceListAssert; 17 | import io.fabric8.kubernetes.api.model.ServicePort; 18 | import io.fabric8.kubernetes.api.model.ServiceSpec; 19 | import io.fabric8.kubernetes.api.model.ServiceSpecAssert; 20 | import io.fabric8.kubernetes.api.model.apps.Deployment; 21 | import io.fabric8.kubernetes.api.model.apps.ReplicaSet; 22 | import io.fabric8.kubernetes.client.KubernetesClient; 23 | import io.fabric8.openshift.api.model.DeploymentConfig; 24 | import io.fabric8.openshift.client.OpenShiftAPIGroups; 25 | import io.fabric8.openshift.client.OpenShiftClient; 26 | import org.assertj.core.api.AbstractAssert; 27 | import org.assertj.core.api.ListAssert; 28 | 29 | import java.io.IOException; 30 | import java.util.ArrayList; 31 | import java.util.List; 32 | 33 | import static io.fabric8.kubernetes.assertions.Assertions.assertThat; 34 | import static org.assertj.core.api.Assertions.assertThat; 35 | import static org.assertj.core.api.Assertions.fail; 36 | 37 | 38 | /** 39 | * An assertion class for making assertions about the current pods, services and replication controllers 40 | * using the assertj library 41 | */ 42 | public class KubernetesAssert extends AbstractAssert { 43 | private final KubernetesClient client; 44 | 45 | public KubernetesAssert(KubernetesClient client) { 46 | super(client, KubernetesAssert.class); 47 | this.client = client; 48 | } 49 | 50 | /** 51 | * Creates an asserter using the given namespace 52 | * 53 | * @param namespace namespace name 54 | * @return an asserter using the given namespace 55 | */ 56 | public KubernetesNamespaceAssert namespace(String namespace) { 57 | return new KubernetesNamespaceAssert(client, namespace); 58 | } 59 | 60 | /** 61 | * Finds all the resources that create pod selections (Deployment, DeploymentConfig, ReplicaSet, ReplicationController) 62 | * and create a {@link HasPodSelectionAssert} to make assertions on their pods that they startup etc. 63 | * 64 | * @return the assertion object for the deployment 65 | */ 66 | public HasPodSelectionAssert deployments() { 67 | List asserters = new ArrayList<>(); 68 | List resources = new ArrayList<>(); 69 | try { 70 | resources = KubernetesHelper.findKubernetesResourcesOnClasspath(new Controller(client)); 71 | } catch (IOException e) { 72 | fail("Failed to load kubernetes resources on the classpath: " + e, e); 73 | } 74 | for (HasMetadata resource : resources) { 75 | HasPodSelectionAssert asserter = createPodSelectionAssert(resource); 76 | if (asserter != null) { 77 | asserters.add(asserter); 78 | } 79 | } 80 | String message = "No pod selection kinds found on the classpath such as Deployment, DeploymentConfig, ReplicaSet, ReplicationController"; 81 | // TODO we don't yet support size > 1 82 | assertThat(asserters).describedAs(message).isNotEmpty(); 83 | if( asserters.size() == 1 ) { 84 | return asserters.get(0); 85 | } 86 | return new MultiHasPodSelectionAssert(asserters); 87 | } 88 | 89 | protected HasPodSelectionAssert createPodSelectionAssert(HasMetadata resource) { 90 | if (resource instanceof DeploymentConfig) { 91 | DeploymentConfig deployment = (DeploymentConfig) resource; 92 | return new DeploymentConfigPodsAssert(client, deployment); 93 | } else if (resource instanceof Deployment) { 94 | Deployment deployment = (Deployment) resource; 95 | return new DeploymentPodsAssert(client, deployment); 96 | } else if (resource instanceof ReplicaSet) { 97 | ReplicaSet replica = (ReplicaSet) resource; 98 | return new ReplicaSetPodsAssert(client, replica); 99 | } else if (resource instanceof ReplicationController) { 100 | ReplicationController replica = (ReplicationController) resource; 101 | return new ReplicationControllerPodsAssert(client, replica); 102 | } else { 103 | return null; 104 | } 105 | } 106 | 107 | /** 108 | * Asserts that there is a deployment of the given name 109 | * 110 | * @return the assertion object for the deployment 111 | */ 112 | public HasPodSelectionAssert deployment(String deploymentName) { 113 | String namespace = namespace(); 114 | String qualifiedName = namespace + "." + deploymentName; 115 | OpenShiftClient openShiftClient = new Controller(client).getOpenShiftClientOrNull(); 116 | if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.APPS)) { 117 | DeploymentConfig deployment = openShiftClient.deploymentConfigs().inNamespace(namespace).withName(deploymentName).get(); 118 | assertThat(deployment).describedAs("DeploymentConfig: " + qualifiedName).isNotNull().metadata().name().isEqualTo(deploymentName); 119 | return new DeploymentConfigPodsAssert(client, deployment); 120 | } else { 121 | Deployment deployment = client.extensions().deployments().inNamespace(namespace).withName(deploymentName).get(); 122 | assertThat(deployment).describedAs("Deployment: " + qualifiedName).isNotNull().metadata().name().isEqualTo(deploymentName); 123 | return new DeploymentPodsAssert(client, deployment); 124 | } 125 | } 126 | 127 | public String namespace() { 128 | return client.getNamespace(); 129 | } 130 | 131 | /** 132 | * Asserts that there is a ReplicaSet or ReplicationController of the given name 133 | * 134 | * @return the assertion object for the replicas 135 | */ 136 | public HasPodSelectionAssert replicas(String replicaName) { 137 | String namespace = namespace(); 138 | String qualifiedName = namespace + "." + replicaName; 139 | ReplicaSet replicasSet = client.extensions().replicaSets().withName(replicaName).get(); 140 | if (replicasSet != null) { 141 | assertThat(replicasSet).describedAs("ReplicaSet: " + qualifiedName).metadata().name().isEqualTo(replicaName); 142 | return new ReplicaSetPodsAssert(client, replicasSet); 143 | } else { 144 | ReplicationController replicationController = client.replicationControllers().withName(replicaName).get(); 145 | assertThat(replicationController).describedAs("No ReplicaSet or ReplicationController called: " + qualifiedName).isNotNull(); 146 | assertThat(replicationController).describedAs("ReplicationController: " + qualifiedName).metadata().name().isEqualTo(replicaName); 147 | return new ReplicationControllerPodsAssert(client, replicationController); 148 | } 149 | } 150 | 151 | public PodsAssert podList() { 152 | PodList pods = client.pods().inNamespace(namespace()).list(); 153 | return podList(pods); 154 | } 155 | 156 | public PodsAssert podList(PodList pods) { 157 | assertThat(pods).isNotNull(); 158 | return podList(pods.getItems()); 159 | } 160 | 161 | public PodsAssert podList(List pods) { 162 | assertThat(pods).isNotNull(); 163 | return new PodsAssert(pods, client); 164 | } 165 | 166 | public PodsAssert pods() { 167 | List pods = getPods(namespace()); 168 | return podList(pods); 169 | } 170 | 171 | protected List getPods(String namespace) { 172 | PodList podList = client.pods().inNamespace(namespace).list(); 173 | assertThat(podList).isNotNull(); 174 | List pods = podList.getItems(); 175 | podList(pods).isNotNull(); 176 | return pods; 177 | } 178 | 179 | public ReplicationControllerListAssert replicationControllerList() { 180 | ReplicationControllerList replicationControllers = client.replicationControllers().list(); 181 | return assertThat(replicationControllers).isNotNull(); 182 | } 183 | 184 | public ListAssert replicationControllers() { 185 | ReplicationControllerList replicationControllerList = client.replicationControllers().inNamespace(namespace()).list(); 186 | assertThat(replicationControllerList).isNotNull(); 187 | List replicationControllers = replicationControllerList.getItems(); 188 | return (ListAssert) assertThat(replicationControllers); 189 | } 190 | 191 | public ServiceListAssert serviceList() { 192 | ServiceList serviceList = client.services().inNamespace(namespace()).list(); 193 | return assertThat(serviceList).isNotNull(); 194 | } 195 | 196 | public ServicesAssert services() { 197 | ServiceList serviceList = client.services().inNamespace(namespace()).list(); 198 | assertThat(serviceList).isNotNull(); 199 | List services = serviceList.getItems(); 200 | return new ServicesAssert(client, services); 201 | } 202 | 203 | /** 204 | * Asserts that the given service name exist 205 | * 206 | * @return the assertion object on the given service 207 | */ 208 | public ServicePodsAssert service(String serviceName) { 209 | Service service = client.services().inNamespace(namespace()).withName(serviceName).get(); 210 | assertThat(service).describedAs("No service exists for name: " + serviceName).isNotNull(); 211 | return new ServicePodsAssert(client, service); 212 | } 213 | 214 | /** 215 | * Asserts that we can find the given replication controller and match it to a list of pods, returning the pods for further assertions 216 | */ 217 | public PodsAssert podsForReplicationController(String replicationControllerName) { 218 | ReplicationController replicationController = getReplicationController(replicationControllerName, namespace()); 219 | return podsForReplicationController(replicationController); 220 | } 221 | 222 | /** 223 | * Asserts that we can find the given replication controller and match it to a list of pods, returning the pods for further assertions 224 | */ 225 | public PodsAssert podsForReplicationController(ReplicationController replicationController) { 226 | List allPods = getPods(replicationController.getMetadata().getNamespace()); 227 | List pods = KubernetesHelper.getPodsForReplicationController(replicationController, allPods); 228 | return podList(pods); 229 | } 230 | 231 | 232 | /** 233 | * Asserts that we can find the given service and match it to a list of pods, returning the pods for further assertions 234 | */ 235 | public PodsAssert podsForService(String serviceName) { 236 | Service service = getService(serviceName, namespace()); 237 | return podsForService(service); 238 | } 239 | 240 | /** 241 | * Asserts that we can find the given service and match it to a list of pods, returning the pods for further assertions 242 | */ 243 | public PodsAssert podsForService(Service service) { 244 | List allPods = getPods(service.getMetadata().getNamespace()); 245 | List pods = KubernetesHelper.getPodsForService(service, allPods); 246 | return podList(pods); 247 | } 248 | 249 | /** 250 | * Asserts that the replication controller can be found for the given name 251 | */ 252 | public ReplicationControllerAssert replicationController(String replicationControllerName) { 253 | return assertThat(getReplicationController(replicationControllerName, namespace())); 254 | } 255 | 256 | 257 | protected ReplicationController getReplicationController(String replicationControllerId, String namespace) { 258 | assertThat(replicationControllerId).isNotNull(); 259 | ReplicationController replicationController = null; 260 | try { 261 | replicationController = client.replicationControllers().inNamespace(namespace).withName(replicationControllerId).get(); 262 | } catch (Exception e) { 263 | fail("Could not find replicationController for '" + replicationControllerId + "'"); 264 | } 265 | assertThat(replicationController).isNotNull(); 266 | return replicationController; 267 | } 268 | 269 | /** 270 | * Asserts that the service can be found for the given name and has a port of the given value 271 | */ 272 | public void hasServicePort(String serviceId, int port) { 273 | ServiceSpec spec = getServiceSpec(serviceId, namespace()); 274 | boolean found = false; 275 | List ports = spec.getPorts(); 276 | List portNumbers = new ArrayList<>(); 277 | if (ports != null) { 278 | for (ServicePort servicePort : ports) { 279 | Integer aPort = servicePort.getPort(); 280 | if (aPort != null) { 281 | if (aPort == port) { 282 | found = true; 283 | break; 284 | } else { 285 | portNumbers.add(aPort); 286 | } 287 | 288 | } 289 | } 290 | } 291 | assertThat(found).describedAs("No port found for " + port + " but found ports: " + portNumbers).isTrue(); 292 | } 293 | 294 | 295 | /** 296 | * Asserts that the service spec can be found for the given name 297 | */ 298 | public ServiceSpecAssert serviceSpec(String serviceName) { 299 | return assertThat(getServiceSpec(serviceName, namespace())); 300 | } 301 | 302 | protected Service getService(String serviceId, String namespace) { 303 | assertThat(serviceId).isNotNull(); 304 | Service service = null; 305 | try { 306 | service = client.services().inNamespace(namespace).withName(serviceId).get(); 307 | } catch (Exception e) { 308 | fail("Could not find service for '" + serviceId + "'"); 309 | } 310 | assertThat(service).isNotNull(); 311 | return service; 312 | } 313 | 314 | protected ServiceSpec getServiceSpec(String serviceId, String namespace) { 315 | Service service = getService(serviceId, namespace); 316 | ServiceSpec spec = service.getSpec(); 317 | assertThat(spec).isNotNull(); 318 | return spec; 319 | } 320 | 321 | 322 | /** 323 | * Asserts that the pod can be found for the given name 324 | */ 325 | public PodAssert pod(String podName) { 326 | return assertThat(getPod(podName, namespace())); 327 | } 328 | 329 | protected Pod getPod(String podId, String namespace) { 330 | assertThat(podId).isNotNull(); 331 | Pod pod = null; 332 | try { 333 | pod = client.pods().inNamespace(namespace).withName(podId).get(); 334 | } catch (Exception e) { 335 | fail("Could not find pod for '" + podId + "'"); 336 | } 337 | assertThat(pod).isNotNull(); 338 | return pod; 339 | } 340 | 341 | } 342 | -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/KubernetesNamespaceAssert.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.client.KubernetesClient; 4 | 5 | /** 6 | * Provides a set of assertions for a namespace 7 | */ 8 | public class KubernetesNamespaceAssert extends KubernetesAssert { 9 | private final KubernetesClient client; 10 | private final String namespace; 11 | 12 | public KubernetesNamespaceAssert(KubernetesClient client, String namespace) { 13 | super(client); 14 | this.client = client; 15 | this.namespace = namespace; 16 | } 17 | 18 | @Override 19 | public String namespace() { 20 | return namespace; 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/MetadatasListAssert.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.model.HasMetadata; 4 | import org.assertj.core.api.AbstractListAssert; 5 | import org.assertj.core.api.ObjectAssert; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | public class MetadatasListAssert extends AbstractListAssert, List, T, ObjectAssert> { 11 | 12 | public MetadatasListAssert(List actual) { 13 | super(actual, MetadatasListAssert.class); 14 | } 15 | 16 | @Override 17 | protected ObjectAssert toAssert(T value, String description) { 18 | return new ObjectAssert<>(value).describedAs(description); 19 | } 20 | 21 | @Override 22 | protected MetadatasListAssert newAbstractIterableAssert(Iterable iterable) { 23 | List target = new ArrayList<>(); 24 | iterable.forEach(target::add); 25 | return new MetadatasListAssert<>(target); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/MultiHasPodSelectionAssert.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.model.Pod; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.concurrent.atomic.AtomicReference; 8 | 9 | import static org.assertj.core.api.Assertions.fail; 10 | 11 | /** 12 | * 13 | */ 14 | public class MultiHasPodSelectionAssert implements HasPodSelectionAssert { 15 | private List asserters; 16 | 17 | class MultiPodSelectionAssert extends AbstractPodSelectionAssert { 18 | 19 | @Override 20 | public List getPods() { 21 | List rc = new ArrayList<>(); 22 | for (HasPodSelectionAssert asserter : asserters) { 23 | for (Pod pod : asserter.pods().getPods()) { 24 | rc.add(pod); 25 | } 26 | } 27 | return rc; 28 | } 29 | 30 | @Override 31 | public MultiPodSelectionAssert isPodReadyForPeriod(final long notReadyTimeoutMS, final long readyPeriodMS) { 32 | // Do it in parallel so that this does not take longer and long if we have lots of asserters 33 | final AtomicReference failure = new AtomicReference<>(); 34 | ArrayList threads = new ArrayList<>(asserters.size()); 35 | for (HasPodSelectionAssert a : asserters) { 36 | final HasPodSelectionAssert asserter = a; 37 | Thread thread = new Thread("MultiPodSelectionAssert"){ 38 | @Override 39 | public void run() { 40 | try { 41 | asserter.pods().isPodReadyForPeriod(notReadyTimeoutMS, readyPeriodMS); 42 | } catch (Throwable e) { 43 | failure.set(e); 44 | } 45 | } 46 | }; 47 | thread.start(); 48 | threads.add(thread); 49 | } 50 | for (Thread thread : threads) { 51 | try { 52 | thread.join(); 53 | } catch (InterruptedException e) { 54 | fail("Interrupted: "+e); 55 | } 56 | } 57 | Throwable throwable = failure.get(); 58 | if( throwable!=null ) { 59 | if( throwable instanceof Error ) 60 | throw (Error)throwable; 61 | if( throwable instanceof RuntimeException ) 62 | throw (RuntimeException)throwable; 63 | throw new RuntimeException(throwable); 64 | } 65 | return this; 66 | } 67 | } 68 | 69 | public MultiHasPodSelectionAssert(List asserters) { 70 | this.asserters = asserters; 71 | } 72 | 73 | @Override 74 | public AbstractPodSelectionAssert pods() { 75 | return new MultiPodSelectionAssert(); 76 | } 77 | 78 | } -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/NavigationListAssert.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import org.assertj.core.api.AbstractAssert; 4 | import org.assertj.core.api.AssertFactory; 5 | import org.assertj.core.api.FactoryBasedNavigableListAssert; 6 | 7 | import java.util.List; 8 | 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | 12 | /** 13 | * Provides helper methods for navigating a list property in a generated assertion class 14 | * 15 | */ 16 | public class NavigationListAssert> extends FactoryBasedNavigableListAssert, List, T, EA> { 17 | private final AssertFactory assertFactory; 18 | 19 | public NavigationListAssert(List actual, AssertFactory assertFactory) { 20 | super(actual, NavigationListAssert.class, assertFactory); 21 | this.assertFactory = assertFactory; 22 | } 23 | 24 | /** 25 | * Navigates to the first element in the list if the list is not empty 26 | * 27 | * @return the assertion on the first element 28 | */ 29 | public EA first() { 30 | isNotEmpty(); 31 | return toAssert(actual.get(0), Assertions.joinDescription(this, "first()")); 32 | } 33 | 34 | /** 35 | * Navigates to the last element in the list if the list is not empty 36 | * 37 | * @return the assertion on the last element 38 | */ 39 | public EA last() { 40 | isNotEmpty(); 41 | return toAssert(actual.get(actual.size() - 1), Assertions.joinDescription(this, "last()")); 42 | } 43 | 44 | /** 45 | * Navigates to the element at the given index if the index is within the range of the list 46 | * 47 | * @return the assertion on the given element 48 | */ 49 | public EA item(int index) { 50 | isNotEmpty(); 51 | assertThat(index).describedAs(Assertions.joinDescription(this, "index")).isGreaterThanOrEqualTo(0).isLessThan(actual.size()); 52 | return toAssert(actual.get(index), Assertions.joinDescription(this, "index(" + index + ")")); 53 | } 54 | 55 | public EA toAssert(T value, String description) { 56 | return assertFactory.createAssert(value).describedAs(description); 57 | } 58 | } -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/PodLogsAssert.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.utils.IOHelpers; 4 | import org.assertj.core.api.Fail; 5 | import org.assertj.core.api.MapAssert; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | 9 | import java.io.File; 10 | import java.io.IOException; 11 | import java.util.HashMap; 12 | import java.util.List; 13 | import java.util.Map; 14 | import java.util.Set; 15 | 16 | /** 17 | * Collect the logs from a number of pods so that they can be asserted on 18 | */ 19 | public class PodLogsAssert extends MapAssert { 20 | private static final transient Logger LOG = LoggerFactory.getLogger(PodLogsAssert.class); 21 | 22 | private final String containerName; 23 | private final Map logPrefixes; 24 | 25 | public PodLogsAssert(Map actual, String containerName) { 26 | this(actual, containerName, new HashMap()); 27 | } 28 | 29 | public PodLogsAssert(Map actual, String containerName, Map logPrefixes) { 30 | super(actual); 31 | this.containerName = containerName; 32 | this.logPrefixes = logPrefixes; 33 | writeLogs(); 34 | } 35 | 36 | public PodLogsAssert afterText(String startText) { 37 | Map newLogs = new HashMap<>(); 38 | Map prefixes = new HashMap<>(); 39 | Set> entries = actual.entrySet(); 40 | for (Map.Entry entry : entries) { 41 | String podName = entry.getKey(); 42 | String value = entry.getValue(); 43 | File file = podLogFileName(podName); 44 | int idx = value.indexOf(startText); 45 | if (idx >= 0) { 46 | int splitIdx = idx + startText.length(); 47 | String remaining = value.substring(splitIdx); 48 | String prefix = value.substring(0, splitIdx); 49 | newLogs.put(podName, remaining); 50 | prefixes.put(podName, prefix); 51 | } 52 | } 53 | return new PodLogsAssert(newLogs, containerName, prefixes); 54 | } 55 | 56 | 57 | public void containsText(String... texts) { 58 | for (String text : texts) { 59 | Set> entries = actual.entrySet(); 60 | for (Map.Entry entry : entries) { 61 | String podName = entry.getKey(); 62 | String value = entry.getValue(); 63 | File file = podLogFileName(podName); 64 | int idx = value.indexOf(text); 65 | if (idx < 0) { 66 | Fail.fail("Log of pod " + podName + " in file: " + file + " does not contains text `" + text + "` last log: " + lastLineOf(file)); 67 | } 68 | } 69 | } 70 | } 71 | 72 | protected String lastLineOf(File file) { 73 | try { 74 | List lines = IOHelpers.readLines(file); 75 | int size = lines.size(); 76 | if (size > 0) { 77 | return lines.get(size - 1); 78 | } 79 | } catch (IOException e) { 80 | LOG.debug("Failed to load: " + file + ". " + e, e ); 81 | } 82 | return ""; 83 | } 84 | 85 | public void doesNotContainText(String... texts) { 86 | for (String text : texts) { 87 | Set> entries = actual.entrySet(); 88 | for (Map.Entry entry : entries) { 89 | String podName = entry.getKey(); 90 | String value = entry.getValue(); 91 | File file = podLogFileName(podName); 92 | int idx = value.indexOf(text); 93 | if (idx >= 0) { 94 | Fail.fail("Log of pod " + podName + " in file: " + file + " contains text `" + text 95 | + "` at " + logFileCoords(podName, value, idx)); 96 | } else { 97 | LOG.debug("does not contain '" + text + "' in Log of pod " + podName + " in file: " + file); 98 | } 99 | } 100 | } 101 | } 102 | 103 | public void doesNotContainTextCount(int count, String... texts) { 104 | if (count == 1) { 105 | doesNotContainText(texts); 106 | } 107 | for (String text : texts) { 108 | Set> entries = actual.entrySet(); 109 | for (Map.Entry entry : entries) { 110 | String podName = entry.getKey(); 111 | String value = entry.getValue(); 112 | File file = podLogFileName(podName); 113 | int idx = 0; 114 | for (int i = 0; idx >= 0 && i < count; i++) { 115 | int next = value.indexOf(text, idx); 116 | if (next >= 0) { 117 | idx = next + 1; 118 | } else { 119 | idx = next; 120 | } 121 | } 122 | if (idx >= 0) { 123 | String logText = fullLogText(podName, value.substring(0, idx - 1)); 124 | Fail.fail("Log of pod " + podName + " in file: " + file + " contains text `" + text 125 | + "` " + count + " times with the last at at " + textCoords(logText)); 126 | } else { 127 | LOG.debug("does not contain '" + text + "' in Log of pod " + podName + " in file: " + file + " " + count + " times"); 128 | } 129 | } 130 | } 131 | } 132 | 133 | 134 | /** 135 | * Returns the coordinates in the log file of the end of the bit of text 136 | */ 137 | protected String logFileCoords(String podName, String value, int idx) { 138 | String prefix = ""; 139 | if (value != null && value.length() > 0 && idx > 0) { 140 | prefix = value.substring(0, idx); 141 | } 142 | String logText = fullLogText(podName, prefix); 143 | return textCoords(logText); 144 | } 145 | 146 | protected String fullLogText(String podName, String text) { 147 | String logText = text; 148 | String logPrefix = logPrefixes.get(podName); 149 | if (logPrefix != null) { 150 | logText = logPrefix + logText; 151 | } 152 | return logText; 153 | } 154 | 155 | 156 | /** 157 | * Returns the line number and column of the end of text 158 | */ 159 | public static String textCoords(String text) { 160 | int line = 1; 161 | int idx = 0; 162 | while (true) { 163 | int next =text.indexOf('\n', idx); 164 | if (next < 0) { 165 | break; 166 | } 167 | idx = next + 1; 168 | line += 1; 169 | } 170 | int column = 1 + text.length() - idx; 171 | return "" + line + ":" + column; 172 | } 173 | 174 | protected File podLogFileName(String podName) { 175 | // lets return the file we use to store the pod logs 176 | String basedir = System.getProperty("basedir", "."); 177 | File dir = new File(basedir, "target/fabric8/systest/logs"); 178 | String name = podName; 179 | if (containerName != null) { 180 | name += "." + containerName; 181 | } 182 | name += ".log"; 183 | File answer = new File(dir, name); 184 | answer.getParentFile().mkdirs(); 185 | return answer; 186 | } 187 | 188 | private void writeLogs() { 189 | Set> entries = actual.entrySet(); 190 | for (Map.Entry entry : entries) { 191 | String podName = entry.getKey(); 192 | String value = entry.getValue(); 193 | String logText = fullLogText(podName, value); 194 | File file = podLogFileName(podName); 195 | try { 196 | IOHelpers.writeFully(file, logText); 197 | } catch (IOException e) { 198 | LOG.error("Failed to write log of pod " + podName + " container:" + containerName + " to file: "+ file + ". " + e, e); 199 | } 200 | } 201 | } 202 | } -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/PodSelectionAssert.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.model.Pod; 4 | import io.fabric8.kubernetes.api.model.PodList; 5 | import io.fabric8.kubernetes.api.model.LabelSelectorRequirement; 6 | import io.fabric8.kubernetes.client.KubernetesClient; 7 | import io.fabric8.kubernetes.client.Watch; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import io.fabric8.kubernetes.assertions.support.PodWatcher; 11 | 12 | import java.util.List; 13 | import java.util.Map; 14 | 15 | import static org.assertj.core.api.Java6Assertions.assertThat; 16 | 17 | /** 18 | * Assertion helper for performing assertions on a selection of pods 19 | */ 20 | public class PodSelectionAssert extends AbstractPodSelectionAssert { 21 | 22 | private static final transient Logger LOG = LoggerFactory.getLogger(PodSelectionAssert.class); 23 | 24 | private final KubernetesClient client; 25 | private final Integer replicas; 26 | private final Map matchLabels; 27 | private final List matchExpressions; 28 | private final String description; 29 | 30 | 31 | public PodSelectionAssert(KubernetesClient client, Integer replicas, Map matchLabels, List matchExpressions, String description) { 32 | this.client = client; 33 | this.replicas = replicas; 34 | this.matchLabels = matchLabels; 35 | this.matchExpressions = matchExpressions; 36 | this.description = description; 37 | } 38 | 39 | public KubernetesClient getClient() { 40 | return client; 41 | } 42 | 43 | public String getDescription() { 44 | return description; 45 | } 46 | 47 | public Integer getReplicas() { 48 | return replicas; 49 | } 50 | 51 | public Map getMatchLabels() { 52 | return matchLabels; 53 | } 54 | 55 | public List getMatchExpressions() { 56 | return matchExpressions; 57 | } 58 | 59 | /** 60 | * Asserts that a pod is ready for this deployment all become ready within the given time and that each one keeps being ready for the given time 61 | */ 62 | public PodSelectionAssert isPodReadyForPeriod(long notReadyTimeoutMS, long readyPeriodMS) { 63 | if (replicas.intValue() <= 0) { 64 | LOG.warn("Not that the pod selection for: " + description + " has no replicas defined so we cannot assert there is a pod ready"); 65 | return this; 66 | } 67 | 68 | try (PodWatcher podWatcher = new PodWatcher(this, notReadyTimeoutMS, readyPeriodMS); 69 | Watch watch = client.pods().withLabels(matchLabels).watch(podWatcher); 70 | ) { 71 | podWatcher.loadCurrentPods(); 72 | podWatcher.waitForPodReady(); 73 | 74 | } 75 | return this; 76 | } 77 | 78 | /** 79 | * Loads the current pods for this selection 80 | * 81 | * @return the current pods 82 | */ 83 | public List getPods() { 84 | PodList list = getClient().pods().withLabels(getMatchLabels()).list(); 85 | assertThat(list).describedAs(getDescription() + " pods").isNotNull(); 86 | return list.getItems(); 87 | } 88 | } -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/PodsAssert.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.model.ObjectMeta; 4 | import io.fabric8.kubernetes.api.model.Pod; 5 | import io.fabric8.kubernetes.client.KubernetesClient; 6 | import io.fabric8.utils.Strings; 7 | 8 | import java.util.HashMap; 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | /** 13 | * Adds some extra assertion operations 14 | */ 15 | public class PodsAssert extends HasMetadatasAssert { 16 | private final KubernetesClient client; 17 | 18 | public PodsAssert(List actual, KubernetesClient client) { 19 | super(actual); 20 | this.client = client; 21 | } 22 | 23 | /** 24 | * Returns an assertion on the logs of the pods 25 | */ 26 | public PodLogsAssert logs() { 27 | return logs(null); 28 | } 29 | 30 | /** 31 | * Returns an assertion on the logs of the pods for the given container name 32 | */ 33 | public PodLogsAssert logs(String containerName) { 34 | Map logs = new HashMap<>(); 35 | List pods = get(); 36 | for (Pod pod : pods) { 37 | ObjectMeta metadata = pod.getMetadata(); 38 | if (metadata != null) { 39 | String name = metadata.getName(); 40 | String namespace = metadata.getNamespace(); 41 | String key = name; 42 | if (Strings.isNotBlank(namespace)) { 43 | key = namespace + "/" + name; 44 | } 45 | // TODO deal with more than one container!!! 46 | //String log = client.pods().inNamespace(namespace).withName(name).getLog(containerName, true); 47 | String log = client.pods().inNamespace(namespace).withName(name).getLog(true); 48 | if (log != null) { 49 | logs.put(key, log); 50 | } 51 | } 52 | } 53 | return new PodLogsAssert(logs, containerName); 54 | } 55 | 56 | /** 57 | * Returns the filtered list of pods which have running status 58 | */ 59 | public PodsAssert runningStatus() { 60 | return filter(Conditions.runningStatus()); 61 | } 62 | 63 | /** 64 | * Returns the filtered list of pods which have waiting status 65 | */ 66 | public PodsAssert waitingStatus() { 67 | return filter(Conditions.waitingStatus()); 68 | } 69 | 70 | /** 71 | * Returns the filtered list of pods which have error status 72 | */ 73 | public PodsAssert errorStatus() { 74 | return filter(Conditions.errorStatus()); 75 | } 76 | 77 | @Override 78 | protected PodsAssert createListAssert(List list) { 79 | return new PodsAssert(list, client); 80 | } 81 | } -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/ReplicaSetPodsAssert.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.KubernetesHelper; 4 | import io.fabric8.kubernetes.api.model.LabelSelector; 5 | import io.fabric8.kubernetes.api.model.LabelSelectorRequirement; 6 | import io.fabric8.kubernetes.api.model.apps.ReplicaSet; 7 | import io.fabric8.kubernetes.api.model.apps.ReplicaSetAssert; 8 | import io.fabric8.kubernetes.api.model.apps.ReplicaSetSpec; 9 | import io.fabric8.kubernetes.client.KubernetesClient; 10 | 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | /** 15 | */ 16 | public class ReplicaSetPodsAssert extends ReplicaSetAssert implements HasPodSelectionAssert { 17 | private final KubernetesClient client; 18 | 19 | public ReplicaSetPodsAssert(KubernetesClient client, ReplicaSet replicasSet) { 20 | super(replicasSet); 21 | this.client = client; 22 | } 23 | 24 | 25 | @Override 26 | public PodSelectionAssert pods() { 27 | spec().isNotNull().selector().isNotNull(); 28 | ReplicaSetSpec spec = this.actual.getSpec(); 29 | Integer replicas = spec.getReplicas(); 30 | LabelSelector selector = spec.getSelector(); 31 | Map matchLabels = selector.getMatchLabels(); 32 | List matchExpressions = selector.getMatchExpressions(); 33 | return new PodSelectionAssert(client, replicas, matchLabels, matchExpressions, "ReplicaSet " + KubernetesHelper.getName(actual)); 34 | } 35 | } -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/ReplicationControllerPodsAssert.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.KubernetesHelper; 4 | import io.fabric8.kubernetes.api.model.ReplicationController; 5 | import io.fabric8.kubernetes.api.model.ReplicationControllerAssert; 6 | import io.fabric8.kubernetes.api.model.ReplicationControllerSpec; 7 | import io.fabric8.kubernetes.api.model.LabelSelectorRequirement; 8 | import io.fabric8.kubernetes.client.KubernetesClient; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | /** 15 | */ 16 | public class ReplicationControllerPodsAssert extends ReplicationControllerAssert implements HasPodSelectionAssert { 17 | private final KubernetesClient client; 18 | 19 | public ReplicationControllerPodsAssert(KubernetesClient client, ReplicationController replicationController) { 20 | super(replicationController); 21 | this.client = client; 22 | } 23 | 24 | public PodSelectionAssert pods() { 25 | spec().isNotNull().selector().isNotNull(); 26 | ReplicationControllerSpec spec = this.actual.getSpec(); 27 | Integer replicas = spec.getReplicas(); 28 | Map matchLabels = spec.getSelector(); 29 | List matchExpressions = new ArrayList<>(); 30 | return new PodSelectionAssert(client, replicas, matchLabels, matchExpressions, "ReplicationController " + KubernetesHelper.getName(actual)); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/ServicePodsAssert.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.KubernetesHelper; 4 | import io.fabric8.kubernetes.api.model.Endpoints; 5 | import io.fabric8.kubernetes.api.model.EndpointsList; 6 | import io.fabric8.kubernetes.api.model.Service; 7 | import io.fabric8.kubernetes.api.model.ServiceAssert; 8 | import io.fabric8.kubernetes.api.model.ServiceSpec; 9 | import io.fabric8.kubernetes.api.model.LabelSelector; 10 | import io.fabric8.kubernetes.api.model.LabelSelectorRequirement; 11 | import io.fabric8.kubernetes.client.KubernetesClient; 12 | 13 | import java.util.List; 14 | import java.util.Map; 15 | 16 | import static io.fabric8.kubernetes.assertions.AbstractPodSelectionAssert.getDefaultNotReadyTimeoutMs; 17 | import static io.fabric8.kubernetes.assertions.AbstractPodSelectionAssert.getDefaultReadyPeriodMs; 18 | import static io.fabric8.kubernetes.api.KubernetesHelper.getLabels; 19 | 20 | /** 21 | * Adds assertions for asserting that a Service has ready pods etc 22 | */ 23 | public class ServicePodsAssert extends ServiceAssert implements HasPodSelectionAssert { 24 | private final KubernetesClient client; 25 | 26 | public ServicePodsAssert(KubernetesClient client, Service actual) { 27 | super(actual); 28 | this.client = client; 29 | } 30 | 31 | @Override 32 | public PodSelectionAssert pods() { 33 | spec().isNotNull().selector().isNotNull(); 34 | ServiceSpec spec = this.actual.getSpec(); 35 | int replicas = 1; 36 | LabelSelector selector = null; 37 | Map matchLabels = spec.getSelector(); 38 | List matchExpressions = selector.getMatchExpressions(); 39 | return new PodSelectionAssert(client, replicas, matchLabels, matchExpressions, "Service " + KubernetesHelper.getName(actual)); 40 | } 41 | 42 | 43 | /** 44 | * Asserts that either this service has a valid Endpoint or that a pod is Ready for a period of time 45 | */ 46 | public ServicePodsAssert hasEndpointOrReadyPod() { 47 | return hasEndpointOrReadyPod(getDefaultNotReadyTimeoutMs(), getDefaultReadyPeriodMs()); 48 | } 49 | 50 | /** 51 | * Asserts that either this service has a valid Endpoint or that a pod is Ready for a period of time 52 | */ 53 | public ServicePodsAssert hasEndpointOrReadyPod(long notReadyTimeoutMS, long readyPeriodMS) { 54 | EndpointsList list = client.endpoints().withLabels(getLabels(actual)).list(); 55 | if (list != null) { 56 | List items = list.getItems(); 57 | if (items.size() > 0) { 58 | return this; 59 | } 60 | } 61 | pods().isPodReadyForPeriod(notReadyTimeoutMS, readyPeriodMS); 62 | return this; 63 | } 64 | } -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/ServicesAssert.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.model.Service; 4 | import io.fabric8.kubernetes.client.KubernetesClient; 5 | import org.assertj.core.api.ListAssert; 6 | 7 | import java.util.List; 8 | import java.util.Objects; 9 | 10 | import static io.fabric8.kubernetes.api.KubernetesHelper.getName; 11 | import static org.assertj.core.api.Assertions.assertThat; 12 | 13 | /** 14 | */ 15 | public class ServicesAssert extends ListAssert { 16 | private final KubernetesClient client; 17 | private final List actual; 18 | 19 | public ServicesAssert(KubernetesClient client, List actual) { 20 | super(actual); 21 | this.client = client; 22 | this.actual = actual; 23 | } 24 | 25 | public ServicesAssert assertAllServicesHaveEndpointOrReadyPod() { 26 | for (Service service : actual) { 27 | ServicePodsAssert asserter = new ServicePodsAssert(client, service); 28 | asserter.hasEndpointOrReadyPod(); 29 | } 30 | return this; 31 | } 32 | 33 | /** 34 | * Asserts that the given service name exist 35 | * 36 | * @return the assertion object on the given service 37 | */ 38 | public ServicePodsAssert service(String serviceName) { 39 | Service service = null; 40 | for (Service aService : actual) { 41 | String name = getName(aService); 42 | if (Objects.equals(name, serviceName)) { 43 | service = aService; 44 | } 45 | } 46 | assertThat(service).describedAs("No service could be found for name: " + serviceName).isNotNull(); 47 | return new ServicePodsAssert(client, service); 48 | } 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/support/LogHelpers.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions.support; 2 | 3 | import io.fabric8.kubernetes.api.model.Container; 4 | import io.fabric8.kubernetes.api.model.ContainerStatus; 5 | import io.fabric8.kubernetes.api.model.Pod; 6 | import io.fabric8.kubernetes.api.model.PodStatus; 7 | 8 | import java.io.File; 9 | import java.util.List; 10 | 11 | /** 12 | */ 13 | public class LogHelpers { 14 | public static final String LOG_FILE_POSTFIX = ".log"; 15 | 16 | public static File getLogFileName(File baseDir, String podName, Container container, int restartCount) { 17 | File logDir = new File(baseDir, "target/test-pod-logs/"); 18 | String containerName = container.getName(); 19 | String restartCountText = ""; 20 | if (restartCount > 0) { 21 | restartCountText = "-" + restartCount; 22 | } 23 | String logFileName = podName + "-" + containerName + restartCountText + LOG_FILE_POSTFIX; 24 | File logFile = new File(logDir, logFileName); 25 | logFile.getParentFile().mkdirs(); 26 | return logFile; 27 | } 28 | 29 | public static int getRestartCount(Pod pod) { 30 | int restartCount = 0; 31 | PodStatus podStatus = pod.getStatus(); 32 | if (podStatus != null) { 33 | List containerStatuses = podStatus.getContainerStatuses(); 34 | for (ContainerStatus containerStatus : containerStatuses) { 35 | if (restartCount == 0) { 36 | Integer restartCountValue = containerStatus.getRestartCount(); 37 | if (restartCountValue != null) { 38 | restartCount = restartCountValue.intValue(); 39 | } 40 | } 41 | } 42 | } 43 | return restartCount; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/support/PodAsserter.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions.support; 2 | 3 | import io.fabric8.kubernetes.api.KubernetesHelper; 4 | import io.fabric8.kubernetes.api.model.ObjectMeta; 5 | import io.fabric8.kubernetes.api.model.Pod; 6 | import io.fabric8.kubernetes.client.KubernetesClient; 7 | import io.fabric8.utils.Strings; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | 11 | import java.io.Closeable; 12 | import java.util.Timer; 13 | import java.util.TimerTask; 14 | 15 | import static org.fusesource.jansi.Ansi.Color.YELLOW; 16 | import static org.fusesource.jansi.Ansi.ansi; 17 | 18 | /** 19 | */ 20 | public class PodAsserter implements Closeable { 21 | private static final transient Logger LOG = LoggerFactory.getLogger(PodAsserter.class); 22 | 23 | private final PodWatcher watcher; 24 | private final String name; 25 | private final Pod pod; 26 | private Timer timer; 27 | 28 | private TimerTask task = new TimerTask() { 29 | @Override 30 | public void run() { 31 | watcher.podIsReadyForEntireDuration(name, pod); 32 | } 33 | }; 34 | 35 | public PodAsserter(PodWatcher watcher, String name, Pod pod) { 36 | this.watcher = watcher; 37 | this.name = name; 38 | this.pod = pod; 39 | updated(pod); 40 | } 41 | 42 | public void close() { 43 | cancelTimer(); 44 | } 45 | 46 | public void updated(Pod pod) { 47 | String statusText = KubernetesHelper.getPodStatusText(pod); 48 | boolean ready = KubernetesHelper.isPodReady(pod); 49 | 50 | String message = "Pod " + name + " has status: " + statusText + " isReady: " + ready; 51 | LOG.info(ansi().fg(YELLOW).a(message).reset().toString()); 52 | 53 | if (ready) { 54 | watcher.podIsReady(name, pod); 55 | 56 | if (timer == null) { 57 | timer = new Timer(watcher.getDescription()); 58 | timer.schedule(task, watcher.getReadyPeriodMS()); 59 | } 60 | } else { 61 | cancelTimer(); 62 | } 63 | } 64 | 65 | protected void cancelTimer() { 66 | if (timer != null) { 67 | timer.cancel(); 68 | timer = null; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/support/PodLogWatcher.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions.support; 2 | 3 | import io.fabric8.kubernetes.api.model.Container; 4 | import io.fabric8.kubernetes.api.model.ObjectMeta; 5 | import io.fabric8.kubernetes.api.model.Pod; 6 | import io.fabric8.kubernetes.api.model.PodSpec; 7 | import io.fabric8.kubernetes.client.KubernetesClient; 8 | import io.fabric8.kubernetes.client.dsl.LogWatch; 9 | 10 | import java.io.Closeable; 11 | import java.io.File; 12 | import java.io.FileNotFoundException; 13 | import java.io.FileOutputStream; 14 | import java.io.IOException; 15 | import java.util.List; 16 | 17 | /** 18 | */ 19 | public class PodLogWatcher implements Closeable { 20 | private final LogWatch logWatch; 21 | 22 | public PodLogWatcher(PodWatcher podWatcher, String name, Pod pod, String containerName, File logFile) throws FileNotFoundException { 23 | KubernetesClient client = podWatcher.getClient(); 24 | ObjectMeta metadata = pod.getMetadata(); 25 | logFile.getParentFile().mkdirs(); 26 | PodSpec spec = pod.getSpec(); 27 | this.logWatch = client.pods().inNamespace(metadata.getNamespace()).withName(name).inContainer(containerName).watchLog(new FileOutputStream(logFile)); 28 | } 29 | 30 | @Override 31 | public void close() throws IOException { 32 | if (logWatch != null) { 33 | logWatch.close(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/io/fabric8/kubernetes/assertions/support/PodWatcher.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions.support; 2 | 3 | import io.fabric8.kubernetes.assertions.PodSelectionAssert; 4 | import io.fabric8.kubernetes.api.KubernetesHelper; 5 | import io.fabric8.kubernetes.api.model.Container; 6 | import io.fabric8.kubernetes.api.model.Pod; 7 | import io.fabric8.kubernetes.api.model.PodSpec; 8 | import io.fabric8.kubernetes.client.KubernetesClient; 9 | import io.fabric8.kubernetes.client.KubernetesClientException; 10 | import io.fabric8.kubernetes.client.Watcher; 11 | import org.slf4j.Logger; 12 | import org.slf4j.LoggerFactory; 13 | 14 | import java.io.Closeable; 15 | import java.io.File; 16 | import java.io.IOException; 17 | import java.util.List; 18 | import java.util.Map; 19 | import java.util.Set; 20 | import java.util.concurrent.ConcurrentHashMap; 21 | import java.util.concurrent.CountDownLatch; 22 | import java.util.concurrent.TimeUnit; 23 | 24 | import static io.fabric8.kubernetes.assertions.support.LogHelpers.getRestartCount; 25 | import static io.fabric8.kubernetes.api.KubernetesHelper.getName; 26 | import static org.assertj.core.api.Assertions.fail; 27 | import static org.fusesource.jansi.Ansi.Color.GREEN; 28 | import static org.fusesource.jansi.Ansi.ansi; 29 | 30 | /** 31 | */ 32 | public class PodWatcher implements Watcher, Closeable { 33 | private static final transient Logger LOG = LoggerFactory.getLogger(PodWatcher.class); 34 | 35 | private final PodSelectionAssert podSelectionAssert; 36 | private final long readyTimeoutMS; 37 | private final long readyPeriodMS; 38 | private Map podAsserts = new ConcurrentHashMap<>(); 39 | private Map podLogWatchers = new ConcurrentHashMap<>(); 40 | private CountDownLatch podReady = new CountDownLatch(1); 41 | private CountDownLatch podReadyForEntireDuration = new CountDownLatch(1); 42 | private File basedir; 43 | 44 | public PodWatcher(PodSelectionAssert podSelectionAssert, long readyTimeoutMS, long readyPeriodMS) { 45 | this.podSelectionAssert = podSelectionAssert; 46 | this.readyTimeoutMS = readyTimeoutMS; 47 | this.readyPeriodMS = readyPeriodMS; 48 | } 49 | 50 | public KubernetesClient getClient() { 51 | return podSelectionAssert.getClient(); 52 | } 53 | 54 | public String getDescription() { 55 | return podSelectionAssert.getDescription(); 56 | } 57 | 58 | public long getReadyTimeoutMS() { 59 | return readyTimeoutMS; 60 | } 61 | 62 | public long getReadyPeriodMS() { 63 | return readyPeriodMS; 64 | } 65 | 66 | /** 67 | * Lets load the current pods as we don't get watch events for current pods 68 | */ 69 | public void loadCurrentPods() { 70 | List pods = podSelectionAssert.getPods(); 71 | for (Pod pod : pods) { 72 | String name = getName(pod); 73 | if (!podAsserts.containsKey(name)) { 74 | onPod(name, pod); 75 | } 76 | } 77 | } 78 | 79 | @Override 80 | public void eventReceived(Action action, Pod pod) { 81 | String name = getName(pod); 82 | if (action.equals(Action.ERROR)) { 83 | LOG.warn("Got error for pod " + name); 84 | } else if (action.equals(Action.DELETED)) { 85 | closeCloser(name, this.podAsserts); 86 | closeCloser(name, this.podLogWatchers); 87 | } else { 88 | onPod(name, pod); 89 | } 90 | 91 | } 92 | 93 | protected void onPod(String name, Pod pod) { 94 | PodAsserter asserter = podAsserts.get(name); 95 | if (asserter == null) { 96 | asserter = new PodAsserter(this, name, pod); 97 | podAsserts.put(name, asserter); 98 | } else { 99 | asserter.updated(pod); 100 | } 101 | int restartCount = getRestartCount(pod); 102 | PodSpec spec = pod.getSpec(); 103 | if (spec != null) { 104 | if (KubernetesHelper.isPodRunning(pod)) { 105 | List containers = spec.getContainers(); 106 | for (Container container : containers) { 107 | File logFileName = LogHelpers.getLogFileName(getBaseDir(), name, container, restartCount); 108 | String key = logFileName.getName(); 109 | PodLogWatcher logWatcher = podLogWatchers.get(key); 110 | if (logWatcher == null) { 111 | try { 112 | String containerName = container.getName(); 113 | logWatcher = new PodLogWatcher(this, name, pod, containerName, logFileName); 114 | podLogWatchers.put(key, logWatcher); 115 | LOG.info("Watching pod " + name + " container " + containerName + " log at file: " + logFileName.getAbsolutePath()); 116 | } catch (Exception e) { 117 | LOG.warn("Failed to create PodLogWatcher: " + e, e); 118 | } 119 | } 120 | } 121 | } 122 | } 123 | File yamlFile = new File(getBaseDir(), "target/test-pod-status/" + name + ".yml"); 124 | yamlFile.getParentFile().mkdirs(); 125 | try { 126 | KubernetesHelper.saveYaml(pod, yamlFile); 127 | } catch (IOException e) { 128 | LOG.warn("Failed to write " + yamlFile + ". " + e, e); 129 | } 130 | } 131 | 132 | public File getBaseDir() { 133 | if (basedir == null) { 134 | basedir = new File(System.getProperty("basedir", ".")); 135 | } 136 | return basedir; 137 | } 138 | 139 | public void setBasedir(File basedir) { 140 | this.basedir = basedir; 141 | } 142 | 143 | @Override 144 | public void onClose(KubernetesClientException e) { 145 | LOG.info("onClose: " + e); 146 | } 147 | 148 | public void close() { 149 | closeAllClosers(podAsserts); 150 | closeAllClosers(podLogWatchers); 151 | } 152 | 153 | protected void closeAllClosers(Map closers) { 154 | while (!closers.isEmpty()) { 155 | Set keys = closers.keySet(); 156 | for (String key : keys) { 157 | closeCloser(key, closers); 158 | } 159 | } 160 | } 161 | 162 | private void closeCloser(String name, Map closers) { 163 | Closeable closer = closers.remove(name); 164 | if (closer != null) { 165 | try { 166 | closer.close(); 167 | } catch (Exception e) { 168 | LOG.warn("Failed to close " + closer + ". " + e, e); 169 | } 170 | } 171 | } 172 | 173 | public void waitForPodReady() { 174 | boolean ready = false; 175 | try { 176 | ready = podReady.await(readyTimeoutMS, TimeUnit.MILLISECONDS); 177 | } catch (InterruptedException e) { 178 | LOG.warn("Interupted waiting for podReady: " + e); 179 | ready = podReady.getCount() == 0L; 180 | } 181 | if (!ready) { 182 | fail(podSelectionAssert.getDescription() + " did not have a pod become Ready within " + readyTimeoutMS + " millis"); 183 | } 184 | 185 | try { 186 | ready = podReadyForEntireDuration.await(readyPeriodMS * 2, TimeUnit.MILLISECONDS); 187 | } catch (InterruptedException e) { 188 | LOG.warn("Interupted waiting for podReadyForEntireDuration: " + e); 189 | ready = podReadyForEntireDuration.getCount() == 0L; 190 | } 191 | if (!ready) { 192 | fail(podSelectionAssert.getDescription() + " did not have a pod Ready for a duration of " + readyPeriodMS + " millis"); 193 | } 194 | } 195 | 196 | public void podIsReadyForEntireDuration(String name, Pod pod) { 197 | String message = "Pod " + name + " has been Ready now for " + getReadyPeriodMS() + " millis!"; 198 | LOG.info(ansi().fg(GREEN).a(message).reset().toString()); 199 | podReadyForEntireDuration.countDown(); 200 | } 201 | 202 | public void podIsReady(String name, Pod pod) { 203 | if (podReady.getCount() > 0) { 204 | String message = "Pod " + name + " is Ready!"; 205 | LOG.info(ansi().fg(GREEN).a(message).reset().toString()); 206 | podReady.countDown(); 207 | } 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/kubernetes/assertions/AssertionTests.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static io.fabric8.kubernetes.assertions.Assertions.assertThat; 6 | 7 | class AssertionTests extends TestBase { 8 | 9 | @Test 10 | void testDeploymentPodIsReadyForPeriod() { 11 | assertThat(kClient).deployments().pods().isPodReadyForPeriod(); 12 | } 13 | 14 | @Test 15 | void testAllServicesHaveEndpointOrReadyPod() { 16 | assertThat(kClient).services().assertAllServicesHaveEndpointOrReadyPod(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/kubernetes/assertions/ConditionsTests.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.KubernetesHelper; 4 | import io.fabric8.kubernetes.api.PodStatusType; 5 | import io.fabric8.kubernetes.api.model.Pod; 6 | import org.assertj.core.api.Condition; 7 | import org.junit.jupiter.api.Test; 8 | 9 | import java.util.Objects; 10 | 11 | import static io.fabric8.kubernetes.assertions.Conditions.matchesLabel; 12 | import static org.assertj.core.api.Assertions.assertThat; 13 | 14 | class ConditionsTests { 15 | @Test 16 | void testHasLabel() { 17 | final String key = "someKey", value = "someValue"; 18 | Condition expectedCondition = new Condition() { 19 | @Override 20 | public String toString() { 21 | return "hasLabel(" + key + " = " + value + ")"; 22 | } 23 | 24 | @Override 25 | public boolean matches(Pod resource) { 26 | return matchesLabel(resource.getMetadata().getLabels(), key, value); 27 | } 28 | }; 29 | 30 | Condition actualCondition = Conditions.hasLabel(key, value); 31 | assertThat(expectedCondition.toString()).isEqualTo(actualCondition.toString()); 32 | } 33 | 34 | @Test 35 | void testHasName() { 36 | final String name = "podName"; 37 | Condition expectedCondition = new Condition() { 38 | @Override 39 | public String toString() { 40 | return "hasName(" + name + ")"; 41 | } 42 | 43 | @Override 44 | public boolean matches(Pod resource) { 45 | return Objects.equals(name, resource.getMetadata().getName()); 46 | } 47 | }; 48 | 49 | Condition actualCondition = Conditions.hasName(name); 50 | assertThat(expectedCondition.toString()).isEqualTo(actualCondition.toString()); 51 | } 52 | 53 | @Test 54 | void testHasNamespace() { 55 | final String namespace = "default"; 56 | Condition expectedCondition = new Condition() { 57 | @Override 58 | public String toString() { 59 | return "hasNamespace(" + namespace + ")"; 60 | } 61 | 62 | @Override 63 | public boolean matches(Pod resource) { 64 | return Objects.equals(namespace, resource.getMetadata().getNamespace()); 65 | } 66 | }; 67 | 68 | Condition actualCondition = Conditions.hasNamespace(namespace); 69 | assertThat(expectedCondition.toString()).isEqualTo(actualCondition.toString()); 70 | } 71 | 72 | @Test 73 | void testStatus() { 74 | PodStatusType status = PodStatusType.OK; 75 | Condition actualCondition = Conditions.status(status); 76 | assertPodStatus(status, actualCondition); 77 | } 78 | 79 | @Test 80 | void testRunningStatus() { 81 | Condition actualCondition = Conditions.runningStatus(); 82 | assertPodStatus(PodStatusType.OK, actualCondition); 83 | } 84 | 85 | @Test 86 | void testWaitingStatus() { 87 | Condition actualCondition = Conditions.waitingStatus(); 88 | assertPodStatus(PodStatusType.WAIT, actualCondition); 89 | } 90 | 91 | @Test 92 | void testErrorStatus() { 93 | Condition actualCondition = Conditions.errorStatus(); 94 | assertPodStatus(PodStatusType.ERROR, actualCondition); 95 | } 96 | 97 | private void assertPodStatus(final PodStatusType status, Condition actualCondition) { 98 | Condition expectedCondition = new Condition() { 99 | @Override 100 | public String toString() { 101 | return "podStatus(" + status + ")"; 102 | } 103 | 104 | @Override 105 | public boolean matches(Pod pod) { 106 | return Objects.equals(status, KubernetesHelper.getPodStatus(pod)); 107 | } 108 | }; 109 | assertThat(expectedCondition.toString()).isEqualTo(actualCondition.toString()); 110 | } 111 | 112 | 113 | } 114 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/kubernetes/assertions/DeploymentConfigPodsAssertTests.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.openshift.api.model.DeploymentConfig; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.assertj.core.api.Assertions.assertThat; 7 | 8 | public class DeploymentConfigPodsAssertTests extends TestBase { 9 | @Test 10 | void testPods() { 11 | DeploymentConfigPodsAssert deploymentConfigPodsAssert = new DeploymentConfigPodsAssert(kClient, new DeploymentConfig()); 12 | assertThat(deploymentConfigPodsAssert.pods()).isNotNull(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/kubernetes/assertions/DeploymentPodsAssertTests.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.model.apps.Deployment; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.assertj.core.api.Assertions.assertThat; 7 | 8 | public class DeploymentPodsAssertTests extends TestBase { 9 | @Test 10 | void testPods() { 11 | DeploymentPodsAssert deploymentPodsAssert = new DeploymentPodsAssert(kClient, new Deployment()); 12 | assertThat(deploymentPodsAssert.pods()).isNotNull(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/kubernetes/assertions/Example.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.client.DefaultKubernetesClient; 4 | import io.fabric8.kubernetes.client.KubernetesClient; 5 | import io.fabric8.kubernetes.client.KubernetesClientException; 6 | import io.fabric8.utils.Block; 7 | 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | import static io.fabric8.kubernetes.assertions.Assertions.assertThat; 12 | import static io.fabric8.utils.Asserts.assertAssertionError; 13 | 14 | public class Example { 15 | public static void main(String[] args) { 16 | try { 17 | final KubernetesClient client = new DefaultKubernetesClient(); 18 | 19 | 20 | assertThat(client).pods().runningStatus().hasSize(6); 21 | assertThat(client).pods().runningStatus().filterLabel("provider", "fabric8").assertSize().isGreaterThan(0); 22 | 23 | assertThat(client.services().inNamespace("default").withName("fabric8").get().getMetadata()).name().isEqualTo("fabric8"); 24 | 25 | Map consoleLabels = new HashMap<>(); 26 | consoleLabels.put("component", "console"); 27 | consoleLabels.put("provider", "fabric8"); 28 | 29 | assertThat(client).podsForService("fabric8").runningStatus().extracting("metadata").extracting("labels").contains(consoleLabels); 30 | assertThat(client).podsForService("fabric8").runningStatus().hasSize(1).extracting("metadata").extracting("labels").contains(consoleLabels); 31 | 32 | 33 | assertThat(client).podsForService("fabric8").logs().doesNotContainText("Exception", "Error"); 34 | assertThat(client).pods().logs().doesNotContainText("Exception", "Error"); 35 | 36 | assertAssertionError(new Block() { 37 | @Override 38 | public void invoke() throws Exception { 39 | try { 40 | assertThat(client.services().inNamespace("default").withName("doesNotExist").get().getMetadata()).name().isEqualTo("fabric8-console-controller"); 41 | } catch (KubernetesClientException e) { 42 | if (e.getCode() != 404) { 43 | throw e; 44 | } else { 45 | throw new AssertionError(e); 46 | } 47 | } 48 | } 49 | }); 50 | 51 | assertAssertionError(new Block() { 52 | @Override 53 | public void invoke() throws Exception { 54 | try { 55 | assertThat(client).pods().runningStatus().filterLabel("component", "doesNotExist").hasSize(1); 56 | } catch (KubernetesClientException e) { 57 | if (e.getCode() != 404) { 58 | throw e; 59 | } else { 60 | throw new AssertionError(e); 61 | } 62 | } 63 | } 64 | }); 65 | 66 | 67 | System.out.println("Done!"); 68 | } catch (Throwable e) { 69 | System.out.println("Caught: " + e); 70 | e.printStackTrace(); 71 | } 72 | } 73 | } -------------------------------------------------------------------------------- /src/test/java/io/fabric8/kubernetes/assertions/HasMetadatasAssertTests.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.model.HasMetadata; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | public class HasMetadatasAssertTests { 12 | HasMetadatasAssert expectedMetadatasAssert = new HasMetadatasAssert(new ArrayList()) { 13 | @Override 14 | protected HasMetadatasAssert createListAssert(List list) { 15 | return this; 16 | } 17 | }; 18 | 19 | @Test 20 | void testAssertThat() { 21 | HasMetadatasAssert actualAssert = expectedMetadatasAssert.assertThat(expectedMetadatasAssert.get()); 22 | assertThat(actualAssert.get()).isEqualTo(expectedMetadatasAssert.get()); 23 | } 24 | 25 | @Test 26 | void testAssertSize() { 27 | expectedMetadatasAssert.assertSize().isEqualTo(0); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/kubernetes/assertions/KubernetesAssertTests.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.model.Pod; 4 | import io.fabric8.openshift.api.model.DeploymentConfig; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import java.util.ArrayList; 8 | 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | public class KubernetesAssertTests extends TestBase { 12 | 13 | 14 | @Test 15 | void testNamespace() { 16 | String namespace = "default"; 17 | KubernetesNamespaceAssert expected = new KubernetesNamespaceAssert(kClient, namespace); 18 | KubernetesNamespaceAssert actual = kAssert.namespace(namespace); 19 | assertThat(expected.toString()).isEqualTo(actual.toString()); 20 | } 21 | 22 | @Test 23 | void testDeployments() { 24 | HasPodSelectionAssert deployments = kAssert.deployments(); 25 | assertThat(deployments).isNotNull(); 26 | } 27 | 28 | @Test 29 | void testCreatePodSelectionAssert() { 30 | DeploymentConfig deploymentConfig = new DeploymentConfig(); 31 | DeploymentConfigPodsAssert expected = new DeploymentConfigPodsAssert(kClient, deploymentConfig); 32 | HasPodSelectionAssert actual = kAssert.createPodSelectionAssert(deploymentConfig); 33 | assertThat(expected.toString()).isEqualTo(actual.toString()); 34 | } 35 | 36 | @Test 37 | void testReplicas() { 38 | String replicaName = "test"; 39 | HasPodSelectionAssert actual = kAssert.replicas(replicaName); 40 | assertThat(actual).isNull(); 41 | } 42 | 43 | @Test 44 | void testPodList() { 45 | PodsAssert expected = new PodsAssert(new ArrayList(), kClient); 46 | PodsAssert actual = kAssert.podList(); 47 | assertThat(expected.size()).isEqualTo(actual.size()); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/kubernetes/assertions/KubernetesNamespaceAssertTests.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.assertj.core.api.Assertions.assertThat; 6 | 7 | public class KubernetesNamespaceAssertTests extends TestBase { 8 | @Test 9 | void testNamespace() { 10 | String namespace = "default"; 11 | KubernetesNamespaceAssert kNamespaceAssert = new KubernetesNamespaceAssert(kClient, namespace); 12 | assertThat(namespace).isEqualTo(kNamespaceAssert.namespace()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/kubernetes/assertions/MetadatasListAssertTests.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.openshift.api.model.DeploymentConfig; 4 | import org.assertj.core.api.ObjectAssert; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import java.util.ArrayList; 8 | 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | public class MetadatasListAssertTests { 12 | @Test 13 | void testToAssert() { 14 | MetadatasListAssert metadatasListAssert = new MetadatasListAssert<>(new ArrayList()); 15 | ObjectAssert oAssert = metadatasListAssert.toAssert(new DeploymentConfig(), "test"); 16 | assertThat(oAssert.descriptionText()).isEqualTo("test"); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/kubernetes/assertions/NavigationAssertionTests.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.model.ObjectMeta; 4 | import io.fabric8.kubernetes.api.model.Pod; 5 | import io.fabric8.kubernetes.api.model.PodList; 6 | import io.fabric8.kubernetes.api.model.ReplicationController; 7 | import io.fabric8.utils.Block; 8 | import org.junit.jupiter.api.Test; 9 | 10 | import java.util.ArrayList; 11 | import java.util.Arrays; 12 | import java.util.HashMap; 13 | import java.util.Map; 14 | 15 | import static io.fabric8.kubernetes.assertions.Assertions.assertThat; 16 | import static io.fabric8.utils.Asserts.assertAssertionError; 17 | 18 | 19 | /** 20 | */ 21 | class NavigationAssertionTests { 22 | 23 | @Test 24 | void testNavigationAssertions() throws Exception { 25 | String expectedId = "abc"; 26 | Map expectedLabels = new HashMap<>(); 27 | expectedLabels.put("foo", "bar"); 28 | 29 | final Pod pod = new Pod(); 30 | pod.setMetadata(new ObjectMeta()); 31 | pod.getMetadata().setName(expectedId); 32 | pod.getMetadata().setLabels(expectedLabels); 33 | 34 | 35 | assertThat(pod).metadata().name().isEqualTo(expectedId); 36 | assertThat(pod).metadata().labels().isEqualTo(expectedLabels); 37 | 38 | assertAssertionError(new Block() { 39 | @Override 40 | public void invoke() throws Exception { 41 | assertThat(pod).metadata().name().isEqualTo("cheese"); 42 | } 43 | }); 44 | 45 | assertAssertionError(new Block() { 46 | @Override 47 | public void invoke() throws Exception { 48 | assertThat(pod).describedAs("my pod").metadata().name().isEqualTo("cheese"); 49 | } 50 | }); 51 | 52 | assertAssertionError(new Block() { 53 | @Override 54 | public void invoke() throws Exception { 55 | Map wrongLabels = new HashMap<>(); 56 | wrongLabels.put("bar", "whatnot"); 57 | assertThat(pod).metadata().labels().isEqualTo(wrongLabels); 58 | } 59 | }); 60 | } 61 | 62 | @Test 63 | void testNullNavigationOnPod() throws Exception { 64 | final Pod pod = new Pod(); 65 | pod.setMetadata(null); 66 | 67 | assertAssertionError(new Block() { 68 | @Override 69 | public void invoke() throws Exception { 70 | assertThat(pod).metadata().name().isEqualTo("cheese"); 71 | } 72 | }); 73 | } 74 | 75 | @Test 76 | void testNullNavigationOnRC() throws Exception { 77 | final ReplicationController rc = new ReplicationController(); 78 | 79 | assertAssertionError(new Block() { 80 | @Override 81 | public void invoke() throws Exception { 82 | assertThat(rc).spec().template().spec().containers().first().image().isEqualTo("someDockerImageName"); 83 | } 84 | }); 85 | } 86 | 87 | @Test 88 | void testNavigationListAssertions() throws Exception { 89 | final String id1 = "abc"; 90 | final String id2 = "def"; 91 | Map labels1 = new HashMap<>(); 92 | labels1.put("foo", "bar"); 93 | Map labels2 = new HashMap<>(); 94 | labels2.put("whatnot", "cheese"); 95 | 96 | final Pod pod1 = new Pod(); 97 | pod1.setMetadata(new ObjectMeta()); 98 | pod1.getMetadata().setName(id1); 99 | pod1.getMetadata().setLabels(labels1); 100 | 101 | final Pod pod2 = new Pod(); 102 | pod2.setMetadata(new ObjectMeta()); 103 | pod2.getMetadata().setName(id2); 104 | pod2.getMetadata().setLabels(labels2); 105 | 106 | 107 | final PodList emptyPodList = new PodList(); 108 | final PodList podList = new PodList(); 109 | podList.setItems(new ArrayList(Arrays.asList(pod1, pod2))); 110 | 111 | assertThat(emptyPodList).describedAs("emptyPodList").items().isEmpty(); 112 | assertThat(podList).describedAs("podListWith2Items").items().first().metadata().name().isEqualTo(id1); 113 | assertThat(podList).describedAs("podListWith2Items").items().last().metadata().name().isEqualTo(id2); 114 | 115 | assertAssertionError(new Block() { 116 | @Override 117 | public void invoke() throws Exception { 118 | assertThat(podList).describedAs("podListWith2Items").items().item(-1).isNotNull(); 119 | } 120 | }); 121 | 122 | assertAssertionError(new Block() { 123 | @Override 124 | public void invoke() throws Exception { 125 | assertThat(podList).describedAs("podListWith2Items").items().item(2).isNotNull(); 126 | } 127 | }); 128 | 129 | assertAssertionError(new Block() { 130 | @Override 131 | public void invoke() throws Exception { 132 | assertThat(podList).describedAs("podListWith2Items").items().first().metadata().name().isEqualTo("shouldNotMatch"); 133 | } 134 | }); 135 | } 136 | 137 | } -------------------------------------------------------------------------------- /src/test/java/io/fabric8/kubernetes/assertions/PodSelectionAssertTests.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.model.LabelSelectorRequirement; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.ArrayList; 7 | import java.util.HashMap; 8 | 9 | public class PodSelectionAssertTests extends TestBase { 10 | @Test 11 | void testIsPodReadyForPeriod() { 12 | PodSelectionAssert psAssert = 13 | new PodSelectionAssert(kClient, 1, new HashMap(), new ArrayList(), ""); 14 | // Needs Kubernetes to be running 15 | psAssert.isPodReadyForPeriod(5000, 1000); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/kubernetes/assertions/PodsAssertTests.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.PodStatusType; 4 | import io.fabric8.kubernetes.api.model.Pod; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | public class PodsAssertTests extends TestBase { 11 | @Test 12 | void testLogs() { 13 | List pods = new ArrayList<>(); 14 | Pod pod1 = new Pod(); 15 | Pod pod2 = new Pod(); 16 | pods.add(pod1); 17 | pods.add(pod2); 18 | 19 | PodsAssert pAssert = new PodsAssert(pods, kClient); 20 | pAssert.logs().isEmpty(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/kubernetes/assertions/ReplicaSetPodsAssertTests.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.model.apps.ReplicaSet; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.assertj.core.api.Assertions.assertThat; 7 | 8 | public class ReplicaSetPodsAssertTests extends TestBase { 9 | @Test 10 | void testPods() { 11 | ReplicaSetPodsAssert rspAssert = new ReplicaSetPodsAssert(kClient, new ReplicaSet()); 12 | // Needs Kubernetes to be running 13 | assertThat(rspAssert.pods().getReplicas()).isEqualTo(0); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/kubernetes/assertions/ServicePodsAssertTests.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.model.ObjectMeta; 4 | import io.fabric8.kubernetes.api.model.Service; 5 | import org.junit.jupiter.api.Test; 6 | 7 | public class ServicePodsAssertTests extends TestBase { 8 | @Test 9 | void testHasEndpointOrReadyPod() { 10 | Service service = new Service(); 11 | ObjectMeta metadata = new ObjectMeta(); 12 | metadata.setName("Test Service"); 13 | service.setMetadata(metadata); 14 | ServicePodsAssert spAssert = new ServicePodsAssert(kClient, service); 15 | // Needs Kubernetes to be running 16 | spAssert.hasEndpointOrReadyPod(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/kubernetes/assertions/ServicesAssertTests.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.api.model.ObjectMeta; 4 | import io.fabric8.kubernetes.api.model.Service; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import java.util.Arrays; 8 | 9 | public class ServicesAssertTests extends TestBase { 10 | @Test 11 | void testAssertAllServicesHaveEndpointOrReadyPod() { 12 | Service service = new Service(); 13 | ObjectMeta metadata = new ObjectMeta(); 14 | metadata.setName("Test Service"); 15 | service.setMetadata(metadata); 16 | ServicesAssert sAssert = new ServicesAssert(kClient, Arrays.asList(service)); 17 | sAssert.assertAllServicesHaveEndpointOrReadyPod(); 18 | } 19 | 20 | @Test 21 | void testService() { 22 | String serviceName = "Test Service"; 23 | Service service = new Service(); 24 | ObjectMeta metadata = new ObjectMeta(); 25 | metadata.setName(serviceName); 26 | service.setMetadata(metadata); 27 | ServicesAssert sAssert = new ServicesAssert(kClient, Arrays.asList(service)); 28 | sAssert.service(serviceName); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/kubernetes/assertions/TestBase.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions; 2 | 3 | import io.fabric8.kubernetes.client.DefaultKubernetesClient; 4 | import io.fabric8.kubernetes.client.KubernetesClient; 5 | 6 | public class TestBase { 7 | final KubernetesClient kClient = new DefaultKubernetesClient(); 8 | final KubernetesAssert kAssert = new KubernetesAssert(kClient); 9 | } 10 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/kubernetes/assertions/support/LogHelpersTests.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.kubernetes.assertions.support; 2 | 3 | import io.fabric8.kubernetes.api.PodStatusType; 4 | import io.fabric8.kubernetes.api.model.Container; 5 | import io.fabric8.kubernetes.api.model.Pod; 6 | import io.fabric8.kubernetes.api.model.PodStatus; 7 | import org.junit.jupiter.api.Test; 8 | 9 | import java.io.File; 10 | 11 | import static org.assertj.core.api.Assertions.assertThat; 12 | 13 | public class LogHelpersTests { 14 | @Test 15 | void testGetLogFileName() { 16 | String expected = "C:\\target\\test-pod-logs\\testPod-testContainer-1.log"; 17 | File baseDir = new File("/"); 18 | String podName = "testPod"; 19 | Container container = new Container(); 20 | container.setName("testContainer"); 21 | String actual = LogHelpers.getLogFileName(baseDir, podName, container, 1).getAbsolutePath(); 22 | 23 | assertThat(expected).isEqualTo(actual); 24 | } 25 | 26 | @Test 27 | void testGetRestartCount() { 28 | Pod pod = new Pod(); 29 | pod.setStatus(new PodStatus()); 30 | int restartCount = LogHelpers.getRestartCount(pod); 31 | 32 | assertThat(restartCount).isEqualTo(0); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2005-2016 Red Hat, Inc. 3 | # 4 | # Red Hat licenses this file to you under the Apache License, version 5 | # 2.0 (the "License"); you may not use this file except in compliance 6 | # with the License. You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 13 | # implied. See the License for the specific language governing 14 | # permissions and limitations under the License. 15 | # 16 | 17 | log4j.rootLogger=INFO, stdout 18 | 19 | #The logging properties used during tests.. 20 | # CONSOLE appender not used by default 21 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 22 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 23 | log4j.appender.stdout.layout.ConversionPattern=%-5p %-30.30c{1} - %m%n 24 | log4j.appender.stdout.threshold=DEBUG 25 | 26 | --------------------------------------------------------------------------------