Skip to content

Commit

Permalink
ftest: Partial implementation for global SSC functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Mar 27, 2024
1 parent 26ba73f commit a868c28
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.spockframework.runtime.model.FieldInfo
import org.spockframework.runtime.model.SpecInfo

import com.fortify.cli.ftest._common.spec.TestResource
import com.fortify.cli.ftest._common.util.RuntimeResourcesHelper
import com.fortify.cli.ftest._common.util.TempDirHelper

import groovy.transform.CompileStatic
Expand All @@ -23,28 +24,9 @@ class TestResourceExtension extends AbstractTempDirExtension {
@Override
protected Path getPathForField(FieldInfo field) {
def annotation = field.getAnnotation(TestResource.class)
return annotation==null ? null : extractResource(annotation.value())
return annotation==null ? null : RuntimeResourcesHelper.extractResource(annotation.value())
}

private Path extractResource(String resourceFile) {
getResource(resourceFile).withCloseable {
Path outputFilePath = tempDir.resolve(resourceFile)
outputFilePath.parent.toFile().mkdirs()
Files.copy(it, outputFilePath, StandardCopyOption.REPLACE_EXISTING)
return outputFilePath
}
}

private InputStream getResource(String resourceFile) {
def cl = TestResourceExtension.class.classLoader
def stream = cl.getResourceAsStream(resourceFile)
if ( stream==null ) {
stream = cl.getResourceAsStream(resourceFile+"-no-shadow")
}
if ( stream==null ) {
throw new IllegalStateException("${resourceFile} (or ${resourceFile}-no-shadow) referenced in @TestResource not found")
}
return stream
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2023 Open Text.
*
* The only warranties for products and services of Open Text
* and its affiliates and licensors ("Open Text") are as may
* be set forth in the express warranty statements accompanying
* such products and services. Nothing herein should be construed
* as constituting an additional warranty. Open Text shall not be
* liable for technical or editorial errors or omissions contained
* herein. The information contained herein is subject to change
* without notice.
*/
package com.fortify.cli.ftest._common.util

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption

class RuntimeResourcesHelper {
private static tempDir = TempDirHelper.create();

static void close() {
TempDirHelper.rm(tempDir);
}

static Path extractResource(String resourceFile) {
Path outputFilePath = tempDir.resolve(resourceFile);
if ( !Files.exists(outputFilePath) ) {
getResource(resourceFile).withCloseable {
outputFilePath.parent.toFile().mkdirs()
Files.copy(it, outputFilePath, StandardCopyOption.REPLACE_EXISTING)
}
}
return outputFilePath
}

private static InputStream getResource(String resourceFile) {
def cl = RuntimeResourcesHelper.class.classLoader
def stream = cl.getResourceAsStream(resourceFile)
if ( stream==null ) {
stream = cl.getResourceAsStream(resourceFile+"-no-shadow")
}
if ( stream==null ) {
throw new IllegalStateException("${resourceFile} (or ${resourceFile}-no-shadow) referenced in @TestResource not found")
}
return stream
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 2023 Open Text.
*
* The only warranties for products and services of Open Text
* and its affiliates and licensors ("Open Text") are as may
* be set forth in the express warranty statements accompanying
* such products and services. Nothing herein should be construed
* as constituting an additional warranty. Open Text shall not be
* liable for technical or editorial errors or omissions contained
* herein. The information contained herein is subject to change
* without notice.
*/
package com.fortify.cli.ftest.ssc

import static com.fortify.cli.ftest._common.spec.FcliSessionType.SSC

import com.fortify.cli.ftest._common.spec.FcliBaseSpec
import com.fortify.cli.ftest._common.spec.FcliSession
import com.fortify.cli.ftest._common.spec.Prefix
import com.fortify.cli.ftest.ssc._common.SSCGlobal

@Prefix("test") @FcliSession(SSC)
class SSCTestSpec extends FcliBaseSpec {
def setupSpec() {
println "setup"
}

def "t"() {
println SSCGlobal.AppVersion.eightball.version
when:
def a = "b"
then:
a=="b"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class SSCAppVersionSupplier implements Closeable, AutoCloseable {



public class SSCAppVersion {
public static class SSCAppVersion {
private final String random = System.currentTimeMillis()
private final String fcliVariableName = "ssc_appversion_"+random
private final String appName = "fcli-"+random
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.fortify.cli.ftest.ssc._common;

import java.nio.file.Path

import org.spockframework.runtime.extension.IGlobalExtension

import com.fortify.cli.ftest._common.Fcli
import com.fortify.cli.ftest._common.spec.TestResource
import com.fortify.cli.ftest._common.util.RuntimeResourcesHelper
import com.fortify.cli.ftest.ssc._common.SSCAppVersionSupplier.SSCAppVersion

import groovy.transform.CompileStatic

@CompileStatic
class SSCGlobal implements IGlobalExtension {
@Override
public void start() {}

@Override
public void stop() {
AppVersion.values().each { v->v.close() }
}

public static enum AppVersion implements Closeable, AutoCloseable {
empty(AppVersion.&initEmpty),
eightball(AppVersion.&initEightball),

private SSCAppVersion version;
private Closure initVersion;
private AppVersion(Closure initVersion) {
this.initVersion = initVersion;
}

private static void initEmpty(SSCAppVersion version) {

}

private static void initEightball(SSCAppVersion version) {
Path fprPath = RuntimeResourcesHelper.extractResource("runtime/shared/EightBall-22.1.0.fpr");
Fcli.run("ssc artifact upload -f $fprPath --appversion ${version.variableRef} --store globalEightBallFpr")
Fcli.run("ssc artifact wait-for ::globalEightBallFpr:: -i 2s")
}

public SSCAppVersion getVersion() {
if ( !version ) {
version = new SSCAppVersion().create()
initVersion(version)
}
return version
}

@Override
public void close() {
if ( version ) {
version.close();
version = null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
com.fortify.cli.ftest.ssc._common.SSCGlobal
com.fortify.cli.ftest._common.extension.FcliInitializerExtension
com.fortify.cli.ftest._common.extension.TestResourceExtension
com.fortify.cli.ftest._common.extension.TempPathExtension
Expand Down

0 comments on commit a868c28

Please sign in to comment.