open-nomad/api/sentinel_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

102 lines
2.2 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
//go:build ent
2017-09-19 14:47:10 +00:00
package api
import (
"testing"
"github.com/hashicorp/nomad/api/internal/testutil"
"github.com/shoenig/test/must"
2017-09-19 14:47:10 +00:00
)
func TestSentinelPolicies_ListUpsert(t *testing.T) {
testutil.Parallel(t)
2017-09-19 14:47:10 +00:00
c, s, _ := makeACLClient(t, nil, nil)
defer s.Stop()
ap := c.SentinelPolicies()
// Listing when nothing exists returns empty
result, qm, err := ap.List(nil)
must.NoError(t, err)
must.Positive(t, qm.LastIndex)
must.SliceEmpty(t, result)
2017-09-19 14:47:10 +00:00
// Register a policy
policy := &SentinelPolicy{
Name: "test",
Description: "test",
EnforcementLevel: "advisory",
Scope: "submit-job",
Policy: "main = rule { true }",
}
wm, err := ap.Upsert(policy, nil)
must.NoError(t, err)
2017-09-19 14:47:10 +00:00
assertWriteMeta(t, wm)
// Check the list again
result, qm, err = ap.List(nil)
must.NoError(t, err)
2017-09-19 14:47:10 +00:00
assertQueryMeta(t, qm)
must.Len(t, 1, result)
2017-09-19 14:47:10 +00:00
}
func TestSentinelPolicies_Delete(t *testing.T) {
testutil.Parallel(t)
2017-09-19 14:47:10 +00:00
c, s, _ := makeACLClient(t, nil, nil)
defer s.Stop()
ap := c.SentinelPolicies()
// Register a policy
policy := &SentinelPolicy{
Name: "test",
Description: "test",
EnforcementLevel: "advisory",
Scope: "submit-job",
Policy: "main = rule { true } ",
}
wm, err := ap.Upsert(policy, nil)
must.NoError(t, err)
2017-09-19 14:47:10 +00:00
assertWriteMeta(t, wm)
// Delete the policy
wm, err = ap.Delete(policy.Name, nil)
must.NoError(t, err)
2017-09-19 14:47:10 +00:00
assertWriteMeta(t, wm)
// Check the list again
result, qm, err := ap.List(nil)
must.NoError(t, err)
2017-09-19 14:47:10 +00:00
assertQueryMeta(t, qm)
must.SliceEmpty(t, result)
2017-09-19 14:47:10 +00:00
}
func TestSentinelPolicies_Info(t *testing.T) {
testutil.Parallel(t)
2017-09-19 14:47:10 +00:00
c, s, _ := makeACLClient(t, nil, nil)
defer s.Stop()
ap := c.SentinelPolicies()
// Register a policy
policy := &SentinelPolicy{
Name: "test",
Description: "test",
EnforcementLevel: "advisory",
Scope: "submit-job",
Policy: "main = rule { true }",
}
wm, err := ap.Upsert(policy, nil)
must.NoError(t, err)
2017-09-19 14:47:10 +00:00
assertWriteMeta(t, wm)
// Query the policy
out, qm, err := ap.Info(policy.Name, nil)
must.NoError(t, err)
2017-09-19 14:47:10 +00:00
assertQueryMeta(t, qm)
must.Eq(t, policy.Name, out.Name)
2017-09-19 14:47:10 +00:00
}