setting global config values in karate - karate

What i am trying is that to set the global karate.config values from a feature file.
I have something very similar to this https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/oauth/oauth2.feature
but i want to set the global config from the feature file or use the feature file in karate-config.js, then how do i access a variable from the feature file when calling using karate.call. And what is the recommended pathing for it.

To set a global variable, use karate-config.js as explained here: https://github.com/intuit/karate#karate-configjs
When using the call keyword, all global variables are inherited.
There is no recommended pathing. If you want you can create complex JSON for e.g. if you return { foo: { a: 1 } } from karate-config.js you can use the value of foo.a anywhere in a feature file, for e.g:
* path 'blah', foo.a

Related

Use env variable in VueJS SPA components at runtime?

I'm building a simple SPA with VueJs and Webpack, and would like to use/access env variables from my VueJS components.
I know I will not be able to change those variables "on the fly" but instead I need to recompile-rebuild-redeploy the entire application to see the changes, but for now it's ok since the actual need is to show different info and/or sections base on the deploy environment (local, staging, production).
To be more specific, for now I'm trying to get the env var COMMIT_HASH just to display it as some applications already do just for development info.
This variable would primarily used by the pipeline for the staging deployment, as in local development would be not that useful and probably not used at all for production deployment.
The problem is that I cannot figure out how to access env variables values from my VueJS components scripts sections at runtime, as process.env is a Node method accessible only during webpack compilation and not at runtime.
I thought about using string replacement and found this question, but then I would need to update Webpack script for any new env variable I need to use, so it looks quite inelegant to me.
I also tought of loading all the env variables in a js object and somehow pass it down to the files being compiled by Webpack, but this also looks inelegant/inefficient to me.
So now I'm stuck as can't figure out how to access env vars from my VueJS components at runtime.
I ended up using string-replace-loader with regex matching and a callback for dynamic replacement.
Basically I'll use env.SOME_VARIABLE in my code and search/replace it with the variable value.
For some reason I couldn't make the regex work with the \w to match for word characters and used [a-zA-Z_] instead.
{
test: /resources.*\.js$/,
loader: 'string-replace-loader',
options: {
search: 'env\.([a-zA-Z_]+)',
replace(match, p1, offset, string) {
return process.env[p1];
},
flags: 'g'
}
}
Notes
Since it is text replacement I cannot simply assign env variable values to vue component data properties as it would be interpreted as a variable:
// E.g. given env `COMMIT_HASH=some_hash`
data() {
return {
/**
* WRONG:
* this would be parsed as
* hash: some_hash
* leading to an
* Uncaught ReferenceError: some_hash is not defined
*/
hash: env.COMMIT_HASH,
/**
* RIGHT:
* wrap string to be replaced in single/double quotes so after
* replacement it became a string literal assignment.
* hash: "some_hash"
*/
hash: "env.COMMIT_HASH",
};
},

Vue. How to pass a variable I have on server (process.env.SERVER) to the browser

On the frontend server I have a file I can read with:
if(!!process.env.SERVER) {
require("dotenv").config( { path: '/hw/.env', debug: true } )
console.log(process.env.myvar)
}
But how can I pass the vlue of process.env.myvar to the browser?
Thanks.
If you want to get some variables for all cases you can create an enviroment file with just .env as the name next to package.json in your project.
To get variables from that file you need to declare them first in the .env like this:
VUE_APP_MYVAR = 'myvar'
In your app you can use it like this:
process.env.VUE_APP_MYVAR
If you need some variables depending on the process, respectively for your enviroment, your myvar can't be called with process.env.SERVER. If SERVER is the process, you need a .env.server file next to your package.json.
You can then, again, declare myvar in .env.server like this:
VUE_APP_MYVAR = 'myvar'
To get process.env.VUE_APP_MYVAR this time you have to ensure, that you build or serve your enviroment named server. Else, your app trying to get process.env.VUE_APP_MYVAR with the process you used.
For Example: If you have .env.development and .env.server, both can contain VUE_APP_MYVAR = 'myvar' with different assigned values. Which one is picked depends on the enviroment you build or serve.
For more information, see the docs: Modes and Environment Variables
#Modes

Not able to use variable defined in karate-config.js in my feature file

I want to set a Global variable for Base Test Data location which can be used in all of my feature files.
In karate-config.js i have made below changes -
var config = {
env: env,
INPUT_JSON_PATH: 'com/company/project/module/TestData'
}
And in my feature file I am trying to use it as -
Given path '/myService'
And request read('classname:INPUT_JSON_PATH/Exception_Handling/Mandatory_Fields_missing.json')
When method POST
Then status 400
But somehow its not getting resolved and I am getting error -
could not find or read file: classname:INPUT_JSON_PATH/Exception_Handling/Mandatory_Fields_missing.json
Any idea, what am i missing here?
Thanks,
Abhi
Just keep in mind that read() and other parts of the right-hand-side are treated as ordinary JavaScript. Maybe you were intending to do this:
And request read('classpath:' + INPUT_JSON_PATH + '/Exception_Handling/Mandatory_Fields_missing.json')

karate| env specific config file

karate framework| I am trying to create multiple karate-config.js file for different env like 'test', 'stage' can somebody help me with an example how I can call env specific config values from different karate config file.
I refer this https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/karate-config-contract.js
but doesn't clarify what exactly need to do for calling different config.
This part of the karate documentation explains how to check the karate.env property in your karate-config.js in order to set configurations and variables depending on your environment.
An other way to archive different configurations per environment is explained here: environment-specific-config
All these approaches solves the issue, to configure different urls for example in your test cases.
Hence, there is no need to call or check for the environment in your feature file in order to get different configuration values. It's done by karate. Just refer to the variables you assigned in karate-config.js.
You just do something like:
Background:
* url baseUrls.userSystem
Where your karate-config.js could look like:
function fn() {
if (!env) {
env = 'local';
}
var config = {
baseUrls : {
userSystem : "http://localhost"
}
}
if (env === 'dev') {
config.baseUrls.userSystem = "http://usersystem.default.svc"
}
return config
}
The approach above demonstrate how to use just one karate-config.js for all enviroments. One file for all.
If you want to create a karate-config-<env>.js per enviroment, follow the environment-specific-config documentation.
You will find here https://github.com/intuit/karate/tree/master/karate-demo/src/test/java a default karate-config.js that will be evaluated for every environment. The karate-config-contract.js will only be evaluated after the karate-config.js file has been evaluated if and only if the karate.env property is contract.
Please read the karate documentation. Peter did a great job to document nearly every little feature karate offers.

How to set Terraform workspace variables via CLI?

I'm using workspaces in terraform to separate environments at runtime, so that I can separate deployments using a different configs.
However, I'm trying to figure out how to set CLI variables.
my variables.tf:
locals {
environment = "${terraform.workspace}"
lambda_vars = {
deploy_version = "0.1"
deploy_name = "deployment"
deploy_secret_1 = "somesupersecretsecret"
}
}
These variables are used throughout my config.
I'm trying to set new variables using the CLI, but it doesnt work, and I can't seem to find any reference to how to achieve this. I've tried:
terraform apply -var 'local.lambda_vars={ deploy_secret_1 = "somesupersecretsecret1" }'
I feel like I'm going about this the wrong way.
Can anyone help?
If you want to be able to configure the variables then you should use actual variables instead of locals. Locals are a way of composing things so you avoid repeating yourself or can use interpolation. Variables don't allow for any interpolation at all.
So in your case you should declare separate variables for things you want to be able configure. If this is just the deploy_secret_1 then you can do this with something like the following:
variable "deploy_secret_1" {
default = "somesupersecretsecret"
}
locals {
environment = terraform.workspace
lambda_vars = {
deploy_version = "0.1"
deploy_name = "deployment"
deploy_secret_1 = var.deploy_secret_1
}
}
Now if you run the following command:
terraform apply -var 'deploy_secret_1=overriding-secret'
It should overwrite the deploy_secret_1 part but leave the rest as is. If you don't specify the deploy_secret_1 variable either by the command line, environment variables or a tfvars file then it will default to somesupersecretsecret. If you'd rather force it to be defined and error if you don't specify the variable then just omit the default argument to the variable declaration.
If you wanted to be able to override more things then you should declare more variables.