Skip to content

Commit

Permalink
implementation, documentation, unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andysworkshop committed Nov 17, 2024
1 parent 053b573 commit cb3e7bd
Show file tree
Hide file tree
Showing 24 changed files with 341 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
*.iml
out/

# Visual Studio Code
.vscode/

# Gradle
.gradle/
build/

4 changes: 4 additions & 0 deletions docs/config-from-project-properties.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ number of seconds (e.g. `210`).
Defaults to `$rootDir/.gradle/helm/cache`.
| icon:check[]

| `helm.suppressEnvironmentLogging`
| If true, do not log the gradle environment at Info level.
|

|===


Expand Down
4 changes: 4 additions & 0 deletions docs/configure-helm.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ The following properties can be set on the `helm` extension in the Gradle build
| `remoteTimeout`
| Time to wait for any individual Kubernetes operation (like Jobs for hooks).
| `--timeout` (on certain commands like `install`, `upgrade` or `uninstall`)

| `suppressEnvironmentLogging`
| If true, do not log the gradle process environment at Info level. The default is false.
|
|===

[NOTE]
Expand Down
6 changes: 6 additions & 0 deletions helm-plugin/api/helm-plugin.api
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public abstract interface class com/citi/gradle/plugins/helm/command/Configurabl
public abstract fun getDebug ()Lorg/gradle/api/provider/Property;
public abstract fun getExecutable ()Lorg/gradle/api/provider/Property;
public abstract fun getExtraArgs ()Lorg/gradle/api/provider/ListProperty;
public abstract fun getSuppressEnvironmentLogging ()Lorg/gradle/api/provider/Property;
public abstract fun getXdgCacheHome ()Lorg/gradle/api/file/DirectoryProperty;
public abstract fun getXdgConfigHome ()Lorg/gradle/api/file/DirectoryProperty;
public abstract fun getXdgDataHome ()Lorg/gradle/api/file/DirectoryProperty;
Expand Down Expand Up @@ -74,6 +75,7 @@ public abstract interface class com/citi/gradle/plugins/helm/command/GlobalHelmO
public abstract fun getDebug ()Lorg/gradle/api/provider/Provider;
public abstract fun getExecutable ()Lorg/gradle/api/provider/Provider;
public abstract fun getExtraArgs ()Lorg/gradle/api/provider/Provider;
public abstract fun getSuppressEnvironmentLogging ()Lorg/gradle/api/provider/Provider;
public abstract fun getXdgCacheHome ()Lorg/gradle/api/provider/Provider;
public abstract fun getXdgConfigHome ()Lorg/gradle/api/provider/Provider;
public abstract fun getXdgDataHome ()Lorg/gradle/api/provider/Provider;
Expand Down Expand Up @@ -111,6 +113,7 @@ public abstract interface class com/citi/gradle/plugins/helm/command/HelmExecSpe
public abstract fun flag (Ljava/lang/String;ZZ)V
public abstract fun option (Ljava/lang/String;Ljava/lang/Object;)V
public abstract fun option (Ljava/lang/String;Lorg/gradle/api/provider/Provider;)V
public abstract fun suppressEnvironmentLogging (Z)V
}

public final class com/citi/gradle/plugins/helm/command/HelmExecSpec$DefaultImpls {
Expand All @@ -123,6 +126,7 @@ public final class com/citi/gradle/plugins/helm/command/HelmExecSpec$DefaultImpl
public static synthetic fun flag$default (Lcom/citi/gradle/plugins/helm/command/HelmExecSpec;Ljava/lang/String;ZZILjava/lang/Object;)V
public static fun option (Lcom/citi/gradle/plugins/helm/command/HelmExecSpec;Ljava/lang/String;Ljava/lang/Object;)V
public static fun option (Lcom/citi/gradle/plugins/helm/command/HelmExecSpec;Ljava/lang/String;Lorg/gradle/api/provider/Provider;)V
public static synthetic fun suppressEnvironmentLogging$default (Lcom/citi/gradle/plugins/helm/command/HelmExecSpec;ZILjava/lang/Object;)V
}

public abstract interface class com/citi/gradle/plugins/helm/command/HelmInstallFromRepositoryOptions : com/citi/gradle/plugins/helm/command/HelmInstallationOptions {
Expand Down Expand Up @@ -170,6 +174,7 @@ public final class com/citi/gradle/plugins/helm/command/internal/DelegateGlobalH
public fun getDebug ()Lorg/gradle/api/provider/Provider;
public fun getExecutable ()Lorg/gradle/api/provider/Provider;
public fun getExtraArgs ()Lorg/gradle/api/provider/Provider;
public fun getSuppressEnvironmentLogging ()Lorg/gradle/api/provider/Provider;
public fun getXdgCacheHome ()Lorg/gradle/api/provider/Provider;
public fun getXdgConfigHome ()Lorg/gradle/api/provider/Provider;
public fun getXdgDataHome ()Lorg/gradle/api/provider/Provider;
Expand Down Expand Up @@ -412,6 +417,7 @@ public abstract class com/citi/gradle/plugins/helm/command/tasks/AbstractHelmCom
protected final fun getRegistryConfigFile ()Lorg/gradle/api/provider/Provider;
protected final fun getRepositoryCacheDir ()Lorg/gradle/api/provider/Provider;
protected final fun getRepositoryConfigFile ()Lorg/gradle/api/provider/Provider;
public final fun getSuppressEnvironmentLogging ()Lorg/gradle/api/provider/Provider;
public final fun getXdgCacheHome ()Lorg/gradle/api/provider/Provider;
public final fun getXdgConfigHome ()Lorg/gradle/api/provider/Provider;
public final fun getXdgDataHome ()Lorg/gradle/api/provider/Provider;
Expand Down
23 changes: 22 additions & 1 deletion helm-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ plugins {
alias(libs.plugins.binaryCompatibilityValidator)
}

val functionalTest by sourceSets.creating

dependencies {

Expand All @@ -26,10 +27,13 @@ dependencies {
testImplementation(libs.unbrokenDomeTestUtils)
testImplementation(libs.bundles.defaultTests)
testRuntimeOnly(libs.junitEngine)

"functionalTestImplementation"(project(":plugin-test-utils"))
}


gradlePlugin {
testSourceSets(functionalTest)
plugins {
create("helmCommandsPlugin") {
id = "com.citi.helm-commands"
Expand All @@ -50,4 +54,21 @@ gradlePlugin {

apiValidation {
ignoredPackages.add("com.citi.gradle.plugins.helm.dsl.internal")
}
}

val functionalTestTask = tasks.register<Test>("functionalTest") {
description = "Runs the integration tests."
group = "verification"
testClassesDirs = functionalTest.output.classesDirs
classpath = functionalTest.runtimeClasspath
mustRunAfter(tasks.test)

val urlOverrideProperty = "com.citi.gradle.helm.plugin.distribution.url.prefix"
findProperty(urlOverrideProperty)?.let { urlOverride ->
systemProperty(urlOverrideProperty, urlOverride)
}
}

tasks.build {
dependsOn(functionalTestTask)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.citi.gradle.plugins.helm.tests.functional

import java.io.File

/**
* Helping class creating helm executable that does nothing
*
* Is needed to simulate real helm (we don't need to verify this) and ignore actions done by other parts of a plugin.
*/

internal object HelmExecutable {
private val helmExecutablesDirectory = File("./src/functionalTest/resources/executable")
private val fileExtension = let {
val isWindows = System.getProperty("os.name").contains("Windows", ignoreCase = true)

if (isWindows) {
"bat"
} else {
"sh"
}
}

private val executableDummyHelm = helmExecutablesDirectory.resolve("helm.$fileExtension")

/**
* 2. Creates gradle parameter with path to that executable
*/
fun getExecutableParameterForHelm(
temporaryFolder: File
): HelmExecutableParameter {

val destinationExecutableFile = temporaryFolder.resolve("helm.$fileExtension")

executableDummyHelm.copyTo(destinationExecutableFile)
destinationExecutableFile.setExecutable(true)

return HelmExecutableParameter(destinationExecutableFile)
}

private fun getAbsoluteNormalizedPath(file: File): String {
return file
.absoluteFile
.normalize()
.path
.replace('\\', '/')
}

internal class HelmExecutableParameter(
path: File
) {
val parameterValue = let {
val normalizedFilePath = getAbsoluteNormalizedPath(path)

"-Phelm.executable=${normalizedFilePath}"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package com.citi.gradle.plugins.helm.tests.functional

import com.citi.gradle.plugins.helm.plugin.test.utils.DefaultGradleRunnerParameters
import com.citi.gradle.plugins.helm.plugin.test.utils.GradleRunnerProvider
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldNotContain
import java.io.File
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.io.TempDir
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource

internal class SuppressEnvironmentLoggingTest {

private val sourceDirectory = File("./src/functionalTest/resources/test/suppress-environment-logging")

@TempDir
private lateinit var testProjectDir: File

@BeforeEach
fun setup() {
sourceDirectory.copyRecursively(target = testProjectDir)
}

@ParameterizedTest
@MethodSource("com.citi.gradle.plugins.helm.plugin.test.utils.DefaultGradleRunnerParameters#getDefaultParameterSet")
fun doNotSuppressEnvironmentLoggingByDefault(parameters: DefaultGradleRunnerParameters) {
// given

selectBuildGradle("without-suppress")

val helmExecutableParameter =
HelmExecutable.getExecutableParameterForHelm(testProjectDir)

val arguments = listOf(
"helmPackage",
"--info",
helmExecutableParameter.parameterValue
)

val gradleRunner = GradleRunnerProvider
.createRunner(parameters)
.withProjectDir(testProjectDir)
.withArguments(arguments)

// when
val result = gradleRunner.build()

// then
val output = result.output

output shouldContain "BUILD SUCCESSFUL"
output shouldContain "with environment:"
output shouldNotContain "(environment logging is suppressed)"
}

@ParameterizedTest
@MethodSource("com.citi.gradle.plugins.helm.plugin.test.utils.DefaultGradleRunnerParameters#getDefaultParameterSet")
fun suppressEnvironmentLoggingWithCommandLineProperty(parameters: DefaultGradleRunnerParameters) {
// given

selectBuildGradle("without-suppress")

val helmExecutableParameter =
HelmExecutable.getExecutableParameterForHelm(testProjectDir)

val arguments = listOf(
"helmPackage",
"--info",
"-Phelm.suppressEnvironmentLogging=true",
helmExecutableParameter.parameterValue
)

val gradleRunner = GradleRunnerProvider
.createRunner(parameters)
.withProjectDir(testProjectDir)
.withArguments(arguments)

// when
val result = gradleRunner.build()

// then
val output = result.output

output shouldContain "BUILD SUCCESSFUL"
output shouldContain "(environment logging is suppressed)"
output shouldNotContain "with environment:"
}

@ParameterizedTest
@MethodSource("com.citi.gradle.plugins.helm.plugin.test.utils.DefaultGradleRunnerParameters#getDefaultParameterSet")
fun doNotSuppressEnvironmentLoggingWithCommandLineProperty(parameters: DefaultGradleRunnerParameters) {
// given

selectBuildGradle("without-suppress")

val helmExecutableParameter =
HelmExecutable.getExecutableParameterForHelm(testProjectDir)

val arguments = listOf(
"helmPackage",
"--info",
"-Phelm.suppressEnvironmentLogging=false",
helmExecutableParameter.parameterValue
)

val gradleRunner = GradleRunnerProvider
.createRunner(parameters)
.withProjectDir(testProjectDir)
.withArguments(arguments)

// when
val result = gradleRunner.build()

// then
val output = result.output

output shouldContain "BUILD SUCCESSFUL"
output shouldContain "with environment:"
output shouldNotContain "(environment logging is suppressed)"
}

@ParameterizedTest
@MethodSource("com.citi.gradle.plugins.helm.plugin.test.utils.DefaultGradleRunnerParameters#getDefaultParameterSet")
fun suppressEnvironmentLoggingWithDsl(parameters: DefaultGradleRunnerParameters) {
// given

selectBuildGradle("with-suppress")

val helmExecutableParameter =
HelmExecutable.getExecutableParameterForHelm(testProjectDir)

val arguments = listOf(
"helmPackage",
"--info",
helmExecutableParameter.parameterValue
)

val gradleRunner = GradleRunnerProvider
.createRunner(parameters)
.withProjectDir(testProjectDir)
.withArguments(arguments)

// when
val result = gradleRunner.build()

// then
val output = result.output

output shouldContain "BUILD SUCCESSFUL"
output shouldContain "(environment logging is suppressed)"
output shouldNotContain "with environment:"
}

private fun selectBuildGradle(selection: String) {
File(testProjectDir, "build.gradle.$selection")
.copyTo(File(testProjectDir, "build.gradle"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rem Dummy executable just to simulate real helm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Dummy executable just to simulate real helm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
id 'com.citi.helm'
}

helm {
suppressEnvironmentLogging = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
id 'com.citi.helm'
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v2
name: dummy
description: A dummy helm chart
version: 0.0.1
appVersion: "0.0.1"
type: application
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# dummy
Loading

0 comments on commit cb3e7bd

Please sign in to comment.