From 974db5aafe0fd20d6348e97a194d933f078c69cb Mon Sep 17 00:00:00 2001 From: Andrew Pitt Date: Mon, 6 Jan 2020 12:15:30 -0500 Subject: [PATCH] Added tests and selenium. --- pom.xml | 46 ++++++- resources/app-template.yaml | 116 ++++++++++++++++++ resources/build-template.yaml | 95 ++++++++++++++ src/main/resources/templates/welcome.html | 2 +- src/test/java/integration/MainPageIT.java | 45 +++++++ src/test/java/integration/SeleniumConfig.java | 26 ++++ 6 files changed, 328 insertions(+), 2 deletions(-) create mode 100644 resources/app-template.yaml create mode 100644 resources/build-template.yaml create mode 100644 src/test/java/integration/MainPageIT.java create mode 100644 src/test/java/integration/SeleniumConfig.java diff --git a/pom.xml b/pom.xml index 7031b8352..28c17f0eb 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.springframework.boot spring-boot-starter-parent - 2.1.6.RELEASE + 2.2.2.RELEASE petclinic @@ -108,6 +108,11 @@ spring-boot-devtools true + + + org.seleniumhq.selenium + selenium-java + @@ -208,9 +213,48 @@ + + + maven-surefire-plugin + + + **/integration/* + + + + + + failsafe + + + + maven-failsafe-plugin + 2.22.0 + + + **/integration/* + + + **/test/* + + + + + + integration-test + verify + + + + + + + + + diff --git a/resources/app-template.yaml b/resources/app-template.yaml new file mode 100644 index 000000000..bd655510c --- /dev/null +++ b/resources/app-template.yaml @@ -0,0 +1,116 @@ +kind: Template +apiVersion: v1 +metadata: + name: app-template + annotations: + openshift.io/display-name: "Demo App Example: Front End App" + description: >- + Front End App + openshift.io/long-description: >- + This template defines resources needed to deploy a simple demo app. + tags: "quickstart,java,demo" + iconClass: icon-openjdk + openshift.io/provider-display-name: "Nobody, Inc." + openshift.io/documentation-url: "https://github.com/pittar/ocp-devops-setup" + openshift.io/support-url: "https://google.ca" +message: "Your app is being instantiated." +labels: + template: "demo-app-frontend-template" + app: "demo-app-frontend" +objects: +- apiVersion: apps.openshift.io/v1 + kind: DeploymentConfig + metadata: + name: frontend + spec: + replicas: 1 + revisionHistoryLimit: 10 + selector: + deploymentConfig: frontend + strategy: + activeDeadlineSeconds: 21600 + resources: {} + rollingParams: + intervalSeconds: 1 + maxSurge: 25% + maxUnavailable: 25% + timeoutSeconds: 600 + updatePeriodSeconds: 1 + type: Rolling + template: + metadata: + labels: + deploymentConfig: frontend + name: frontend + spec: + containers: + - name: frontend + envFrom: + - configMapRef: + name: frontend-cm + imagePullPolicy: Always + ports: + - containerPort: 8778 + name: jolokia + protocol: TCP + - containerPort: 8080 + name: http + protocol: TCP + - containerPort: 8443 + name: https + protocol: TCP + resources: {} + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + dnsPolicy: ClusterFirst + restartPolicy: Always + schedulerName: default-scheduler + securityContext: {} + terminationGracePeriodSeconds: 75 + test: false + triggers: + - imageChangeParams: + automatic: false + containerNames: + - frontend + from: + kind: ImageStreamTag + name: ${IMAGE_NAME}:${IMAGE_TAG} + namespace: cicd + type: ImageChange +- apiVersion: v1 + kind: Service + metadata: + name: frontend + spec: + ports: + - port: 8080 + protocol: TCP + targetPort: 8080 + selector: + deploymentConfig: frontend + sessionAffinity: None + type: ClusterIP + status: + loadBalancer: {} +- apiVersion: route.openshift.io/v1 + kind: Route + metadata: + name: frontend + spec: + to: + kind: Service + name: frontend + weight: 100 + wildcardPolicy: None +parameters: + - name: IMAGE_NAME + displayName: Frontend image + description: The frontend image to use. + value: frontend + required: true + - name: IMAGE_TAG + displayName: Frontend image tag + description: The tag (version) of the frontend image to use. + value: latest + required: true diff --git a/resources/build-template.yaml b/resources/build-template.yaml new file mode 100644 index 000000000..8c226990d --- /dev/null +++ b/resources/build-template.yaml @@ -0,0 +1,95 @@ +apiVersion: v1 +kind: Template +metadata: + name: jenkins-java-pipeline +parameters: +- name: APP_NAME + description: Name of the container to build. + displayName: Application container name. + required: true +- name: GIT_SOURCE_URL + description: The source URL for the application + displayName: Source URL + required: true +- name: GIT_SOURCE_REF + description: The source Ref for the application + displayName: Source Ref + required: true + value: master +- description: Github trigger secret. A difficult to guess string encoded as part of the webhook URL. Not encrypted. + displayName: GitHub Webhook Secret + from: '[a-zA-Z0-9]{40}' + generate: expression + name: GITHUB_WEBHOOK_SECRET + required: true +- description: A secret string used to configure the Generic webhook. + displayName: Generic Webhook Secret + from: '[a-zA-Z0-9]{40}' + generate: expression + name: GENERIC_WEBHOOK_SECRET + required: true +labels: + template: "demo-app-build-template" + app: "demo-app-build" +objects: +- apiVersion: v1 + kind: ImageStream + metadata: + name: ${APP_NAME} + spec: {} + status: + dockerImageRepository: "" +- apiVersion: v1 + kind: BuildConfig + metadata: + creationTimestamp: null + labels: + name: ${APP_NAME} + name: ${APP_NAME} + spec: + strategy: + jenkinsPipelineStrategy: + jenkinsfilePath: Jenkinsfile + env: + - name: APP_NAME + value: ${APP_NAME} + - name: GIT_SOURCE_URL + value: ${GIT_SOURCE_URL} + - name: GIT_SOURCE_REF + value: ${GIT_SOURCE_REF} + type: JenkinsPipeline + source: + git: + uri: "${GIT_SOURCE_URL}" + ref: "${GIT_SOURCE_REF}" + type: "Git" + triggers: + - github: + secret: "${GITHUB_WEBHOOK_SECRET}" + type: GitHub + - generic: + secret: "${GENERIC_WEBHOOK_SECRET}" + type: Generic +- apiVersion: v1 + kind: BuildConfig + metadata: + name: "${APP_NAME}-build" + spec: + output: + to: + kind: ImageStreamTag + name: ${APP_NAME}:latest + postCommit: {} + resources: {} + runPolicy: Serial + source: + binary: {} + type: Binary + strategy: + sourceStrategy: + from: + kind: ImageStreamTag + name: redhat-openjdk18-openshift:1.4 + namespace: openshift + type: Source + triggers: [] diff --git a/src/main/resources/templates/welcome.html b/src/main/resources/templates/welcome.html index 3dbfcc5e8..2358c5da9 100644 --- a/src/main/resources/templates/welcome.html +++ b/src/main/resources/templates/welcome.html @@ -4,7 +4,7 @@ -

Welcome to the Pet Clinic!

+

Welcome to the Pet Clinic!

diff --git a/src/test/java/integration/MainPageIT.java b/src/test/java/integration/MainPageIT.java new file mode 100644 index 000000000..4c3f2701b --- /dev/null +++ b/src/test/java/integration/MainPageIT.java @@ -0,0 +1,45 @@ +package integration; + +import static org.junit.Assert.assertEquals; + +import java.net.MalformedURLException; + +import org.junit.After; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; + +public class MainPageIT { + + private SeleniumConfig config; + private String url = "http://app-petclinic-dev.apps.cluster-ottawa-630b.ottawa-630b.example.opentlc.com/"; + + public MainPageIT() throws MalformedURLException { + config = new SeleniumConfig(); + } + + public String getTitle() { + return this.config.getDriver().getTitle(); + } + + @After + public void closeConnection() { + config.getDriver().quit(); + } + + @Test + public void googleTitleIT() { + WebDriver driver = config.getDriver(); + driver.get(url); + assertEquals("Title not as expected: ", "PetClinic :: a Spring Framework demonstration", getTitle()); + // Save the random value from the page. + String welcomeText = driver.findElement(By.id("welcome")).getText(); + // Reload the page and get a new random value. + driver.get(url); + String value2 = driver.findElement(By.id("random-value")).getText(); + // Values should not be the same. + assertEquals("Wrong welcome text.", "Welcome to the Pet Clinic!", welcomeText); + System.out.println("Text extracted: " + welcomeText); + } + +} diff --git a/src/test/java/integration/SeleniumConfig.java b/src/test/java/integration/SeleniumConfig.java new file mode 100644 index 000000000..4c8c80b80 --- /dev/null +++ b/src/test/java/integration/SeleniumConfig.java @@ -0,0 +1,26 @@ +package integration; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.concurrent.TimeUnit; + +import org.openqa.selenium.Capabilities; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxOptions; +import org.openqa.selenium.remote.RemoteWebDriver; + +public class SeleniumConfig { + private WebDriver driver; + + public SeleniumConfig() throws MalformedURLException { + Capabilities capabilities = new FirefoxOptions(); + driver = new RemoteWebDriver( + new URL("http://selenium-hub-cicd.apps.cluster-ottawa-630b.ottawa-630b.example.opentlc.com/wd/hub"), + capabilities); + driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); + } + + public WebDriver getDriver() { + return driver; + } +}