diff --git a/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpClientSteps.java b/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpClientSteps.java index da0d32d1..1c24e425 100644 --- a/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpClientSteps.java +++ b/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpClientSteps.java @@ -18,8 +18,6 @@ package org.citrusframework.yaks.http; import javax.net.ssl.SSLContext; -import java.net.MalformedURLException; -import java.net.URL; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; @@ -118,16 +116,11 @@ public void setClient(String id) { @Given("^(?:URL|url): ([^\\s]+)$") public void setUrl(String url) { - try { - URL requestURL = new URL(url); - if (requestURL.getProtocol().equalsIgnoreCase("https")) { - httpClient.getEndpointConfiguration().setRequestFactory(sslRequestFactory()); - } - - this.requestUrl = url; - } catch (MalformedURLException e) { - throw new CitrusRuntimeException(e); + if (url.startsWith("https")) { + httpClient.getEndpointConfiguration().setRequestFactory(sslRequestFactory()); } + + this.requestUrl = url; } @Then("^(?:expect|verify) HTTP response header ([^\\s]+)(?:=| is )\"(.+)\"$") diff --git a/java/steps/yaks-http/src/test/resources/org/citrusframework/yaks/http/http.client.feature b/java/steps/yaks-http/src/test/resources/org/citrusframework/yaks/http/http.client.feature index 7804e958..4ffb84c0 100644 --- a/java/steps/yaks-http/src/test/resources/org/citrusframework/yaks/http/http.client.feature +++ b/java/steps/yaks-http/src/test/resources/org/citrusframework/yaks/http/http.client.feature @@ -1,7 +1,8 @@ Feature: Http client Background: - Given URL: http://localhost:8080 + Given variable port is "8080" + Given URL: http://localhost:${port} Scenario: GET When send GET /todo diff --git a/java/steps/yaks-standard/pom.xml b/java/steps/yaks-standard/pom.xml index 4db5a06f..d59576e5 100644 --- a/java/steps/yaks-standard/pom.xml +++ b/java/steps/yaks-standard/pom.xml @@ -58,6 +58,11 @@ junit test + + org.assertj + assertj-core + test + io.cucumber cucumber-junit diff --git a/java/steps/yaks-standard/src/main/java/org/citrusframework/yaks/hooks/InjectEnvVarsHook.java b/java/steps/yaks-standard/src/main/java/org/citrusframework/yaks/hooks/InjectEnvVarsHook.java new file mode 100644 index 00000000..8fe98b4d --- /dev/null +++ b/java/steps/yaks-standard/src/main/java/org/citrusframework/yaks/hooks/InjectEnvVarsHook.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.citrusframework.yaks.hooks; + +import java.util.Optional; + +import com.consol.citrus.TestCaseRunner; +import com.consol.citrus.actions.AbstractTestAction; +import com.consol.citrus.annotations.CitrusResource; +import com.consol.citrus.context.TestContext; +import io.cucumber.java.Before; +import io.cucumber.java.Scenario; + +/** + * Cucumber hook injects environment variables as test variables before the scenario is executed. + * @author Christoph Deppisch + */ +public class InjectEnvVarsHook { + + public static final String DEFAULT_DOMAIN_SUFFIX = ".svc.cluster.local"; + public static final String CLUSTER_WILDCARD_DOMAIN = "CLUSTER_WILDCARD_DOMAIN"; + public static final String YAKS_NAMESPACE = "YAKS_NAMESPACE"; + + @CitrusResource + private TestCaseRunner runner; + + @Before + public void injectEnvVars(Scenario scenario) { + runner.run(new AbstractTestAction() { + @Override + public void doExecute(TestContext context) { + if (scenario != null) { + context.setVariable("SCENARIO_ID", scenario.getId()); + context.setVariable("SCENARIO_NAME", scenario.getName()); + } + + Optional namespaceEnv = getEnvSetting(YAKS_NAMESPACE); + Optional domainEnv = getEnvSetting(CLUSTER_WILDCARD_DOMAIN); + + if (namespaceEnv.isPresent()) { + context.setVariable(YAKS_NAMESPACE, namespaceEnv.get()); + + if (!domainEnv.isPresent()) { + context.setVariable(CLUSTER_WILDCARD_DOMAIN, namespaceEnv.get() + DEFAULT_DOMAIN_SUFFIX); + } + } + + domainEnv.ifPresent(var -> context.setVariable(CLUSTER_WILDCARD_DOMAIN, var)); + } + }); + } + + /** + * Read environment setting. If setting is not present default to empty value. + * @param name + * @return + */ + protected Optional getEnvSetting(String name) { + return Optional.ofNullable(System.getenv(name)); + } +} diff --git a/java/steps/yaks-standard/src/test/java/org/citrusframework/yaks/hooks/InjectEnvVarsHookTest.java b/java/steps/yaks-standard/src/test/java/org/citrusframework/yaks/hooks/InjectEnvVarsHookTest.java new file mode 100644 index 00000000..900d8b65 --- /dev/null +++ b/java/steps/yaks-standard/src/test/java/org/citrusframework/yaks/hooks/InjectEnvVarsHookTest.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.citrusframework.yaks.hooks; + +import java.util.Optional; + +import com.consol.citrus.DefaultTestCaseRunner; +import com.consol.citrus.TestCaseRunner; +import com.consol.citrus.annotations.CitrusAnnotations; +import com.consol.citrus.context.TestContext; +import com.consol.citrus.context.TestContextFactory; +import org.assertj.core.api.Assertions; +import org.junit.Test; + +/** + * @author Christoph Deppisch + */ +public class InjectEnvVarsHookTest { + + @Test + @SuppressWarnings("CucumberJavaStepDefClassIsPublic") + public void shouldInjectEnvVars() { + InjectEnvVarsHook hook = new InjectEnvVarsHook() { + @Override + protected Optional getEnvSetting(String name) { + return Optional.of("foo"); + } + }; + + TestContext context = TestContextFactory.newInstance().getObject(); + TestCaseRunner runner = new DefaultTestCaseRunner(context); + CitrusAnnotations.injectTestRunner(hook, runner); + + hook.injectEnvVars(null); + + Assertions.assertThat(context.getVariable(InjectEnvVarsHook.YAKS_NAMESPACE)).isEqualTo("foo"); + Assertions.assertThat(context.getVariable(InjectEnvVarsHook.CLUSTER_WILDCARD_DOMAIN)).isEqualTo("foo"); + } + + @Test + @SuppressWarnings("CucumberJavaStepDefClassIsPublic") + public void shouldInjectEnvVarsDefaultValues() { + InjectEnvVarsHook hook = new InjectEnvVarsHook() { + @Override + protected Optional getEnvSetting(String name) { + if (name.equals(InjectEnvVarsHook.YAKS_NAMESPACE)) { + return Optional.of("foo"); + } + + return Optional.empty(); + } + }; + + TestContext context = TestContextFactory.newInstance().getObject(); + TestCaseRunner runner = new DefaultTestCaseRunner(context); + CitrusAnnotations.injectTestRunner(hook, runner); + + hook.injectEnvVars(null); + + Assertions.assertThat(context.getVariable(InjectEnvVarsHook.YAKS_NAMESPACE)).isEqualTo("foo"); + Assertions.assertThat(context.getVariable(InjectEnvVarsHook.CLUSTER_WILDCARD_DOMAIN)).isEqualTo("foo" + InjectEnvVarsHook.DEFAULT_DOMAIN_SUFFIX); + } +} diff --git a/java/steps/yaks-standard/src/test/java/org/citrusframework/yaks/standard/StandardFeatureTest.java b/java/steps/yaks-standard/src/test/java/org/citrusframework/yaks/standard/StandardFeatureTest.java index f2110992..a134035e 100644 --- a/java/steps/yaks-standard/src/test/java/org/citrusframework/yaks/standard/StandardFeatureTest.java +++ b/java/steps/yaks-standard/src/test/java/org/citrusframework/yaks/standard/StandardFeatureTest.java @@ -27,7 +27,10 @@ @RunWith(Cucumber.class) @CucumberOptions( strict = true, - glue = { "org.citrusframework.yaks.standard" }, + glue = { + "org.citrusframework.yaks.hooks", + "org.citrusframework.yaks.standard" + }, plugin = { "org.citrusframework.yaks.report.TestReporter" } ) public class StandardFeatureTest { } diff --git a/pkg/cmd/test.go b/pkg/cmd/test.go index 1235b241..3fa11d68 100644 --- a/pkg/cmd/test.go +++ b/pkg/cmd/test.go @@ -57,6 +57,8 @@ const ( ) const ( + NamespaceEnv = "YAKS_NAMESPACE" + CucumberOptions = "CUCUMBER_OPTIONS" CucumberGlue = "CUCUMBER_GLUE" CucumberFeatures = "CUCUMBER_FEATURES" @@ -433,6 +435,8 @@ func (o *testCmdOptions) uploadArtifacts(runConfig *config.RunConfig) error { func (o *testCmdOptions) setupEnvSettings(test *v1alpha1.Test, runConfig *config.RunConfig) error { env := make([]string, 0) + env = append(env, NamespaceEnv+"="+runConfig.Config.Namespace.Name) + if o.tags != nil { env = append(env, CucumberFilterTags+"="+strings.Join(o.tags, ",")) } else if len(runConfig.Config.Runtime.Cucumber.Tags) > 0 { @@ -609,7 +613,7 @@ func runScript(scriptFile, desc, namespace, baseDir, timeout string) error { command := exec.CommandContext(ctx, scriptFile) command.Env = os.Environ() - command.Env = append(command.Env, fmt.Sprintf("YAKS_NAMESPACE=%s", namespace)) + command.Env = append(command.Env, fmt.Sprintf("%s=%s", NamespaceEnv, namespace)) command.Dir = baseDir