@@ -4,77 +4,105 @@ import (
44 "fmt"
55 "testing"
66
7- r "github.com/hashicorp/terraform/helper/resource"
7+ "github.com/hashicorp/terraform/helper/acctest"
8+ "github.com/hashicorp/terraform/helper/resource"
89 "github.com/hashicorp/terraform/terraform"
910 "github.com/hashicorp/vault/api"
1011)
1112
1213func TestResourceAuth (t * testing.T ) {
13- r .Test (t , r.TestCase {
14+ path := "github-" + acctest .RandString (10 )
15+ resource .Test (t , resource.TestCase {
1416 Providers : testProviders ,
1517 PreCheck : func () { testAccPreCheck (t ) },
16- Steps : []r .TestStep {
17- r .TestStep {
18- Config : testResourceAuth_initialConfig ,
19- Check : testResourceAuth_initialCheck ,
18+ Steps : []resource .TestStep {
19+ resource .TestStep {
20+ Config : testResourceAuth_initialConfig ( path ) ,
21+ Check : testResourceAuth_initialCheck ( path ) ,
2022 },
21- r .TestStep {
23+ resource .TestStep {
2224 Config : testResourceAuth_updateConfig ,
2325 Check : testResourceAuth_updateCheck ,
2426 },
2527 },
2628 })
2729}
2830
29- var testResourceAuth_initialConfig = `
31+ func testAccCheckAuthBackendDestroy (s * terraform.State ) error {
32+ client := testProvider .Meta ().(* api.Client )
33+
34+ auths , err := client .Sys ().ListAuth ()
35+ if err != nil {
36+ return err
37+ }
38+ for _ , rs := range s .RootModule ().Resources {
39+ if rs .Type != "vault_auth_backend" {
40+ continue
41+ }
42+ instanceState := rs .Primary
43+ if instanceState == nil {
44+ return fmt .Errorf ("resource not found in state" )
45+ }
46+
47+ if _ , ok := auths [instanceState .ID ]; ok {
48+ return fmt .Errorf ("Auth backend still exists." )
49+ }
50+ }
51+ return nil
52+ }
3053
54+ func testResourceAuth_initialConfig (path string ) string {
55+ return fmt .Sprintf (`
3156resource "vault_auth_backend" "test" {
3257 type = "github"
58+ path = "%s"
59+ description = "Test auth backend"
60+ }` , path )
3361}
3462
35- `
63+ func testResourceAuth_initialCheck (expectedPath string ) resource.TestCheckFunc {
64+ return func (s * terraform.State ) error {
65+ resourceState := s .Modules [0 ].Resources ["vault_auth_backend.test" ]
66+ if resourceState == nil {
67+ return fmt .Errorf ("resource not found in state" )
68+ }
3669
37- func testResourceAuth_initialCheck (s * terraform.State ) error {
38- resourceState := s .Modules [0 ].Resources ["vault_auth_backend.test" ]
39- if resourceState == nil {
40- return fmt .Errorf ("resource not found in state" )
41- }
70+ instanceState := resourceState .Primary
71+ if instanceState == nil {
72+ return fmt .Errorf ("resource has no primary instance" )
73+ }
4274
43- instanceState := resourceState .Primary
44- if instanceState == nil {
45- return fmt .Errorf ("resource has no primary instance" )
46- }
75+ path := instanceState .ID
4776
48- name := instanceState .ID
77+ if path + "/" != instanceState .Attributes ["path" ] {
78+ return fmt .Errorf ("id doesn't match path" )
79+ }
4980
50- if name != instanceState . Attributes [ "type" ] {
51- return fmt .Errorf ("id doesn't match name" )
52- }
81+ if path != expectedPath {
82+ return fmt .Errorf ("unexpected auth path %q, expected %q" , path , expectedPath )
83+ }
5384
54- if name != "github" {
55- return fmt .Errorf ("unexpected auth name %s" , name )
56- }
85+ client := testProvider .Meta ().(* api.Client )
86+ auths , err := client .Sys ().ListAuth ()
5787
58- client := testProvider .Meta ().(* api.Client )
59- auths , err := client .Sys ().ListAuth ()
88+ if err != nil {
89+ return fmt .Errorf ("error reading back auth: %s" , err )
90+ }
6091
61- if err != nil {
62- return fmt .Errorf ("error reading back auth: %s" , err )
63- }
92+ found := false
93+ for serverPath := range auths {
94+ if serverPath == expectedPath + "/" {
95+ found = true
96+ break
97+ }
98+ }
6499
65- found := false
66- for _ , auth := range auths {
67- if auth .Type == name {
68- found = true
69- break
100+ if ! found {
101+ return fmt .Errorf ("could not find auth backend %q in %+v" , expectedPath , auths )
70102 }
71- }
72103
73- if ! found {
74- return fmt .Errorf ("could not find auth backend %s in %+v" , name , auths )
104+ return nil
75105 }
76-
77- return nil
78106}
79107
80108var testResourceAuth_updateConfig = `
0 commit comments