<commit_before>package levelkv

import "bytes"
import "fmt"
import "os"
import "testing"

import "github.com/syndtr/goleveldb/leveldb"
import "github.com/syndtr/goleveldb/leveldb/storage"

var db *DB

func TestMain(m *testing.M) {
	ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
	if err != nil {
		fmt.Printf("Unable to create levelDB database: %v\n", err)
		os.Exit(-1)
	}
	db = Wrap(ldb, NewConfig())

	r := m.Run()
	db.Close()
	os.Exit(r)
}

func TestPutGet(t *testing.T) {
	testKey, testVal := []byte("bananas"), []byte("monkeys")
	putErr := db.Put(testKey, testVal, nil)
	if putErr != nil {
		t.Fatalf("Database put failed: %v\n", putErr)
	}

	getVal, getErr := db.Get(testKey, nil)
	if getErr != nil {
		t.Fatalf("Database get failed: %v\n", getErr)
	}
	if bytes.Compare(getVal, testVal) != 0 {
		t.Errorf("Database get received %v instead of %v\n", getVal, testVal)
	}
}

func TestPutGetPutGet(t *testing.T) {
	testKey, testVal := []byte("apples"), []byte("bananas")
	putErr := db.Put(testKey, testVal, nil)
	if putErr != nil {
		t.Fatalf("Database put failed: %v\n", putErr)
	}

	getVal, getErr := db.Get(testKey, nil)
	if getErr != nil {
		t.Fatalf("Database get failed: %v\n", getErr)
	}
	if bytes.Compare(getVal, testVal) != 0 {
		t.Errorf("Database get received %v instead of %v\n", getVal, testVal)
	}

	testVal2 := []byte("oranges")
	putErr2 := db.Put(testKey, testVal2, nil)
	if putErr2 != nil {
		t.Fatalf("Database put failed: %v\n", putErr2)
	}

	getVal2, getErr2 := db.Get(testKey, nil)
	if getErr2 != nil {
		t.Fatalf("Database get failed: %v\n", getErr2)
	}
	if bytes.Compare(getVal2, testVal2) != 0 {
		t.Errorf("Database get received %v instead of %v\n", getVal2, testVal2)
	}
}

func TestMultiPutGet(t *testing.T) {
	testKey, testVal := []byte("apples"), []byte("bananas")
	testVal2 := []byte("oranges")

	// Interlace requests between the two go routines
	sync := make(chan bool, 1)
	// sync <- true
	finished := make(chan bool, 2)
	go func() {
		for i := 0; i < 1000; i++ {
			putErr := db.Put(testKey, testVal, nil)
			if putErr != nil {
				t.Fatalf("Database put failed: %v\n", putErr)
			}

			sync <- true
			_ = <-sync

			getVal, getErr := db.Get(testKey, nil)
			if getErr != nil {
				t.Fatalf("Database get failed: %v\n", getErr)
			}
			if bytes.Compare(getVal, testVal) != 0 && bytes.Compare(getVal, testVal2) != 0 {
				t.Errorf("Database get received %v instead of %v\n", getVal, testVal)
			}
		}
		finished <- true
	}()

	go func() {
		for i := 0; i < 1000; i++ {
			putErr2 := db.Put(testKey, testVal2, nil)
			if putErr2 != nil {
				t.Fatalf("Database put failed: %v\n", putErr2)
			}

			sync <- true
			_ = <-sync
			getVal2, getErr2 := db.Get(testKey, nil)
			if getErr2 != nil {
				t.Fatalf("Database get failed: %v\n", getErr2)
			}
			if bytes.Compare(getVal2, testVal2) != 0 && bytes.Compare(getVal2, testVal) != 0 {
				t.Errorf("Database get received %v instead of %v\n", getVal2, testVal2)
			}
		}
		finished <- true
	}()

	_ = <-finished
	_ = <-finished
}
<commit_msg>Fix test to allow for natural goroutine interlacing<commit_after>package levelkv

import "bytes"
import "fmt"
import "os"
import "testing"

import "github.com/syndtr/goleveldb/leveldb"
import "github.com/syndtr/goleveldb/leveldb/storage"

var db *DB

func TestMain(m *testing.M) {
	ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
	if err != nil {
		fmt.Printf("Unable to create levelDB database: %v\n", err)
		os.Exit(-1)
	}
	db = Wrap(ldb, NewConfig())

	r := m.Run()
	db.Close()
	os.Exit(r)
}

func TestPutGet(t *testing.T) {
	testKey, testVal := []byte("bananas"), []byte("monkeys")
	putErr := db.Put(testKey, testVal, nil)
	if putErr != nil {
		t.Fatalf("Database put failed: %v\n", putErr)
	}

	getVal, getErr := db.Get(testKey, nil)
	if getErr != nil {
		t.Fatalf("Database get failed: %v\n", getErr)
	}
	if bytes.Compare(getVal, testVal) != 0 {
		t.Errorf("Database get received %v instead of %v\n", getVal, testVal)
	}
}

func TestPutGetPutGet(t *testing.T) {
	testKey, testVal := []byte("apples"), []byte("bananas")
	putErr := db.Put(testKey, testVal, nil)
	if putErr != nil {
		t.Fatalf("Database put failed: %v\n", putErr)
	}

	getVal, getErr := db.Get(testKey, nil)
	if getErr != nil {
		t.Fatalf("Database get failed: %v\n", getErr)
	}
	if bytes.Compare(getVal, testVal) != 0 {
		t.Errorf("Database get received %v instead of %v\n", getVal, testVal)
	}

	testVal2 := []byte("oranges")
	putErr2 := db.Put(testKey, testVal2, nil)
	if putErr2 != nil {
		t.Fatalf("Database put failed: %v\n", putErr2)
	}

	getVal2, getErr2 := db.Get(testKey, nil)
	if getErr2 != nil {
		t.Fatalf("Database get failed: %v\n", getErr2)
	}
	if bytes.Compare(getVal2, testVal2) != 0 {
		t.Errorf("Database get received %v instead of %v\n", getVal2, testVal2)
	}
}

func TestMultiPutGet(t *testing.T) {
	testKey, testVal := []byte("apples"), []byte("bananas")
	testVal2 := []byte("oranges")

	// Interlace requests between the two go routines
	finished := make(chan bool, 2)
	go func() {
		for i := 0; i < 1000; i++ {
			putErr := db.Put(testKey, testVal, nil)
			if putErr != nil {
				t.Fatalf("Database put failed: %v\n", putErr)
			}

			getVal, getErr := db.Get(testKey, nil)
			if getErr != nil {
				t.Fatalf("Database get failed: %v\n", getErr)
			}
			if bytes.Compare(getVal, testVal) != 0 && bytes.Compare(getVal, testVal2) != 0 {
				t.Errorf("Database get received %v instead of %v\n", getVal, testVal)
			}
		}
		finished <- true
	}()

	go func() {
		for i := 0; i < 1000; i++ {
			putErr2 := db.Put(testKey, testVal2, nil)
			if putErr2 != nil {
				t.Fatalf("Database put failed: %v\n", putErr2)
			}

			getVal2, getErr2 := db.Get(testKey, nil)
			if getErr2 != nil {
				t.Fatalf("Database get failed: %v\n", getErr2)
			}
			if bytes.Compare(getVal2, testVal2) != 0 && bytes.Compare(getVal2, testVal) != 0 {
				t.Errorf("Database get received %v instead of %v\n", getVal2, testVal2)
			}
		}
		finished <- true
	}()

	_ = <-finished
	_ = <-finished
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"os"
	"strings"
	"time"

	"io/ioutil"

	"github.com/docker/libkv"
	"github.com/docker/libkv/store"
	"github.com/docker/libkv/store/etcd"
	"github.com/docker/libkv/store/consul"
	"github.com/docker/libkv/store/zookeeper"
)

const (
	appName = "kviator"
	version = "0.0.6"

	helpText = `
	kviator is a cli client for accessing consul, etcd, or zookeper KV.

	Syntax:

	    kviator --kvstore [consul|etcd|zzookeper] --client <kv_addr> <command> <key> [<val>]

	Options:
	    --kvstore     The kvstore to connect to. Can be consul, etcd, or zookeper.
	    --client      The url of the kvstore. (eg. localhost:8500)
	    --show-value  Show the value when using the list command.

	Commands:
	    put           put a key value pair in the kvstore
	    get           retrieve a key value pair from the kvstore
	    del           removes a key value pair from the kvstore
	    deltree       removes an entire tree structure in the kvstore
	    list          list all kv of a given subtree/key.
	    cas           put a key value pair in the keystore only when it's empty
	    exists        returns true when key value pair exists

	Arguments:
	    key           The key. Required for all commands.
	    val           The value. required for put and cas.

	Note:

	    kviator can also read the value from Stdin. The syntax would look like this:

	        cmd | kviator ... put -
	        kviator ... put - < val.file

	    The - character is necessary to force kviator to read from Stdin. Without the -, Stdin
	    is ignored.
	`
)

var (
	kvstore   string
	client    string
	showValue bool
)

func init() {
	flag.StringVar(&kvstore, "kvstore", "", "the kvstore to connect to. Can be consul, etcd, or zookeper.")
	flag.StringVar(&client, "client", "", "the client IP address")
	flag.BoolVar(&showValue, "show-value", false, "show the value of the listed keys")
	flag.Usage = help
	flag.Parse()
}

func main() {
	switch flag.Arg(0) {
	case "put":
		key := flag.Arg(1)
		val := strings.Join(flag.Args()[2:], " ")
		val = parseVal(val)
		save(key, val)
	case "get":
		key := flag.Arg(1)
		retrieve(key)
	case "del":
		key := flag.Arg(1)
		delete(key)
	case "deltree":
		key := flag.Arg(1)
		deleteTree(key)
	case "list":
		key := flag.Arg(1)
		list(key)
	case "cas":
		key := flag.Arg(1)
		val := strings.Join(flag.Args()[2:], " ")
		val = parseVal(val)
		checkAndSave(key, val)
	case "exists":
		key := flag.Arg(1)
		keyExists(key)
	default:
		help()
		os.Exit(8)
	}
}

func parseVal(arg string) string {
	arg = strings.TrimSpace(arg)
	if arg == "-" {
		stat, err := os.Stdin.Stat()
		if err == nil {
			if (stat.Mode() & os.ModeCharDevice) == 0 {
				in, _ := ioutil.ReadAll(os.Stdin)
				inStr := strings.TrimSuffix(string(in), "\n")
				inStr = strings.TrimSpace(inStr)
				return inStr
			}
		}
	}
	return arg
}

func kvstoreConn(kvstore, client string) store.Store {
	var backend store.Backend
	switch kvstore {
	case "consul":
		backend = store.CONSUL
		consul.Register()
	case "etcd":
		backend = store.ETCD
		etcd.Register()
	case "zookeper":
		backend = store.ZK
		zookeeper.Register()
	}
	kv, err := libkv.NewStore(
		backend,
		[]string{client},
		&store.Config{
			ConnectionTimeout: 10 * time.Second,
		},
	)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	return kv
}

func save(key, val string) {
	kv := kvstoreConn(kvstore, client)
	err := kv.Put(key, []byte(val), nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(2)
	}
}

func retrieve(key string) {
	kv := kvstoreConn(kvstore, client)
	kvPair, err := kv.Get(key)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(3)
	} else {
		fmt.Fprintln(os.Stdout, string(kvPair.Value))
	}
}

func delete(key string) {
	kv := kvstoreConn(kvstore, client)
	err := kv.Delete(key)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(4)
	}
}

func deleteTree(key string) {
	if key == "" {
		fmt.Fprintln(os.Stderr, "Please specify subtree. To delete all, use /.")
		os.Exit(11)
	}
	if key == "/" {
		key = ""
	}
	kv := kvstoreConn(kvstore, client)
	err := kv.DeleteTree(key)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(10)
	}
}

func list(key string) {
	kv := kvstoreConn(kvstore, client)
	kvPairs, err := kv.List(key)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(11)
	}
	for _, kvPair := range kvPairs {
		k := kvPair.Key
		v := string(kvPair.Value)
		if showValue {
			fmt.Fprintf(os.Stdout, "%s=%s\n", k, v)
		} else {
			fmt.Fprintln(os.Stdout, k)
		}

	}
}

func checkAndSave(key, val string) {
	kv := kvstoreConn(kvstore, client)
	_, err := kv.Get(key)
	if err != nil {
		err := kv.Put(key, []byte(val), nil)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(6)
		}
	} else {
		fmt.Fprintln(os.Stderr, "key is already set")
		os.Exit(5)
	}
}

func keyExists(key string) {
	kv := kvstoreConn(kvstore, client)
	_, err := kv.Get(key)
	if err != nil {
		fmt.Fprintln(os.Stderr, "false")
		os.Exit(7)
	} else {
		fmt.Fprintln(os.Stdout, "true")
	}
}

func help() {
	fmt.Fprintf(os.Stdout, "\n\t%s %s\n\n%s\n", appName, version, helpText)
}
<commit_msg>TLS support<commit_after>package main

import (
	"flag"
	"fmt"
	"os"
	"strings"
	"time"

	"io/ioutil"
	"github.com/coreos/etcd/pkg/transport"

	"github.com/docker/libkv"
	"github.com/docker/libkv/store"
	"github.com/docker/libkv/store/etcd"
	"github.com/docker/libkv/store/consul"
	"github.com/docker/libkv/store/zookeeper"
)

const (
	appName = "kviator"
	version = "0.0.6"

	helpText = `
	kviator is a cli client for accessing consul, etcd, or zookeper KV.

	Syntax:

	    kviator --kvstore [consul|etcd|zzookeper] --client <kv_addr> <command> <key> [<val>]

	Options:
	    --kvstore     The kvstore to connect to. Can be consul, etcd, or zookeper.
	    --client      The url of the kvstore. (eg. localhost:8500)
	    --show-value  Show the value when using the list command.
	    --ca-cert     The CA certificate to use for TLS
	    --client-cert The Client cert to use for TLS authentication
	    --client-key  The client key to use for TLS authentication

	Commands:
	    put           put a key value pair in the kvstore
	    get           retrieve a key value pair from the kvstore
	    del           removes a key value pair from the kvstore
	    deltree       removes an entire tree structure in the kvstore
	    list          list all kv of a given subtree/key.
	    cas           put a key value pair in the keystore only when it's empty
	    exists        returns true when key value pair exists

	Arguments:
	    key           The key. Required for all commands.
	    val           The value. required for put and cas.

	Note:

	    kviator can also read the value from Stdin. The syntax would look like this:

	        cmd | kviator ... put -
	        kviator ... put - < val.file

	    The - character is necessary to force kviator to read from Stdin. Without the -, Stdin
	    is ignored.
	`
)

var (
	kvstore   string
	client    string
	caCert    string
	clientCert string
	clientKey  string
	showValue bool
)

func init() {
	flag.StringVar(&kvstore, "kvstore", "", "the kvstore to connect to. Can be consul, etcd, or zookeper.")
	flag.StringVar(&client, "client", "", "the client IP address")
	flag.BoolVar(&showValue, "show-value", false, "show the value of the listed keys")
	flag.StringVar(&caCert, "ca-cert", "", "the path to the CA certificate to use for TLS")
	flag.StringVar(&clientCert, "client-cert", "", "the path to the client certificate to use for TLS")
	flag.StringVar(&clientKey, "client-key", "", "the path to the client key to use for TLS")
	flag.Usage = help
	flag.Parse()
}

func main() {
	switch flag.Arg(0) {
	case "put":
		key := flag.Arg(1)
		val := strings.Join(flag.Args()[2:], " ")
		val = parseVal(val)
		save(key, val)
	case "get":
		key := flag.Arg(1)
		retrieve(key)
	case "del":
		key := flag.Arg(1)
		delete(key)
	case "deltree":
		key := flag.Arg(1)
		deleteTree(key)
	case "list":
		key := flag.Arg(1)
		list(key)
	case "cas":
		key := flag.Arg(1)
		val := strings.Join(flag.Args()[2:], " ")
		val = parseVal(val)
		checkAndSave(key, val)
	case "exists":
		key := flag.Arg(1)
		keyExists(key)
	default:
		help()
		os.Exit(8)
	}
}

func parseVal(arg string) string {
	arg = strings.TrimSpace(arg)
	if arg == "-" {
		stat, err := os.Stdin.Stat()
		if err == nil {
			if (stat.Mode() & os.ModeCharDevice) == 0 {
				in, _ := ioutil.ReadAll(os.Stdin)
				inStr := strings.TrimSuffix(string(in), "\n")
				inStr = strings.TrimSpace(inStr)
				return inStr
			}
		}
	}
	return arg
}

func kvstoreConn(kvstore, client string) store.Store {
	var backend store.Backend
	switch kvstore {
	case "consul":
		backend = store.CONSUL
		consul.Register()
	case "etcd":
		backend = store.ETCD
		etcd.Register()
	case "zookeper":
		backend = store.ZK
		zookeeper.Register()
	}

	var cfg store.Config

	if caCert != "" && clientCert != "" && clientKey != "" {
		var tlsInfo = transport.TLSInfo{
			CAFile:   caCert,
			CertFile: clientCert,
			KeyFile:  clientKey,
			TrustedCAFile: caCert,
			ClientCertAuth: true,
		}
		t, err := transport.NewTransport(tlsInfo, 10 * time.Second)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}

		cfg = store.Config{
			ConnectionTimeout: 10 * time.Second,
			TLS: t.TLSClientConfig,
		}
	} else {
		cfg = store.Config{
				ConnectionTimeout: 10 * time.Second,
			}
	}

	kv, err := libkv.NewStore(
		backend,
		[]string{client},
		&cfg,
	)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	return kv
}

func save(key, val string) {
	kv := kvstoreConn(kvstore, client)
	err := kv.Put(key, []byte(val), nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(2)
	}
}

func retrieve(key string) {
	kv := kvstoreConn(kvstore, client)
	kvPair, err := kv.Get(key)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(3)
	} else {
		fmt.Fprintln(os.Stdout, string(kvPair.Value))
	}
}

func delete(key string) {
	kv := kvstoreConn(kvstore, client)
	err := kv.Delete(key)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(4)
	}
}

func deleteTree(key string) {
	if key == "" {
		fmt.Fprintln(os.Stderr, "Please specify subtree. To delete all, use /.")
		os.Exit(11)
	}
	if key == "/" {
		key = ""
	}
	kv := kvstoreConn(kvstore, client)
	err := kv.DeleteTree(key)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(10)
	}
}

func list(key string) {
	kv := kvstoreConn(kvstore, client)
	kvPairs, err := kv.List(key)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(11)
	}
	for _, kvPair := range kvPairs {
		k := kvPair.Key
		v := string(kvPair.Value)
		if showValue {
			fmt.Fprintf(os.Stdout, "%s=%s\n", k, v)
		} else {
			fmt.Fprintln(os.Stdout, k)
		}

	}
}

func checkAndSave(key, val string) {
	kv := kvstoreConn(kvstore, client)
	_, err := kv.Get(key)
	if err != nil {
		err := kv.Put(key, []byte(val), nil)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(6)
		}
	} else {
		fmt.Fprintln(os.Stderr, "key is already set")
		os.Exit(5)
	}
}

func keyExists(key string) {
	kv := kvstoreConn(kvstore, client)
	_, err := kv.Get(key)
	if err != nil {
		fmt.Fprintln(os.Stderr, "false")
		os.Exit(7)
	} else {
		fmt.Fprintln(os.Stdout, "true")
	}
}

func help() {
	fmt.Fprintf(os.Stdout, "\n\t%s %s\n\n%s\n", appName, version, helpText)
}
<|endoftext|>


<commit_before>package template

import (
	. "github.com/dave/jennifer/jen"
	"github.com/devimteam/microgen/parser"
	"github.com/devimteam/microgen/util"
)

type ClientTemplate struct {
}

// Renders endpoints file.
//
//		// This file was automatically generated by "microgen" utility.
//		// Please, do not edit.
//		package stringsvc
//
//		import (
//			context "context"
//			transportlayer "github.com/devimteam/go-kit/transportlayer"
//		)
//
//		type client struct {
//			tc transportlayer.Client
//		}
//
//		func NewClient(tc transportlayer.Client) StringService {
//			return &client{tc}
//		}
//
//		func (c *client) Count(ctx context.Context, text string, symbol string) (count int, positions []int) {
//			req := CountRequest{
//				Symbol: symbol,
//				Text:   text,
//			}
//			resp, err := c.tc.Call(ctx, &req)
//			if err != nil {
//				return
//			}
//			return resp.(*CountResponse).Count, resp.(*CountResponse).Positions
//		}
//
func (ClientTemplate) Render(i *parser.Interface) *File {
	f := NewFile(i.PackageName)

	f.Type().Id("client").Struct(
		Id("tc").Qual(PackagePathTransportLayer, "Client"),
	)

	f.Func().Id("NewClient").Call(Id("tc").Qual(PackagePathTransportLayer, "Client")).Id(i.Name).Block(
		Return().Op("&").Id("client").Values(
			Id("tc"),
		),
	)
	f.Line()
	for _, signature := range i.FuncSignatures {
		f.Add(clientMethod(signature)).Line()
	}

	return f
}

func (ClientTemplate) Path() string {
	return "./client.go"
}

// Render full endpoints method.
//
//		func (c *client) Count(ctx context.Context, text string, symbol string) (count int, positions []int) {
//			req := CountRequest{
//				Symbol: symbol,
//				Text:   text,
//			}
//			resp, err := c.tc.Call(ctx, &req)
//			if err != nil {
//				return
//			}
//			return resp.(*CountResponse).Count, resp.(*CountResponse).Positions
//		}
//
func clientMethod(signature *parser.FuncSignature) *Statement {
	return methodDefinition("client", signature).
		BlockFunc(clientMethodBody(signature))
}

// Render interface method body.
//
//		req := CountRequest{
//			Symbol: symbol,
//			Text:   text,
//		}
//		resp, err := c.tc.Call(ctx, &req)
//		if err != nil {
//			return
//		}
//		return resp.(*CountResponse).Count, resp.(*CountResponse).Positions
//
func clientMethodBody(signature *parser.FuncSignature) func(g *Group) {
	errName := getFirstErrorFieldName(signature.Results)
	return func(g *Group) {
		g.Id("req").Op(":=").Id(requestStructName(signature)).Values(dictByFuncFields(removeContextIfFirst(signature.Params)))
		g.List(Id("resp"), Id(errName)).Op(":=").Id(util.FirstLowerChar("client")).Dot("tc").Dot("Call").Call(Id(firstArgName(signature)), Op("&").Id("req"))
		g.If(Id(errName).Op("!=").Nil()).Block(
			Return(),
		)
		g.ReturnFunc(func(group *Group) {
			for _, field := range signature.Results {
				group.Id("resp").Assert(Op("*").Id(responseStructName(signature))).Op(".").Add(structFieldName(field))
			}
		})
	}
}

// Get from function field slice
// If field with error type not found, it return `err`
func getFirstErrorFieldName(fields []*parser.FuncField) string {
	for _, field := range fields {
		if field.Type == "error" {
			return field.Name
		}
	}
	return "err"
}
<commit_msg>typo(client): fix comments<commit_after>package template

import (
	. "github.com/dave/jennifer/jen"
	"github.com/devimteam/microgen/parser"
	"github.com/devimteam/microgen/util"
)

type ClientTemplate struct {
}

// Renders whole client file.
//
//		// This file was automatically generated by "microgen" utility.
//		// Please, do not edit.
//		package stringsvc
//
//		import (
//			context "context"
//			transportlayer "github.com/devimteam/go-kit/transportlayer"
//		)
//
//		type client struct {
//			tc transportlayer.Client
//		}
//
//		func NewClient(tc transportlayer.Client) StringService {
//			return &client{tc}
//		}
//
//		func (c *client) Count(ctx context.Context, text string, symbol string) (count int, positions []int) {
//			req := CountRequest{
//				Symbol: symbol,
//				Text:   text,
//			}
//			resp, err := c.tc.Call(ctx, &req)
//			if err != nil {
//				return
//			}
//			return resp.(*CountResponse).Count, resp.(*CountResponse).Positions
//		}
//
func (ClientTemplate) Render(i *parser.Interface) *File {
	f := NewFile(i.PackageName)

	f.Type().Id("client").Struct(
		Id("tc").Qual(PackagePathTransportLayer, "Client"),
	)

	f.Func().Id("NewClient").Call(Id("tc").Qual(PackagePathTransportLayer, "Client")).Id(i.Name).Block(
		Return().Op("&").Id("client").Values(
			Id("tc"),
		),
	)
	f.Line()
	for _, signature := range i.FuncSignatures {
		f.Add(clientMethod(signature)).Line()
	}

	return f
}

func (ClientTemplate) Path() string {
	return "./client.go"
}

// Render full client method.
//
//		func (c *client) Count(ctx context.Context, text string, symbol string) (count int, positions []int) {
//			req := CountRequest{
//				Symbol: symbol,
//				Text:   text,
//			}
//			resp, err := c.tc.Call(ctx, &req)
//			if err != nil {
//				return
//			}
//			return resp.(*CountResponse).Count, resp.(*CountResponse).Positions
//		}
//
func clientMethod(signature *parser.FuncSignature) *Statement {
	return methodDefinition("client", signature).
		BlockFunc(clientMethodBody(signature))
}

// Render interface client method body.
//
//		req := CountRequest{
//			Symbol: symbol,
//			Text:   text,
//		}
//		resp, err := c.tc.Call(ctx, &req)
//		if err != nil {
//			return
//		}
//		return resp.(*CountResponse).Count, resp.(*CountResponse).Positions
//
func clientMethodBody(signature *parser.FuncSignature) func(g *Group) {
	errName := getFirstErrorFieldName(signature.Results)
	return func(g *Group) {
		g.Id("req").Op(":=").Id(requestStructName(signature)).Values(dictByFuncFields(removeContextIfFirst(signature.Params)))
		g.List(Id("resp"), Id(errName)).Op(":=").Id(util.FirstLowerChar("client")).Dot("tc").Dot("Call").Call(Id(firstArgName(signature)), Op("&").Id("req"))
		g.If(Id(errName).Op("!=").Nil()).Block(
			Return(),
		)
		g.ReturnFunc(func(group *Group) {
			for _, field := range signature.Results {
				group.Id("resp").Assert(Op("*").Id(responseStructName(signature))).Op(".").Add(structFieldName(field))
			}
		})
	}
}

// Get from function field slice
// If field with error type not found, it return `err`
func getFirstErrorFieldName(fields []*parser.FuncField) string {
	for _, field := range fields {
		if field.Type == "error" {
			return field.Name
		}
	}
	return "err"
}
<|endoftext|>


<commit_before>package render

import (
	"bufio"
	"bytes"
	"net/http"

	"github.com/lomik/graphite-clickhouse/helper/log"
	"github.com/lomik/graphite-clickhouse/helper/point"
	"github.com/lomik/graphite-clickhouse/helper/rollup"
	"go.uber.org/zap"
)

func (h *Handler) ReplyProtobuf(w http.ResponseWriter, r *http.Request, data *Data, from, until uint32, prefix string, rollupObj *rollup.Rules) {
	points := data.Points.List()

	if len(points) == 0 {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	logger := log.FromContext(r.Context())

	// var multiResponse carbonzipperpb.MultiFetchResponse
	writer := bufio.NewWriterSize(w, 1024*1024)
	defer writer.Flush()

	mb := new(bytes.Buffer)

	writeMetric := func(name string, points []point.Point) {
		points, step, err := rollupObj.RollupMetric(data.Points.MetricName(points[0].MetricID), from, points)
		if err != nil {
			logger.Error("rollup failed", zap.Error(err))
			return
		}

		start := from - (from % step)
		if start < from {
			start += step
		}
		stop := until - (until % step)
		count := ((stop - start) / step) + 1

		mb.Reset()

		// name
		VarintWrite(mb, (1<<3)+2) // tag
		VarintWrite(mb, uint64(len(name)))
		mb.WriteString(name)

		// start
		VarintWrite(mb, 2<<3)
		VarintWrite(mb, uint64(start))

		// stop
		VarintWrite(mb, 3<<3)
		VarintWrite(mb, uint64(stop))

		// step
		VarintWrite(mb, 4<<3)
		VarintWrite(mb, uint64(step))

		// start write to output

		// repeated FetchResponse metrics = 1;
		// write tag and len
		VarintWrite(writer, (1<<3)+2)
		VarintWrite(writer,
			uint64(mb.Len())+
				2+ // tags of <repeated double values = 5;> and <repeated bool isAbsent = 6;>
				VarintLen(uint64(8*count))+ // len of packed <repeated double values>
				VarintLen(uint64(count))+ // len of packed <repeated bool isAbsent>
				uint64(9*count), // packed <repeated double values> and <repeated bool isAbsent>
		)

		writer.Write(mb.Bytes())

		// Write values
		VarintWrite(writer, (5<<3)+2)
		VarintWrite(writer, uint64(8*count))

		last := start - step
		for _, point := range points {
			if point.Time < start || point.Time > stop {
				continue
			}

			if point.Time > last+step {
				ProtobufWriteDoubleN(writer, 0, int(((point.Time-last)/step)-1))
			}

			ProtobufWriteDouble(writer, point.Value)

			last = point.Time
		}

		if stop > last {
			ProtobufWriteDoubleN(writer, 0, int((stop-last)/step))
		}

		// Write isAbsent
		VarintWrite(writer, (6<<3)+2)
		VarintWrite(writer, uint64(count))

		last = start - step
		for _, point := range points {
			if point.Time < start || point.Time > stop {
				continue
			}

			if point.Time > last+step {
				WriteByteN(writer, '\x01', int(((point.Time-last)/step)-1))
			}

			writer.WriteByte('\x00')

			last = point.Time
		}

		if stop > last {
			WriteByteN(writer, '\x01', int((stop-last)/step))
		}
	}

	// group by Metric
	var i, n, k int
	// i - current position of iterator
	// n - position of the first record with current metric
	l := len(points)

	for i = 1; i < l; i++ {
		if points[i].MetricID != points[n].MetricID {
			a := data.Aliases[data.Points.MetricName(points[n].MetricID)]
			for k = 0; k < len(a); k += 2 {
				writeMetric(a[k], points[n:i])
			}
			n = i
			continue
		}
	}
	a := data.Aliases[data.Points.MetricName(points[n].MetricID)]
	for k = 0; k < len(a); k += 2 {
		writeMetric(a[k], points[n:i])
	}
}
<commit_msg>rollup once<commit_after>package render

import (
	"bufio"
	"bytes"
	"net/http"

	"github.com/lomik/graphite-clickhouse/helper/log"
	"github.com/lomik/graphite-clickhouse/helper/point"
	"github.com/lomik/graphite-clickhouse/helper/rollup"
	"go.uber.org/zap"
)

func (h *Handler) ReplyProtobuf(w http.ResponseWriter, r *http.Request, data *Data, from, until uint32, prefix string, rollupObj *rollup.Rules) {
	points := data.Points.List()

	if len(points) == 0 {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	logger := log.FromContext(r.Context())

	// var multiResponse carbonzipperpb.MultiFetchResponse
	writer := bufio.NewWriterSize(w, 1024*1024)
	defer writer.Flush()

	mb := new(bytes.Buffer)

	writeAlias := func(name string, points []point.Point, step uint32) {
		start := from - (from % step)
		if start < from {
			start += step
		}
		stop := until - (until % step)
		count := ((stop - start) / step) + 1

		mb.Reset()

		// name
		VarintWrite(mb, (1<<3)+2) // tag
		VarintWrite(mb, uint64(len(name)))
		mb.WriteString(name)

		// start
		VarintWrite(mb, 2<<3)
		VarintWrite(mb, uint64(start))

		// stop
		VarintWrite(mb, 3<<3)
		VarintWrite(mb, uint64(stop))

		// step
		VarintWrite(mb, 4<<3)
		VarintWrite(mb, uint64(step))

		// start write to output

		// repeated FetchResponse metrics = 1;
		// write tag and len
		VarintWrite(writer, (1<<3)+2)
		VarintWrite(writer,
			uint64(mb.Len())+
				2+ // tags of <repeated double values = 5;> and <repeated bool isAbsent = 6;>
				VarintLen(uint64(8*count))+ // len of packed <repeated double values>
				VarintLen(uint64(count))+ // len of packed <repeated bool isAbsent>
				uint64(9*count), // packed <repeated double values> and <repeated bool isAbsent>
		)

		writer.Write(mb.Bytes())

		// Write values
		VarintWrite(writer, (5<<3)+2)
		VarintWrite(writer, uint64(8*count))

		last := start - step
		for _, point := range points {
			if point.Time < start || point.Time > stop {
				continue
			}

			if point.Time > last+step {
				ProtobufWriteDoubleN(writer, 0, int(((point.Time-last)/step)-1))
			}

			ProtobufWriteDouble(writer, point.Value)

			last = point.Time
		}

		if stop > last {
			ProtobufWriteDoubleN(writer, 0, int((stop-last)/step))
		}

		// Write isAbsent
		VarintWrite(writer, (6<<3)+2)
		VarintWrite(writer, uint64(count))

		last = start - step
		for _, point := range points {
			if point.Time < start || point.Time > stop {
				continue
			}

			if point.Time > last+step {
				WriteByteN(writer, '\x01', int(((point.Time-last)/step)-1))
			}

			writer.WriteByte('\x00')

			last = point.Time
		}

		if stop > last {
			WriteByteN(writer, '\x01', int((stop-last)/step))
		}
	}

	writeMetric := func(points []point.Point) {
		metricName := data.Points.MetricName(points[0].MetricID)
		points, step, err := rollupObj.RollupMetric(metricName, from, points)
		if err != nil {
			logger.Error("rollup failed", zap.Error(err))
			return
		}

		a := data.Aliases[metricName]
		for k := 0; k < len(a); k += 2 {
			writeAlias(a[k], points, step)
		}
	}

	// group by Metric
	var i, n int
	// i - current position of iterator
	// n - position of the first record with current metric
	l := len(points)

	for i = 1; i < l; i++ {
		if points[i].MetricID != points[n].MetricID {
			writeMetric(points[n:i])
			n = i
			continue
		}
	}
	writeMetric(points[n:i])
}
<|endoftext|>


<commit_before>package renderweb

import (
	"fmt"
	"io/ioutil"
	"strings"

	"github.com/crackcomm/renderer/middlewares"

	"gopkg.in/yaml.v2"
)

// RoutesFromFile - Reads routes from yaml file.
func RoutesFromFile(filename string) (Routes, error) {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}
	m := make(routesFile)
	err = yaml.Unmarshal([]byte(data), &m)
	if err != nil {
		return nil, err
	}
	return m.toRoutes()
}

type routesFile map[string]*Handler

func (file routesFile) toRoutes() (routes Routes, err error) {
	routes = make(Routes)
	for r, v := range file {
		var route Route
		route, err = parseRoute(r)
		if err != nil {
			return
		}
		for _, m := range v.Middlewares {
			if !middlewares.Exists(m.Name) {
				err = fmt.Errorf("middleware %q doesn't exist", m.Name)
				return
			}
		}
		routes[route] = v
	}
	return
}

func parseRoute(str string) (r Route, err error) {
	s := strings.Split(str, " ")
	if len(s) != 2 {
		err = fmt.Errorf("invalid route %q", str)
		return
	}
	r.Method = s[0]
	r.Path = s[1]
	return
}
<commit_msg>renderweb routes: no more map[interface{}]interface{}<commit_after>package renderweb

import (
	"fmt"
	"io/ioutil"
	"strings"

	"gopkg.in/yaml.v2"

	"github.com/crackcomm/renderer/helpers"
	"github.com/crackcomm/renderer/middlewares"
	"github.com/crackcomm/renderer/options"
)

// RoutesFromFile - Reads routes from yaml file.
func RoutesFromFile(filename string) (Routes, error) {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}
	m := make(routesFile)
	err = yaml.Unmarshal([]byte(data), &m)
	if err != nil {
		return nil, err
	}
	return m.toRoutes()
}

type routesFile map[string]*Handler

func (file routesFile) toRoutes() (routes Routes, err error) {
	routes = make(Routes)
	for r, v := range file {
		var route Route
		route, err = parseRoute(r)
		if err != nil {
			return
		}
		for _, m := range v.Middlewares {
			if !middlewares.Exists(m.Name) {
				err = fmt.Errorf("middleware %q doesn't exist", m.Name)
				return
			}
			if err := cleanMiddleware(m); err != nil {
				return nil, fmt.Errorf("route %q: %v", r, err)
			}
		}
		routes[route] = v
	}
	return
}

func cleanMiddleware(m *middlewares.Middleware) error {
	// Clean middleware options
	// because yaml gives us ugly maps map[interface{}]interface{}
	// which is not we want in 99.99% of cases and this makes me
	// write tons of unnecessary code to handle conversions
	// lets convert it always here, JSON doesn't have this problem
	v, ok := helpers.CleanMapDeep(map[string]interface{}(m.Options))
	if !ok {
		return fmt.Errorf("middleware %q invalid options", m.Name)
	}

	m.Options = options.Options(v.(map[string]interface{}))

	// Clean templates map
	v, ok = helpers.CleanMapDeep(map[string]interface{}(m.Template))
	if !ok {
		return fmt.Errorf("middleware %q invalid templates", m.Name)
	}
	m.Template = options.Options(v.(map[string]interface{}))

	// Clean context map
	v, ok = helpers.CleanMapDeep(map[string]interface{}(m.Context))
	if !ok {
		return fmt.Errorf("middleware %q invalid context", m.Name)
	}
	m.Context = options.Options(v.(map[string]interface{}))
	return nil
}

func parseRoute(str string) (r Route, err error) {
	s := strings.Split(str, " ")
	if len(s) != 2 {
		err = fmt.Errorf("invalid route %q", str)
		return
	}
	r.Method = s[0]
	r.Path = s[1]
	return
}
<|endoftext|>


<commit_before>package jsonschema

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"testing"
)

func TestDraft4(t *testing.T) {
	testResources := filepath.Join("JSON-Schema-Test-Suite", "tests", "draft4")
	if _, err := os.Stat(testResources); err != nil {
		t.Error("Test suite missing. Run `git submodule update --init` to download it.")
	}
	var failures, successes int
	err := filepath.Walk(testResources, testFileRunner(t, &failures, &successes))
	if err != nil {
		t.Error(err.Error())
	}
	t.Logf("%d failed, %d succeeded.", failures, successes)
}

func testFileRunner(t *testing.T, failures, successes *int) func(string, os.FileInfo, error) error {
	return func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() {
			return nil
		}

		file, err := os.Open(path)
		if err != nil {
			return err
		}
		var testFile []testCase
		err = json.NewDecoder(file).Decode(&testFile)
		if err != nil {
			return err
		}

		for _, cse := range testFile {
			schema, err := Parse(bytes.NewReader(cse.Schema))
			if err != nil {
				return err
			}
			for _, tst := range cse.Tests {
				var data interface{}
				json.Unmarshal(tst.Data, &data)
				errorList, err := schema.Validate(data)

				if err != nil {
					t.Error(errorMessage(err, path, cse, tst))
					*failures += 1
					continue
				}

				validated, err := correctValidation(errorList, path, cse, tst)
				if err != nil {
					t.Error(failureMessage(err, path, cse, tst, validated, errorList))
					*failures += 1
				} else {
					*successes += 1
				}
			}
		}
		return nil
	}
}

type testCase struct {
	Description string          `json:"description"`
	Schema      json.RawMessage `json:"schema"`
	Properties  json.RawMessage `json:"properties"`
	Required    json.RawMessage `json:"required"`
	Tests       []testInstance  `json:"tests"`
}

type testInstance struct {
	Description string          `json:"description"`
	Data        json.RawMessage `json:"data"`
	Valid       bool            `json:"valid"`
}

func correctValidation(errorList []ValidationError, path string, cse testCase, tst testInstance) (bool, error) {
	validated := true
	if len(errorList) > 0 {
		validated = false
	}

	var failureName string
	if validated && !tst.Valid {
		failureName = "schema.Validate validated bad data."
	} else if !validated && tst.Valid {
		failureName = "schema.Validate failed to validate good data."
	}

	if len(failureName) > 0 {
		return validated, errors.New(failureName)
	}
	return validated, nil
}

func errorMessage(err error, path string, cse testCase, tst testInstance) string {
	return fmt.Sprintf(`VALIDATION ERROR: %s
file: %s
test case description: %s
schema: %s
test instance description: %s
test data: %s

`, err, path, cse.Description, cse.Schema, tst.Description, tst.Data)
}

func failureMessage(err error, path string, cse testCase, tst testInstance, validated bool, errorList []ValidationError) string {
	return fmt.Sprintf(`%s
file: %s
test case description: %s
schema: %s
test instance description: %s
test data: %s
result of schema.Validate:
	expected: %t
	actual: %t
	actual validation errors: %s

`, err.Error(), path, cse.Description, cse.Schema, tst.Description, tst.Data, tst.Valid, validated, errorList)
}
<commit_msg>Simplify tests.<commit_after>package jsonschema

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"testing"
)

func TestDraft4(t *testing.T) {
	testResources := filepath.Join("JSON-Schema-Test-Suite", "tests", "draft4")
	if _, err := os.Stat(testResources); err != nil {
		t.Error("Test suite missing. Run `git submodule update --init` to download it.")
	}
	var failures, successes int
	err := filepath.Walk(testResources, testFileRunner(t, &failures, &successes))
	if err != nil {
		t.Error(err.Error())
	}
	t.Logf("%d failed, %d succeeded.", failures, successes)
}

func testFileRunner(t *testing.T, failures, successes *int) func(string, os.FileInfo, error) error {
	return func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() {
			return nil
		}

		file, err := os.Open(path)
		if err != nil {
			return err
		}
		var testFile []testCase
		err = json.NewDecoder(file).Decode(&testFile)
		if err != nil {
			return err
		}

		for _, cse := range testFile {
			schema, err := Parse(bytes.NewReader(cse.Schema))
			if err != nil {
				return err
			}
			for _, tst := range cse.Tests {
				var data interface{}
				json.Unmarshal(tst.Data, &data)
				errorList, err := schema.Validate(data)

				if err != nil {
					t.Error(errorMessage(err, path, cse, tst))
					*failures += 1
					continue
				}

				err = correctValidation(errorList, path, cse, tst)
				if err != nil {
					t.Error(failureMessage(err, path, cse, tst, errorList))
					*failures += 1
				} else {
					*successes += 1
				}
			}
		}
		return nil
	}
}

type testCase struct {
	Description string          `json:"description"`
	Schema      json.RawMessage `json:"schema"`
	Properties  json.RawMessage `json:"properties"`
	Required    json.RawMessage `json:"required"`
	Tests       []testInstance  `json:"tests"`
}

type testInstance struct {
	Description string          `json:"description"`
	Data        json.RawMessage `json:"data"`
	Valid       bool            `json:"valid"`
}

func correctValidation(errorList []ValidationError, path string, cse testCase, tst testInstance) error {
	validated := len(errorList) == 0
	var failureName string
	if validated && !tst.Valid {
		failureName = "schema.Validate validated bad data."
	} else if !validated && tst.Valid {
		failureName = "schema.Validate failed to validate good data."
	}

	if len(failureName) > 0 {
		return errors.New(failureName)
	}
	return nil
}

func errorMessage(err error, path string, cse testCase, tst testInstance) string {
	return fmt.Sprintf(`VALIDATION ERROR: %s
file: %s
test case description: %s
schema: %s
test instance description: %s
test data: %s

`, err, path, cse.Description, cse.Schema, tst.Description, tst.Data)
}

func failureMessage(err error, path string, cse testCase, tst testInstance, errorList []ValidationError) string {
	return fmt.Sprintf(`%s
file: %s
test case description: %s
schema: %s
test instance description: %s
test data: %s
result of schema.Validate:
	expected: %t
	actual: %t
	actual validation errors: %s

`, err.Error(), path, cse.Description, cse.Schema, tst.Description, tst.Data, tst.Valid, len(errorList) == 0, errorList)
}
<|endoftext|>


<commit_before>package db

import (
	"database/sql"
	"encoding/json"
	"github.com/OpenBazaar/openbazaar-go/repo"
	"sync"
	"testing"
)

var sdb repo.ConfigurationStore
var settings repo.SettingsData

func init() {
	conn, _ := sql.Open("sqlite3", ":memory:")
	initDatabaseTables(conn, "")
	sdb = NewConfigurationStore(conn, new(sync.Mutex))
	c := "UNITED_STATES"
	settings = repo.SettingsData{
		Country: &c,
	}
}

func TestSettingsPut(t *testing.T) {
	err := sdb.Put(settings)
	if err != nil {
		t.Error(err)
	}
	set := repo.SettingsData{}
	stmt, err := sdb.PrepareQuery("select value from config where key=?")
	defer stmt.Close()
	var settingsBytes []byte
	err = stmt.QueryRow("settings").Scan(&settingsBytes)
	if err != nil {
		t.Error(err)
	}
	err = json.Unmarshal(settingsBytes, &set)
	if err != nil {
		t.Error(err)
	}
	if *set.Country != "UNITED_STATES" {
		t.Error("Settings put failed to put correct value")
	}
}

func TestInvalidSettingsGet(t *testing.T) {
	tx, err := sdb.BeginTransaction()
	if err != nil {
		t.Error(err)
	}
	stmt, _ := tx.Prepare("insert or replace into config(key, value) values(?,?)")
	defer stmt.Close()

	_, err = stmt.Exec("settings", string("Test fail"))
	if err != nil {
		tx.Rollback()
		t.Error(err)
	}
	tx.Commit()
	_, err = sdb.Get()
	if err == nil {
		t.Error("settings get didn't error with invalid data")
	}
}

func TestSettingsGet(t *testing.T) {
	err := sdb.Put(settings)
	if err != nil {
		t.Error(err)
	}
	set, err := sdb.Get()
	if err != nil {
		t.Error(err)
	}
	if *set.Country != "UNITED_STATES" {
		t.Error("Settings put failed to put correct value")
	}
}

func TestSettingsUpdate(t *testing.T) {
	err := sdb.Put(settings)
	if err != nil {
		t.Error(err)
	}
	l := "/openbazaar-go:0.4/"
	setUpdt := repo.SettingsData{
		Version: &l,
	}
	err = sdb.Update(setUpdt)
	if err != nil {
		t.Error(err)
	}
	r := "None"
	setUpdt2 := repo.SettingsData{
		TermsAndConditions: &r,
	}
	err = sdb.Update(setUpdt2)
	if err != nil {
		t.Error(err)
	}
	set, err := sdb.Get()
	if err != nil {
		t.Error(err)
	}
	if *set.Country != "UNITED_STATES" {
		t.Error("Settings update failed to put correct value")
	}
	if *set.Version != "/openbazaar-go:0.4/" {
		t.Error("Settings update failed to put correct value")
	}
	if *set.TermsAndConditions != "None" {
		t.Error("Settings update failed to put correct value")
	}
}
<commit_msg>[#1004] Isolate dependencies in repo/db/settings_test.go<commit_after>package db_test

import (
	"encoding/json"
	"sync"
	"testing"

	"github.com/OpenBazaar/openbazaar-go/repo"
	"github.com/OpenBazaar/openbazaar-go/repo/db"
	"github.com/OpenBazaar/openbazaar-go/schema"
)

func buildConfigurationStore() (repo.ConfigurationStore, func(), error) {
	appSchema := schema.MustNewCustomSchemaManager(schema.SchemaContext{
		DataPath:        schema.GenerateTempPath(),
		TestModeEnabled: true,
	})
	if err := appSchema.BuildSchemaDirectories(); err != nil {
		return nil, nil, err
	}
	if err := appSchema.InitializeDatabase(); err != nil {
		return nil, nil, err
	}
	database, err := appSchema.OpenDatabase()
	if err != nil {
		return nil, nil, err
	}
	return db.NewConfigurationStore(database, new(sync.Mutex)), appSchema.DestroySchemaDirectories, nil
}

func TestSettingsPut(t *testing.T) {
	sdb, teardown, err := buildConfigurationStore()
	if err != nil {
		t.Fatal(err)
	}
	defer teardown()

	country := "UNITED_STATES"
	settings := repo.SettingsData{
		Country: &country,
	}
	err = sdb.Put(settings)
	if err != nil {
		t.Error(err)
	}
	set := repo.SettingsData{}
	stmt, err := sdb.PrepareQuery("select value from config where key=?")
	defer stmt.Close()
	var settingsBytes []byte
	err = stmt.QueryRow("settings").Scan(&settingsBytes)
	if err != nil {
		t.Error(err)
	}
	err = json.Unmarshal(settingsBytes, &set)
	if err != nil {
		t.Error(err)
	}
	if *set.Country != "UNITED_STATES" {
		t.Error("Settings put failed to put correct value")
	}
}

func TestInvalidSettingsGet(t *testing.T) {
	sdb, teardown, err := buildConfigurationStore()
	if err != nil {
		t.Fatal(err)
	}
	defer teardown()

	tx, err := sdb.BeginTransaction()
	if err != nil {
		t.Error(err)
	}
	stmt, _ := tx.Prepare("insert or replace into config(key, value) values(?,?)")
	defer stmt.Close()

	_, err = stmt.Exec("settings", string("Test fail"))
	if err != nil {
		tx.Rollback()
		t.Error(err)
	}
	tx.Commit()
	_, err = sdb.Get()
	if err == nil {
		t.Error("settings get didn't error with invalid data")
	}
}

func TestSettingsGet(t *testing.T) {
	sdb, teardown, err := buildConfigurationStore()
	if err != nil {
		t.Fatal(err)
	}
	defer teardown()

	country := "UNITED_STATES"
	settings := repo.SettingsData{
		Country: &country,
	}
	err = sdb.Put(settings)
	if err != nil {
		t.Error(err)
	}
	set, err := sdb.Get()
	if err != nil {
		t.Error(err)
	}
	if *set.Country != "UNITED_STATES" {
		t.Error("Settings put failed to put correct value")
	}
}

func TestSettingsUpdate(t *testing.T) {
	sdb, teardown, err := buildConfigurationStore()
	if err != nil {
		t.Fatal(err)
	}
	defer teardown()

	country := "UNITED_STATES"
	settings := repo.SettingsData{
		Country: &country,
	}
	err = sdb.Put(settings)
	if err != nil {
		t.Error(err)
	}
	l := "/openbazaar-go:0.4/"
	setUpdt := repo.SettingsData{
		Version: &l,
	}
	err = sdb.Update(setUpdt)
	if err != nil {
		t.Error(err)
	}
	r := "None"
	setUpdt2 := repo.SettingsData{
		TermsAndConditions: &r,
	}
	err = sdb.Update(setUpdt2)
	if err != nil {
		t.Error(err)
	}
	set, err := sdb.Get()
	if err != nil {
		t.Error(err)
	}
	if *set.Country != "UNITED_STATES" {
		t.Error("Settings update failed to put correct value")
	}
	if *set.Version != "/openbazaar-go:0.4/" {
		t.Error("Settings update failed to put correct value")
	}
	if *set.TermsAndConditions != "None" {
		t.Error("Settings update failed to put correct value")
	}
}
<|endoftext|>


<commit_before>package commands

import (
	"errors"
	"fmt"

	cmds "github.com/jbenet/go-ipfs/commands"
	"github.com/jbenet/go-ipfs/core"
	"github.com/jbenet/go-ipfs/updates"
)

type UpdateOutput struct {
	OldVersion string
	NewVersion string
}

var UpdateCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline:          "Downloads and installs updates for IPFS",
		ShortDescription: "ipfs update is a utility command used to check for updates and apply them.",
	},

	Run: func(req cmds.Request) (interface{}, error) {
		n, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}
		return updateApply(n)
	},
	Type: &UpdateOutput{},
	Subcommands: map[string]*cmds.Command{
		"check": UpdateCheckCmd,
		"log":   UpdateLogCmd,
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) ([]byte, error) {
			v := res.Output().(*UpdateOutput)
			s := ""
			if v.NewVersion != v.OldVersion {
				s = fmt.Sprintf("Successfully updated to IPFS version '%s' (from '%s')",
					v.NewVersion, v.OldVersion)
			} else {
				s = fmt.Sprintf("Already updated to latest version ('%s')", v.NewVersion)
			}
			return []byte(s), nil
		},
	},
}

var UpdateCheckCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "Checks if updates are available",
		ShortDescription: `
'ipfs update check' checks if any updates are available for IPFS.

Nothing will be downloaded or installed.
`,
	},

	Run: func(req cmds.Request) (interface{}, error) {
		n, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}
		return updateCheck(n)
	},
	Type: &UpdateOutput{},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) ([]byte, error) {
			v := res.Output().(*UpdateOutput)
			s := ""
			if v.NewVersion != v.OldVersion {
				s = fmt.Sprintf("A new version of IPFS is available ('%s', currently running '%s')",
					v.NewVersion, v.OldVersion)
			} else {
				s = fmt.Sprintf("Already updated to latest version ('%s')", v.NewVersion)
			}
			return []byte(s), nil
		},
	},
}

var UpdateLogCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline:          "List the changelog for the latest versions of IPFS",
		ShortDescription: "This command is not yet implemented.",
	},

	Run: func(req cmds.Request) (interface{}, error) {
		n, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}
		return updateLog(n)
	},
}

// updateApply applies an update of the ipfs binary and shuts down the node if successful
func updateApply(n *core.IpfsNode) (*UpdateOutput, error) {
	// TODO: 'force bool' param that stops the daemon (if running) before update

	output := &UpdateOutput{
		OldVersion: updates.Version,
	}

	u, err := updates.CheckForUpdate()
	if err != nil {
		return nil, err
	}

	if u == nil {
		output.NewVersion = updates.Version
		return output, nil
	}

	output.NewVersion = u.Version

	if n.OnlineMode() {
		return nil, errors.New(`You must stop the IPFS daemon before updating.`)
	}

	if err = updates.Apply(u); err != nil {
		return nil, err
	}

	return output, nil
}

// updateCheck checks wether there is an update available
func updateCheck(n *core.IpfsNode) (*UpdateOutput, error) {
	output := &UpdateOutput{
		OldVersion: updates.Version,
	}

	u, err := updates.CheckForUpdate()
	if err != nil {
		return nil, err
	}

	if u == nil {
		output.NewVersion = updates.Version
		return output, nil
	}

	output.NewVersion = u.Version
	return output, nil
}

// updateLog lists the version available online
func updateLog(n *core.IpfsNode) (interface{}, error) {
	// TODO
	return nil, errors.New("Not yet implemented")
}
<commit_msg>fix(2/update) newlines<commit_after>package commands

import (
	"errors"
	"fmt"

	cmds "github.com/jbenet/go-ipfs/commands"
	"github.com/jbenet/go-ipfs/core"
	"github.com/jbenet/go-ipfs/updates"
)

type UpdateOutput struct {
	OldVersion string
	NewVersion string
}

var UpdateCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline:          "Downloads and installs updates for IPFS",
		ShortDescription: "ipfs update is a utility command used to check for updates and apply them.",
	},

	Run: func(req cmds.Request) (interface{}, error) {
		n, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}
		return updateApply(n)
	},
	Type: &UpdateOutput{},
	Subcommands: map[string]*cmds.Command{
		"check": UpdateCheckCmd,
		"log":   UpdateLogCmd,
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) ([]byte, error) {
			v := res.Output().(*UpdateOutput)
			s := ""
			if v.NewVersion != v.OldVersion {
				s = fmt.Sprintf("Successfully updated to IPFS version '%s' (from '%s')\n",
					v.NewVersion, v.OldVersion)
			} else {
				s = fmt.Sprintf("Already updated to latest version ('%s')\n", v.NewVersion)
			}
			return []byte(s), nil
		},
	},
}

var UpdateCheckCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "Checks if updates are available",
		ShortDescription: `
'ipfs update check' checks if any updates are available for IPFS.

Nothing will be downloaded or installed.
`,
	},

	Run: func(req cmds.Request) (interface{}, error) {
		n, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}
		return updateCheck(n)
	},
	Type: &UpdateOutput{},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) ([]byte, error) {
			v := res.Output().(*UpdateOutput)
			s := ""
			if v.NewVersion != v.OldVersion {
				s = fmt.Sprintf("A new version of IPFS is available ('%s', currently running '%s')\n",
					v.NewVersion, v.OldVersion)
			} else {
				s = fmt.Sprintf("Already updated to latest version ('%s')\n", v.NewVersion)
			}
			return []byte(s), nil
		},
	},
}

var UpdateLogCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline:          "List the changelog for the latest versions of IPFS",
		ShortDescription: "This command is not yet implemented.",
	},

	Run: func(req cmds.Request) (interface{}, error) {
		n, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}
		return updateLog(n)
	},
}

// updateApply applies an update of the ipfs binary and shuts down the node if successful
func updateApply(n *core.IpfsNode) (*UpdateOutput, error) {
	// TODO: 'force bool' param that stops the daemon (if running) before update

	output := &UpdateOutput{
		OldVersion: updates.Version,
	}

	u, err := updates.CheckForUpdate()
	if err != nil {
		return nil, err
	}

	if u == nil {
		output.NewVersion = updates.Version
		return output, nil
	}

	output.NewVersion = u.Version

	if n.OnlineMode() {
		return nil, errors.New(`You must stop the IPFS daemon before updating.`)
	}

	if err = updates.Apply(u); err != nil {
		return nil, err
	}

	return output, nil
}

// updateCheck checks wether there is an update available
func updateCheck(n *core.IpfsNode) (*UpdateOutput, error) {
	output := &UpdateOutput{
		OldVersion: updates.Version,
	}

	u, err := updates.CheckForUpdate()
	if err != nil {
		return nil, err
	}

	if u == nil {
		output.NewVersion = updates.Version
		return output, nil
	}

	output.NewVersion = u.Version
	return output, nil
}

// updateLog lists the version available online
func updateLog(n *core.IpfsNode) (interface{}, error) {
	// TODO
	return nil, errors.New("Not yet implemented")
}
<|endoftext|>


<commit_before>package sloth

import (
	"os"
	"testing"
	"time"
)

func init() {
	timeNow = func() time.Time {
		return time.Date(2016, time.February, 3, 15, 40, 0, 0, time.UTC)
	}
}

func TestRotate(t *testing.T) {
	logger := &Logger{
		Filename: "./test.log",
	}

	logger.rotate()

	f, err := os.Open("./test_20160203_1540.log")
	if os.IsNotExist(err) {
		t.Error("Expect file ./test_20160203_1540.log is exist.")
	}
	f.Close()
}

func TestRotate_InFolder(t *testing.T) {
	logger := &Logger{
		Filename: "test/test.log",
	}

	logger.rotate()

	f, err := os.Open("test/test_20160203_1540.log")
	if os.IsNotExist(err) {
		t.Error("Expect file test/test_20160203_1540.log is exist.")
	}
	f.Close()
}
<commit_msg>Refactoring tests<commit_after>package sloth

import (
	"os"
	"testing"
	"time"
)

func init() {
	timeNow = func() time.Time {
		return time.Date(2016, time.February, 3, 15, 40, 0, 0, time.UTC)
	}
}

var logRotateTestCases = []struct {
	logger   *Logger
	filename string
}{
	{
		&Logger{Filename: "./test.log"}, "./test_20160203_1540.log",
	},
	{
		&Logger{Filename: "./test/test.log"}, "./test/test_20160203_1540.log",
	},
}

func TestRotate(t *testing.T) {

	for _, testcase := range logRotateTestCases {
		testcase.logger.rotate()

		f, err := os.Open(testcase.filename)
		if os.IsNotExist(err) {
			t.Errorf("Expect file %s is exist.", testcase.filename)
		}
		f.Close()
	}
}
<|endoftext|>


<commit_before>// Memory Layout
//
// Peripherals
// 0000000 LCD  [320 x 240]
//         0: Control
//            0: on or off
//            1: clear the LCD
//            2: clear color
//            3: show test message on LCD
//         4-2404: Video Memory
//
// 0002408 Keyboard:
//         0: Control
//            0: on or off
//         2412: Code
//  	       2416: Down
//
// 0002420 Mouse:
// 	    0: Control
// 	    	   0: on or off
// 	    	2424: pos x
// 	    	2428: pos y
// 	    	2432: Mask
//
// 2436: Timer
//         0: Control
//             0: timer 0 is active
//             1: timer 1 is active
//             2: timer 2 is active
//             3: timer 3 is active
//          2440: current time
//          2444: timer 0 deadline
//          2448: timer 1 deadline
//          2452: timer 2 deadline
//          2456: timer 3 deadline
//
// 3000: Interrupt: invalid memory access
// 3004: Interrupt: Keyboard Interrupt
// 3008: Interrupt: mouse
// 3012: timer 0
// 3016: timer 1
// 3020: timer 2
// 3024: timer 3
// 3028: user defined int 0
// 3032: user defined int 1
// 3036: user defined int 2
// 3040: user defined int3
//
// 4000: main program start
package machine

import (
	"asmfiddle"

	"fmt"
	"time"
)

type ram []int

func (c *cpu) Set(loc, val int) {
	if loc%4 != 0 {
		// set some flag to indicate what has happened
		c.registers.SetEIP(3000)
	}

	if loc == 2412 || loc == 2416 || loc == 2424 || loc == 2428 || loc == 2432 || loc == 2440 {
		c.registers.SetEIP(3000)
	}

	loc = loc / 4

	if loc >= 1000 {
		if loc >= 1000+len(c.ram) {
			c.registers.SetEIP(3000)
		}
		c.ram[loc-1000] = val
		return
	}

	c.special[loc] = val
}

func (c *cpu) Get(loc int) int {
	if loc%4 != 0 {
		// set some flag to indicate what has happened
		c.registers.SetEIP(3000)
	}

	loc = loc / 4

	if loc == 2444 {
		return int((time.Now().UnixNano() / 1000000) & 0xFFFFFFFF)
	}

	if loc >= 1000 {
		if loc >= 1000+len(c.ram) {
			c.registers.SetEIP(3000)
		}
		return c.ram[loc-1000]
	}

	return c.special[loc]
}

type cpu struct {
	keyboard asmfiddle.Keyboard
	mouse    asmfiddle.Mouse
	lcd      asmfiddle.LCD
	console  asmfiddle.Console
	fs       asmfiddle.FileSystem

	ram       ram
	registers *registers
	stack     []int

	special [760]int
}

type registers struct {
	data [18]int
}

func (r *registers) ESP() int {
	return r.data[0]
}

func (r *registers) EBP() int {
	return r.data[1]
}

func (r *registers) EIP() int {
	return r.data[2]
}

func (r *registers) EAX() int {
	return r.data[3]
}

func (r *registers) EBX() int {
	return r.data[4]
}

func (r *registers) ECX() int {
	return r.data[5]
}

func (r *registers) EDX() int {
	return r.data[6]
}

func (r *registers) ESI() int {
	return r.data[7]
}

func (r *registers) EDI() int {
	return r.data[8]
}

func (r *registers) R08() int {
	return r.data[9]
}

func (r *registers) R09() int {
	return r.data[10]
}

func (r *registers) R10() int {
	return r.data[11]
}

func (r *registers) R11() int {
	return r.data[12]
}

func (r *registers) R12() int {
	return r.data[13]
}

func (r *registers) R13() int {
	return r.data[14]
}

func (r *registers) R14() int {
	return r.data[15]
}

func (r *registers) R15() int {
	return r.data[16]
}

func (r *registers) FLAGS() int {
	return r.data[17]
}

func (r *registers) SetEIP(val int) {
	r.data[2] = val
}

func (r *registers) IncrEIP(incr int) {
	r.data[2] += incr * 4
}

func (r *registers) String() string {
	return fmt.Sprintf(`
	// Stack pointer, points to the top of the stack
	ESP = %d
	// Base pointer, points to the base of the stack
	EBP = %d
	// Instruction pointer, this is modified with the
	// jump commands, never directly
	EIP = %d

	EAX = %d
	EBX = %d
	ECX = %d
	EDX = %d

	ESI = %d
	EDI = %d

	R08 = %d
	R09 = %d
	R10 = %d
	R11 = %d
	R12 = %d
	R13 = %d
	R14 = %d
	R15 = %d

	FLAGS = %b`, r.data[0], r.data[1], r.data[2], r.data[3], r.data[4], r.data[5],
		r.data[6], r.data[7], r.data[8], r.data[9], r.data[10], r.data[11], r.data[12],
		r.data[13], r.data[14], r.data[15], r.data[16], r.data[17])
}

func (r *registers) Set(i, val int) {
	r.data[i] = val
}

func (c *cpu) Push(val int) {
	c.stack[c.registers.data[0]] = val
	c.registers.data[0] += 1
}

func (c *cpu) Pop() int {
	c.registers.data[0] -= 1
	return c.stack[c.registers.data[0]]
}

func NewCPU(
	keyboard asmfiddle.Keyboard, mouse asmfiddle.Mouse, lcd asmfiddle.LCD,
	fs asmfiddle.FileSystem, console asmfiddle.Console,
	ramsize int, stacksize int,
) asmfiddle.Machine {
	c := &cpu{
		keyboard: keyboard,
		mouse:    mouse,
		lcd:      lcd,
		fs:       fs,
		console:  console,

		ram:       make([]int, ramsize),
		registers: &registers{},
		stack:     make([]int, stacksize),
	}
	c.loadfs()
	c.registers.SetEIP(4000) // main

	// register event handlers
	// c.keyboard.OnKey(asmfiddle.KeyboardHandler(c.onKey))
	// c.mouse.OnMouse(asmfiddle.MouseHandler(c.onMouse))

	return c
}

func (c *cpu) stackDump() string {
	return fmt.Sprintf(`
		stack: %v
		pointer: %d`, c.stack, c.registers.ESP())
}

func (c *cpu) onKey(asmfiddle.KeyEvent) {

}

func (c *cpu) onMouse(asmfiddle.MouseEvent) {

}

func (c *cpu) RAM() []int {
	return []int(c.ram)
}

func (c *cpu) Registers() asmfiddle.Registers {
	return c.registers
}

func (c *cpu) Stack() ([]int, int) {
	return nil, 0
}

func (c *cpu) loadfs() {
	// iterate through c.fs, if any file has a name <int>.ext or <int>
	// load the content of that file in c.ram.

	// eg 2000.txt contains "hello world", so write ram[2000:2012] = "hello world\0"
}

func (c *cpu) readOp() OpCode {
	op := OpCode(c.Get(c.registers.EIP()))
	c.registers.IncrEIP(1)
	return op
}

func (c *cpu) readOne() int {
	one := c.Get(c.registers.EIP())
	c.registers.IncrEIP(1)
	return one
}

func (c *cpu) readTwo() (int, int) {
	one := c.Get(c.registers.EIP())
	two := c.Get(c.registers.EIP() + 4)
	c.registers.IncrEIP(2)
	return one, two
}

func (c *cpu) Run() {
	for {
		if c.registers.EIP() > 4000+len(c.ram)*4 {
			return
		}

		op := c.readOp() // fetch
		switch op {      // decode
		case OpMovRI:
			// execute
			argr, argi := c.readTwo()
			c.registers.Set(argr, argi)
		case OpMovRM:
			argr, argm := c.readTwo()
			c.registers.Set(argr, c.Get(argm))
		case OpMovRR:
			r1, r2 := c.readTwo()
			c.registers.Set(r1, c.registers.data[r2])
		case OpMovMI:
			argm, argi := c.readTwo()
			c.Set(argm, argi)
		case OpMovMM:
			m1, m2 := c.readTwo()
			c.Set(m1, c.Get(m2))
		case OpMovMR:
			m, r := c.readTwo()
			c.Set(m, c.registers.data[r])

		case OpPushI:
			c.Push(c.readOne())
		case OpPushM:
			c.Push(c.Get(c.readOne()))
		case OpPushR:
			c.Push(c.registers.data[c.readOne()])

		case OpPrnII:
			argi := c.readOne()
			c.console.Print(fmt.Sprintf("%d", argi))
		case OpPrnIR:
			r := c.readOne()
			c.console.Print(fmt.Sprintf("%d", c.registers.data[r]))
		case OpPrnIM:
			m := c.readOne()
			c.console.Print(fmt.Sprintf("%d", c.Get(m)))
		case OpHalt:
			return
		}
	}
}

type OpCode int

func (o OpCode) String() string {
	switch o {
	case OpMovRI:
		return "MovRI"
	case OpPrnII:
		return "PrnII"
	case OpHalt:
		return "Halt"
	default:
		return fmt.Sprintf("Unknown: %d", o)
	}
}

// I -> immediate
// R -> register
// M -> memory

const (
	OpMovRI OpCode = iota
	OpMovRR
	OpMovRM
	OpMovMI
	OpMovMR
	OpMovMM
	OpPushI
	OpPushR
	OpPushM
	OpPop
	OpPushf
	OpPopf
	OpCall
	OpRet
	OpInc
	OpDec
	OpAdd
	OpSub
	OpMul
	OpDiv
	OpMod
	OpRem
	OpNot
	OpXor
	OpOr
	OpAnd
	OpShl
	OpShr
	OpCmp
	OpJmp
	OpJE
	OpJne
	OpJg
	OpJge
	OpJl
	OpJle
	OpPrnII
	OpPrnIR
	OpPrnIM
	OpPrnSI
	OpPrnSR
	OpPrnSM
	OpInt
	OpHalt
)
<commit_msg>OpPop, OpInc, OpDec and OpAdd<commit_after>// Memory Layout
//
// Peripherals
// 0000000 LCD  [320 x 240]
//         0: Control
//            0: on or off
//            1: clear the LCD
//            2: clear color
//            3: show test message on LCD
//         4-2404: Video Memory
//
// 0002408 Keyboard:
//         0: Control
//            0: on or off
//         2412: Code
//  	       2416: Down
//
// 0002420 Mouse:
// 	    0: Control
// 	    	   0: on or off
// 	    	2424: pos x
// 	    	2428: pos y
// 	    	2432: Mask
//
// 2436: Timer
//         0: Control
//             0: timer 0 is active
//             1: timer 1 is active
//             2: timer 2 is active
//             3: timer 3 is active
//          2440: current time
//          2444: timer 0 deadline
//          2448: timer 1 deadline
//          2452: timer 2 deadline
//          2456: timer 3 deadline
//
// 3000: Interrupt: invalid memory access
// 3004: Interrupt: Keyboard Interrupt
// 3008: Interrupt: mouse
// 3012: timer 0
// 3016: timer 1
// 3020: timer 2
// 3024: timer 3
// 3028: user defined int 0
// 3032: user defined int 1
// 3036: user defined int 2
// 3040: user defined int3
//
// 4000: main program start
package machine

import (
	"asmfiddle"

	"fmt"
	"time"
)

type ram []int

func (c *cpu) Set(loc, val int) {
	if loc%4 != 0 {
		// set some flag to indicate what has happened
		c.registers.SetEIP(3000)
	}

	if loc == 2412 || loc == 2416 || loc == 2424 || loc == 2428 || loc == 2432 || loc == 2440 {
		c.registers.SetEIP(3000)
	}

	loc = loc / 4

	if loc >= 1000 {
		if loc >= 1000+len(c.ram) {
			c.registers.SetEIP(3000)
		}
		c.ram[loc-1000] = val
		return
	}

	c.special[loc] = val
}

func (c *cpu) Get(loc int) int {
	if loc%4 != 0 {
		// set some flag to indicate what has happened
		c.registers.SetEIP(3000)
	}

	loc = loc / 4

	if loc == 2444 {
		return int((time.Now().UnixNano() / 1000000) & 0xFFFFFFFF)
	}

	if loc >= 1000 {
		if loc >= 1000+len(c.ram) {
			c.registers.SetEIP(3000)
		}
		return c.ram[loc-1000]
	}

	return c.special[loc]
}

type cpu struct {
	keyboard asmfiddle.Keyboard
	mouse    asmfiddle.Mouse
	lcd      asmfiddle.LCD
	console  asmfiddle.Console
	fs       asmfiddle.FileSystem

	ram       ram
	registers *registers
	stack     []int

	special [760]int
}

type registers struct {
	data [18]int
}

func (r *registers) ESP() int {
	return r.data[0]
}

func (r *registers) EBP() int {
	return r.data[1]
}

func (r *registers) EIP() int {
	return r.data[2]
}

func (r *registers) EAX() int {
	return r.data[3]
}

func (r *registers) EBX() int {
	return r.data[4]
}

func (r *registers) ECX() int {
	return r.data[5]
}

func (r *registers) EDX() int {
	return r.data[6]
}

func (r *registers) ESI() int {
	return r.data[7]
}

func (r *registers) EDI() int {
	return r.data[8]
}

func (r *registers) R08() int {
	return r.data[9]
}

func (r *registers) R09() int {
	return r.data[10]
}

func (r *registers) R10() int {
	return r.data[11]
}

func (r *registers) R11() int {
	return r.data[12]
}

func (r *registers) R12() int {
	return r.data[13]
}

func (r *registers) R13() int {
	return r.data[14]
}

func (r *registers) R14() int {
	return r.data[15]
}

func (r *registers) R15() int {
	return r.data[16]
}

func (r *registers) FLAGS() int {
	return r.data[17]
}

func (r *registers) SetEIP(val int) {
	r.data[2] = val
}

func (r *registers) IncrEIP(incr int) {
	r.data[2] += incr * 4
}

func (r *registers) String() string {
	return fmt.Sprintf(`
	// Stack pointer, points to the top of the stack
	ESP = %d
	// Base pointer, points to the base of the stack
	EBP = %d
	// Instruction pointer, this is modified with the
	// jump commands, never directly
	EIP = %d

	EAX = %d
	EBX = %d
	ECX = %d
	EDX = %d

	ESI = %d
	EDI = %d

	R08 = %d
	R09 = %d
	R10 = %d
	R11 = %d
	R12 = %d
	R13 = %d
	R14 = %d
	R15 = %d

	FLAGS = %b`, r.data[0], r.data[1], r.data[2], r.data[3], r.data[4], r.data[5],
		r.data[6], r.data[7], r.data[8], r.data[9], r.data[10], r.data[11], r.data[12],
		r.data[13], r.data[14], r.data[15], r.data[16], r.data[17])
}

func (r *registers) Set(i, val int) {
	r.data[i] = val
}

func (c *cpu) Push(val int) {
	c.stack[c.registers.data[0]] = val
	c.registers.data[0] += 1
}

func (c *cpu) Pop() int {
	c.registers.data[0] -= 1
	return c.stack[c.registers.data[0]]
}

func NewCPU(
	keyboard asmfiddle.Keyboard, mouse asmfiddle.Mouse, lcd asmfiddle.LCD,
	fs asmfiddle.FileSystem, console asmfiddle.Console,
	ramsize int, stacksize int,
) asmfiddle.Machine {
	c := &cpu{
		keyboard: keyboard,
		mouse:    mouse,
		lcd:      lcd,
		fs:       fs,
		console:  console,

		ram:       make([]int, ramsize),
		registers: &registers{},
		stack:     make([]int, stacksize),
	}
	c.loadfs()
	c.registers.SetEIP(4000) // main

	// register event handlers
	// c.keyboard.OnKey(asmfiddle.KeyboardHandler(c.onKey))
	// c.mouse.OnMouse(asmfiddle.MouseHandler(c.onMouse))

	return c
}

func (c *cpu) stackDump() string {
	return fmt.Sprintf(`
		stack: %v
		pointer: %d`, c.stack, c.registers.ESP())
}

func (c *cpu) onKey(asmfiddle.KeyEvent) {

}

func (c *cpu) onMouse(asmfiddle.MouseEvent) {

}

func (c *cpu) RAM() []int {
	return []int(c.ram)
}

func (c *cpu) Registers() asmfiddle.Registers {
	return c.registers
}

func (c *cpu) Stack() ([]int, int) {
	return nil, 0
}

func (c *cpu) loadfs() {
	// iterate through c.fs, if any file has a name <int>.ext or <int>
	// load the content of that file in c.ram.

	// eg 2000.txt contains "hello world", so write ram[2000:2012] = "hello world\0"
}

func (c *cpu) readOp() OpCode {
	op := OpCode(c.Get(c.registers.EIP()))
	c.registers.IncrEIP(1)
	return op
}

func (c *cpu) readOne() int {
	one := c.Get(c.registers.EIP())
	c.registers.IncrEIP(1)
	return one
}

func (c *cpu) readTwo() (int, int) {
	one := c.Get(c.registers.EIP())
	two := c.Get(c.registers.EIP() + 4)
	c.registers.IncrEIP(2)
	return one, two
}

func (c *cpu) Run() {
	for {
		if c.registers.EIP() > 4000+len(c.ram)*4 {
			return
		}

		op := c.readOp() // fetch
		switch op {      // decode
		case OpMovRI:
			// execute
			argr, argi := c.readTwo()
			c.registers.Set(argr, argi)
		case OpMovRM:
			argr, argm := c.readTwo()
			c.registers.Set(argr, c.Get(argm))
		case OpMovRR:
			r1, r2 := c.readTwo()
			c.registers.Set(r1, c.registers.data[r2])
		case OpMovMI:
			argm, argi := c.readTwo()
			c.Set(argm, argi)
		case OpMovMM:
			m1, m2 := c.readTwo()
			c.Set(m1, c.Get(m2))
		case OpMovMR:
			m, r := c.readTwo()
			c.Set(m, c.registers.data[r])

		case OpPushI:
			c.Push(c.readOne())
		case OpPushM:
			c.Push(c.Get(c.readOne()))
		case OpPushR:
			c.Push(c.registers.data[c.readOne()])

		case OpPopR:
			c.registers.data[c.readOne()] = c.Pop()
		case OpPopM:
			c.Set(c.readOne(), c.Pop())

		case OpIncR:
			c.registers.data[c.readOne()]++
		case OpIncM:
			m := c.readOne()
			c.Set(m, c.Get(m)+1)

		case OpDecR:
			c.registers.data[c.readOne()]--
		case OpDecM:
			m := c.readOne()
			c.Set(m, c.Get(m)-1)

		case OpAddRI:
			argr, argi := c.readTwo()
			c.registers.Set(argr, argi+c.registers.data[argr])
		case OpAddRR:
			r1, r2 := c.readTwo()
			c.registers.Set(r1, c.registers.data[r1]+c.registers.data[r2])
		case OpAddRM:
			argr, argm := c.readTwo()
			c.registers.Set(argr, c.registers.data[argr]+c.Get(argm))
		case OpAddMI:
			argm, argi := c.readTwo()
			c.Set(argm, c.Get(argm)+argi)
		case OpAddMR:
			m, r := c.readTwo()
			c.Set(m, c.registers.data[r]+c.Get(m))
		case OpAddMM:
			m1, m2 := c.readTwo()
			c.Set(m1, c.Get(m2)+c.Get(m1))

		case OpPrnII:
			argi := c.readOne()
			c.console.Print(fmt.Sprintf("%d", argi))
		case OpPrnIR:
			r := c.readOne()
			c.console.Print(fmt.Sprintf("%d", c.registers.data[r]))
		case OpPrnIM:
			m := c.readOne()
			c.console.Print(fmt.Sprintf("%d", c.Get(m)))
		case OpHalt:
			return
		}
	}
}

type OpCode int

func (o OpCode) String() string {
	switch o {
	case OpMovRI:
		return "MovRI"
	case OpPrnII:
		return "PrnII"
	case OpHalt:
		return "Halt"
	default:
		return fmt.Sprintf("Unknown: %d", o)
	}
}

// I -> immediate
// R -> register
// M -> memory

const (
	OpMovRI OpCode = iota
	OpMovRR
	OpMovRM
	OpMovMI
	OpMovMR
	OpMovMM

	OpPushI
	OpPushR
	OpPushM

	OpPopR
	OpPopM

	OpCallI
	OpCallM
	OpCallR

	OpRetI
	OpRetM
	OpRetR

	OpIncR
	OpIncM

	OpDecR
	OpDecM

	OpAddRI
	OpAddRR
	OpAddRM
	OpAddMI
	OpAddMR
	OpAddMM

	OpSubRI
	OpSubRM
	OpSubRR
	OpSubMI
	OpSubMM
	OpSubMR

	OpMulRI
	OpMulRR
	OpMulRM
	OpMulMI
	OpMulMR
	OpMulMM

	OpDivRI
	OpDivRR
	OpDivRM
	OpDivMI
	OpDivMM
	OpDivMR

	OpModRI
	OpModRM
	OpModRR
	OpModMI
	OpModMR
	OpModMM

	OpRemRI
	OpRemRM
	OpRemRR
	OpRemMI
	OpRemMR
	OpRemMM

	OpNotRI
	OpNotRM
	OpNotRR
	OpNotMI
	OpNotMR
	OpNotMM

	OpXorRI
	OpXorRM
	OpXorRR
	OpXorMI
	OpXorMR
	OpXorMM

	OpOrRI
	OpOrRM
	OpOrRR
	OpOrMI
	OpOrMM
	OpOrMR

	OpAndRI
	OpAndRM
	OpAndRR
	OpAndMI
	OpAndMM
	OpAndMR

	OpShlRI
	OpShlRR
	OpShlRM
	OpShlMI
	OpShlMR
	OpShlMM

	OpShrRI
	OpShrRR
	OpShrRM
	OpShrMI
	OpShrMR
	OpShrMM

	OpCmpII
	OpCmpIR
	OpCmpIM
	OpCmpRI
	OpCmpRR
	OpCmpRM
	OpCmpMI
	OpCmpMR
	OpCmpMM

	OpJmpI
	OpJmpR
	OpJmpM

	OpJEI
	OpJER
	OpJEM

	OpJneI
	OpJneR
	OpJneM

	OpJgI
	OpJgR
	OpJgM

	OpJgeI
	OpJgeR
	OpJgeM

	OpJlI
	OpJlR
	OpJlM

	OpJleI
	OpJleR
	OpJleM

	OpPrnII
	OpPrnIR
	OpPrnIM
	OpPrnSI
	OpPrnSR
	OpPrnSM

	OpIntI
	OpIntM
	OpIntR

	OpHalt
)
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"

	"github.com/codegangsta/cli"
	"github.com/koding/klientctl/logging"
	"github.com/leeola/service"
)

// newService provides a preconfigured (based on klientctl's config)
// service object to install, uninstall, start and stop Klient.
func newService() (service.Service, error) {
	// TODO: Add hosts's username
	svcConfig := &service.Config{
		Name:        "klient",
		DisplayName: "klient",
		Description: "Koding Service Connector",
		Executable:  filepath.Join(KlientDirectory, "klient.sh"),
		Option: map[string]interface{}{
			"LogStderr": true,
			"LogStdout": true,
		},
	}

	return service.New(&serviceProgram{}, svcConfig)
}

type serviceProgram struct{}

func (p *serviceProgram) Start(s service.Service) error {
	fmt.Println("Error: serviceProgram Start called")
	return nil
}

func (p *serviceProgram) Stop(s service.Service) error {
	fmt.Println("Error: serviceProgram Stop called")
	return nil
}

// InstallCommandFactory is the factory method for InstallCommand.
func InstallCommandFactory(c *cli.Context) int {
	if len(c.Args()) != 1 {
		cli.ShowCommandHelp(c, "install")
		return 1
	}

	// Create the log file early in the install process, so that we can log the
	// install process and any troubles encountered.
	err := createLogFiles(LogFilePath)
	if err != nil {
		fmt.Println(`Error: Unable to create log files.`)
		return 1
	}

	// Now that we created the logfile, set our logger handler to use that newly created
	// file, so that we can log errors during installation.
	f, err := os.OpenFile(LogFilePath, os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		fmt.Println(`Error: Unable to open log files.`)
		return 1
	}
	log.SetHandler(logging.NewWriterHandler(f))
	log.Infof("Installation created log file")

	authToken := c.Args().Get(0)

	// We need to check if the authToken is somehow empty, because klient
	// will default to user/pass if there is no auth token (despite setting
	// the token flag)
	if strings.TrimSpace(authToken) == "" {
		cli.ShowCommandHelp(c, "install")
		return 1
	}

	// Get the supplied kontrolURL, defaulting to the prod kontrol if
	// empty.
	kontrolURL := strings.TrimSpace(c.String("kontrol"))
	if kontrolURL == "" {
		// Default to the config's url
		kontrolURL = KontrolURL
	}

	klientShPath, err := filepath.Abs(filepath.Join(KlientDirectory, "klient.sh"))
	if err != nil {
		fmt.Printf("Error getting %s wrapper path: '%s'\n", KlientName, err)
		return 1
	}

	klientBinPath, err := filepath.Abs(filepath.Join(KlientDirectory, "klient"))
	if err != nil {
		fmt.Printf("Error getting %s path: '%s'\n", KlientName, err)
		return 1
	}

	// Create the installation dir, if needed.
	err = os.MkdirAll(KlientDirectory, 0755)
	if err != nil {
		fmt.Printf("Error creating directory to hold %s: %q\n", KlientName, err)
		return 1
	}

	// TODO: Accept `kd install --user foo` flag to replace the
	// environ checking.
	var sudoCmd string
	for _, s := range os.Environ() {
		env := strings.Split(s, "=")

		if len(env) != 2 {
			continue
		}

		if env[0] == "SUDO_USER" {
			sudoCmd = fmt.Sprintf("sudo -u %s ", env[1])
			break
		}
	}

	// TODO: Stop using this klient.sh file.
	// If the klient.sh file is missing, write it. We can use build tags
	// for os specific tags, if needed.
	_, err = os.Stat(klientShPath)
	if err != nil {
		if os.IsNotExist(err) {
			klientShFile := []byte(fmt.Sprintf(`#!/bin/sh
%sKITE_HOME=%s %s --kontrol-url=%s
`,
				sudoCmd, KiteHome, klientBinPath, kontrolURL))

			// perm -rwr-xr-x, same as klient
			err := ioutil.WriteFile(klientShPath, klientShFile, 0755)
			if err != nil {
				fmt.Printf("Error creating %s wrapper: '%s'\n", KlientName, err)
				return 1
			}

			fmt.Printf("Created %s\n", klientShPath)

		} else {
			// Unknown error stating (possibly permission), exit
			// TODO: Print UX friendly err
			fmt.Println("Error:", err)
			return 1
		}
	}

	fmt.Println("Downloading...")

	if err = downloadRemoteToLocal(S3KlientPath, klientBinPath); err != nil {
		fmt.Printf("Error downloading %s: '%s'\n", KlientName, err)
		return 1
	}

	fmt.Printf("Created %s\n", klientBinPath)
	fmt.Printf(`Authenticating you to the %s

`, KlientName)

	cmd := exec.Command(klientBinPath, "-register",
		"-token", authToken,
		"--kontrol-url", kontrolURL, "--kite-home", KiteHome)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Stdin = os.Stdin

	err = cmd.Run()
	if err != nil {
		// TODO: Log the error, or handle it somehow so it doesn't leak.
		// log.Errorf("Error registering klient. %q", err)
		fmt.Printf(`Error: Failed to authenticate the %s.

Please go back to Koding to get a new code and try again.
`,
			KlientName)
		return 1
	}

	fmt.Printf("Created %s\n", filepath.Join(KiteHome, "kite.key"))

	// Klient is setting the wrong file permissions when installed by ctl,
	// so since this is just ctl problem, we'll just fix the permission
	// here for now.
	if err = os.Chmod(KiteHome, 0755); err != nil {
		fmt.Printf("Error installing %s: '%s'\n", KlientName, err)
		return 1
	}

	if err = os.Chmod(filepath.Join(KiteHome, "kite.key"), 0644); err != nil {
		fmt.Printf("Error installing kite.key: '%s'\n", err)
		return 1
	}

	// Create our interface to the OS specific service
	s, err := newService()
	if err != nil {
		fmt.Printf("Error starting service: '%s'\n", err)
		return 1
	}

	// Install the klient binary as a OS service
	if err = s.Install(); err != nil {
		fmt.Printf("Error installing service: '%s'\n", err)
		return 1
	}

	// Tell the service to start. Normally it starts automatically, but
	// if the user told the service to stop (previously), it may not
	// start automatically.
	//
	// Note that the service may error if it is already running, so
	// we're ignoring any starting errors here. We will verify the
	// connection below, anyway.
	s.Start()

	fmt.Println("Verifying installation...")
	err = WaitUntilStarted(KlientAddress, 5, 1*time.Second)

	// After X times, if err != nil we failed to connect to klient.
	// Inform the user.
	if err != nil {
		fmt.Printf("Error verifying the installation of %s: '%s'\n", KlientName, err)
		return 1
	}

	fmt.Printf("\n\nSuccessfully installed and started the %s!\n", KlientName)

	return 0
}

// createLogFiles creates the logfile(s) as needed.
func createLogFiles(kdLog string) error {
	f, err := os.Create(kdLog)
	if err != nil {
		return err
	}
	defer f.Close()

	// Chmod the kd.log file to 666, so that everyone (even non-sudo) can
	// write to it.
	err = f.Chmod(0666)
	if err != nil {
		return err
	}

	return nil
}
<commit_msg>styleguide: Return err<commit_after>package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"

	"github.com/codegangsta/cli"
	"github.com/koding/klientctl/logging"
	"github.com/leeola/service"
)

// newService provides a preconfigured (based on klientctl's config)
// service object to install, uninstall, start and stop Klient.
func newService() (service.Service, error) {
	// TODO: Add hosts's username
	svcConfig := &service.Config{
		Name:        "klient",
		DisplayName: "klient",
		Description: "Koding Service Connector",
		Executable:  filepath.Join(KlientDirectory, "klient.sh"),
		Option: map[string]interface{}{
			"LogStderr": true,
			"LogStdout": true,
		},
	}

	return service.New(&serviceProgram{}, svcConfig)
}

type serviceProgram struct{}

func (p *serviceProgram) Start(s service.Service) error {
	fmt.Println("Error: serviceProgram Start called")
	return nil
}

func (p *serviceProgram) Stop(s service.Service) error {
	fmt.Println("Error: serviceProgram Stop called")
	return nil
}

// InstallCommandFactory is the factory method for InstallCommand.
func InstallCommandFactory(c *cli.Context) int {
	if len(c.Args()) != 1 {
		cli.ShowCommandHelp(c, "install")
		return 1
	}

	// Create the log file early in the install process, so that we can log the
	// install process and any troubles encountered.
	err := createLogFiles(LogFilePath)
	if err != nil {
		fmt.Println(`Error: Unable to create log files.`)
		return 1
	}

	// Now that we created the logfile, set our logger handler to use that newly created
	// file, so that we can log errors during installation.
	f, err := os.OpenFile(LogFilePath, os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		fmt.Println(`Error: Unable to open log files.`)
		return 1
	}
	log.SetHandler(logging.NewWriterHandler(f))
	log.Infof("Installation created log file")

	authToken := c.Args().Get(0)

	// We need to check if the authToken is somehow empty, because klient
	// will default to user/pass if there is no auth token (despite setting
	// the token flag)
	if strings.TrimSpace(authToken) == "" {
		cli.ShowCommandHelp(c, "install")
		return 1
	}

	// Get the supplied kontrolURL, defaulting to the prod kontrol if
	// empty.
	kontrolURL := strings.TrimSpace(c.String("kontrol"))
	if kontrolURL == "" {
		// Default to the config's url
		kontrolURL = KontrolURL
	}

	klientShPath, err := filepath.Abs(filepath.Join(KlientDirectory, "klient.sh"))
	if err != nil {
		fmt.Printf("Error getting %s wrapper path: '%s'\n", KlientName, err)
		return 1
	}

	klientBinPath, err := filepath.Abs(filepath.Join(KlientDirectory, "klient"))
	if err != nil {
		fmt.Printf("Error getting %s path: '%s'\n", KlientName, err)
		return 1
	}

	// Create the installation dir, if needed.
	err = os.MkdirAll(KlientDirectory, 0755)
	if err != nil {
		fmt.Printf("Error creating directory to hold %s: %q\n", KlientName, err)
		return 1
	}

	// TODO: Accept `kd install --user foo` flag to replace the
	// environ checking.
	var sudoCmd string
	for _, s := range os.Environ() {
		env := strings.Split(s, "=")

		if len(env) != 2 {
			continue
		}

		if env[0] == "SUDO_USER" {
			sudoCmd = fmt.Sprintf("sudo -u %s ", env[1])
			break
		}
	}

	// TODO: Stop using this klient.sh file.
	// If the klient.sh file is missing, write it. We can use build tags
	// for os specific tags, if needed.
	_, err = os.Stat(klientShPath)
	if err != nil {
		if os.IsNotExist(err) {
			klientShFile := []byte(fmt.Sprintf(`#!/bin/sh
%sKITE_HOME=%s %s --kontrol-url=%s
`,
				sudoCmd, KiteHome, klientBinPath, kontrolURL))

			// perm -rwr-xr-x, same as klient
			err := ioutil.WriteFile(klientShPath, klientShFile, 0755)
			if err != nil {
				fmt.Printf("Error creating %s wrapper: '%s'\n", KlientName, err)
				return 1
			}

			fmt.Printf("Created %s\n", klientShPath)

		} else {
			// Unknown error stating (possibly permission), exit
			// TODO: Print UX friendly err
			fmt.Println("Error:", err)
			return 1
		}
	}

	fmt.Println("Downloading...")

	if err = downloadRemoteToLocal(S3KlientPath, klientBinPath); err != nil {
		fmt.Printf("Error downloading %s: '%s'\n", KlientName, err)
		return 1
	}

	fmt.Printf("Created %s\n", klientBinPath)
	fmt.Printf(`Authenticating you to the %s

`, KlientName)

	cmd := exec.Command(klientBinPath, "-register",
		"-token", authToken,
		"--kontrol-url", kontrolURL, "--kite-home", KiteHome)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Stdin = os.Stdin

	err = cmd.Run()
	if err != nil {
		// TODO: Log the error, or handle it somehow so it doesn't leak.
		// log.Errorf("Error registering klient. %q", err)
		fmt.Printf(`Error: Failed to authenticate the %s.

Please go back to Koding to get a new code and try again.
`,
			KlientName)
		return 1
	}

	fmt.Printf("Created %s\n", filepath.Join(KiteHome, "kite.key"))

	// Klient is setting the wrong file permissions when installed by ctl,
	// so since this is just ctl problem, we'll just fix the permission
	// here for now.
	if err = os.Chmod(KiteHome, 0755); err != nil {
		fmt.Printf("Error installing %s: '%s'\n", KlientName, err)
		return 1
	}

	if err = os.Chmod(filepath.Join(KiteHome, "kite.key"), 0644); err != nil {
		fmt.Printf("Error installing kite.key: '%s'\n", err)
		return 1
	}

	// Create our interface to the OS specific service
	s, err := newService()
	if err != nil {
		fmt.Printf("Error starting service: '%s'\n", err)
		return 1
	}

	// Install the klient binary as a OS service
	if err = s.Install(); err != nil {
		fmt.Printf("Error installing service: '%s'\n", err)
		return 1
	}

	// Tell the service to start. Normally it starts automatically, but
	// if the user told the service to stop (previously), it may not
	// start automatically.
	//
	// Note that the service may error if it is already running, so
	// we're ignoring any starting errors here. We will verify the
	// connection below, anyway.
	s.Start()

	fmt.Println("Verifying installation...")
	err = WaitUntilStarted(KlientAddress, 5, 1*time.Second)

	// After X times, if err != nil we failed to connect to klient.
	// Inform the user.
	if err != nil {
		fmt.Printf("Error verifying the installation of %s: '%s'\n", KlientName, err)
		return 1
	}

	fmt.Printf("\n\nSuccessfully installed and started the %s!\n", KlientName)

	return 0
}

// createLogFiles creates the logfile(s) as needed.
func createLogFiles(kdLog string) error {
	f, err := os.Create(kdLog)
	if err != nil {
		return err
	}
	defer f.Close()

	// Chmod the kd.log file to 666, so that everyone (even non-sudo) can
	// write to it.
	return f.Chmod(0666)
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net"
	"os"
	"os/exec"
	"strings"
	"syscall"
	"time"

	"gopkg.in/check.v1"

	"github.com/containers/image/v5/manifest"
)

// This image is known to be x86_64 only right now
const knownNotManifestListedImage_x8664 = "docker://quay.io/coreos/11bot"

const expectedProxySemverMajor = "0.2"

// request is copied from proxy.go
// We intentionally copy to ensure that we catch any unexpected "API" changes
// in the JSON.
type request struct {
	// Method is the name of the function
	Method string `json:"method"`
	// Args is the arguments (parsed inside the fuction)
	Args []interface{} `json:"args"`
}

// reply is copied from proxy.go
type reply struct {
	// Success is true if and only if the call succeeded.
	Success bool `json:"success"`
	// Value is an arbitrary value (or values, as array/map) returned from the call.
	Value interface{} `json:"value"`
	// PipeID is an index into open pipes, and should be passed to FinishPipe
	PipeID uint32 `json:"pipeid"`
	// Error should be non-empty if Success == false
	Error string `json:"error"`
}

// maxMsgSize is also copied from proxy.go
const maxMsgSize = 32 * 1024

type proxy struct {
	c *net.UnixConn
}

type pipefd struct {
	// id is the remote identifier "pipeid"
	id uint
	fd *os.File
}

func (self *proxy) call(method string, args []interface{}) (rval interface{}, fd *pipefd, err error) {
	req := request{
		Method: method,
		Args:   args,
	}
	reqbuf, err := json.Marshal(&req)
	if err != nil {
		return
	}
	n, err := self.c.Write(reqbuf)
	if err != nil {
		return
	}
	if n != len(reqbuf) {
		err = fmt.Errorf("short write during call of %d bytes", n)
		return
	}
	oob := make([]byte, syscall.CmsgSpace(1))
	replybuf := make([]byte, maxMsgSize)
	n, oobn, _, _, err := self.c.ReadMsgUnix(replybuf, oob)
	if err != nil {
		err = fmt.Errorf("reading reply: %v", err)
		return
	}
	var reply reply
	err = json.Unmarshal(replybuf[0:n], &reply)
	if err != nil {
		err = fmt.Errorf("Failed to parse reply: %w", err)
		return
	}
	if !reply.Success {
		err = fmt.Errorf("remote error: %s", reply.Error)
		return
	}

	if reply.PipeID > 0 {
		var scms []syscall.SocketControlMessage
		scms, err = syscall.ParseSocketControlMessage(oob[:oobn])
		if err != nil {
			err = fmt.Errorf("failed to parse control message: %v", err)
			return
		}
		if len(scms) != 1 {
			err = fmt.Errorf("Expected 1 received fd, found %d", len(scms))
			return
		}
		var fds []int
		fds, err = syscall.ParseUnixRights(&scms[0])
		if err != nil {
			err = fmt.Errorf("failed to parse unix rights: %v", err)
			return
		}
		fd = &pipefd{
			fd: os.NewFile(uintptr(fds[0]), "replyfd"),
			id: uint(reply.PipeID),
		}
	}

	rval = reply.Value
	return
}

func (self *proxy) callNoFd(method string, args []interface{}) (rval interface{}, err error) {
	var fd *pipefd
	rval, fd, err = self.call(method, args)
	if err != nil {
		return
	}
	if fd != nil {
		err = fmt.Errorf("Unexpected fd from method %s", method)
		return
	}
	return rval, nil
}

func (self *proxy) callReadAllBytes(method string, args []interface{}) (rval interface{}, buf []byte, err error) {
	var fd *pipefd
	rval, fd, err = self.call(method, args)
	if err != nil {
		return
	}
	if fd == nil {
		err = fmt.Errorf("Expected fd from method %s", method)
		return
	}
	fetchchan := make(chan byteFetch)
	go func() {
		manifestBytes, err := ioutil.ReadAll(fd.fd)
		fetchchan <- byteFetch{
			content: manifestBytes,
			err:     err,
		}
	}()
	_, err = self.callNoFd("FinishPipe", []interface{}{fd.id})
	if err != nil {
		return
	}
	select {
	case fetchRes := <-fetchchan:
		err = fetchRes.err
		if err != nil {
			return
		}

		buf = fetchRes.content
	case <-time.After(5 * time.Minute):
		err = fmt.Errorf("timed out during proxy fetch")
	}
	return
}

func newProxy() (*proxy, error) {
	fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_SEQPACKET, 0)
	if err != nil {
		return nil, err
	}
	myfd := os.NewFile(uintptr(fds[0]), "myfd")
	defer myfd.Close()
	theirfd := os.NewFile(uintptr(fds[1]), "theirfd")
	defer theirfd.Close()

	mysock, err := net.FileConn(myfd)
	if err != nil {
		return nil, err
	}

	// Note ExtraFiles starts at 3
	proc := exec.Command("skopeo", "experimental-image-proxy", "--sockfd", "3")
	proc.Stderr = os.Stderr
	proc.SysProcAttr = &syscall.SysProcAttr{
		Pdeathsig: syscall.SIGTERM,
	}
	proc.ExtraFiles = append(proc.ExtraFiles, theirfd)

	if err = proc.Start(); err != nil {
		return nil, err
	}

	p := &proxy{
		c: mysock.(*net.UnixConn),
	}

	v, err := p.callNoFd("Initialize", nil)
	if err != nil {
		return nil, err
	}
	semver, ok := v.(string)
	if !ok {
		return nil, fmt.Errorf("proxy Initialize: Unexpected value %T", v)
	}
	if !strings.HasPrefix(semver, expectedProxySemverMajor) {
		return nil, fmt.Errorf("Unexpected semver %s", semver)
	}
	return p, nil
}

func init() {
	check.Suite(&ProxySuite{})
}

type ProxySuite struct {
}

func (s *ProxySuite) SetUpSuite(c *check.C) {
}

func (s *ProxySuite) TearDownSuite(c *check.C) {
}

type byteFetch struct {
	content []byte
	err     error
}

func runTestGetManifest(p *proxy, img string) error {
	v, err := p.callNoFd("OpenImage", []interface{}{knownNotManifestListedImage_x8664})
	if err != nil {
		return err
	}

	imgidv, ok := v.(float64)
	if !ok {
		return fmt.Errorf("OpenImage return value is %T", v)
	}
	imgid := uint32(imgidv)

	v, manifestBytes, err := p.callReadAllBytes("GetManifest", []interface{}{imgid})
	if err != nil {
		return err
	}
	_, err = manifest.OCI1FromManifest(manifestBytes)
	if err != nil {
		return err
	}

	_, err = p.callNoFd("CloseImage", []interface{}{imgid})

	return nil
}

func (s *ProxySuite) TestProxy(c *check.C) {
	p, err := newProxy()
	c.Assert(err, check.IsNil)

	err = runTestGetManifest(p, knownNotManifestListedImage_x8664)
	if err != nil {
		err = fmt.Errorf("Testing image %s: %v", knownNotManifestListedImage_x8664, err)
	}
	c.Assert(err, check.IsNil)

	err = runTestGetManifest(p, knownListImage)
	if err != nil {
		err = fmt.Errorf("Testing image %s: %v", knownListImage, err)
	}
	c.Assert(err, check.IsNil)
}
<commit_msg>proxy_test: Test `GetConfig`<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net"
	"os"
	"os/exec"
	"strings"
	"syscall"
	"time"

	"gopkg.in/check.v1"

	"github.com/containers/image/v5/manifest"
	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
)

// This image is known to be x86_64 only right now
const knownNotManifestListedImage_x8664 = "docker://quay.io/coreos/11bot"

const expectedProxySemverMajor = "0.2"

// request is copied from proxy.go
// We intentionally copy to ensure that we catch any unexpected "API" changes
// in the JSON.
type request struct {
	// Method is the name of the function
	Method string `json:"method"`
	// Args is the arguments (parsed inside the fuction)
	Args []interface{} `json:"args"`
}

// reply is copied from proxy.go
type reply struct {
	// Success is true if and only if the call succeeded.
	Success bool `json:"success"`
	// Value is an arbitrary value (or values, as array/map) returned from the call.
	Value interface{} `json:"value"`
	// PipeID is an index into open pipes, and should be passed to FinishPipe
	PipeID uint32 `json:"pipeid"`
	// Error should be non-empty if Success == false
	Error string `json:"error"`
}

// maxMsgSize is also copied from proxy.go
const maxMsgSize = 32 * 1024

type proxy struct {
	c *net.UnixConn
}

type pipefd struct {
	// id is the remote identifier "pipeid"
	id uint
	fd *os.File
}

func (self *proxy) call(method string, args []interface{}) (rval interface{}, fd *pipefd, err error) {
	req := request{
		Method: method,
		Args:   args,
	}
	reqbuf, err := json.Marshal(&req)
	if err != nil {
		return
	}
	n, err := self.c.Write(reqbuf)
	if err != nil {
		return
	}
	if n != len(reqbuf) {
		err = fmt.Errorf("short write during call of %d bytes", n)
		return
	}
	oob := make([]byte, syscall.CmsgSpace(1))
	replybuf := make([]byte, maxMsgSize)
	n, oobn, _, _, err := self.c.ReadMsgUnix(replybuf, oob)
	if err != nil {
		err = fmt.Errorf("reading reply: %v", err)
		return
	}
	var reply reply
	err = json.Unmarshal(replybuf[0:n], &reply)
	if err != nil {
		err = fmt.Errorf("Failed to parse reply: %w", err)
		return
	}
	if !reply.Success {
		err = fmt.Errorf("remote error: %s", reply.Error)
		return
	}

	if reply.PipeID > 0 {
		var scms []syscall.SocketControlMessage
		scms, err = syscall.ParseSocketControlMessage(oob[:oobn])
		if err != nil {
			err = fmt.Errorf("failed to parse control message: %v", err)
			return
		}
		if len(scms) != 1 {
			err = fmt.Errorf("Expected 1 received fd, found %d", len(scms))
			return
		}
		var fds []int
		fds, err = syscall.ParseUnixRights(&scms[0])
		if err != nil {
			err = fmt.Errorf("failed to parse unix rights: %v", err)
			return
		}
		fd = &pipefd{
			fd: os.NewFile(uintptr(fds[0]), "replyfd"),
			id: uint(reply.PipeID),
		}
	}

	rval = reply.Value
	return
}

func (self *proxy) callNoFd(method string, args []interface{}) (rval interface{}, err error) {
	var fd *pipefd
	rval, fd, err = self.call(method, args)
	if err != nil {
		return
	}
	if fd != nil {
		err = fmt.Errorf("Unexpected fd from method %s", method)
		return
	}
	return rval, nil
}

func (self *proxy) callReadAllBytes(method string, args []interface{}) (rval interface{}, buf []byte, err error) {
	var fd *pipefd
	rval, fd, err = self.call(method, args)
	if err != nil {
		return
	}
	if fd == nil {
		err = fmt.Errorf("Expected fd from method %s", method)
		return
	}
	fetchchan := make(chan byteFetch)
	go func() {
		manifestBytes, err := ioutil.ReadAll(fd.fd)
		fetchchan <- byteFetch{
			content: manifestBytes,
			err:     err,
		}
	}()
	_, err = self.callNoFd("FinishPipe", []interface{}{fd.id})
	if err != nil {
		return
	}
	select {
	case fetchRes := <-fetchchan:
		err = fetchRes.err
		if err != nil {
			return
		}

		buf = fetchRes.content
	case <-time.After(5 * time.Minute):
		err = fmt.Errorf("timed out during proxy fetch")
	}
	return
}

func newProxy() (*proxy, error) {
	fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_SEQPACKET, 0)
	if err != nil {
		return nil, err
	}
	myfd := os.NewFile(uintptr(fds[0]), "myfd")
	defer myfd.Close()
	theirfd := os.NewFile(uintptr(fds[1]), "theirfd")
	defer theirfd.Close()

	mysock, err := net.FileConn(myfd)
	if err != nil {
		return nil, err
	}

	// Note ExtraFiles starts at 3
	proc := exec.Command("skopeo", "experimental-image-proxy", "--sockfd", "3")
	proc.Stderr = os.Stderr
	proc.SysProcAttr = &syscall.SysProcAttr{
		Pdeathsig: syscall.SIGTERM,
	}
	proc.ExtraFiles = append(proc.ExtraFiles, theirfd)

	if err = proc.Start(); err != nil {
		return nil, err
	}

	p := &proxy{
		c: mysock.(*net.UnixConn),
	}

	v, err := p.callNoFd("Initialize", nil)
	if err != nil {
		return nil, err
	}
	semver, ok := v.(string)
	if !ok {
		return nil, fmt.Errorf("proxy Initialize: Unexpected value %T", v)
	}
	if !strings.HasPrefix(semver, expectedProxySemverMajor) {
		return nil, fmt.Errorf("Unexpected semver %s", semver)
	}
	return p, nil
}

func init() {
	check.Suite(&ProxySuite{})
}

type ProxySuite struct {
}

func (s *ProxySuite) SetUpSuite(c *check.C) {
}

func (s *ProxySuite) TearDownSuite(c *check.C) {
}

type byteFetch struct {
	content []byte
	err     error
}

func runTestGetManifestAndConfig(p *proxy, img string) error {
	v, err := p.callNoFd("OpenImage", []interface{}{knownNotManifestListedImage_x8664})
	if err != nil {
		return err
	}

	imgidv, ok := v.(float64)
	if !ok {
		return fmt.Errorf("OpenImage return value is %T", v)
	}
	imgid := uint32(imgidv)

	v, manifestBytes, err := p.callReadAllBytes("GetManifest", []interface{}{imgid})
	if err != nil {
		return err
	}
	_, err = manifest.OCI1FromManifest(manifestBytes)
	if err != nil {
		return err
	}

	v, configBytes, err := p.callReadAllBytes("GetConfig", []interface{}{imgid})
	if err != nil {
		return err
	}
	var config imgspecv1.ImageConfig
	err = json.Unmarshal(configBytes, &config)
	if err != nil {
		return err
	}

	// Validate that the config seems sane
	if len(config.Cmd) == 0 && len(config.Entrypoint) == 0 {
		return fmt.Errorf("No CMD or ENTRYPOINT set")
	}

	_, err = p.callNoFd("CloseImage", []interface{}{imgid})

	return nil
}

func (s *ProxySuite) TestProxy(c *check.C) {
	p, err := newProxy()
	c.Assert(err, check.IsNil)

	err = runTestGetManifestAndConfig(p, knownNotManifestListedImage_x8664)
	if err != nil {
		err = fmt.Errorf("Testing image %s: %v", knownNotManifestListedImage_x8664, err)
	}
	c.Assert(err, check.IsNil)

	err = runTestGetManifestAndConfig(p, knownListImage)
	if err != nil {
		err = fmt.Errorf("Testing image %s: %v", knownListImage, err)
	}
	c.Assert(err, check.IsNil)
}
<|endoftext|>