github.com/go-chef/chef@v0.30.1/policy_group_test.go (about) 1 package chef 2 3 import ( 4 "fmt" 5 "net/http" 6 "os" 7 "testing" 8 ) 9 10 const policyGroupResponseFile = "test/policy_group_response.json" 11 const policyGroupFile = "test/policy_group.json" 12 const revisionDetailsResponseFile = "test/revision_details_response.json" 13 14 func TestPolicyGroupList(t *testing.T) { 15 setup() 16 defer teardown() 17 18 file, err := os.ReadFile(policyGroupResponseFile) 19 if err != nil { 20 t.Error(err) 21 } 22 23 mux.HandleFunc("/policy_groups", func(w http.ResponseWriter, r *http.Request) { 24 fmt.Fprintf(w, string(file)) 25 }) 26 27 data, err := client.PolicyGroups.List() 28 if err != nil { 29 t.Error(err) 30 } 31 32 if data == nil { 33 t.Fatal("We should have some data") 34 } 35 36 if len(data) != 1 { 37 t.Error("Mismatch in expected policy group count. Expected 1, Got: ", len(data)) 38 } 39 40 if _, ok := data["demo_policy_group"]; !ok { 41 t.Error("demo_policy_group policy should be listed") 42 } 43 44 } 45 46 func TestPolicyGroupGet(t *testing.T) { 47 setup() 48 defer teardown() 49 50 file, err := os.ReadFile(policyGroupFile) 51 if err != nil { 52 t.Error(err) 53 } 54 55 mux.HandleFunc("/policy_groups/testgroup", func(w http.ResponseWriter, r *http.Request) { 56 fmt.Fprintf(w, string(file)) 57 }) 58 59 data, err := client.PolicyGroups.Get("testgroup") 60 if err != nil { 61 t.Error(err) 62 } 63 64 if _, ok := data.Policies["testsamp"]; !ok { 65 t.Error("testsamp policy should be listed") 66 } 67 68 } 69 70 func TestPolicyGroupDelete(t *testing.T) { 71 setup() 72 defer teardown() 73 74 file, err := os.ReadFile(policyGroupFile) 75 if err != nil { 76 t.Error(err) 77 } 78 79 mux.HandleFunc("/policy_groups/testgroup", func(w http.ResponseWriter, r *http.Request) { 80 fmt.Fprintf(w, string(file)) 81 }) 82 83 data, err := client.PolicyGroups.Delete("testgroup") 84 if err != nil { 85 t.Error(err) 86 } 87 88 if _, ok := data.Policies["testsamp"]; !ok { 89 t.Error("testsamp policy should be listed") 90 } 91 92 } 93 94 func TestPolicyGroupGetPolicy(t *testing.T) { 95 setup() 96 defer teardown() 97 98 file, err := os.ReadFile(revisionDetailsResponseFile) 99 if err != nil { 100 t.Error(err) 101 } 102 103 mux.HandleFunc("/policy_groups/testgroup/policies/testsamp", func(w http.ResponseWriter, r *http.Request) { 104 fmt.Fprintf(w, string(file)) 105 }) 106 107 data, err := client.PolicyGroups.GetPolicy("testgroup", "testsamp") 108 if err != nil { 109 t.Error(err) 110 } 111 112 if data.Name != "testsamp" { 113 t.Error("testsamp policy should be retrieved") 114 } 115 116 } 117 118 func TestPolicyGroupDeletePolicy(t *testing.T) { 119 setup() 120 defer teardown() 121 122 file, err := os.ReadFile(revisionDetailsResponseFile) 123 if err != nil { 124 t.Error(err) 125 } 126 127 mux.HandleFunc("/policy_groups/testgroup/policies/testsamp", func(w http.ResponseWriter, r *http.Request) { 128 fmt.Fprintf(w, string(file)) 129 }) 130 131 data, err := client.PolicyGroups.DeletePolicy("testgroup", "testsamp") 132 if err != nil { 133 t.Error(err) 134 } 135 136 if data.Name != "testsamp" { 137 t.Error("testsamp policy should be retrieved") 138 } 139 140 }