Skip to content

Commit

Permalink
Merge branch 'release/1.56.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Sprey committed Aug 25, 2022
2 parents e19d7a0 + b10d47b commit 07b0522
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 6 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.56.0](https://github.com/cloudogu/ces-build-lib/releases/tag/1.56.0) - 2022-08-25
### Added
- New class `DockerRegistry` providing methods to: #83
- push a dogu (json)
- push a k8s component (yaml)

## [1.55.0](https://github.com/cloudogu/ces-build-lib/releases/tag/1.55.0) - 2022-07-06
### Added
- Link checker #81 see `README.md#Link-checker`
- Markdown Link checker #81 see `README.md#Link-checker`

## [1.54.0](https://github.com/cloudogu/ces-build-lib/releases/tag/1.54.0) - 2022-06-21
### Added
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Jenkins Pipeline Shared library, that contains additional features for Git, Mave
- [Pull Requests](#pull-requests)
- [HttpClient](#httpclient)
- [K3d](#k3d)
- [DoguRegistry](#doguregistry)
- [Steps](#steps)
- [mailIfStatusChanged](#mailifstatuschanged)
- [isPullRequest](#ispullrequest)
Expand Down Expand Up @@ -1085,6 +1086,22 @@ try {
}
```

# DoguRegistry

`DoguRegistry` provides functions to easily push dogus and k8s components to a configured registry.

Example:

```groovy
DoguRegistry registry = new DoguRegistry(this)
// push dogu
registry.pushDogu()
// push k8s component
registry.pushK8sYaml("pathToMyK8sYaml.yaml", "k8s-dogu-operator", "mynamespace", "0.9.0")
```

# Makefile

`Makefile` provides function regarding the `Makefile` from the current directory.
Expand All @@ -1096,6 +1113,19 @@ Example:
String currentVersion = makefile.getVersion()
```

# Markdown

`Markdown` provides function regardig the `Markdown Files` from the projects docs directory

```groovy
Markdown markdown = new Markdown(this)
markdown.check()
```

`markdown.check` executes the function defined in `Markdown`
running a container with the latest https://github.com/tcort/markdown-link-check image
and verifies that the links in the defined project directory are alive

# Steps

## mailIfStatusChanged
Expand Down
2 changes: 1 addition & 1 deletion docs/development/development_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Run tests with
./mvnw test
```

If you want to run tests within IntelliJ you need to install Groovy 2.5.3 into the project's `Plattform settings` -> `Global settings`.
If you want to run tests within IntelliJ you need to use Java 8.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.cloudogu.ces</groupId>
<artifactId>ces-build-lib</artifactId>
<name>ces-build-lib</name>
<version>1.55.0</version>
<version>1.56.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -69,8 +69,9 @@

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<version>3.8.1</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
</configuration>
Expand Down
83 changes: 83 additions & 0 deletions src/com/cloudogu/ces/cesbuildlib/DoguRegistry.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.cloudogu.ces.cesbuildlib

/**
* This class contain methods and workflows to upload dogus (json) or k8s components (yaml) to a specified dogu registry.
*/
class DoguRegistry {
public Sh sh
public HttpClient doguRegistryHttpClient

private script
private String backendCredentialsID
private String doguRegistryURL

private static String DOGU_POST_ENDPOINT = "api/v2/dogus"
private static String K8S_POST_ENDPOINT = "api/v1/k8s"

/**
* Create an object to upload dogus or k8s components to a specified registry.
*
* @param script The Jenkins script you are coming from (aka "this").
* @param doguRegistryURL Url to the actual dogu registry. Default: 'https://dogu.cloudogu.com'.
* @param backendCredentialsID Identifier of credentials used to log into the backend. Default: cesmarvin-setup.
*/
DoguRegistry(script, String doguRegistryURL = "https://dogu.cloudogu.com", String backendCredentialsID = "cesmarvin-setup") {
this.script = script
this.backendCredentialsID = backendCredentialsID
this.doguRegistryURL = doguRegistryURL
this.doguRegistryHttpClient = new HttpClient(script, backendCredentialsID)
this.sh = new Sh(script)
}

/**
* Pushes a dogu to the dogu registry.
*
* @param pathToDoguJson path to the dogu.json file. The path should be relative to the workspace.
*/
void pushDogu(String pathToDoguJson = "dogu.json") {
def doguJson = script.readJSON file: pathToDoguJson
def doguVersion = doguJson.Version
def doguNameWithNamespace = doguJson.Name
script.sh "echo 'Push Dogu:\n-Namespace/Name: ${doguNameWithNamespace}\n-Version: ${doguVersion}'"

def doguString = this.sh.returnStdOut("cat ${pathToDoguJson}")
def trimmedUrl = trimSuffix(doguRegistryURL, '/')
def result = doguRegistryHttpClient.put("${trimmedUrl}/${DOGU_POST_ENDPOINT}/${doguNameWithNamespace}", "application/json", doguString)
checkStatus(result, pathToDoguJson)
}

/**
* Pushes a yaml tapestry to the dogu registry for k8s components.
*
* @param pathToYaml Path to the yaml containing the k8s component.
* @param k8sName Name of the k8s component.
* @param k8sNamespace Namespace of the k8s component.
* @param versionWithoutVPrefix The version of the component without the version prefix.
*/
void pushK8sYaml(String pathToYaml, String k8sName, String k8sNamespace, String versionWithoutVPrefix) {
script.sh "echo 'Push Yaml:\n-Name: ${k8sName}\n-Namespace: ${k8sNamespace}\n-Version: ${versionWithoutVPrefix}'"

def k8sComponentYaml = this.sh.returnStdOut("cat ${pathToYaml}")
def trimmedUrl = trimSuffix(doguRegistryURL, '/')
def result = doguRegistryHttpClient.put("${trimmedUrl}/${K8S_POST_ENDPOINT}/${k8sNamespace}/${k8sName}/${versionWithoutVPrefix}", "application/yaml", k8sComponentYaml)
checkStatus(result, pathToYaml)
}

private static String trimSuffix(String original, String suffix) {
if(original.endsWith(suffix)) {
return original.substring(0, original.length() - suffix.length())
}
return original
}

private void checkStatus(LinkedHashMap<String, Serializable> result, String fileName) {
def status = result["httpCode"]
def body = result["body"]

if ((status as Integer) >= 400) {
script.sh "echo 'Error pushing ${fileName}'"
script.sh "echo '${body}'"
script.sh "exit 1"
}
}
}
141 changes: 141 additions & 0 deletions test/com/cloudogu/ces/cesbuildlib/DoguRegistryTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.cloudogu.ces.cesbuildlib

import groovy.json.JsonSlurper
import org.junit.Test

import static org.mockito.Mockito.mock
import static org.mockito.Mockito.when

class DoguRegistryTest extends GroovyTestCase {

@Test
void testCreateRegistryObjectWithDefaults() {
// when
DoguRegistry sut = new DoguRegistry("script")

// then
assertTrue(sut != null)
}

@Test
void testPushDogu() {
// given
String doguPath = "dogu.json"
String doguStr = '{"Name": "testing/ldap", "Version": "2.4.48-4"}'
ScriptMock scriptMock = new ScriptMock()
scriptMock.jsonFiles.put(doguPath, new JsonSlurper().parseText(doguStr))
scriptMock.expectedShRetValueForScript.put("cat ${doguPath}".toString(), doguStr)

def httpMock = mock(HttpClient.class)
when(httpMock.put('http://url.de/api/v2/dogus/testing/ldap', 'application/json', doguStr)).then({ invocation ->
return [
httpCode: '200',
body : 'td'
]
})

DoguRegistry sut = new DoguRegistry(scriptMock, "http://url.de")
sut.doguRegistryHttpClient = httpMock

// when
sut.pushDogu(doguPath)

// then
assertEquals("echo 'Push Dogu:\n-Namespace/Name: testing/ldap\n-Version: 2.4.48-4'", scriptMock.allActualArgs.get(0))
assertEquals("cat dogu.json", scriptMock.allActualArgs.get(1))
}

@Test
void testExitOnHttpErrorJson() {
// given
String doguPath = "dogu.json"
String doguStr = '{"Name": "testing/ldap", "Version": "2.4.48-4"}'
ScriptMock scriptMock = new ScriptMock()
scriptMock.jsonFiles.put(doguPath, new JsonSlurper().parseText(doguStr))
scriptMock.expectedShRetValueForScript.put("cat ${doguPath}".toString(), doguStr)

def httpMock = mock(HttpClient.class)
when(httpMock.put('http://url.de/api/v2/dogus/testing/ldap', 'application/json', doguStr)).then({ invocation ->
return [
httpCode: '500',
body : 'body'
]
})

DoguRegistry sut = new DoguRegistry(scriptMock, "http://url.de")
sut.doguRegistryHttpClient = httpMock

// when
sut.pushDogu(doguPath)

// then
assertEquals("echo 'Push Dogu:\n-Namespace/Name: testing/ldap\n-Version: 2.4.48-4'", scriptMock.allActualArgs.get(0))
assertEquals("cat dogu.json", scriptMock.allActualArgs.get(1))
assertEquals("echo 'Error pushing ${doguPath}'", scriptMock.allActualArgs.get(2))
assertEquals("echo 'body'", scriptMock.allActualArgs.get(3))
assertEquals("exit 1", scriptMock.allActualArgs.get(4))
}

@Test
void testPushYaml() {
// given
String yamlPath = "path.yaml"
String yaml = "apiVersion: 1"
String k8sName = "dogu-operator"
String namespace = "testing"
String version = "1.0.0"
ScriptMock scriptMock = new ScriptMock()
scriptMock.expectedShRetValueForScript.put("cat ${yamlPath}".toString(), yaml)

def httpMock = mock(HttpClient.class)
when(httpMock.put('http://url.de/api/v1/k8s/testing/dogu-operator/1.0.0', 'application/yaml', yaml)).then({ invocation ->
return [
"httpCode": '200',
"body" : 'td'
]
})

DoguRegistry sut = new DoguRegistry(scriptMock, "http://url.de")
sut.doguRegistryHttpClient = httpMock

// when
sut.pushK8sYaml(yamlPath, k8sName, namespace, version)

// then
assertEquals("echo 'Push Yaml:\n-Name: ${k8sName}\n-Namespace: ${namespace}\n-Version: ${version}'", scriptMock.allActualArgs.get(0))
assertEquals("cat path.yaml", scriptMock.allActualArgs.get(1))
}

@Test
void testExitOnHttpErrorYaml() {
// given
String yamlPath = "path.yaml"
String yaml = "apiVersion: 1"
String k8sName = "dogu-operator"
String namespace = "testing"
String version = "1.0.0"
ScriptMock scriptMock = new ScriptMock()
scriptMock.expectedShRetValueForScript.put("cat ${yamlPath}".toString(), yaml)

def httpMock = mock(HttpClient.class)
when(httpMock.put('http://url.de/api/v1/k8s/testing/dogu-operator/1.0.0', 'application/yaml', yaml)).then({ invocation ->
return [
"httpCode": '491',
"body" : 'body'
]
})

DoguRegistry sut = new DoguRegistry(scriptMock, "http://url.de")
sut.doguRegistryHttpClient = httpMock

// when
sut.pushK8sYaml(yamlPath, k8sName, namespace, version)

// then
assertEquals("echo 'Push Yaml:\n-Name: ${k8sName}\n-Namespace: ${namespace}\n-Version: ${version}'", scriptMock.allActualArgs.get(0))
assertEquals("cat path.yaml", scriptMock.allActualArgs.get(1))
assertEquals("echo 'Error pushing ${yamlPath}'", scriptMock.allActualArgs.get(2))
assertEquals("echo 'body'", scriptMock.allActualArgs.get(3))
assertEquals("exit 1", scriptMock.allActualArgs.get(4))
}
}
15 changes: 13 additions & 2 deletions test/com/cloudogu/ces/cesbuildlib/ScriptMock.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class ScriptMock {
Map actualStringArgs
Map files = new HashMap<String, String>()
List<List<String>> actualWithEnv = []

Map<String, Object> jsonFiles = new HashMap<>()

String actualDir
def actualGitArgs
Expand Down Expand Up @@ -150,8 +152,17 @@ class ScriptMock {

Object readJSON(Map<String, Object> args) {
String text = args.get('text')
def slurper = new JsonSlurper()
return slurper.parseText(text)
if (text != null) {
def slurper = new JsonSlurper()
return slurper.parseText(text)
}

String path = args.get("file")
if (path != null) {
return jsonFiles.get(path)
}

throw new InputMismatchException()
}

void dir(String dir, Closure closure) {
Expand Down

0 comments on commit 07b0522

Please sign in to comment.