|
| 1 | +package tailscale_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/google/go-cmp/cmp" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 11 | + |
| 12 | + "github.com/tailscale/hujson" |
| 13 | + "github.com/tailscale/terraform-provider-tailscale/tailscale" |
| 14 | +) |
| 15 | + |
| 16 | +func TestAccTailscaleACL(t *testing.T) { |
| 17 | + resourceName := "data.tailscale_acl.acl" |
| 18 | + |
| 19 | + resource.Test(t, resource.TestCase{ |
| 20 | + PreCheck: func() { testAccPreCheck(t) }, |
| 21 | + ProviderFactories: testAccProviderFactories(t), |
| 22 | + Steps: []resource.TestStep{ |
| 23 | + { |
| 24 | + Config: `data "tailscale_acl" "acl" {}`, |
| 25 | + Check: func(s *terraform.State) error { |
| 26 | + client := testAccProvider.Meta().(*tailscale.Clients).V2 |
| 27 | + acl, err := client.PolicyFile().Raw(context.Background()) |
| 28 | + if err != nil { |
| 29 | + return fmt.Errorf("unable to get ACL: %s", err) |
| 30 | + } |
| 31 | + |
| 32 | + huj, err := hujson.Parse([]byte(acl)) |
| 33 | + if err != nil { |
| 34 | + return fmt.Errorf("Failed to parse ACL as HuJSON: %s", err) |
| 35 | + } |
| 36 | + expected := huj.String() |
| 37 | + |
| 38 | + rs := s.RootModule().Resources[resourceName].Primary |
| 39 | + actual := rs.Attributes["hujson"] |
| 40 | + if diff := cmp.Diff(expected, actual); diff != "" { |
| 41 | + return fmt.Errorf("wrong ACL (-got, +want): %s", diff) |
| 42 | + } |
| 43 | + |
| 44 | + return nil |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }) |
| 49 | +} |
0 commit comments