thiagonache / terraformtest

Easier way to unit test terraform
MIT License
51 stars 8 forks source link

Add new function to do test paperwork #14

Open thiagonache opened 3 years ago

thiagonache commented 3 years ago

I want to reduce the paperwork required to run a test. Eg.:

        tfPlan, err := terraformtest.ReadPlan("./mymodule.plan.json")
    if err != nil {
        t.Fatalf("Cannot read plan file ./mymodule.plan.json: %v", err)
    }

    wantResource := terraformtest.Resource{
        Address: "module.vpc.aws_vpc.this[0]",
        Metadata: map[string]string{
            "type": "aws_vpc",
            "name": "this",
        },
        Values: map[string]string{
            "enable_dns_hostnames":             "true",
            "enable_dns_support":               "true",
            "assign_generated_ipv6_cidr_block": "false",
            "cidr_block":                       "10.0.0.0/16",
            "instance_tenancy":                 "default",
        },
    }

    gotRS := tfPlan.Resources
    if !gotRS.Contains(wantResource) {
        t.Error(gotRS.Diff())
    }

Could be simplified to

    wantResource := terraformtest.Resource{
        Address: "module.vpc.aws_vpc.this[0]",
        Metadata: map[string]string{
            "type": "aws_vpc",
            "name": "this",
        },
        Values: map[string]string{
            "enable_dns_hostnames":             "true",
            "enable_dns_support":               "true",
            "assign_generated_ipv6_cidr_block": "false",
            "cidr_block":                       "10.0.0.0/16",
            "instance_tenancy":                 "default",
        },
    }

        r := terraformtest.New(
                terraformtest.WithPlanFile("./mymodule.plan.json")
        )
    terraformtest.Contains(wantResource)
thiagonache commented 3 years ago

I'll check this issue.