Skip to content

Commit

Permalink
Added new task GetNamespace
Browse files Browse the repository at this point in the history
  • Loading branch information
dancc authored and dancc committed Mar 3, 2018
1 parent 0ad2fb5 commit 772ce86
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.bmuschko.gradle.kubernetes.plugin.tasks.namespaces

import com.bmuschko.gradle.kubernetes.plugin.AbstractFunctionalTest
import org.gradle.testkit.runner.BuildResult

/**
*
* All functional tests for the `DeleteNamespace` task.
*
*/
class DeleteNamespaceFunctionalTest extends AbstractFunctionalTest {

def generateName = randomString()
def "Create namespace, execute reactive-streams, and delete namespace with config"() {

buildFile << """
import com.bmuschko.gradle.kubernetes.plugin.tasks.namespaces.CreateNamespace
import com.bmuschko.gradle.kubernetes.plugin.tasks.namespaces.DeleteNamespace
import com.bmuschko.gradle.kubernetes.plugin.tasks.namespaces.ListNamespaces
task createNamespace(type: CreateNamespace) {
doFirst {
logger.quiet "Creating namespace ${generateName}"
}
config {
withName("${generateName}")
}
}
task deleteNamespace(type: DeleteNamespace, dependsOn: createNamespace) {
config {
withName(tasks.createNamespace.response().getMetadata().getName())
}
onError { exc ->
logger.quiet "$SHOULD_NOT_REACH_HERE: \${exc}"
}
onNext { output ->
if (output) {
logger.quiet "$ON_NEXT_REACHED"
}
}
onComplete {
logger.quiet '$ON_COMPLETE_REACHED'
}
doLast {
if (response()) {
logger.quiet '$RESPONSE_SET_MESSAGE'
}
}
}
task listNamespaces(type: ListNamespaces, dependsOn: deleteNamespace) {
doFirst {
// sleep to give kubernetes time to delete the resource
sleep 10000
}
onNext { output ->
if (output.getMetadata().getName() == "${generateName}") {
logger.quiet "$SHOULD_NOT_REACH_HERE with name ${generateName}"
}
}
}
task workflow(dependsOn: listNamespaces)
"""

when:
BuildResult result = build('workflow')

then:
result.output.contains('Deleting namespace...')
!result.output.contains(SHOULD_NOT_REACH_HERE)
result.output.contains(ON_NEXT_REACHED)
result.output.contains(ON_COMPLETE_REACHED)
result.output.contains(RESPONSE_SET_MESSAGE)
}

def "Delete non-existent namespace"() {

buildFile << """
import com.bmuschko.gradle.kubernetes.plugin.tasks.namespaces.DeleteNamespace
task deleteNamespace(type: DeleteNamespace) {
config {
withName("${randomString()}")
}
onError { exc ->
logger.quiet "$SHOULD_REACH_HERE value=\${exc}"
}
}
task workflow(dependsOn: deleteNamespace)
"""

when:
BuildResult result = build('workflow')

then:
result.output.contains('Deleting namespace...')
result.output.contains(SHOULD_REACH_HERE)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.bmuschko.gradle.kubernetes.plugin.tasks.namespaces

import com.bmuschko.gradle.kubernetes.plugin.tasks.AbstractKubernetesTask
import org.gradle.api.GradleException
import org.gradle.api.tasks.Input

/**
* Get a namespace.
*/
class GetNamespace extends AbstractKubernetesTask {

@Input
Closure<String> namespace

@Override
def handleClient(kubernetesClient) {

logger.quiet 'Getting namespace...'
def objToConfigure = kubernetesClient.namespaces()

// apply user-defined inputs
def objWithUserInputs = applyUserDefinedInputs(objToConfigure)

// no real options so supply so should amount to a no-op
def objReconfigured = configureOn(objWithUserInputs)

// get the namespace
def localResponse = objReconfigured.fromServer().get()
if (!localResponse) {
throw new GradleException("Namespace '" + namespace.call() + "' could not be found.")
}

// register response for downstream use which in this case
// is just a `Namespace` instance.
responseOn(localResponse)
}

def applyUserDefinedInputs(objectToApplyInputsOn) {
objectToApplyInputsOn.withName(namespace.call())
}
}

0 comments on commit 772ce86

Please sign in to comment.