<commit_before>// Copyright 2019 The OPA Authors.  All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.

package e2e

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"strings"
	"sync"
	"testing"
	"time"

	"github.com/open-policy-agent/opa/logging"
	"github.com/open-policy-agent/opa/logging/test"
	"github.com/open-policy-agent/opa/runtime"
	"github.com/open-policy-agent/opa/server/types"
	"github.com/open-policy-agent/opa/util"
)

const (
	defaultAddr = ":0" // default listening address for server, use a random open port
)

// NewAPIServerTestParams creates a new set of runtime.Params with enough
// default values filled in to start the server. Options can/should
// be customized for the test case.
func NewAPIServerTestParams() runtime.Params {
	params := runtime.NewParams()

	// Add in some defaults
	params.Addrs = &[]string{defaultAddr}

	params.Logging = runtime.LoggingConfig{
		Level:  "debug",
		Format: "json-pretty",
	}

	params.GracefulShutdownPeriod = 10 // seconds

	return params
}

// TestRuntime holds metadata and provides helper methods
// to interact with the runtime being tested.
type TestRuntime struct {
	Params         runtime.Params
	Runtime        *runtime.Runtime
	Ctx            context.Context
	Cancel         context.CancelFunc
	Client         *http.Client
	ConsoleLogger  *test.Logger
	url            string
	urlMtx         *sync.Mutex
	waitForBundles bool
}

// NewTestRuntime returns a new TestRuntime.
func NewTestRuntime(params runtime.Params) (*TestRuntime, error) {
	return NewTestRuntimeWithOpts(TestRuntimeOpts{}, params)
}

// NewTestRuntimeWithOpts returns a new TestRuntime.
func NewTestRuntimeWithOpts(opts TestRuntimeOpts, params runtime.Params) (*TestRuntime, error) {

	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)

	rt, err := runtime.NewRuntime(ctx, params)
	if err != nil {
		cancel()
		return nil, fmt.Errorf("unable to create new runtime: %s", err)
	}

	return &TestRuntime{
		Params:         params,
		Runtime:        rt,
		Ctx:            ctx,
		Cancel:         cancel,
		Client:         &http.Client{},
		urlMtx:         new(sync.Mutex),
		waitForBundles: opts.WaitForBundles,
	}, nil<commit_msg>test/e2e: silence logs by default with test params (#3989)<commit_after>// Copyright 2019 The OPA Authors.  All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.

package e2e

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"strings"
	"sync"
	"testing"
	"time"

	"github.com/open-policy-agent/opa/logging"
	"github.com/open-policy-agent/opa/logging/test"
	"github.com/open-policy-agent/opa/runtime"
	"github.com/open-policy-agent/opa/server/types"
	"github.com/open-policy-agent/opa/util"
)

const (
	defaultAddr = ":0" // default listening address for server, use a random open port
)

// NewAPIServerTestParams creates a new set of runtime.Params with enough
// default values filled in to start the server. Options can/should
// be customized for the test case.
func NewAPIServerTestParams() runtime.Params {
	params := runtime.NewParams()

	// Add in some defaults
	params.Addrs = &[]string{defaultAddr}

	params.Logging = runtime.LoggingConfig{
		Level:  "debug",
		Format: "json-pretty",
	}

	// unless overridden, don't log from tests
	params.Logger = logging.NewNoOpLogger()

	params.GracefulShutdownPeriod = 10 // seconds

	return params
}

// TestRuntime holds metadata and provides helper methods
// to interact with the runtime being tested.
type TestRuntime struct {
	Params         runtime.Params
	Runtime        *runtime.Runtime
	Ctx            context.Context
	Cancel         context.CancelFunc
	Client         *http.Client
	ConsoleLogger  *test.Logger
	url            string
	urlMtx         *sync.Mutex
	waitForBundles bool
}

// NewTestRuntime returns a new TestRuntime.
func NewTestRuntime(params runtime.Params) (*TestRuntime, error) {
	return NewTestRuntimeWithOpts(TestRuntimeOpts{}, params)
}

// NewTestRuntimeWithOpts returns a new TestRuntime.
func NewTestRuntimeWithOpts(opts TestRuntimeOpts, params runtime.Params) (*TestRuntime, error) {

	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)

	rt, err := runtime.NewRuntime(ctx, params)
	if err != nil {
		cancel()
		return nil, fmt.Errorf("create new runtime: %w", err)
	}

	return &TestRuntime{
		Params:         params,
		Runtime:        rt,
		Ctx:            ctx,
		Cancel:         cancel,
		Client:         &http.Client{},
		urlMtx:         new(sync.Mutex),
		waitForBundles: opts.WaitForBundles,
	}, nil
{'fraction_non_alphanumeric': 0.10819878381211995, 'fraction_numerical': 0.004822814007129377, 'mean_word_length': 3.356164383561644, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6051213', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1513, 'n_words': 431}