<commit_before>package testutil

import (
	"bytes"
	"fmt"
	"path/filepath"
	"reflect"
	"sort"
	"strings"
	"time"

	"veyron.io/tools/lib/collect"
	"veyron.io/tools/lib/util"
)

// methods parses the given signature, which is expected to be
// generated by the "vrpc describe ..." command, extracting the list
// of methods contained in the signature of a veyron RPC server the
// input describes.
func methods(signature string) []string {
	signature = strings.TrimSpace(signature)
	result := []string{}
	lines := strings.Split(signature, "\n")
	for _, line := range lines {
		line = strings.TrimPrefix(line, "func ")
		index := strings.Index(line, "(")
		result = append(result, line[:index])
	}
	sort.Strings(result)
	return result
}

// generateTestSuite generates an xUnit test suite that encapsulates
// the given input.
func generateTestSuite(ctx *util.Context, success bool, pkg string, duration time.Duration, output string) *testSuite {
	// Generate an xUnit test suite describing the result.
	s := testSuite{Name: pkg}
	c := testCase{
		Classname: pkg,
		Name:      "Test",
		Time:      fmt.Sprintf("%.2f", duration.Seconds()),
	}
	if !success {
		fmt.Fprintf(ctx.Stdout(), "%s ... failed\n%v\n", pkg, output)
		f := testFailure{
			Message: "vrpc",
			Data:    output,
		}
		c.Failures = append(c.Failures, f)
		s.Failures++
	} else {
		fmt.Fprintf(ctx.Stdout(), "%s ... ok\n", pkg)
	}
	s.Tests++
	s.Cases = append(s.Cases, c)
	return &s
}

// testProdService test the given production service.
func testProdService(ctx *util.Context, service prodService) (*testSuite, error) {
	root, err := util.VeyronRoot()
	if err != nil {
		return nil, err
	}
	bin := filepath.Join(root, "veyron", "go", "bin", "vrpc")
	var out bytes.Buffer
	opts := ctx.Run().Opts()
	opts.Stdout = &out
	opts.Stderr = &out
	start := time.Now()
	if err := ctx.Run().TimedCommandWithOpts(DefaultTestTimeout, opts, bin, "describe", service.objectName); err != nil {
		return generateTestSuite(ctx, false, service.name, time.Now().Sub(start), out.String()), nil
	}
	if got, want := methods(out.String()), service.signature; !reflect.DeepEqual(got, want) {
		return generateTestSuite(ctx, false, service.name, time.Now().Sub(start), "mismatching signature"), nil
	}
	return generateTestSuite(ctx, true, service.name, time.Now().Sub(start), ""), nil
}

type prodService struct {
	name       string
	objectName string
	signature  []string
}

// veyronProdServicesTest runs a test of veyron production services.
func veyronProdServicesTest(ctx *util.Context, testName string) (_ *TestResult, e error) {
	// Initialize the test.
	cleanup, err := initTest(ctx, testName, nil)
	if err != nil {
		return nil, err
	}
	defer collect.Error(func() error { return cleanup() }, &e)

	// Install the vrpc tool.
	if err := ctx.Run().Command("veyron", "go", "install", "veyron.io/veyron/veyron/tools/vrpc"); err != nil {
		return nil, err
	}

	// Describe the test cases.
	namespaceRoot := "/proxy.envyor.com:8101"
	allPassed, suites := true, []testSuite{}
	services := []prodService{
		prodService{
			name:       "mounttable",
			objectName: namespaceRoot,
			signature:  []string{"Mount", "ResolveStep", "ResolveStepX", "Unmount"},
		},
		prodService{
			name:       "application repository",
			objectName: namespaceRoot + "/applicationd",
			signature:  []string{"Match", "Put", "Remove"},
		},
		prodService{
			name:       "binary repository",
			objectName: namespaceRoot + "/binaryd",
			signature:  []string{"Create", "Delete", "Download", "DownloadURL", "Stat", "Upload"},
		},
		prodService{<commit_msg>tools/lib/testutil: make sure that veyron-prod-services-test uses ns.dev.v.io and not proxy.envyor.com<commit_after>package testutil

import (
	"bytes"
	"fmt"
	"path/filepath"
	"reflect"
	"sort"
	"strings"
	"time"

	"veyron.io/tools/lib/collect"
	"veyron.io/tools/lib/util"
)

// methods parses the given signature, which is expected to be
// generated by the "vrpc describe ..." command, extracting the list
// of methods contained in the signature of a veyron RPC server the
// input describes.
func methods(signature string) []string {
	signature = strings.TrimSpace(signature)
	result := []string{}
	lines := strings.Split(signature, "\n")
	for _, line := range lines {
		line = strings.TrimPrefix(line, "func ")
		index := strings.Index(line, "(")
		result = append(result, line[:index])
	}
	sort.Strings(result)
	return result
}

// generateTestSuite generates an xUnit test suite that encapsulates
// the given input.
func generateTestSuite(ctx *util.Context, success bool, pkg string, duration time.Duration, output string) *testSuite {
	// Generate an xUnit test suite describing the result.
	s := testSuite{Name: pkg}
	c := testCase{
		Classname: pkg,
		Name:      "Test",
		Time:      fmt.Sprintf("%.2f", duration.Seconds()),
	}
	if !success {
		fmt.Fprintf(ctx.Stdout(), "%s ... failed\n%v\n", pkg, output)
		f := testFailure{
			Message: "vrpc",
			Data:    output,
		}
		c.Failures = append(c.Failures, f)
		s.Failures++
	} else {
		fmt.Fprintf(ctx.Stdout(), "%s ... ok\n", pkg)
	}
	s.Tests++
	s.Cases = append(s.Cases, c)
	return &s
}

// testProdService test the given production service.
func testProdService(ctx *util.Context, service prodService) (*testSuite, error) {
	root, err := util.VeyronRoot()
	if err != nil {
		return nil, err
	}
	bin := filepath.Join(root, "veyron", "go", "bin", "vrpc")
	var out bytes.Buffer
	opts := ctx.Run().Opts()
	opts.Stdout = &out
	opts.Stderr = &out
	start := time.Now()
	if err := ctx.Run().TimedCommandWithOpts(DefaultTestTimeout, opts, bin, "describe", service.objectName); err != nil {
		return generateTestSuite(ctx, false, service.name, time.Now().Sub(start), out.String()), nil
	}
	if got, want := methods(out.String()), service.signature; !reflect.DeepEqual(got, want) {
		return generateTestSuite(ctx, false, service.name, time.Now().Sub(start), "mismatching signature"), nil
	}
	return generateTestSuite(ctx, true, service.name, time.Now().Sub(start), ""), nil
}

type prodService struct {
	name       string
	objectName string
	signature  []string
}

// veyronProdServicesTest runs a test of veyron production services.
func veyronProdServicesTest(ctx *util.Context, testName string) (_ *TestResult, e error) {
	// Initialize the test.
	cleanup, err := initTest(ctx, testName, nil)
	if err != nil {
		return nil, err
	}
	defer collect.Error(func() error { return cleanup() }, &e)

	// Install the vrpc tool.
	if err := ctx.Run().Command("veyron", "go", "install", "veyron.io/veyron/veyron/tools/vrpc"); err != nil {
		return nil, err
	}

	// Describe the test cases.
	namespaceRoot := "/ns.dev.v.io:8101"
	allPassed, suites := true, []testSuite{}
	services := []prodService{
		prodService{
			name:       "mounttable",
			objectName: namespaceRoot,
			signature:  []string{"Mount", "ResolveStep", "ResolveStepX", "Unmount"},
		},
		prodService{
			name:       "application repository",
			objectName: namespaceRoot + "/applicationd",
			signature:  []string{"Match", "Put", "Remove"},
		},
		prodService{
			name:       "binary repository",
			objectName: namespaceRoot + "/binaryd",
			signature:  []string{"Create", "Delete", "Download", "DownloadURL", "Stat", "Upload"},
		},
		prodService{
{'fraction_non_alphanumeric': 0.1559303290019353, 'fraction_numerical': 0.0013823610727121925, 'mean_word_length': 4.0277970813064625, 'pattern_counts': {'":': 0, '<': 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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2444540', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2397, 'n_words': 660}