Skip to content

Commit

Permalink
added tests for policy diff subcommand (#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
sagar-connect authored Aug 18, 2022
1 parent 5ef6776 commit ec7d8d3
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions cmd/policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func TestPushPolicyBundleNoPrompt(t *testing.T) {
Args: []string{"push", "./testdata/directory_not_present", "--owner-id", "test-org"},
ExpectedErr: "failed to walk policy directory path: ",
},
{
Name: "fails if policy path points to a file instead of directory",
Args: []string{"push", "./testdata/test0/policy.rego", "--owner-id", "test-org"},
ExpectedErr: "failed to walk policy directory path: policy path is not a directory",
},
{
Name: "no policy files in given policy directory path",
Args: []string{"push", "./testdata/test0/no-valid-policy-files", "--owner-id", "test-org", "--context", "custom"},
Expand Down Expand Up @@ -181,6 +186,99 @@ func TestPushPolicyBundleNoPrompt(t *testing.T) {
}
}

func TestDiffPolicyBundle(t *testing.T) {
testcases := []struct {
Name string
Args []string
ServerHandler http.HandlerFunc
ExpectedErr string
ExpectedStdErr string
ExpectedStdOut string
}{
{
Name: "requires policy bundle directory path ",
Args: []string{"diff", "--owner-id", "ownerID"},
ExpectedErr: "accepts 1 arg(s), received 0",
},
{
Name: "requires owner-id",
Args: []string{"diff", "./testdata/test0/policy.rego"},
ExpectedErr: "required flag(s) \"owner-id\" not set",
},
{
Name: "fails for policy bundle directory path not found",
Args: []string{"diff", "./testdata/directory_not_present", "--owner-id", "test-org"},
ExpectedErr: "failed to walk policy directory path: ",
},
{
Name: "fails if policy path points to a file instead of directory",
Args: []string{"diff", "./testdata/test0/policy.rego", "--owner-id", "test-org"},
ExpectedErr: "failed to walk policy directory path: policy path is not a directory",
},
{
Name: "no policy files in given policy directory path",
Args: []string{"diff", "./testdata/test0/no-valid-policy-files", "--owner-id", "test-org", "--context", "custom"},
ServerHandler: func(w http.ResponseWriter, r *http.Request) {
var body map[string]interface{}
assert.Equal(t, r.Method, "POST")
assert.Equal(t, r.URL.String(), "/api/v1/owner/test-org/context/custom/policy-bundle?dry=true")
assert.NilError(t, json.NewDecoder(r.Body).Decode(&body))
assert.DeepEqual(t, body, map[string]interface{}{
"policies": map[string]interface{}{},
})
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte("{}"))
},
ExpectedStdOut: "{}\n",
},
{
Name: "sends appropriate desired request",
Args: []string{"diff", "./testdata/test0", "--owner-id", "test-org", "--context", "custom"},
ServerHandler: func(w http.ResponseWriter, r *http.Request) {
var body map[string]interface{}
assert.Equal(t, r.Method, "POST")
assert.Equal(t, r.URL.String(), "/api/v1/owner/test-org/context/custom/policy-bundle?dry=true")
assert.NilError(t, json.NewDecoder(r.Body).Decode(&body))
assert.DeepEqual(t, body, map[string]interface{}{
"policies": map[string]interface{}{
filepath.Join("testdata", "test0", "policy.rego"): testdataContent(t, "test0/policy.rego"),
filepath.Join("testdata", "test0", "subdir", "meta-policy-subdir", "meta-policy.rego"): testdataContent(t, "test0/subdir/meta-policy-subdir/meta-policy.rego"),
},
})

w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte("{}"))
},
ExpectedStdOut: "{}\n",
},
}

for _, tc := range testcases {
t.Run(tc.Name, func(t *testing.T) {
if tc.ServerHandler == nil {
tc.ServerHandler = func(w http.ResponseWriter, r *http.Request) {}
}

svr := httptest.NewServer(tc.ServerHandler)
defer svr.Close()

cmd, stdout, stderr := makeCMD()

cmd.SetArgs(append(tc.Args, "--policy-base-url", svr.URL))

err := cmd.Execute()
if tc.ExpectedErr != "" {
assert.ErrorContains(t, err, tc.ExpectedErr)
return
}

assert.NilError(t, err)
assert.Equal(t, stdout.String(), tc.ExpectedStdOut)
assert.Equal(t, stderr.String(), tc.ExpectedStdErr)
})
}
}

func TestFetchPolicyBundle(t *testing.T) {
testcases := []struct {
Name string
Expand Down Expand Up @@ -527,6 +625,11 @@ func TestMakeDecisionCommand(t *testing.T) {
Args: []string{"decide", "--input", "./testdata/test1/test.yml"},
ExpectedErr: "either policy-path or --owner-id is required",
},
{
Name: "fails if both local-policy and owner-id are provided",
Args: []string{"decide", "./testdata/test0/policy.rego", "--input", "./testdata/test1/test.yml", "--owner-id", "test-owner"},
ExpectedErr: "either policy-path or --owner-id is required",
},
{
Name: "fails for input file not found",
Args: []string{"decide", "./testdata/test0/policy.rego", "--input", "./testdata/no_such_file.yml"},
Expand Down

0 comments on commit ec7d8d3

Please sign in to comment.