github.com/fugue/regula/v2@v2.10.0/pkg/rego/rego_test.go (about)

     1  // Copyright 2021 Fugue, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package rego_test
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/fugue/regula/v2/pkg/loader"
    26  	"github.com/fugue/regula/v2/pkg/rego"
    27  	"github.com/open-policy-agent/opa/ast"
    28  	"github.com/open-policy-agent/opa/storage/inmem"
    29  	"github.com/open-policy-agent/opa/tester"
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func formatFailedTest(r *tester.Result) string {
    34  	return fmt.Sprintf("%s.%s in file %s", r.Package, r.Name, r.Location.String())
    35  }
    36  
    37  func runRegoTest(t *testing.T, providers []rego.RegoProvider) {
    38  	rego.RegisterBuiltins()
    39  	modules := map[string]*ast.Module{}
    40  	cb := func(r rego.RegoFile) error {
    41  		module, err := r.AstModule()
    42  		if err != nil {
    43  			return err
    44  		}
    45  		modules[r.Path()] = module
    46  		return nil
    47  	}
    48  	ctx := context.Background()
    49  	for _, p := range providers {
    50  		if err := p(ctx, cb); err != nil {
    51  			assert.Fail(t, "Failed to load rego files", err)
    52  		}
    53  	}
    54  	ch, err := tester.NewRunner().SetStore(inmem.New()).Run(ctx, modules)
    55  	if err != nil {
    56  		assert.Fail(t, "Failed to run tests through OPA", err)
    57  	}
    58  	failedTests := []string{}
    59  	hasFailures := false
    60  	errors := []error{}
    61  	for r := range ch {
    62  		if r.Fail {
    63  			hasFailures = true
    64  			failedTests = append(failedTests, formatFailedTest(r))
    65  		}
    66  		hasFailures = hasFailures || r.Fail
    67  		if r.Error != nil {
    68  			errors = append(errors, r.Error)
    69  		}
    70  	}
    71  
    72  	assert.Empty(t, errors)
    73  	assert.Falsef(t, hasFailures, "Some tests failed:\n%v", strings.Join(failedTests, "\n"))
    74  }
    75  
    76  func TestRegulaLib(t *testing.T) {
    77  	runRegoTest(t,
    78  		[]rego.RegoProvider{
    79  			rego.RegulaLibProvider(),
    80  			rego.LocalProvider([]string{"tests/lib"}),
    81  			rego.TestInputsProvider(
    82  				[]string{"tests/lib"},
    83  				[]loader.InputType{loader.Auto},
    84  			),
    85  		},
    86  	)
    87  }
    88  
    89  func TestRegulaRules(t *testing.T) {
    90  	runRegoTest(t,
    91  		[]rego.RegoProvider{
    92  			rego.RegulaLibProvider(),
    93  			rego.RegulaRulesProvider(),
    94  			rego.LocalProvider([]string{"tests/rules"}),
    95  			rego.TestInputsProvider(
    96  				[]string{"tests/rules"},
    97  				[]loader.InputType{loader.Auto},
    98  			),
    99  		},
   100  	)
   101  }
   102  
   103  func TestRegulaExamples(t *testing.T) {
   104  	runRegoTest(t,
   105  		[]rego.RegoProvider{
   106  			rego.RegulaLibProvider(),
   107  			rego.LocalProvider([]string{"examples", "tests/examples"}),
   108  			rego.TestInputsProvider(
   109  				[]string{"tests/examples"},
   110  				[]loader.InputType{loader.Auto},
   111  			),
   112  		},
   113  	)
   114  }
   115  
   116  // Trick to set the working directory to the rego directory
   117  func init() {
   118  	regoDir, err := filepath.Abs("../../rego")
   119  	if err != nil {
   120  		panic(err)
   121  	}
   122  	if err := os.Chdir(regoDir); err != nil {
   123  		panic(err)
   124  	}
   125  }