https://pulumi.com logo
#python
Title
f

flat-australia-79845

06/24/2020, 7:44 AM
Hi y'all! How do I set up a fake GCP provider for unit testing? I am already setting up mocks as in the documentation, however I need to
gcp.config.region
to have a value during testing. Any idea?
g

gentle-diamond-70147

06/28/2020, 9:30 PM
We don't currently support mocking of the config object, but there's a workaround posted at https://github.com/pulumi/pulumi/issues/4472#issuecomment-618053293. You can subscribe to that issue to get updates on it.
d

damp-elephant-82829

06/29/2020, 7:12 AM
thanks. Is there a transformation of gcp:region into gcp.config.region ? Is there a method on the config to list all the parameters?
f

flat-australia-79845

06/29/2020, 8:02 AM
Thank you @gentle-diamond-70147 what I did in the end was to make Pulumi read one of my stack configuration files:
Copy code
stack = os.environ['PULUMI_STACK']
pulumi.runtime.set_mocks(PulumiMocks())
pulumi.runtime.settings._set_project(project_name)
pulumi.runtime.settings._set_stack(stack)

config_file = f'Pulumi.{stack}.yaml'
assert os.path.exists(config_file), f'Pulumi stack configuration file {config_file} should exist'

with open(config_file) as f:
    config = yaml.load(f)['config']

for key, value in config.items():
    config_value = value if not isinstance(value, (dict, list)) else json.dumps(value)
    pulumi.runtime.set_config(key, config_value)
I should not be calling the private _set_project and set_stack functions though_ šŸ˜‰
d

damp-elephant-82829

06/29/2020, 8:47 AM
šŸ˜„
f

flat-australia-79845

07/02/2020, 8:19 AM
@steep-alligator-79173 this is what I do in my conftest.py (I am using pytest) ā˜ļø
I explicitely set read from one of the config files
is the only way I found
s

steep-alligator-79173

07/02/2020, 8:21 AM
im new with testing so Im trying to follow unittest examples from pulumi website
plumi.config is called in infra.py file
not in test directly
f

flat-australia-79845

07/02/2020, 8:29 AM
I tried what you are doing, results that your config_instance variable will be empty. you need, in your test, to fill the config somehow. The way I found is with
pulumi.runtime.set_config(key, config_value)
s

steep-alligator-79173

07/02/2020, 8:36 AM
ok thx for the tip
I will try it
do you prefer to use pytest or unittest which one is better in your opinion ?
f

flat-australia-79845

07/02/2020, 8:37 AM
I have been using pytest for a long time. I like it more, because
fixtures
It is compatible with your unittest as well
s

steep-alligator-79173

07/02/2020, 9:30 AM
I managed to fix config variable problem with @flat-australia-79845 tip but now Iā€™m getting
Copy code
File "/Users/pawelmadon/git/a4b/poc/qx-pulumi/core-stack/venv/lib/python3.7/site-packages/pulumi/runtime/rpc.py", line 173, in serialize_property
    value = await serialize_property(output.future(), deps, input_transformer)
  File "/Users/pawelmadon/git/a4b/poc/qx-pulumi/core-stack/venv/lib/python3.7/site-packages/pulumi/runtime/rpc.py", line 159, in serialize_property
    future_return = await asyncio.ensure_future(awaitable)
  File "/Users/pawelmadon/git/a4b/poc/qx-pulumi/core-stack/venv/lib/python3.7/site-packages/pulumi/output.py", line 114, in get_value
    val = await self._future
  File "/Users/pawelmadon/git/a4b/poc/qx-pulumi/core-stack/venv/lib/python3.7/site-packages/pulumi/output.py", line 155, in run
    value = await self._future
  File "/Users/pawelmadon/git/a4b/poc/qx-pulumi/core-stack/venv/lib/python3.7/site-packages/pulumi/output.py", line 114, in get_value
    val = await self._future
  File "/Users/pawelmadon/git/a4b/poc/qx-pulumi/core-stack/venv/lib/python3.7/site-packages/pulumi/output.py", line 176, in run
    transformed: Input[U] = func(value)
  File "/Users/pawelmadon/git/a4b/poc/qx-pulumi/core-stack/venv/lib/python3.7/site-packages/pulumi/stack_reference.py", line 69, in <lambda>
    value: Output[Any] = Output.all(Output.from_input(name), self.outputs).apply(lambda l: l[1].get(l[0])) # type: ignore
AttributeError: 'NoneType' object has no attribute 'get'
(venv)
f

flat-australia-79845

07/02/2020, 11:34 AM
I think that's unrelated? was your infra setup working?
I'd need to see more code šŸ˜„
s

steep-alligator-79173

07/02/2020, 4:15 PM
this is a simple ec2 instane
Copy code
import pulumi
import pulumi_aws as aws
from pulumi import get_stack, ResourceOptions, StackReference
from pulumi_aws import ec2, lb

# Get AZS
azs = aws.get_availability_zones(state="available")

# Get network stack outputs
network_stack = StackReference("dev-network")

# Get VPC id
vpc = network_stack.get_output("vpc_id")

# Get public subnets ids
subnet_public_ids = network_stack.get_output("subnet_public_ids")

# Get private subnets ids
subnet_private_ids = network_stack.get_output("subnet_private_ids")

# Get instance stack config
config_instance = pulumi.Config("instance")
config_instance = pulumi.Config("instance")

# SSH access security group
sg_ssh = aws.ec2.SecurityGroup(
  f"qx-poc-{pulumi.get_stack()}-ssh",
  vpc_id=vpc,
  description='Enable SSH access',
  ingress=[
    { 'protocol': 'tcp', 'from_port': 22, 'to_port': 22, 'cidr_blocks': ['0.0.0.0/0'] }
  ],
  tags={
    'Name': f"sg-qx-poc-{pulumi.get_stack()}-ssh",
    'project': pulumi.get_project(),
    'stack': pulumi.get_stack(),
  }
)

sg_http = aws.ec2.SecurityGroup(
  f"qx-poc-{pulumi.get_stack()}-http",
  vpc_id=vpc,
  description='Enable HTTP access',
  ingress=[
    { 'protocol': 'icmp', 'from_port': 8, 'to_port': 0, 'cidr_blocks': ['0.0.0.0/0'] },
    { 'protocol': 'tcp', 'from_port': 80, 'to_port': 80, 'cidr_blocks': ['0.0.0.0/0'] },
  ],
  egress=[
    { 'protocol': 'tcp', 'from_port': 80, 'to_port': 80, 'cidr_blocks': ['0.0.0.0/0'] },
  ]
)

bastion = ec2.Instance(
  f"bastion-qx-poc-{pulumi.get_stack()}",
  instance_type=config_instance.require('bastion_size'),
  vpc_security_group_ids=[sg_ssh.id],
  ami=config_instance.require('bastion_ami'),
  subnet_id=subnet_public_ids[0],
  key_name="pawel.madon",
  tags={
    'Name': f"bastion-qx-poc-{pulumi.get_stack()}",
    'project': pulumi.get_project(),
    'stack': pulumi.get_stack(),
  }
)
f

flat-australia-79845

07/03/2020, 9:05 AM
the error message you sent me before is all you get?