Skip to content

Commit

Permalink
Merge pull request #91 from christophd/issue/90/inject-env-vars
Browse files Browse the repository at this point in the history
fix(#90): Inject environment variables as test variables before scenario
  • Loading branch information
christophd authored Apr 25, 2020
2 parents 1c7853f + 02dd5e3 commit ada111b
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 )\"(.+)\"$")
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 5 additions & 0 deletions java/steps/yaks-standard/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> namespaceEnv = getEnvSetting(YAKS_NAMESPACE);
Optional<String> 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<String> getEnvSetting(String name) {
return Optional.ofNullable(System.getenv(name));
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
6 changes: 5 additions & 1 deletion pkg/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const (
)

const (
NamespaceEnv = "YAKS_NAMESPACE"

CucumberOptions = "CUCUMBER_OPTIONS"
CucumberGlue = "CUCUMBER_GLUE"
CucumberFeatures = "CUCUMBER_FEATURES"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit ada111b

Please sign in to comment.