github.com/rajeev159/opa@v0.45.0/topdown/runtime_test.go (about) 1 // Copyright 2018 The OPA Authors. All rights reserved. 2 // Use of this source code is governed by an Apache2 3 // license that can be found in the LICENSE file. 4 package topdown 5 6 import ( 7 "context" 8 "testing" 9 10 "github.com/open-policy-agent/opa/ast" 11 ) 12 13 func TestOPARuntime(t *testing.T) { 14 15 ctx := context.Background() 16 q := NewQuery(ast.MustParseBody("opa.runtime(x)")) // no runtime info 17 rs, err := q.Run(ctx) 18 if err != nil { 19 t.Fatal(err) 20 } else if len(rs) != 1 { 21 t.Fatal("Expected result set to contain exactly one result") 22 } 23 24 term := rs[0][ast.Var("x")] 25 exp := ast.ObjectTerm() 26 27 if ast.Compare(term, exp) != 0 { 28 t.Fatalf("Expected %v but got %v", exp, term) 29 } 30 31 q = NewQuery(ast.MustParseBody("opa.runtime(x)")).WithRuntime(ast.MustParseTerm(`{"config": {"a": 1}}`)) 32 rs, err = q.Run(ctx) 33 if err != nil { 34 t.Fatal(err) 35 } else if len(rs) != 1 { 36 t.Fatal("Expected result set to contain exactly one result") 37 } 38 39 term = rs[0][ast.Var("x")] 40 exp = ast.MustParseTerm(`{"config": {"a": 1}}`) 41 42 if ast.Compare(term, exp) != 0 { 43 t.Fatalf("Expected %v but got %v", exp, term) 44 } 45 46 } 47 48 func TestOPARuntimeConfigMasking(t *testing.T) { 49 50 ctx := context.Background() 51 q := NewQuery(ast.MustParseBody("opa.runtime(x)")).WithRuntime(ast.MustParseTerm(`{"config": { 52 "labels": {"foo": "bar"}, 53 "services": { 54 "foo": { 55 "url": "https://remote.example.com", 56 "credentials": { 57 "oauth2": { 58 "client_id": "opa_client", 59 "client_secret": "sup3rs3cr3t" 60 } 61 } 62 } 63 } 64 }}`)) 65 rs, err := q.Run(ctx) 66 if err != nil { 67 t.Fatal(err) 68 } else if len(rs) != 1 { 69 t.Fatal("Expected result set to contain exactly one result") 70 } 71 72 term := rs[0][ast.Var("x")] 73 exp := ast.MustParseTerm(`{"config": { 74 "labels": {"foo": "bar"}, 75 "services": { 76 "foo": { 77 "url": "https://remote.example.com" 78 } 79 } 80 }}`) 81 82 if ast.Compare(term, exp) != 0 { 83 t.Fatalf("Expected %v but got %v", exp, term) 84 } 85 }