This message was deleted.
s
This message was deleted.
l
I have various resources I create in my ComponentResource that I don't need to have properties for (Routes, NACL rules, etc.), but I want to test for in my unit tests. If I add properties for them and
apply
on those properties, then I can see the resources in my test context (variables that I populate inside the
newResource
callback in
setMocks
). But I can't reliably see those resources in the context if I apply on other things (e.g. RouteTables or NACLs). If there's a way to avoid creating properties just for testing, I'd be interested.
w
I don’t think I quite follow the full scenario here. Any chance you have a code snippet example of this you could share?
l
It's a bit big. Here's the gist of it. Snippet from my ComponentResource constructor:
Copy code
this.isolatedNacl = new aws.ec2.NetworkAcl(name, {
      vpcId: this.vpc.id,
      subnetIds: this.vpc.isolatedSubnetIds
    }, this.parentOpts(this));
new aws.ec2.NetworkAclRule(name}, {
      ruleNumber: 200,
      ruleAction: "allow", protocol: "-1",
      networkAclId: this.isolatedNacl.id,
      cidrBlock: this.publicSubnet.subnet.cidrBlock
    }, opts);
The NACL rule isn't interesting to my app, it just needs to be created. In the test for my ComponentResource, I want to ensure that the CIDR block of my isolatedNacl's only rule is the same as the CIDR block of the public subnet. I have a
this.publicSubnet.subnet.cidrBlock
that I can
apply
on, all good. But I don't have anything to
apply
to get the rule's CIDR block. So, I was trying to use the stub code in my Mocha test to get it.
Copy code
let isolatedRuleCidrBlock: CidrBlock; // This is within my outermost "describe" function
    pulumi.runtime.setMocks({
      newResource: function (type: string, name: string, inputs: any): { id: string, state: any } {
        let state = { name: name, ...inputs };
        if (type == 'aws:ec2/networkAclRule:NetworkAclRule') { isolatedRuleCidrBlock = state; } // Remember the rule for later
        return {
          id: `${name}_id`,
          state: state,
        };
      },
      call: (token: string, args: any, provider?: string) => {
        switch (token) {
          case "aws:index/getAvailabilityZones:getAvailabilityZones":
            return {
              names: ["ap-southeast-2a", "ap-southeast-2b"],
              zoneIds: ["southeast2a", "southeast2b"],
            };
        }
        return args;
      },
    });
Then my test can be something like:
Copy code
it('should allow access to the isolated subnets from the public subnet', async function check_isolated_public_access() {
      pulumi.output(objectUnderTest.publicSubnet.subnet.cidrBlock).apply((subnetCidr)) => {
        expect(subnetCidir).to.eql(isolatedRuleCidrBlock);
      }
But I couldn't get it to work. I've resorted to storing the rule in my ComponentResource so that I can apply it (and pass it to
pulumi.all()
at the same time as my subnet CIDR block). This is working, but it's more app code than I need...
The rule is definitely in my stack, and I see it get created in the
newResource
function, but it's not there at the right time: my test can't see it. The Pulumi engine creates it correctly, my test is just running too soon because it hasn't run
apply
on the right thing.