Skip to content

Commit

Permalink
Merge branch 'release/1.58.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
robertauer committed Nov 7, 2022
2 parents 17270f6 + aad61d2 commit d126753
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.58.0](https://github.com/cloudogu/ces-build-lib/releases/tag/1.58.0) - 2022-11-07
### Changed
- Push k8s yaml content via file reference #87

## [1.57.0](https://github.com/cloudogu/ces-build-lib/releases/tag/1.57.0) - 2022-10-06
### Added
- New class `Bats` providing methods to easily execute existing bats (Bash Automated Testing System) tests. #85
Expand Down
2 changes: 1 addition & 1 deletion 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.57.0</version>
<version>1.58.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
3 changes: 1 addition & 2 deletions src/com/cloudogu/ces/cesbuildlib/DoguRegistry.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ class DoguRegistry {
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)
def result = doguRegistryHttpClient.putFile("${trimmedUrl}/${K8S_POST_ENDPOINT}/${k8sNamespace}/${k8sName}/${versionWithoutVPrefix}", "application/yaml", pathToYaml)
checkStatus(result, pathToYaml)
}

Expand Down
42 changes: 33 additions & 9 deletions src/com/cloudogu/ces/cesbuildlib/HttpClient.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ class HttpClient implements Serializable {
Map put(String url, String contentType = '', def data = '') {
return httpRequest('PUT', url, contentType, data)
}

Map putFile(String url, String contentType = '', String filePath) {
String command
executeWithCredentials {
command = getUploadFileCurlCommand('PUT', url, contentType, filePath)
}
return httpRequest('PUT', url, contentType, filePath, command)
}

Map post(String url, String contentType = '', def data = '') {
return httpRequest('POST', url, contentType, data)
Expand All @@ -45,20 +53,36 @@ class HttpClient implements Serializable {
protected String getCurlAuthParam() {
"-u ${script.env.CURL_USER}:${script.env.CURL_PASSWORD}"
}

private String getCurlCommand(String httpMethod, String url, String contentType, String data) {
return "curl -i -X ${httpMethod} " +
(credentials ? "${getCurlAuthParam()} " : '') +
(contentType ? "-H 'Content-Type: ${contentType}' " : '') +
(data ? "-d '" + data + "' " : '') +
"${url}"
}

private String getUploadFileCurlCommand(String httpMethod, String url, String contentType, String filePath) {
return "curl -i -X ${httpMethod} " +
(credentials ? "${getCurlAuthParam()} " : '') +
(contentType ? "-H 'Content-Type: ${contentType}' " : '') +
(filePath ? "-T '" + filePath + "' " : '') +
"${url}"
}

protected Map httpRequest(String httpMethod, String url, String contentType, def data) {
protected Map httpRequest(String httpMethod, String url, String contentType, def data, String customCommand = '') {
String httpResponse
def rawHeaders
def body

executeWithCredentials {
String dataStr = data.toString().replaceAll("'", "'\"'\"'")
String curlCommand =
"curl -i -X ${httpMethod} " +
(credentials ? "${getCurlAuthParam()} " : '') +
(contentType ? "-H 'Content-Type: ${contentType}' " : '') +
(data ? "-d '" + dataStr + "' " : '') +
"${url}"
String curlCommand
if (customCommand.isEmpty()) {
String dataStr = data.toString().replaceAll("'", "'\"'\"'")
curlCommand = getCurlCommand(httpMethod, url, contentType, dataStr)
} else {
curlCommand = customCommand
}

// Command must be run inside this closure, otherwise the credentials will not be masked (using '*') in the console
httpResponse = sh.returnStdOut curlCommand
Expand All @@ -82,7 +106,7 @@ class HttpClient implements Serializable {
def headers = [:]
for(String line: rawHeaders) {
// e.g. cache-control: no-cache
def splitLine = line.split(':', 2);
def splitLine = line.split(':', 2)
headers[splitLine[0].trim()] = splitLine[1].trim()
}
return [ httpCode: httpCode, headers: headers, body: body]
Expand Down
16 changes: 5 additions & 11 deletions test/com/cloudogu/ces/cesbuildlib/DoguRegistryTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,13 @@ class DoguRegistryTest extends GroovyTestCase {
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 ->
when(httpMock.putFile('http://url.de/api/v1/k8s/testing/dogu-operator/1.0.0', 'application/yaml', yamlPath)).then({ invocation ->
return [
"httpCode": '200',
"body" : 'td'
Expand All @@ -103,22 +101,19 @@ class DoguRegistryTest extends GroovyTestCase {

// 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 ->
when(httpMock.putFile('http://url.de/api/v1/k8s/testing/dogu-operator/1.0.0', 'application/yaml', yamlPath)).then({ invocation ->
return [
"httpCode": '491',
"body" : 'body'
Expand All @@ -133,9 +128,8 @@ class DoguRegistryTest extends GroovyTestCase {

// 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))
assertEquals("echo 'Error pushing ${yamlPath}'", scriptMock.allActualArgs.get(1))
assertEquals("echo 'body'", scriptMock.allActualArgs.get(2))
assertEquals("exit 1", scriptMock.allActualArgs.get(3))
}
}
17 changes: 17 additions & 0 deletions test/com/cloudogu/ces/cesbuildlib/HttpClientTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ class HttpClientTest {
.isEqualTo('curl -i -X POST -H \'Content-Type: input\' -d \'{"title":"t","description":"d"}\' http://some-url' )
}

@Test
void "request with file upload"() {
def expectedResponse = 'HTTP/1.1 203\n' +
'cache-control: no-cache\n' +
'content-type: output'
scriptMock.expectedDefaultShRetValue = expectedResponse

def actualResponse = http.putFile('http://some-url', 'input', "/path/to/file")

assertThat(actualResponse.httpCode).isEqualTo('203')
assertThat(actualResponse.headers['content-type']).isEqualTo('output')
assertThat(actualResponse.body).isEqualTo('')

assertThat(scriptMock.actualShMapArgs[0])
.isEqualTo('curl -i -X PUT -H \'Content-Type: input\' -T \'/path/to/file\' http://some-url' )
}

@Test
void "response with body"() {
String expectedBody1 = '{"some":"body"}\n'
Expand Down

0 comments on commit d126753

Please sign in to comment.