github.com/djenriquez/nomad-1@v0.8.1/api/sentinel_test.go (about)

     1  // +build pro ent
     2  
     3  package api
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestSentinelPolicies_ListUpsert(t *testing.T) {
    12  	t.Parallel()
    13  	c, s, _ := makeACLClient(t, nil, nil)
    14  	defer s.Stop()
    15  	ap := c.SentinelPolicies()
    16  
    17  	// Listing when nothing exists returns empty
    18  	result, qm, err := ap.List(nil)
    19  	if err != nil {
    20  		t.Fatalf("err: %s", err)
    21  	}
    22  	if qm.LastIndex != 1 {
    23  		t.Fatalf("bad index: %d", qm.LastIndex)
    24  	}
    25  	if n := len(result); n != 0 {
    26  		t.Fatalf("expected 0 policies, got: %d", n)
    27  	}
    28  
    29  	// Register a policy
    30  	policy := &SentinelPolicy{
    31  		Name:             "test",
    32  		Description:      "test",
    33  		EnforcementLevel: "advisory",
    34  		Scope:            "submit-job",
    35  		Policy:           "main = rule { true }",
    36  	}
    37  	wm, err := ap.Upsert(policy, nil)
    38  	assert.Nil(t, err)
    39  	assertWriteMeta(t, wm)
    40  
    41  	// Check the list again
    42  	result, qm, err = ap.List(nil)
    43  	if err != nil {
    44  		t.Fatalf("err: %s", err)
    45  	}
    46  	assertQueryMeta(t, qm)
    47  	if len(result) != 1 {
    48  		t.Fatalf("expected policy, got: %#v", result)
    49  	}
    50  }
    51  
    52  func TestSentinelPolicies_Delete(t *testing.T) {
    53  	t.Parallel()
    54  	c, s, _ := makeACLClient(t, nil, nil)
    55  	defer s.Stop()
    56  	ap := c.SentinelPolicies()
    57  
    58  	// Register a policy
    59  	policy := &SentinelPolicy{
    60  		Name:             "test",
    61  		Description:      "test",
    62  		EnforcementLevel: "advisory",
    63  		Scope:            "submit-job",
    64  		Policy:           "main = rule { true } ",
    65  	}
    66  	wm, err := ap.Upsert(policy, nil)
    67  	assert.Nil(t, err)
    68  	assertWriteMeta(t, wm)
    69  
    70  	// Delete the policy
    71  	wm, err = ap.Delete(policy.Name, nil)
    72  	assert.Nil(t, err)
    73  	assertWriteMeta(t, wm)
    74  
    75  	// Check the list again
    76  	result, qm, err := ap.List(nil)
    77  	if err != nil {
    78  		t.Fatalf("err: %s", err)
    79  	}
    80  	assertQueryMeta(t, qm)
    81  	if len(result) != 0 {
    82  		t.Fatalf("unexpected policy, got: %#v", result)
    83  	}
    84  }
    85  
    86  func TestSentinelPolicies_Info(t *testing.T) {
    87  	t.Parallel()
    88  	c, s, _ := makeACLClient(t, nil, nil)
    89  	defer s.Stop()
    90  	ap := c.SentinelPolicies()
    91  
    92  	// Register a policy
    93  	policy := &SentinelPolicy{
    94  		Name:             "test",
    95  		Description:      "test",
    96  		EnforcementLevel: "advisory",
    97  		Scope:            "submit-job",
    98  		Policy:           "main = rule { true }",
    99  	}
   100  	wm, err := ap.Upsert(policy, nil)
   101  	assert.Nil(t, err)
   102  	assertWriteMeta(t, wm)
   103  
   104  	// Query the policy
   105  	out, qm, err := ap.Info(policy.Name, nil)
   106  	assert.Nil(t, err)
   107  	assertQueryMeta(t, qm)
   108  	assert.Equal(t, policy.Name, out.Name)
   109  }