<commit_before>// Package testutil provides a default Kontrol and RegServ kites for
// using in tests.
package testutil

import (
	"fmt"
	"kite/kitekey"
	"kite/testkeys"
	"os"
	"time"

	"github.com/coreos/go-etcd/etcd"
	"github.com/dgrijalva/jwt-go"
	"github.com/nu7hatch/gouuid"
)

// WriteKiteKey writes a new kite key. (Copied and modified from regserv.go)
// If the host does not have a kite.key file kite.New() panics.
// This is a helper to put a fake key on it's location.
func WriteKiteKey() {
	tknID, err := uuid.NewV4()
	if err != nil {
		panic(err)
	}

	hostname, err := os.Hostname()
	if err != nil {
		panic(err)
	}

	token := jwt.New(jwt.GetSigningMethod("RS256"))

	token.Claims = map[string]interface{}{
		"iss":        "testuser",                    // Issuer
		"sub":        "testuser",                    // Issued to
		"iat":        time.Now().UTC().Unix(),       // Issued At
		"hostname":   hostname,                      // Hostname of registered machine
		"kontrolURL": "ws://localhost:3999/kontrol", // Kontrol URL
		"kontrolKey": testkeys.Public,               // Public key of kontrol
		"jti":        tknID.String(),                // JWT ID
	}

	key, err := token.SignedString([]byte(testkeys.Private))
	if err != nil {
		panic(err)
	}

	err = kitekey.Write(key)
	if err != nil {
		panic(err)
	}
}

func ClearEtcd() {
	etcdClient := etcd.NewClient(nil)
	_, err := etcdClient.Delete("/kites", true)
	if err != nil && err.(*etcd.EtcdError).ErrorCode != 100 { // Key Not Found
		panic(fmt.Errorf("Cannot delete keys from etcd: %s", err))
	}
}<commit_msg>do not panic when etcd is closed<commit_after>// Package testutil provides a default Kontrol and RegServ kites for
// using in tests.
package testutil

import (
	"fmt"
	"kite/kitekey"
	"kite/testkeys"
	"log"
	"os"
	"time"

	"github.com/coreos/go-etcd/etcd"
	"github.com/dgrijalva/jwt-go"
	"github.com/nu7hatch/gouuid"
)

// WriteKiteKey writes a new kite key. (Copied and modified from regserv.go)
// If the host does not have a kite.key file kite.New() panics.
// This is a helper to put a fake key on it's location.
func WriteKiteKey() {
	tknID, err := uuid.NewV4()
	if err != nil {
		panic(err)
	}

	hostname, err := os.Hostname()
	if err != nil {
		panic(err)
	}

	token := jwt.New(jwt.GetSigningMethod("RS256"))

	token.Claims = map[string]interface{}{
		"iss":        "testuser",                    // Issuer
		"sub":        "testuser",                    // Issued to
		"iat":        time.Now().UTC().Unix(),       // Issued At
		"hostname":   hostname,                      // Hostname of registered machine
		"kontrolURL": "ws://localhost:3999/kontrol", // Kontrol URL
		"kontrolKey": testkeys.Public,               // Public key of kontrol
		"jti":        tknID.String(),                // JWT ID
	}

	key, err := token.SignedString([]byte(testkeys.Private))
	if err != nil {
		panic(err)
	}

	err = kitekey.Write(key)
	if err != nil {
		panic(err)
	}
}

func ClearEtcd() {
	etcdClient := etcd.NewClient(nil)
	_, err := etcdClient.Delete("/kites", true)
	if err != nil && err.(*etcd.EtcdError).ErrorCode != 100 { // Key Not Found
		log.Fatalf(fmt.Errorf("Cannot clear etcd: %s", err).Error())
	}
}
{'fraction_non_alphanumeric': 0.15317011927181418, 'fraction_numerical': 0.007532956685499058, 'mean_word_length': 2.5608938547486035, 'pattern_counts': {'":': 14, '<': 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': '6096574', 'n_tokens_mistral': 1217, 'n_tokens_neox': 1152, 'n_words': 295}