jeudi 27 août 2020

Create schema.ResourceData instance with values in it for unit tests

I want to write a unit test for a method similar to this:

package bingo

import "github.com/hashicorp/terraform-plugin-sdk/helper/schema"

func doStuff(d *schema.ResourceData) error {
    foo := d.Get("foo").(string)
    // ... operations using d
    return nil
}

Is there a way to create an instance of type schema.ResourceData with values in it?

I tried creating a "blank" instance of ResourceData and populate it via .Set(...). But it doesn't work, since no schema is present:

package bingo

import "github.com/hashicorp/terraform-plugin-sdk/helper/schema"

func TestDoStuff(t *testing.T) {
    d := schema.ResourceData{}
    err := d.Set("foo", "bar")
    if err != nil {
      t.Errorf("failed to set value: %s", err)
      // > failed to set value: Invalid address to set: []string{"foo"}
    }

    // test doStuff()
}

Aucun commentaire:

Enregistrer un commentaire