Skip to content

Commit

Permalink
Implementation of detailed API Calls and Unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Mudassar Shafique <[email protected]>
  • Loading branch information
mudash authored and MarkGibbons committed Apr 28, 2020
1 parent 67e8cf6 commit 8e74c1e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 16 deletions.
24 changes: 12 additions & 12 deletions policy.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package chef

import (
"fmt"
)

// PolicyService is the service for interacting with chef server policies endpoint
type PolicyService struct {
client *Client
Expand All @@ -21,9 +25,9 @@ type PolicyRevisionDetail map[string]interface{}
// RevisionDetailsResponse is returned from the chef-server for Get Requests to /policies/<policy-name>/revisions/<rev-id>
type RevisionDetailsResponse struct {
RevisionID string `json:"revision_id,omitempty"`
Name string `json:"name,omitempty"`
RunList []string `json:"run_list,omitempty"`
IncludePolicyLocks []string `json:"included_policy_locks,omitempty"`
Name string `json:"name",omitempty`
RunList []string `json:"run_list,omitempty`
IncludePolicLocks []string `json:"included_policy_locks,omitempty"`
CookbookLocks map[string]CookbookLock `json:"cookbook_locks,omitempty"`
DefaultAttributes map[string]interface{} `json:"default_attributes,omitempty"`
OverrideAttributes map[string]interface{} `json:"override_attributes,omitempty"`
Expand All @@ -43,19 +47,19 @@ type SCMDetail struct {
Remote string `json:"remote,omitempty"`
Revision string `json:"revision,omitempty"`
WorkingTreeClean bool `json:"working_tree_clean,omitempty"`
Published bool `json:"published,omitempty"`
published bool `json:"published,omitempty"`
SynchronizedRemoteBranches []string `json:"synchronized_remote_branches,omitempty"`
}
type SolutionDep struct {
PolicyFile [][]string `json:"Policyfile,omitempty"`
Dependencies interface{} `json:"dependencies,omitempty"`
PolicyFile [][]string `json:"Policyfile,omitempty"`
Depdendencies interface{} `json:"dependencies,omitempty"`
}

// List lists the policies in the Chef server.
// Chef API docs: https://docs.chef.io/api_chef_server/#policies
// GET /policies
func (c *PolicyService) List() (data PoliciesGetResponse, err error) {
err = c.client.magicRequestDecoder("GET", "policies", nil, &data)
func (e *PolicyService) List() (data PoliciesGetResponse, err error) {
err = e.client.magicRequestDecoder("GET", "policies", nil, &data)
return
}

Expand All @@ -72,9 +76,5 @@ func (c *PolicyService) Get(name string) (data PolicyGetResponse, err error) {
func (c *PolicyService) GetRevisionDetails(policyName string, revisionID string) (data RevisionDetailsResponse, err error) {
path := fmt.Sprintf("policies/%s/revisions/%s", policyName, revisionID)
err = c.client.magicRequestDecoder("GET", path, nil, &data)
=======
func (e *PolicyService) List() (data PoliciesGetResponse, err error) {
err = e.client.magicRequestDecoder("GET", "policies", nil, &data)
>>>>>>> 8fe0f4a... Policy and Policy Group API calls initial work
return
}
4 changes: 2 additions & 2 deletions policy_group.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package chef

// PolicyService is the service for interacting with chef server policies endpoint
// PolicyGroupService is the service for interacting with chef server policies endpoint
type PolicyGroupService struct {
client *Client
}

// PolicyGetResponse is returned from the chef-server for Get Requests to /policies
// PolicyGroupGetResponse is returned from the chef-server for Get Requests to /policy_groups
type PolicyGroupGetResponse map[string]PolicyGroup

type PolicyGroup struct {
Expand Down
70 changes: 68 additions & 2 deletions policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
)

const policyListResponseFile = "test/policies_response.json"
const policyRevisionResponseFile = "test/policy_revision_response.json"

func TestPolicyList(t *testing.T) {
func TestListPolicies(t *testing.T) {
setup()
defer teardown()

Expand All @@ -30,7 +31,6 @@ func TestPolicyList(t *testing.T) {
if data == nil {
t.Fatal("We should have some data")
}
fmt.Println(data)

if len(data) != 2 {
t.Error("Mismatch in expected policies count. Expected 2, Got: ", len(data))
Expand All @@ -45,3 +45,69 @@ func TestPolicyList(t *testing.T) {
}

}

func TestGetPolicy(t *testing.T) {
setup()
defer teardown()

policyGetJSON := `{
"revisions": {
"8228b0e381fe1de3ee39bf51e93029dbbdcecc61fb5abea4ca8c82591c0b529b": {
}
}
}`
mux.HandleFunc("/policies/base", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, policyGetJSON)
})
mux.HandleFunc("/policies/bad", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not Found", 404)
})

data, err := client.Policies.Get("base")
if err != nil {
t.Error(err)
}

if _, ok := data["revisions"]["8228b0e381fe1de3ee39bf51e93029dbbdcecc61fb5abea4ca8c82591c0b529b"]; !ok {
t.Error("Missing expected revision for this policy")
}

_, err = client.Policies.Get("bad")
if err == nil {
t.Error("We expected this bad request to error", err)
}
}

func TestGetPolicyRevision(t *testing.T) {
setup()
defer teardown()

const policyName = "base"
const policyRevision = "8228b0e381fe1de3ee39bf51e93029dbbdcecc61fb5abea4ca8c82591c0b529b"

file, err := ioutil.ReadFile(policyRevisionResponseFile)
if err != nil {
t.Error(err)
}

mux.HandleFunc(fmt.Sprintf("/policies/%s/revisions/%s", policyName, policyRevision), func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, string(file))
})

data, err := client.Policies.GetRevisionDetails(policyName, policyRevision)
if err != nil {
t.Error(err)
}

if val, ok := data.CookbookLocks["hardening"]; !ok {
t.Error("Expected hardening policy to be present in the policy information")
} else if val.Version != "0.1.0" {
t.Error("Expected hardening policy version to be 0.1.0, got: ", val.Version)
}

//if data.CookbookLocks["hardening"].Version != "0.1.0" {
// t.Fatal("Expected hardening policy version to be 0.1.0, got: %s", data.CookbookLocks["hardening"].Version)
//}

}

0 comments on commit 8e74c1e

Please sign in to comment.