-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
278 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
141
test/com/cloudogu/ces/cesbuildlib/DoguRegistryTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters