<commit_before>package server

import (
        "fmt"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"os"
	"strconv"
	"strings"
	"testing"
	"time"

	"github.com/open-lambda/open-lambda/worker/config"
	sbmanager "github.com/open-lambda/open-lambda/worker/sandbox-manager"
	docker "github.com/fsouza/go-dockerclient"
)

var server *Server
var docker_client *docker.Client

func init() {
	server = RunServer()
        var err error
        docker_client, err = docker.NewClientFromEnv()
        if err != nil {
		log.Fatal("failed to get docker client: ", err)
	}
}

func RunServer() *Server {
	conf, err := config.ParseConfig(os.Getenv("WORKER_CONFIG"))
	if err != nil {
		log.Fatal(err)
	}

	log.Printf(conf.Registry)
	log.Printf("Set skip_pull_existing = true\n")
	conf.Skip_pull_existing = true

	server, err := NewServer(conf)
	if err != nil {
		log.Fatal(err)
	}

	listener, err := net.Listen("tcp", ":8080")
	if err != nil {
		log.Fatal(err)
	}
	mux := http.NewServeMux()
	mux.HandleFunc("/runLambda/", server.RunLambda)
	s := &http.Server{Addr: ":8080", Handler: mux}
	go func() {
		log.Fatal(s.Serve(listener))
	}()

	return server
}

func testReq(lambda_name string, post string) (string, error) {
	url := "http://localhost:8080/runLambda/" + lambda_name
	req, err := http.NewRequest("POST", url, strings.NewReader(post))
	if err != nil {
		return "", err
	}
	req.Header.Set("Content-Type", "application/json")
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}
	return string(body), nil
}

func kill() {
        containers, err := docker_client.ListContainers(docker.ListContainersOptions{})
	if err != nil {
		log.Fatal("failed to get docker container list: ", err)
	}

	for _, container := range containers {
            if container.Labels[sbmanager.DOCKER_LABEL_CLUSTER] == server.config.Cluster_name {
                cid := container.ID
                typ := server.config.Cluster_name

	        container_insp, err := docker_client.InspectContainer(cid)
                if err != nil {
		    log.Fatalf("failed to get inspect docker container ID %v: ", cid, err)
	        }

                if container_insp.State.Paused {
		    fmt.Printf("Unpause container %v (%s)\n", cid, typ)
		    if err := docker_client.UnpauseContainer(cid); err != nil {
			fmt.Printf("%s\n", err.Error())
			fmt.Printf("Failed to unpause container %v (%s).  May require manual cleanup.\n", cid, typ)
		    }
		}

		fmt.Printf("Kill container %v (%s)\n", cid, typ)
		opts := docker.KillContainerOptions{ID: cid}
		if err := docker_client.KillContainer(opts); err != nil {
		    fmt.Printf("%s\n", err.Error())
		    fmt.Printf("Failed to kill container %v (%s).  May require manual cleanup.\n", cid, typ)
		}
            }
        }

}

func TestMain(m *testing.M) {
        ret_val := m.Run()
        fmt.Printf("\n========Cleaning========\n")
        kill()
        fmt.Printf("========================\n\n")
        os.Exit(ret_val)
}

func TestHello(t *testing.T) {
	recv, err := testReq("hello", "{}")
	if err != nil {
		t.Fatal(err)
	}
	expected := "\"hello\""
	if recv != expected {
		t.Fatalf("Expected '%v' from hello but got back '%v'\n", expected, recv)
	}
}

func TestEcho(t *testing.T) {
	values := []string{
		"{}",
		"{\"one\": 1}",
		"1",
		"\"test\"",
	}
	for _, send := range values {
		recv, err := testReq("echo", send)
		if err != nil {
			t.Fatal(err)
		}
		if recv != send {
			t.Fatalf("Sent '%v' to echo but got back '%v'\n", send, recv)
		}
	}
}

func last_count(img string) int {
	logs, err := server.handlers.Get(img).Sandbox().Logs()
	if err != nil {
		log.Fatal(err.Error())
	}
	lines := strings.Split(logs, "\n")
	for i := range lines {
		line := lines[len(lines)-i-1]
		parts := strings.Split(line, "=")
		if parts[0] == "counter" {
			if n, err := strconv.Atoi(parts[1]); err == nil {
				return n
			} else {
				panic("not an int: " + parts[1])
			}
		}
	}
	return 0
}

// thread_counter starts a backup thread that runs forever,
// incrementing a counter between 10ms sleeps.  If pausing works, the
// counter won't tick many times between requests, even if wait
// between them.
func TestThreadPausing(t *testing.T) {
	img := "thread_counter"
	testReq(img, "null")
	count1 := last_count(img)
	time.Sleep(100 * time.Millisecond)
	count2 := last_count(img)
	if count1 <= 0 {
		log.Fatal("count1 isn't positive")
	}
	if count2 != count1 {
		log.Fatal("count1 != count2")
	}
}

func BenchmarkEcho(b *testing.B) {
	values := []string{
		"{\"one\": 1}",
	}
	for i := 0; i < b.N; i++ {
		recv, err := testReq("echo", values[0])
		if err != nil {
			b.Fatal(err)
		}
		if recv != values[0] {
			b.Fatalf("Sent '%v' to echo but got back '%v'\n", values[0], recv)
		}
	}
}

func BenchmarkEchoParallel(b *testing.B) {
	values := []string{
		"{\"one\": 1}",
	}
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			recv, err := testReq("echo", values[0])
			if err != nil {
				b.Fatal(err)
			}
			if recv != values[0] {
				b.Fatalf("Sent '%v' to echo but got back '%v'\n", values[0], recv)
			}
		}
	})
}
<commit_msg>format code<commit_after>package server

import (
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"os"
	"strconv"
	"strings"
	"testing"
	"time"

	docker "github.com/fsouza/go-dockerclient"
	"github.com/open-lambda/open-lambda/worker/config"
	sbmanager "github.com/open-lambda/open-lambda/worker/sandbox-manager"
)

var server *Server
var docker_client *docker.Client

func init() {
	server = RunServer()
	var err error
	docker_client, err = docker.NewClientFromEnv()
	if err != nil {
		log.Fatal("failed to get docker client: ", err)
	}
}

func RunServer() *Server {
	conf, err := config.ParseConfig(os.Getenv("WORKER_CONFIG"))
	if err != nil {
		log.Fatal(err)
	}

	log.Printf(conf.Registry)
	log.Printf("Set skip_pull_existing = true\n")
	conf.Skip_pull_existing = true

	server, err := NewServer(conf)
	if err != nil {
		log.Fatal(err)
	}

	listener, err := net.Listen("tcp", ":8080")
	if err != nil {
		log.Fatal(err)
	}
	mux := http.NewServeMux()
	mux.HandleFunc("/runLambda/", server.RunLambda)
	s := &http.Server{Addr: ":8080", Handler: mux}
	go func() {
		log.Fatal(s.Serve(listener))
	}()

	return server
}

func testReq(lambda_name string, post string) (string, error) {
	url := "http://localhost:8080/runLambda/" + lambda_name
	req, err := http.NewRequest("POST", url, strings.NewReader(post))
	if err != nil {
		return "", err
	}
	req.Header.Set("Content-Type", "application/json")
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}
	return string(body), nil
}

func kill() {
	containers, err := docker_client.ListContainers(docker.ListContainersOptions{})
	if err != nil {
		log.Fatal("failed to get docker container list: ", err)
	}

	for _, container := range containers {
		if container.Labels[sbmanager.DOCKER_LABEL_CLUSTER] == server.config.Cluster_name {
			cid := container.ID
			typ := server.config.Cluster_name

			container_insp, err := docker_client.InspectContainer(cid)
			if err != nil {
				log.Fatalf("failed to get inspect docker container ID %v: ", cid, err)
			}

			if container_insp.State.Paused {
				fmt.Printf("Unpause container %v (%s)\n", cid, typ)
				if err := docker_client.UnpauseContainer(cid); err != nil {
					fmt.Printf("%s\n", err.Error())
					fmt.Printf("Failed to unpause container %v (%s).  May require manual cleanup.\n", cid, typ)
				}
			}

			fmt.Printf("Kill container %v (%s)\n", cid, typ)
			opts := docker.KillContainerOptions{ID: cid}
			if err := docker_client.KillContainer(opts); err != nil {
				fmt.Printf("%s\n", err.Error())
				fmt.Printf("Failed to kill container %v (%s).  May require manual cleanup.\n", cid, typ)
			}
		}
	}

}

func TestMain(m *testing.M) {
	ret_val := m.Run()
	fmt.Printf("\n========Cleaning========\n")
	kill()
	fmt.Printf("========================\n\n")
	os.Exit(ret_val)
}

func TestHello(t *testing.T) {
	recv, err := testReq("hello", "{}")
	if err != nil {
		t.Fatal(err)
	}
	expected := "\"hello\""
	if recv != expected {
		t.Fatalf("Expected '%v' from hello but got back '%v'\n", expected, recv)
	}
}

func TestEcho(t *testing.T) {
	values := []string{
		"{}",
		"{\"one\": 1}",
		"1",
		"\"test\"",
	}
	for _, send := range values {
		recv, err := testReq("echo", send)
		if err != nil {
			t.Fatal(err)
		}
		if recv != send {
			t.Fatalf("Sent '%v' to echo but got back '%v'\n", send, recv)
		}
	}
}

func last_count(img string) int {
	logs, err := server.handlers.Get(img).Sandbox().Logs()
	if err != nil {
		log.Fatal(err.Error())
	}
	lines := strings.Split(logs, "\n")
	for i := range lines {
		line := lines[len(lines)-i-1]
		parts := strings.Split(line, "=")
		if parts[0] == "counter" {
			if n, err := strconv.Atoi(parts[1]); err == nil {
				return n
			} else {
				panic("not an int: " + parts[1])
			}
		}
	}
	return 0
}

// thread_counter starts a backup thread that runs forever,
// incrementing a counter between 10ms sleeps.  If pausing works, the
// counter won't tick many times between requests, even if wait
// between them.
func TestThreadPausing(t *testing.T) {
	img := "thread_counter"
	testReq(img, "null")
	count1 := last_count(img)
	time.Sleep(100 * time.Millisecond)
	count2 := last_count(img)
	if count1 <= 0 {
		log.Fatal("count1 isn't positive")
	}
	if count2 != count1 {
		log.Fatal("count1 != count2")
	}
}

func BenchmarkEcho(b *testing.B) {
	values := []string{
		"{\"one\": 1}",
	}
	for i := 0; i < b.N; i++ {
		recv, err := testReq("echo", values[0])
		if err != nil {
			b.Fatal(err)
		}
		if recv != values[0] {
			b.Fatalf("Sent '%v' to echo but got back '%v'\n", values[0], recv)
		}
	}
}

func BenchmarkEchoParallel(b *testing.B) {
	values := []string{
		"{\"one\": 1}",
	}
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			recv, err := testReq("echo", values[0])
			if err != nil {
				b.Fatal(err)
			}
			if recv != values[0] {
				b.Fatalf("Sent '%v' to echo but got back '%v'\n", values[0], recv)
			}
		}
	})
}
<|endoftext|>


<commit_before>package ogdatv21

import (
	"encoding/json"
	"fmt"
	"github.com/the42/ogdat"
	"io/ioutil"
	"os"
	"path"
	"testing"
)

type checkRequest struct {
	filename    string
	followlinks bool
}

type checkResponse struct {
	message []ogdat.CheckMessage
	err     error
}

type checkTest struct {
	in  *checkRequest
	out *checkResponse
}

var checkTests = []checkTest{
	{
		&checkRequest{"file1_test.json", false},
		nil,
	},
	{
		&checkRequest{"allempty.json", false},
		nil,
	},
}

func TestCheck(t *testing.T) {
	md := &MetaData{}

	for _, val := range checkTests {
		file, err := os.Open(path.Join("./testfiles", val.in.filename))
		if err != nil {
			t.Fatal(err)
		}
		ogdjsonmd, err := ioutil.ReadAll(file)
		if err != nil {
			t.Fatal(err)
		}
		if err := json.Unmarshal(ogdjsonmd, md); err != nil {
			t.Fatalf("Can't unmarshall byte stream: %s\n", err)
		}
		msgs, err := md.Check(val.in.followlinks)
		// TODO: BEGIN remove this code
		fmt.Fprintf(os.Stderr, "%d", len(msgs))
		for idx, val := range msgs {
			fmt.Fprintf(os.Stderr, "%d: [%d]: %s\n", idx, val.OGDID, val.Text)
		}
		// END
		if val.out != nil {
			// TODO: compare results

		}
		file.Close()
	}
}
<commit_msg>Check: actually compare results<commit_after>package ogdatv21

import (
	"encoding/json"
	// "fmt"
	"github.com/the42/ogdat"
	"io/ioutil"
	"os"
	"path"
	"testing"
)

type checkRequest struct {
	filename    string
	followlinks bool
}

type checkResponse struct {
	message []ogdat.CheckMessage
	err     error
}

type checkTest struct {
	in  *checkRequest
	out *checkResponse
}

var checkTests = []checkTest{
	{
		&checkRequest{"file1_test.json", false},
		nil,
	},
	{
		&checkRequest{"allempty.json", false},
		&checkResponse{message: []ogdat.CheckMessage{{Type: 4, OGDID: -1}, {Type: 3, OGDID: 1}, {Type: 3, OGDID: 5}, {Type: 3, OGDID: 8}, {Type: 3, OGDID: 9}, {Type: 2, OGDID: 10}, {Type: 2, OGDID: 11}, {Type: 3, OGDID: 19}, {Type: 3, OGDID: 21}, {Type: 3, OGDID: 24}}},
	},
	{
		&checkRequest{"fullandok.json", false},
		&checkResponse{message: []ogdat.CheckMessage{}},
	},
}

func TestCheck(t *testing.T) {

	for numtest, val := range checkTests {
		file, err := os.Open(path.Join("./testfiles", val.in.filename))
		if err != nil {
			t.Fatal(err)
		}
		ogdjsonmd, err := ioutil.ReadAll(file)
		if err != nil {
			t.Fatal(err)
		}
		md := &MetaData{}
		if err := json.Unmarshal(ogdjsonmd, md); err != nil {
			t.Fatalf("Can't unmarshall byte stream: %s\n", err)
		}
		msgs, err := md.Check(val.in.followlinks)
		if val.out != nil {
			testlen := len(val.out.message)
			retlen := len(msgs)
			if testlen != retlen {
				t.Fatalf("TestCheck [%d] (%s): ungleiche Anzahl an Fehlermeldungen. Test:%d, Return:%d", numtest, val.in.filename, testlen, retlen)
			}

			for i := 0; i < testlen; i++ {
				testtype := val.out.message[i].Type
				testid := val.out.message[i].OGDID
				rettype := msgs[i].Type
				retid := msgs[i].OGDID
				if testtype != rettype || testid != retid {
					t.Fatalf("TestCheck [%d] (%s): [Test.Type=%d, Test.OGDID=%d | Return.Type=%d, Return.OGDID=%d]", numtest, val.in.filename, testtype, testid, rettype, retid)
				}
			}
		}
		file.Close()
	}
}
<|endoftext|>


<commit_before>package core

import (
	"archive/tar"
	"bytes"
	"errors"
	"fmt"
	"regexp"
	"strings"
	"time"

	"github.com/fsouza/go-dockerclient"
	"gopkg.in/inconshreveable/log15.v2"
)

var statusUp = regexp.MustCompile("^Up (.*)")
var imageIdRe = regexp.MustCompile("^(.*)/(.*):(.*)")

type Docker struct {
	client *docker.Client
}

func NewDocker(endpoint string) *Docker {
	log15.Debug("Connected to docker endpoint", "endpoint", endpoint)

	c, _ := docker.NewClient(endpoint)

	return &Docker{client: c}
}

func (d *Docker) Deploy(p *Project, commit string, dockerfile []byte) error {
	log15.Info("Deploying dockerfile", "project", p, "commit", commit)
	if err := d.Clean(p, commit); err != nil {
		return err
	}

	if err := d.BuildImage(p, commit, dockerfile); err != nil {
		return err
	}

	return d.Run(p, commit)
}

func (d *Docker) Clean(p *Project, commit string) error {
	l, err := d.ListContainers(p)
	if err != nil {
		return err
	}

	for _, c := range l {
		if c.IsRunning() && c.Image.IsCommit(commit) {
			return errors.New("Current commit is already running")
		}
	}

	log15.Info("Cleaning all containers", "project", p)
	for _, c := range l {
		log15.Info("Killing and removing image", "project", p, "image", c.GetShortId())
		err := d.killAndRemove(c)
		if err != nil {
			return err
		}
	}

	return nil
}

func (d *Docker) ListContainers(p *Project) ([]*Container, error) {
	log15.Debug("Retrieving current containers", "project", p)

	l, err := d.client.ListContainers(docker.ListContainersOptions{
		All: true,
	})

	if err != nil {
		return nil, err
	}

	r := make([]*Container, 0)
	for _, c := range l {
		i := ImageId(c.Image)
		if i.BelongsTo(p) {
			r = append(r, &Container{Image: i, APIContainers: c})
		}
	}

	return r, nil
}

func (d *Docker) killAndRemove(c *Container) error {
	kopts := docker.KillContainerOptions{ID: c.ID}
	if err := d.client.KillContainer(kopts); err != nil {
		return err
	}

	ropts := docker.RemoveContainerOptions{ID: c.ID}
	if err := d.client.RemoveContainer(ropts); err != nil {
		return err
	}

	return nil
}

func (d *Docker) BuildImage(p *Project, commit string, dockerfile []byte) error {
	log15.Info("Building image", "project", p, "commit", commit)

	inputbuf, outputbuf := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
	d.buildTar(dockerfile, inputbuf)

	image := d.getImageName(p, commit)
	opts := docker.BuildImageOptions{
		Name:         string(image),
		InputStream:  inputbuf,
		OutputStream: outputbuf,
	}

	return d.client.BuildImage(opts)
}

func (d *Docker) Run(p *Project, commit string) error {
	log15.Debug("Creating container from image", "project", p, "commit", commit)
	c, err := d.createContainer(d.getImageName(p, commit))
	if err != nil {
		return err
	}

	log15.Info("Running new container",
		"project", p,
		"commit", commit,
		"container", c.GetShortId(),
	)

	return d.startContainer(c)
}

func (d *Docker) getImageName(p *Project, commit string) ImageId {
	return ImageId(fmt.Sprintf("%s/%s:%s", p.Owner, p.Repository, commit))
}

func (d *Docker) createContainer(image ImageId) (*Container, error) {
	c, err := d.client.CreateContainer(docker.CreateContainerOptions{
		Config: &docker.Config{
			Image: string(image),
		},
	})

	if err != nil {
		return nil, err
	}

	return &Container{Image: image, APIContainers: docker.APIContainers{ID: c.ID}}, nil
}

func (d *Docker) startContainer(c *Container) error {
	return d.client.StartContainer(c.ID, &docker.HostConfig{
		PortBindings: map[docker.Port][]docker.PortBinding{
			"80/tcp": []docker.PortBinding{docker.PortBinding{
				HostIp:   "0.0.0.0",
				HostPort: "212",
			}},
		},
	})
}

func (d *Docker) buildTar(dockerfile []byte, buf *bytes.Buffer) *tar.Writer {
	t := time.Now()

	tr := tar.NewWriter(buf)
	tr.WriteHeader(&tar.Header{
		Name:       "Dockerfile",
		Size:       int64(len(dockerfile)),
		ModTime:    t,
		AccessTime: t,
		ChangeTime: t,
	})

	tr.Write(dockerfile)
	tr.Close()

	return tr
}

type ImageId string

func (i ImageId) BelongsTo(p *Project) bool {
	return strings.HasPrefix(string(i), fmt.Sprintf("%s/%s", p.Owner, p.Repository))
}

func (i ImageId) IsCommit(commit string) bool {
	return strings.HasSuffix(string(i), commit)
}

func (i ImageId) GetInfo() (owner, repository, commit string) {
	m := imageIdRe.FindStringSubmatch(string(i))
	owner, repository, commit = m[1], m[2], m[3]

	return
}

type Container struct {
	Image ImageId
	docker.APIContainers
}

func (c *Container) IsRunning() bool {
	return statusUp.MatchString(c.Status)
}

func (c *Container) GetShortId() string {
	shortLen := 12
	if len(c.ID) < shortLen {
		shortLen = len(c.ID)
	}

	return c.ID[:shortLen]
}

func (c *Container) GetPorts() string {
	result := []string{}
	for _, port := range c.Ports {
		if port.IP == "" {
			result = append(result, fmt.Sprintf("%d/%s", port.PrivatePort, port.Type))
		} else {
			result = append(result, fmt.Sprintf("%s:%d->%d/%s", port.IP, port.PublicPort, port.PrivatePort, port.Type))
		}
	}
	return strings.Join(result, ", ")
}
<commit_msg>containers command<commit_after>package core

import (
	"archive/tar"
	"bytes"
	"errors"
	"fmt"
	"regexp"
	"strings"
	"time"

	"github.com/fsouza/go-dockerclient"
	"gopkg.in/inconshreveable/log15.v2"
)

var statusUp = regexp.MustCompile("^Up (.*)")
var imageIdRe = regexp.MustCompile("^(.*)/(.*):(.*)")

func init() {
	log15.LvlFilterHandler(log.Error, log.StdoutHandler)

}

type Docker struct {
	client *docker.Client
}

func NewDocker(endpoint string) *Docker {
	log15.Debug("Connected to docker endpoint", "endpoint", endpoint)

	c, _ := docker.NewClient(endpoint)

	return &Docker{client: c}
}

func (d *Docker) Deploy(p *Project, commit string, dockerfile []byte) error {
	log15.Info("Deploying dockerfile", "project", p, "commit", commit)
	if err := d.Clean(p, commit); err != nil {
		return err
	}

	if err := d.BuildImage(p, commit, dockerfile); err != nil {
		return err
	}

	return d.Run(p, commit)
}

func (d *Docker) Clean(p *Project, commit string) error {
	l, err := d.ListContainers(p)
	if err != nil {
		return err
	}

	for _, c := range l {
		if c.IsRunning() && c.Image.IsCommit(commit) {
			return errors.New("Current commit is already running")
		}
	}

	log15.Info("Cleaning all containers", "project", p)
	for _, c := range l {
		log15.Info("Killing and removing image", "project", p, "image", c.GetShortId())
		err := d.killAndRemove(c)
		if err != nil {
			return err
		}
	}

	return nil
}

func (d *Docker) ListContainers(p *Project) ([]*Container, error) {
	log15.Debug("Retrieving current containers", "project", p)

	l, err := d.client.ListContainers(docker.ListContainersOptions{
		All: true,
	})

	if err != nil {
		return nil, err
	}

	r := make([]*Container, 0)
	for _, c := range l {
		i := ImageId(c.Image)
		if i.BelongsTo(p) {
			r = append(r, &Container{Image: i, APIContainers: c})
		}
	}

	return r, nil
}

func (d *Docker) killAndRemove(c *Container) error {
	kopts := docker.KillContainerOptions{ID: c.ID}
	if err := d.client.KillContainer(kopts); err != nil {
		return err
	}

	ropts := docker.RemoveContainerOptions{ID: c.ID}
	if err := d.client.RemoveContainer(ropts); err != nil {
		return err
	}

	return nil
}

func (d *Docker) BuildImage(p *Project, commit string, dockerfile []byte) error {
	log15.Info("Building image", "project", p, "commit", commit)

	inputbuf, outputbuf := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
	d.buildTar(dockerfile, inputbuf)

	image := d.getImageName(p, commit)
	opts := docker.BuildImageOptions{
		Name:         string(image),
		InputStream:  inputbuf,
		OutputStream: outputbuf,
	}

	return d.client.BuildImage(opts)
}

func (d *Docker) Run(p *Project, commit string) error {
	log15.Debug("Creating container from image", "project", p, "commit", commit)
	c, err := d.createContainer(d.getImageName(p, commit))
	if err != nil {
		return err
	}

	log15.Info("Running new container",
		"project", p,
		"commit", commit,
		"container", c.GetShortId(),
	)

	return d.startContainer(c)
}

func (d *Docker) getImageName(p *Project, commit string) ImageId {
	return ImageId(fmt.Sprintf("%s/%s:%s", p.Owner, p.Repository, commit))
}

func (d *Docker) createContainer(image ImageId) (*Container, error) {
	c, err := d.client.CreateContainer(docker.CreateContainerOptions{
		Config: &docker.Config{
			Image: string(image),
		},
	})

	if err != nil {
		return nil, err
	}

	return &Container{Image: image, APIContainers: docker.APIContainers{ID: c.ID}}, nil
}

func (d *Docker) startContainer(c *Container) error {
	return d.client.StartContainer(c.ID, &docker.HostConfig{
		PortBindings: map[docker.Port][]docker.PortBinding{
			"80/tcp": []docker.PortBinding{docker.PortBinding{
				HostIp:   "0.0.0.0",
				HostPort: "212",
			}},
		},
	})
}

func (d *Docker) buildTar(dockerfile []byte, buf *bytes.Buffer) *tar.Writer {
	t := time.Now()

	tr := tar.NewWriter(buf)
	tr.WriteHeader(&tar.Header{
		Name:       "Dockerfile",
		Size:       int64(len(dockerfile)),
		ModTime:    t,
		AccessTime: t,
		ChangeTime: t,
	})

	tr.Write(dockerfile)
	tr.Close()

	return tr
}

type ImageId string

func (i ImageId) BelongsTo(p *Project) bool {
	return strings.HasPrefix(string(i), fmt.Sprintf("%s/%s", p.Owner, p.Repository))
}

func (i ImageId) IsCommit(commit string) bool {
	return strings.HasSuffix(string(i), commit)
}

func (i ImageId) GetInfo() (owner, repository, commit string) {
	m := imageIdRe.FindStringSubmatch(string(i))
	owner, repository, commit = m[1], m[2], m[3]

	return
}

type Container struct {
	Image ImageId
	docker.APIContainers
}

func (c *Container) IsRunning() bool {
	return statusUp.MatchString(c.Status)
}

func (c *Container) GetShortId() string {
	shortLen := 12
	if len(c.ID) < shortLen {
		shortLen = len(c.ID)
	}

	return c.ID[:shortLen]
}

func (c *Container) GetPorts() string {
	result := []string{}
	for _, port := range c.Ports {
		if port.IP == "" {
			result = append(result, fmt.Sprintf("%d/%s", port.PrivatePort, port.Type))
		} else {
			result = append(result, fmt.Sprintf("%s:%d->%d/%s", port.IP, port.PublicPort, port.PrivatePort, port.Type))
		}
	}
	return strings.Join(result, ", ")
}
<|endoftext|>


<commit_before>// Copyright (c) 2016 Blue Medora, Inc. All rights reserved.
// This file is subject to the terms and conditions defined in the included file 'LICENSE.txt'.

package webserver

import (
	"fmt"
	"path/filepath"

	"github.com/cloudfoundry/gosteno"
	"github.com/cloudfoundry/sonde-go/events"
)

const (
	metronAgentOrigin       = "MetronAgent"
	syslogDrainBinderOrigin = "syslog_drain_binder"
	tpsWatcherOrigin        = "tps_watcher"
	tpsListenerOrigin       = "tps_listener"
	stagerOrigin            = "stager"
	sshProxyOrigin          = "ssh-proxy"
	senderOrigin            = "sender"
	routeEmitterOrigin      = "route_emitter"
	repOrigin               = "rep"
	receptorOrigin          = "receptor"
	nsyncListenerOrigin     = "nsync_listener"
	nsyncBulkerOrigin       = "nsync_bulker"
	gardenLinuxOrigin       = "garden-linux"
	fileServerOrigin        = "file_server"
	fetcherOrigin           = "fetcher"
	convergerOrigin         = "converger"
	ccUploaderOrigin        = "cc_uploader"
	bbsOrigin               = "bbs"
	auctioneerOrigin        = "auctioneer"
	etcdOrigin              = "etcd"
	dopplerServerOrigin     = "DopplerServer"
	cloudControllerOrigin   = "cc"
	trafficControllerOrigin = "LoggregatorTrafficController"
	goRouterOrigin          = "gorouter"
)

//Resource represents cloud controller data
type Resource struct {
	Deployment     string
	Job            string
	Index          string
	IP             string
	ValueMetrics   map[string]float64
	CounterMetrics map[string]float64
}

func createEnvelopeKey(envelope *events.Envelope) string {
	return fmt.Sprintf("%s | %s | %s | %s", envelope.GetDeployment(), envelope.GetJob(), envelope.GetIndex(), envelope.GetIp())
}

func addMetric(envelope *events.Envelope, valueMetricMap map[string]float64, counterMetricMap map[string]float64, logger *gosteno.Logger) {
	if envelope.GetEventType() == events.Envelope_ValueMetric {
		valueMetric := envelope.GetValueMetric()

		valueMetricMap[valueMetric.GetName()] = valueMetric.GetValue()
		logger.Debugf("Adding Value Event Name %s, Value %d", valueMetric.GetName(), valueMetric.GetValue())
	} else if envelope.GetEventType() == events.Envelope_CounterEvent {
		counterEvent := envelope.GetCounterEvent()

		counterMetricMap[counterEvent.GetName()] = float64(counterEvent.GetTotal())
		logger.Debugf("Adding Counter Event Name %s, Value %d", counterEvent.GetName(), counterEvent.GetTotal())
	} else {
		logger.Warnf("Unkown event type %s", envelope.GetEventType())
	}
}

func getValues(resourceMap map[string]Resource) []Resource {
	resources := make([]Resource, 0, len(resourceMap))

	for _, resource := range resourceMap {
		resources = append(resources, resource)
	}

	return resources
}

func getAbsolutePath(file string, logger *gosteno.Logger) string {
	logger.Infof("Finding absolute path to $s", file)
	absolutePath, err := filepath.Abs(file)

	if err != nil {
		logger.Warnf("Error getting absolute path to $s using relative path due to %v", file, err)
		return file
	}

	return absolutePath
}
<commit_msg>Explicitly ignore known firehose message types<commit_after>// Copyright (c) 2016 Blue Medora, Inc. All rights reserved.
// This file is subject to the terms and conditions defined in the included file 'LICENSE.txt'.

package webserver

import (
	"fmt"
	"path/filepath"

	"github.com/cloudfoundry/gosteno"
	"github.com/cloudfoundry/sonde-go/events"
)

const (
	metronAgentOrigin       = "MetronAgent"
	syslogDrainBinderOrigin = "syslog_drain_binder"
	tpsWatcherOrigin        = "tps_watcher"
	tpsListenerOrigin       = "tps_listener"
	stagerOrigin            = "stager"
	sshProxyOrigin          = "ssh-proxy"
	senderOrigin            = "sender"
	routeEmitterOrigin      = "route_emitter"
	repOrigin               = "rep"
	receptorOrigin          = "receptor"
	nsyncListenerOrigin     = "nsync_listener"
	nsyncBulkerOrigin       = "nsync_bulker"
	gardenLinuxOrigin       = "garden-linux"
	fileServerOrigin        = "file_server"
	fetcherOrigin           = "fetcher"
	convergerOrigin         = "converger"
	ccUploaderOrigin        = "cc_uploader"
	bbsOrigin               = "bbs"
	auctioneerOrigin        = "auctioneer"
	etcdOrigin              = "etcd"
	dopplerServerOrigin     = "DopplerServer"
	cloudControllerOrigin   = "cc"
	trafficControllerOrigin = "LoggregatorTrafficController"
	goRouterOrigin          = "gorouter"
)

//Resource represents cloud controller data
type Resource struct {
	Deployment     string
	Job            string
	Index          string
	IP             string
	ValueMetrics   map[string]float64
	CounterMetrics map[string]float64
}

func createEnvelopeKey(envelope *events.Envelope) string {
	return fmt.Sprintf("%s | %s | %s | %s", envelope.GetDeployment(), envelope.GetJob(), envelope.GetIndex(), envelope.GetIp())
}

func addMetric(envelope *events.Envelope, valueMetricMap map[string]float64, counterMetricMap map[string]float64, logger *gosteno.Logger) {
	switch envelope.GetEventType() {
	case events.Envelope_ValueMetric:
		valueMetric := envelope.GetValueMetric()

		valueMetricMap[valueMetric.GetName()] = valueMetric.GetValue()
		logger.Debugf("Adding Value Event Name %s, Value %d", valueMetric.GetName(), valueMetric.GetValue())
	case events.Envelope_CounterEvent:
		counterEvent := envelope.GetCounterEvent()

		counterMetricMap[counterEvent.GetName()] = float64(counterEvent.GetTotal())
		logger.Debugf("Adding Counter Event Name %s, Value %d", counterEvent.GetName(), counterEvent.GetTotal())
	case events.Envelope_ContainerMetric:
		// ignored message type
	case events.Envelope_LogMessage:
		// ignored message type
	case events.Envelope_HttpStartStop:
		// ignored message type
	case events.Envelope_Error:
		// ignored message type
	case events.Envelope_HttpStart:
		// ignored message type
	case events.Envelope_HttpStop:
		// ignored message type
	default:
		logger.Warnf("Unknown event type %s", envelope.GetEventType())
	}
}

func getValues(resourceMap map[string]Resource) []Resource {
	resources := make([]Resource, 0, len(resourceMap))

	for _, resource := range resourceMap {
		resources = append(resources, resource)
	}

	return resources
}

func getAbsolutePath(file string, logger *gosteno.Logger) string {
	logger.Infof("Finding absolute path to $s", file)
	absolutePath, err := filepath.Abs(file)

	if err != nil {
		logger.Warnf("Error getting absolute path to $s using relative path due to %v", file, err)
		return file
	}

	return absolutePath
}
<|endoftext|>


<commit_before>package restful

// Copyright 2013 Ernest Micklei. All rights reserved.
// Use of this source code is governed by a license
// that can be found in the LICENSE file.

import (
	"strconv"
	"strings"
)

// CrossOriginResourceSharing is used to create a Container Filter that implements CORS.
// Cross-origin resource sharing (CORS) is a mechanism that allows JavaScript on a web page
// to make XMLHttpRequests to another domain, not the domain the JavaScript originated from.
//
// http://en.wikipedia.org/wiki/Cross-origin_resource_sharing
// http://enable-cors.org/server.html
// http://www.html5rocks.com/en/tutorials/cors/#toc-handling-a-not-so-simple-request
type CrossOriginResourceSharing struct {
	ExposeHeaders  []string // list of Header names
	AllowedHeaders []string // list of Header names
	AllowedDomains []string // list of allowed values for Http Origin. If empty all are allowed.
	AllowedMethods []string
	MaxAge         int // number of seconds before requiring new Options request
	CookiesAllowed bool
	Container      *Container
}

// Filter is a filter function that implements the CORS flow as documented on http://enable-cors.org/server.html
// and http://www.html5rocks.com/static/images/cors_server_flowchart.png
func (c CrossOriginResourceSharing) Filter(req *Request, resp *Response, chain *FilterChain) {
	origin := req.Request.Header.Get(HEADER_Origin)
	if len(origin) == 0 {
		if trace {
			traceLogger.Println("no Http header Origin set")
		}
		chain.ProcessFilter(req, resp)
		return
	}
	if len(c.AllowedDomains) > 0 { // if provided then origin must be included
		included := false
		for _, each := range c.AllowedDomains {
			if each == origin {
				included = true
				break
			}
		}
		if !included {
			if trace {
				traceLogger.Println("HTTP Origin:%s is not part of %v", origin, c.AllowedDomains)
			}
			chain.ProcessFilter(req, resp)
			return
		}
	}
	if req.Request.Method != "OPTIONS" {
		c.doActualRequest(req, resp)
		chain.ProcessFilter(req, resp)
		return
	}
	if acrm := req.Request.Header.Get(HEADER_AccessControlRequestMethod); acrm != "" {
		c.doPreflightRequest(req, resp)
	} else {
		c.doActualRequest(req, resp)
	}
}

func (c CrossOriginResourceSharing) doActualRequest(req *Request, resp *Response) {
	c.setOptionsHeaders(req, resp)
	// continue processing the response
}

func (c CrossOriginResourceSharing) doPreflightRequest(req *Request, resp *Response) {
	if len(c.AllowedMethods) == 0 {
		c.AllowedMethods = c.Container.computeAllowedMethods(req)
	}

	acrm := req.Request.Header.Get(HEADER_AccessControlRequestMethod)
	if !c.isValidAccessControlRequestMethod(acrm, c.AllowedMethods) {
		if trace {
			traceLogger.Printf("Http header %s:%s is not in %v",
				HEADER_AccessControlRequestMethod,
				acrm,
				c.AllowedMethods)
		}
		return
	}
	acrhs := req.Request.Header.Get(HEADER_AccessControlRequestHeaders)
	if len(acrhs) > 0 {
		for _, each := range strings.Split(acrhs, ",") {
			if !c.isValidAccessControlRequestHeader(strings.Trim(each, " ")) {
				if trace {
					traceLogger.Printf("Http header %s:%s is not in %v",
						HEADER_AccessControlRequestHeaders,
						acrhs,
						c.AllowedHeaders)
				}
				return
			}
		}
	}
	resp.AddHeader(HEADER_AccessControlAllowMethods, strings.Join(c.AllowedMethods, ","))
	resp.AddHeader(HEADER_AccessControlAllowHeaders, acrhs)
	c.setOptionsHeaders(req, resp)

	// return http 200 response, no body
}

func (c CrossOriginResourceSharing) setOptionsHeaders(req *Request, resp *Response) {
	c.checkAndSetExposeHeaders(resp)
	c.setAllowOriginHeader(req, resp)
	c.checkAndSetAllowCredentials(resp)
	if c.MaxAge > 0 {
		resp.AddHeader(HEADER_AccessControlMaxAge, strconv.Itoa(c.MaxAge))
	}
}

func (c CrossOriginResourceSharing) isOriginAllowed(origin string) bool {
	if len(origin) == 0 {
		return false
	}
	if len(c.AllowedDomains) == 0 {
		return true
	}
	allowed := false
	for _, each := range c.AllowedDomains {
		if each == origin {
			allowed = true
			break
		}
	}
	return allowed
}

func (c CrossOriginResourceSharing) setAllowOriginHeader(req *Request, resp *Response) {
	origin := req.Request.Header.Get(HEADER_Origin)
	if c.isOriginAllowed(origin) {
		resp.AddHeader(HEADER_AccessControlAllowOrigin, origin)
	}
}

func (c CrossOriginResourceSharing) checkAndSetExposeHeaders(resp *Response) {
	if len(c.ExposeHeaders) > 0 {
		resp.AddHeader(HEADER_AccessControlExposeHeaders, strings.Join(c.ExposeHeaders, ","))
	}
}

func (c CrossOriginResourceSharing) checkAndSetAllowCredentials(resp *Response) {
	if c.CookiesAllowed {
		resp.AddHeader(HEADER_AccessControlAllowCredentials, "true")
	}
}

func (c CrossOriginResourceSharing) isValidAccessControlRequestMethod(method string, allowedMethods []string) bool {
	for _, each := range allowedMethods {
		if each == method {
			return true
		}
	}
	return false
}

func (c CrossOriginResourceSharing) isValidAccessControlRequestHeader(header string) bool {
	for _, each := range c.AllowedHeaders {
		if strings.ToLower(each) == strings.ToLower(header) {
			return true
		}
	}
	return false
}
<commit_msg>Issue #174 cors and options filter<commit_after>package restful

// Copyright 2013 Ernest Micklei. All rights reserved.
// Use of this source code is governed by a license
// that can be found in the LICENSE file.

import (
	"strconv"
	"strings"
)

// CrossOriginResourceSharing is used to create a Container Filter that implements CORS.
// Cross-origin resource sharing (CORS) is a mechanism that allows JavaScript on a web page
// to make XMLHttpRequests to another domain, not the domain the JavaScript originated from.
//
// http://en.wikipedia.org/wiki/Cross-origin_resource_sharing
// http://enable-cors.org/server.html
// http://www.html5rocks.com/en/tutorials/cors/#toc-handling-a-not-so-simple-request
type CrossOriginResourceSharing struct {
	ExposeHeaders  []string // list of Header names
	AllowedHeaders []string // list of Header names
	AllowedDomains []string // list of allowed values for Http Origin. If empty all are allowed.
	AllowedMethods []string
	MaxAge         int // number of seconds before requiring new Options request
	CookiesAllowed bool
	Container      *Container
}

// Filter is a filter function that implements the CORS flow as documented on http://enable-cors.org/server.html
// and http://www.html5rocks.com/static/images/cors_server_flowchart.png
func (c CrossOriginResourceSharing) Filter(req *Request, resp *Response, chain *FilterChain) {
	origin := req.Request.Header.Get(HEADER_Origin)
	if len(origin) == 0 {
		if trace {
			traceLogger.Println("no Http header Origin set")
		}
		chain.ProcessFilter(req, resp)
		return
	}
	if len(c.AllowedDomains) > 0 { // if provided then origin must be included
		included := false
		for _, each := range c.AllowedDomains {
			if each == origin {
				included = true
				break
			}
		}
		if !included {
			if trace {
				traceLogger.Printf("HTTP Origin:%s is not part of %v", origin, c.AllowedDomains)
			}
			chain.ProcessFilter(req, resp)
			return
		}
	}
	if req.Request.Method != "OPTIONS" {
		c.doActualRequest(req, resp)
		chain.ProcessFilter(req, resp)
		return
	}
	if acrm := req.Request.Header.Get(HEADER_AccessControlRequestMethod); acrm != "" {
		c.doPreflightRequest(req, resp)
	} else {
		c.doActualRequest(req, resp)
		chain.ProcessFilter(req, resp)
		return
	}
}

func (c CrossOriginResourceSharing) doActualRequest(req *Request, resp *Response) {
	c.setOptionsHeaders(req, resp)
	// continue processing the response
}

func (c *CrossOriginResourceSharing) doPreflightRequest(req *Request, resp *Response) {
	if len(c.AllowedMethods) == 0 {
		c.AllowedMethods = c.Container.computeAllowedMethods(req)
	}

	acrm := req.Request.Header.Get(HEADER_AccessControlRequestMethod)
	if !c.isValidAccessControlRequestMethod(acrm, c.AllowedMethods) {
		if trace {
			traceLogger.Printf("Http header %s:%s is not in %v",
				HEADER_AccessControlRequestMethod,
				acrm,
				c.AllowedMethods)
		}
		return
	}
	acrhs := req.Request.Header.Get(HEADER_AccessControlRequestHeaders)
	if len(acrhs) > 0 {
		for _, each := range strings.Split(acrhs, ",") {
			if !c.isValidAccessControlRequestHeader(strings.Trim(each, " ")) {
				if trace {
					traceLogger.Printf("Http header %s:%s is not in %v",
						HEADER_AccessControlRequestHeaders,
						acrhs,
						c.AllowedHeaders)
				}
				return
			}
		}
	}
	resp.AddHeader(HEADER_AccessControlAllowMethods, strings.Join(c.AllowedMethods, ","))
	resp.AddHeader(HEADER_AccessControlAllowHeaders, acrhs)
	c.setOptionsHeaders(req, resp)

	// return http 200 response, no body
}

func (c CrossOriginResourceSharing) setOptionsHeaders(req *Request, resp *Response) {
	c.checkAndSetExposeHeaders(resp)
	c.setAllowOriginHeader(req, resp)
	c.checkAndSetAllowCredentials(resp)
	if c.MaxAge > 0 {
		resp.AddHeader(HEADER_AccessControlMaxAge, strconv.Itoa(c.MaxAge))
	}
}

func (c CrossOriginResourceSharing) isOriginAllowed(origin string) bool {
	if len(origin) == 0 {
		return false
	}
	if len(c.AllowedDomains) == 0 {
		return true
	}
	allowed := false
	for _, each := range c.AllowedDomains {
		if each == origin {
			allowed = true
			break
		}
	}
	return allowed
}

func (c CrossOriginResourceSharing) setAllowOriginHeader(req *Request, resp *Response) {
	origin := req.Request.Header.Get(HEADER_Origin)
	if c.isOriginAllowed(origin) {
		resp.AddHeader(HEADER_AccessControlAllowOrigin, origin)
	}
}

func (c CrossOriginResourceSharing) checkAndSetExposeHeaders(resp *Response) {
	if len(c.ExposeHeaders) > 0 {
		resp.AddHeader(HEADER_AccessControlExposeHeaders, strings.Join(c.ExposeHeaders, ","))
	}
}

func (c CrossOriginResourceSharing) checkAndSetAllowCredentials(resp *Response) {
	if c.CookiesAllowed {
		resp.AddHeader(HEADER_AccessControlAllowCredentials, "true")
	}
}

func (c CrossOriginResourceSharing) isValidAccessControlRequestMethod(method string, allowedMethods []string) bool {
	for _, each := range allowedMethods {
		if each == method {
			return true
		}
	}
	return false
}

func (c CrossOriginResourceSharing) isValidAccessControlRequestHeader(header string) bool {
	for _, each := range c.AllowedHeaders {
		if strings.ToLower(each) == strings.ToLower(header) {
			return true
		}
	}
	return false
}
<|endoftext|>


<commit_before>/*
MIT License

Copyright (c) 2017 Simon Schmidt

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package cparse

import "github.com/byte-mug/semiparse/scanlist"
import "github.com/byte-mug/semiparse/parser"
import "text/scanner"
import "fmt"

const (
	e_none = uint(iota)
	E_VAR
	E_INT
	E_FLOAT
	E_CHAR
	E_STRING

	E_INCR
	E_DECR
	E_FIELD_DOT // Expr.Field
	E_FIELD_PTR // Expr->Field
	E_UNARY_OP
	E_BINARY_OP
	E_BINARY_OP_ASSIGN
	E_COMPARE
	E_ASSIGN
)

func aR(i ...interface{}) []interface{} { return i }

type Expr struct{
	Type uint
	Text string
	Data []interface{}
}
func (e *Expr) String() string {
	if e==nil { return "NIL" }
	switch e.Type {
	case E_BINARY_OP:
		return fmt.Sprint("(",e.Data[0],e.Text,e.Data[1],")")
	case E_BINARY_OP_ASSIGN:
		return fmt.Sprint("(",e.Data[0],e.Text,"=",e.Data[1],")")
	case E_INCR,E_DECR:
		return fmt.Sprint("(",e.Data,e.Text,")")
	case E_FIELD_DOT:
		return fmt.Sprint("(",e.Data,".",e.Text,")")
	case E_FIELD_PTR:
		return fmt.Sprint("(",e.Data,"->",e.Text,")")
	case E_UNARY_OP:
		return fmt.Sprintf("(",e.Text,e.Data,")")
	case E_VAR,E_INT,E_FLOAT,E_CHAR,E_STRING:
		return fmt.Sprint(e.Text)
	case E_ASSIGN:
		return fmt.Sprint("(",e.Data[0],"=",e.Data[1],")")
	}
	return fmt.Sprint("(",e.Type,"#",e.Text,e.Data,")")
}

func c_expr(p *parser.Parser,tokens *scanlist.Element, left interface{}) parser.ParserResult {
	if tokens==nil { return parser.ResultFail("EOF!",scanner.Position{}) }
	switch tokens.Token {
	case scanner.Ident: return parser.ResultOk(tokens.Next(),&Expr{E_VAR,tokens.TokenText,nil})
	case scanner.Int: return parser.ResultOk(tokens.Next(),&Expr{E_INT,tokens.TokenText,nil})
	case scanner.Float: return parser.ResultOk(tokens.Next(),&Expr{E_FLOAT,tokens.TokenText,nil})
	case scanner.Char: return parser.ResultOk(tokens.Next(),&Expr{E_CHAR,tokens.TokenText,nil})
	case scanner.String,scanner.RawString: return parser.ResultOk(tokens.Next(),&Expr{E_STRING,tokens.TokenText,nil})
	case '*','+','-','!','~':{
		sub := p.MatchNoLeftRecursion("Expr",tokens.Next())
		if sub.Result==parser.RESULT_OK {
			sub.Data = &Expr{E_UNARY_OP,tokens.TokenText,aR(sub.Data)}
		}
		return sub
	    }
	case '(': /*)*/{
		sub := p.Match("Expr",tokens.Next())
		if sub.Result==parser.RESULT_OK {/*(*/
			e,t := parser.Match(parser.Textify,sub.Next,')')
			if e!=nil { return parser.ResultFail(fmt.Sprint(e),sub.Next.SafePos()) }
			sub.Next = t
		}
		return sub
	    }
	}
	return parser.ResultFail("Invalid Expression!",tokens.Pos)
}
func c_expr_trailer(p *parser.Parser,tokens *scanlist.Element, left interface{}) parser.ParserResult {
	if ok,t := parser.FastMatch(tokens,'+','+'); ok {
		return parser.ResultOk(t,&Expr{E_INCR,"++",aR(left)})
	}
	if ok,t := parser.FastMatch(tokens,'-','-'); ok {
		return parser.ResultOk(t,&Expr{E_DECR,"--",aR(left)})
	}
	if ok,t := parser.FastMatch(tokens,'-','>',scanner.Ident); ok {
		return parser.ResultOk(t,&Expr{E_FIELD_PTR,tokens.Next().Next().TokenText,aR(left)})
	}
	if ok,t := parser.FastMatch(tokens,'.',scanner.Ident); ok {
		return parser.ResultOk(t,&Expr{E_FIELD_DOT,tokens.Next().TokenText,aR(left)})
	}
	/*
	TODO: function(call);
	*/

	ok,t := parser.FastMatch(tokens,'+')
	s := ""
	if !ok { ok,t = parser.FastMatch(tokens,'-') }
	if !ok { ok,t = parser.FastMatch(tokens,'*') }
	if !ok { ok,t = parser.FastMatch(tokens,'/') }
	if !ok { ok,t = parser.FastMatch(tokens,'%') }
	if !ok { ok,t = parser.FastMatch(tokens,'>','>'); if ok { s=">>" } }
	if !ok { ok,t = parser.FastMatch(tokens,'<','<'); if ok { s="<<" } }
	if !ok { ok,t = parser.FastMatch(tokens,'&','&'); if ok { s="&&" } }
	if !ok { ok,t = parser.FastMatch(tokens,'|','|'); if ok { s="||" } }
	if !ok { ok,t = parser.FastMatch(tokens,'^') }

	if ok {
		if s=="" { s = tokens.TokenText }
		op := E_BINARY_OP
		if t.SafeToken()=='=' {
			op = E_BINARY_OP_ASSIGN
			t = t.SafeNext()
		}
		sub := p.MatchNoLeftRecursion("Expr",t)
		if sub.Result==parser.RESULT_OK {
			sub.Data = &Expr{op,s,aR(left,sub.Data)}
		}
		return sub
		/*
		if sub.Result==parser.RESULT_OK {
			sub.Data = &Expr{E_BINARY_OP,tokens.TokenText,aR(left,sub.Data)}
		}
		*/
	}

	ok,t = parser.FastMatch(tokens,'=','='); if ok { s="==" }
	if !ok { ok,t = parser.FastMatch(tokens,'!','='); if ok { s="!=" } }
	if !ok { ok,t = parser.FastMatch(tokens,'<','='); if ok { s="<=" } }
	if !ok { ok,t = parser.FastMatch(tokens,'<'); if ok { s="<" } }
	if !ok { ok,t = parser.FastMatch(tokens,'>','='); if ok { s=">=" } }
	if !ok { ok,t = parser.FastMatch(tokens,'>'); if ok { s=">" } }
	if ok {
		sub := p.MatchNoLeftRecursion("Expr",t)
		if sub.Result==parser.RESULT_OK {
			sub.Data = &Expr{E_COMPARE,s,aR(left,sub.Data)}
		}
		return sub
	}

	ok,t = parser.FastMatch(tokens,'=')

	if ok {
		sub := p.Match("Expr",t)
		if sub.Result==parser.RESULT_OK {
			sub.Data = &Expr{E_ASSIGN,"=",aR(left,sub.Data)}
		}
		return sub
	}

	return parser.ResultFail("No trailer.",tokens.SafePos())
}

func RegisterExpr(p *parser.Parser) {
	p.Define("Expr",false,parser.Pfunc(c_expr))
	p.Define("Expr",true,parser.Pfunc(c_expr_trailer))
}

<commit_msg>Function call<commit_after>/*
MIT License

Copyright (c) 2017 Simon Schmidt

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package cparse

import "github.com/byte-mug/semiparse/scanlist"
import "github.com/byte-mug/semiparse/parser"
import "text/scanner"
import "fmt"

const (
	e_none = uint(iota)
	E_VAR
	E_INT
	E_FLOAT
	E_CHAR
	E_STRING

	E_INCR
	E_DECR
	E_FIELD_DOT // Expr.Field
	E_FIELD_PTR // Expr->Field
	E_UNARY_OP
	E_BINARY_OP
	E_BINARY_OP_ASSIGN
	E_COMPARE
	E_ASSIGN
	E_FUNCTION_CALL // Expr( [Expr [,Expr]* ] )
)

func aR(i ...interface{}) []interface{} { return i }

type Expr struct{
	Type uint
	Text string
	Data []interface{}
}
func (e *Expr) String() string {
	if e==nil { return "NIL" }
	switch e.Type {
	case E_BINARY_OP:
		return fmt.Sprint("(",e.Data[0],e.Text,e.Data[1],")")
	case E_BINARY_OP_ASSIGN:
		return fmt.Sprint("(",e.Data[0],e.Text,"=",e.Data[1],")")
	case E_INCR,E_DECR:
		return fmt.Sprint("(",e.Data[0],e.Text,")")
	case E_FIELD_DOT:
		return fmt.Sprint("(",e.Data,".",e.Text,")")
	case E_FIELD_PTR:
		return fmt.Sprint("(",e.Data,"->",e.Text,")")
	case E_UNARY_OP:
		return fmt.Sprint("(",e.Text,e.Data[0],")")
	case E_VAR,E_INT,E_FLOAT,E_CHAR,E_STRING:
		return fmt.Sprint(e.Text)
	case E_ASSIGN:
		return fmt.Sprint("(",e.Data[0],"=",e.Data[1],")")
	case E_FUNCTION_CALL:
		return fmt.Sprint("(",e.Data[0]," (",e.Data[1:],") )")
	}
	return fmt.Sprint("(",e.Type,"#",e.Text,e.Data,")")
}

func c_expr_list(p *parser.Parser,tokens *scanlist.Element, sep rune, r []interface{}) parser.ParserResult {
	sub := p.Match("Expr",tokens)
	if sub.Result!=parser.RESULT_OK { return sub }
	r = append(r,sub.Data)
	for sub.Next.SafeToken() == sep {
		sub = p.Match("Expr",sub.Next.Next())
		if sub.Result!=parser.RESULT_OK { return sub }
		r = append(r,sub.Data)
	}
	sub.Data = r
	return sub
}
func c_expr(p *parser.Parser,tokens *scanlist.Element, left interface{}) parser.ParserResult {
	if tokens==nil { return parser.ResultFail("EOF!",scanner.Position{}) }
	switch tokens.Token {
	case scanner.Ident: return parser.ResultOk(tokens.Next(),&Expr{E_VAR,tokens.TokenText,nil})
	case scanner.Int: return parser.ResultOk(tokens.Next(),&Expr{E_INT,tokens.TokenText,nil})
	case scanner.Float: return parser.ResultOk(tokens.Next(),&Expr{E_FLOAT,tokens.TokenText,nil})
	case scanner.Char: return parser.ResultOk(tokens.Next(),&Expr{E_CHAR,tokens.TokenText,nil})
	case scanner.String,scanner.RawString: return parser.ResultOk(tokens.Next(),&Expr{E_STRING,tokens.TokenText,nil})
	case '*','+','-','!','~':{
		sub := p.MatchNoLeftRecursion("Expr",tokens.Next())
		if sub.Result==parser.RESULT_OK {
			sub.Data = &Expr{E_UNARY_OP,tokens.TokenText,aR(sub.Data)}
		}
		return sub
	    }
	case '(': /*)*/{
		sub := p.Match("Expr",tokens.Next())
		if sub.Result==parser.RESULT_OK {/*(*/
			e,t := parser.Match(parser.Textify,sub.Next,')')
			if e!=nil { return parser.ResultFail(fmt.Sprint(e),sub.Next.SafePos()) }
			sub.Next = t
		}
		return sub
	    }
	}
	return parser.ResultFail("Invalid Expression!",tokens.Pos)
}
func c_expr_trailer(p *parser.Parser,tokens *scanlist.Element, left interface{}) parser.ParserResult {
	if ok,t := parser.FastMatch(tokens,'+','+'); ok {
		return parser.ResultOk(t,&Expr{E_INCR,"++",aR(left)})
	}
	if ok,t := parser.FastMatch(tokens,'-','-'); ok {
		return parser.ResultOk(t,&Expr{E_DECR,"--",aR(left)})
	}
	if ok,t := parser.FastMatch(tokens,'-','>',scanner.Ident); ok {
		return parser.ResultOk(t,&Expr{E_FIELD_PTR,tokens.Next().Next().TokenText,aR(left)})
	}
	if ok,t := parser.FastMatch(tokens,'.',scanner.Ident); ok {
		return parser.ResultOk(t,&Expr{E_FIELD_DOT,tokens.Next().TokenText,aR(left)})
	}
	if tokens.SafeToken()=='(' /*)*/ {
		sub := c_expr_list(p,tokens.Next(),',',aR(left))
		if sub.Result==parser.RESULT_OK {/*(*/
			e,t := parser.Match(parser.Textify,sub.Next,')')
			if e!=nil { return parser.ResultFail(fmt.Sprint(e),sub.Next.SafePos()) }
			sub.Next = t
			sub.Data = &Expr{E_FUNCTION_CALL,"()",sub.Data.([]interface{})}
		}
		return sub
	}

	ok,t := parser.FastMatch(tokens,'+')
	s := ""
	if !ok { ok,t = parser.FastMatch(tokens,'-') }
	if !ok { ok,t = parser.FastMatch(tokens,'*') }
	if !ok { ok,t = parser.FastMatch(tokens,'/') }
	if !ok { ok,t = parser.FastMatch(tokens,'%') }
	if !ok { ok,t = parser.FastMatch(tokens,'>','>'); if ok { s=">>" } }
	if !ok { ok,t = parser.FastMatch(tokens,'<','<'); if ok { s="<<" } }
	if !ok { ok,t = parser.FastMatch(tokens,'&','&'); if ok { s="&&" } }
	if !ok { ok,t = parser.FastMatch(tokens,'|','|'); if ok { s="||" } }
	if !ok { ok,t = parser.FastMatch(tokens,'^') }

	if ok {
		if s=="" { s = tokens.TokenText }
		op := E_BINARY_OP
		if t.SafeToken()=='=' {
			op = E_BINARY_OP_ASSIGN
			t = t.SafeNext()
		}
		sub := p.MatchNoLeftRecursion("Expr",t)
		if sub.Result==parser.RESULT_OK {
			sub.Data = &Expr{op,s,aR(left,sub.Data)}
		}
		return sub
		/*
		if sub.Result==parser.RESULT_OK {
			sub.Data = &Expr{E_BINARY_OP,tokens.TokenText,aR(left,sub.Data)}
		}
		*/
	}

	ok,t = parser.FastMatch(tokens,'=','='); if ok { s="==" }
	if !ok { ok,t = parser.FastMatch(tokens,'!','='); if ok { s="!=" } }
	if !ok { ok,t = parser.FastMatch(tokens,'<','='); if ok { s="<=" } }
	if !ok { ok,t = parser.FastMatch(tokens,'<'); if ok { s="<" } }
	if !ok { ok,t = parser.FastMatch(tokens,'>','='); if ok { s=">=" } }
	if !ok { ok,t = parser.FastMatch(tokens,'>'); if ok { s=">" } }
	if ok {
		sub := p.MatchNoLeftRecursion("Expr",t)
		if sub.Result==parser.RESULT_OK {
			sub.Data = &Expr{E_COMPARE,s,aR(left,sub.Data)}
		}
		return sub
	}

	ok,t = parser.FastMatch(tokens,'=')

	if ok {
		sub := p.Match("Expr",t)
		if sub.Result==parser.RESULT_OK {
			sub.Data = &Expr{E_ASSIGN,"=",aR(left,sub.Data)}
		}
		return sub
	}

	return parser.ResultFail("No trailer.",tokens.SafePos())
}

func RegisterExpr(p *parser.Parser) {
	p.Define("Expr",false,parser.Pfunc(c_expr))
	p.Define("Expr",true,parser.Pfunc(c_expr_trailer))
}

<|endoftext|>


<commit_before>// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.

package crdb

import (
	"context"
)

// Tx abstracts the operations needed by ExecuteInTx so that different
// frameworks (e.g. go's sql package, pgx, gorm) can be used with ExecuteInTx.
type Tx interface {
	Exec(context.Context, string, ...interface{}) error
	Commit(context.Context) error
	Rollback(context.Context) error
}

// ExecuteInTx runs fn inside tx. This method is primarily intended for internal
// use. See other packages for higher-level, framework-specific ExecuteTx()
// functions.
//
// *WARNING*: It is assumed that no statements have been executed on the
// supplied Tx. ExecuteInTx will only retry statements that are performed within
// the supplied closure (fn). Any statements performed on the tx before
// ExecuteInTx is invoked will *not* be re-run if the transaction needs to be
// retried.
//
// fn is subject to the same restrictions as the fn passed to ExecuteTx.
func ExecuteInTx(ctx context.Context, tx Tx, fn func() error) (err error) {
	defer func() {
		r := recover()

		if r == nil && err == nil {
			// Ignore commit errors. The tx has already been committed by RELEASE.
			_ = tx.Commit(ctx)
			return
		}

		// We always need to execute a Rollback() so sql.DB releases the
		// connection.
		_ = tx.Rollback(ctx)

		if r != nil {
			panic(r)
		}
	}()

	// Specify that we intend to retry this txn in case of CockroachDB retryable
	// errors.
	if err = tx.Exec(ctx, "SAVEPOINT cockroach_restart"); err != nil {
		return err
	}

	// TODO(rafi): make the maxRetryCount configurable. Maybe pass it in the context?)
	const maxRetries = 50
	retryCount := 0
	for {
		releaseFailed := false
		if err = fn(); err == nil {
			// RELEASE acts like COMMIT in CockroachDB. We use it since it gives us an
			// opportunity to react to retryable errors, whereas tx.Commit() doesn't.
			if err = tx.Exec(ctx, "RELEASE SAVEPOINT cockroach_restart"); err == nil {
				return nil
			}
			releaseFailed = true
		}

		// We got an error (i.e. err != nil) if at this point

		// Let's see if it's a retryable one and, if so, restart.
		if !errIsRetryable(err) {
			if releaseFailed {
				err = newAmbiguousCommitError(err)
			}
			return err
		}

		if rollbackErr := tx.Exec(ctx, "ROLLBACK TO SAVEPOINT cockroach_restart"); rollbackErr != nil {
			err = newTxnRestartError(rollbackErr, err)
			return err
		}

		retryCount++
		if retryCount > maxRetries {
			err = newMaxRetriesExceededError(err, maxRetries)
			return err
		}
	}
}
<commit_msg>Fix comment, undo unnecessary var assignment<commit_after>// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.

package crdb

import (
	"context"
)

// Tx abstracts the operations needed by ExecuteInTx so that different
// frameworks (e.g. go's sql package, pgx, gorm) can be used with ExecuteInTx.
type Tx interface {
	Exec(context.Context, string, ...interface{}) error
	Commit(context.Context) error
	Rollback(context.Context) error
}

// ExecuteInTx runs fn inside tx. This method is primarily intended for internal
// use. See other packages for higher-level, framework-specific ExecuteTx()
// functions.
//
// *WARNING*: It is assumed that no statements have been executed on the
// supplied Tx. ExecuteInTx will only retry statements that are performed within
// the supplied closure (fn). Any statements performed on the tx before
// ExecuteInTx is invoked will *not* be re-run if the transaction needs to be
// retried.
//
// fn is subject to the same restrictions as the fn passed to ExecuteTx.
func ExecuteInTx(ctx context.Context, tx Tx, fn func() error) (err error) {
	defer func() {
		r := recover()

		if r == nil && err == nil {
			// Ignore commit errors. The tx has already been committed by RELEASE.
			_ = tx.Commit(ctx)
			return
		}

		// We always need to execute a Rollback() so sql.DB releases the
		// connection.
		_ = tx.Rollback(ctx)

		if r != nil {
			panic(r)
		}
	}()

	// Specify that we intend to retry this txn in case of CockroachDB retryable
	// errors.
	if err = tx.Exec(ctx, "SAVEPOINT cockroach_restart"); err != nil {
		return err
	}

	// TODO(rafi): make the maxRetryCount configurable. Maybe pass it in the context?)
	const maxRetries = 50
	retryCount := 0
	for {
		releaseFailed := false
		err = fn()
		if err == nil {
			// RELEASE acts like COMMIT in CockroachDB. We use it since it gives us an
			// opportunity to react to retryable errors, whereas tx.Commit() doesn't.
			if err = tx.Exec(ctx, "RELEASE SAVEPOINT cockroach_restart"); err == nil {
				return nil
			}
			releaseFailed = true
		}

		// We got an error; let's see if it's a retryable one and, if so, restart.
		if !errIsRetryable(err) {
			if releaseFailed {
				err = newAmbiguousCommitError(err)
			}
			return err
		}

		if rollbackErr := tx.Exec(ctx, "ROLLBACK TO SAVEPOINT cockroach_restart"); rollbackErr != nil {
			return newTxnRestartError(rollbackErr, err)
		}

		retryCount++
		if retryCount > maxRetries {
			return newMaxRetriesExceededError(err, maxRetries)
		}
	}
}
<|endoftext|>


<commit_before>package commenttags

import (
	"fmt"
	"strings"
)

// CommentTag store data for a code comment tag
type CommentTag struct {
	Tag     string
	Line    int
	Message string
}

// Use the ParseComment func to parse a source and return a new CommentTag.
func ParseComment(source string) (*CommentTag, bool) {
	var tag *CommentTag
	var found bool
	for i := 0; i < 5; i++ {
		switch i {
		case 0:
			tag, found = ParseCommentTag(source, TagFIXME)
			break
		case 1:
			tag, found = ParseCommentTag(source, TagTODO)
			break
		case 2:
			tag, found = ParseCommentTag(source, TagHACK)
			break
		case 3:
			tag, found = ParseCommentTag(source, TagUNDONE)
			break
		case 4:
			tag, found = ParseCommentTag(source, TagXXX)
			break
		}
		if found {
			return tag, true
		}
	}
	return &CommentTag{}, false
}

func ParseCommentTag(source, tag string) (*CommentTag, bool) {
	r := CommentTag{}
	// check if tag exist
	search := tag + ":"
	// fmt.Println("Search:", search, "Tag:", tag)
	if strings.Contains(source, search) {
		// get message
		splitted := strings.SplitAfter(source, search)
		if len(splitted) > 1 {
			r.Message = strings.TrimSpace(splitted[1])
			r.Tag = tag
			return &r, true
		}
	}
	return &r, false
}

// Print the data to stdout
func (t *CommentTag) Pretty() string {
	return fmt.Sprintf("Tag: %s \tLine: %d \tMessage: '%s'\n", t.Tag, t.Line, t.Message)
}

func (t *CommentTag) PrettyPrint() {
	fmt.Printf(t.Pretty())
}
<commit_msg>set json keys at CommentTag<commit_after>package commenttags

import (
	"fmt"
	"strings"
)

// CommentTag store data for a code comment tag
type CommentTag struct {
	Tag     string `json:"tag"`
	Line    int    `json:"line"`
	Message string `json:"message"`
}

// Use the ParseComment func to parse a source and return a new CommentTag.
func ParseComment(source string) (*CommentTag, bool) {
	var tag *CommentTag
	var found bool
	for i := 0; i < 5; i++ {
		switch i {
		case 0:
			tag, found = ParseCommentTag(source, TagFIXME)
			break
		case 1:
			tag, found = ParseCommentTag(source, TagTODO)
			break
		case 2:
			tag, found = ParseCommentTag(source, TagHACK)
			break
		case 3:
			tag, found = ParseCommentTag(source, TagUNDONE)
			break
		case 4:
			tag, found = ParseCommentTag(source, TagXXX)
			break
		}
		if found {
			return tag, true
		}
	}
	return &CommentTag{}, false
}

func ParseCommentTag(source, tag string) (*CommentTag, bool) {
	r := CommentTag{}
	// check if tag exist
	search := tag + ":"
	// fmt.Println("Search:", search, "Tag:", tag)
	if strings.Contains(source, search) {
		// get message
		splitted := strings.SplitAfter(source, search)
		if len(splitted) > 1 {
			r.Message = strings.TrimSpace(splitted[1])
			r.Tag = tag
			return &r, true
		}
	}
	return &r, false
}

// Print the data to stdout
func (t *CommentTag) Pretty() string {
	return fmt.Sprintf("Tag %s @line %d \t '%s'\n", t.Tag, t.Line, t.Message)
}

func (t *CommentTag) PrettyPrint() {
	fmt.Printf(t.Pretty())
}
<|endoftext|>


<commit_before>/*
Copyright 2015 Nodetemple <hostmaster@nodetemple.com>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package command

import (
	"github.com/codegangsta/cli"
	"github.com/nodetemple/nodetemple/nodectl/util"
)

func DemoCmd() cli.Command {
	return cli.Command{
		Name:  "demo",
		Usage: "A simple `hello world` demo",
		Description: "A simple `hello world` demo with output of flags, args, etc.",
		/*Flags: []cli.Flag{
			cli.StringFlag{Name: "demo-flag, d", Value: "", Usage: "Demo flag usage"},
			cli.BoolFlag{Name: "demo-bool", Usage: "Demo bool usage"},
		},
		Action: demoCmdFunc,*/
		Subcommands: []cli.Command{
			{
				Name:  "add",
				Usage: "Add a new template",
				Action: func(c *cli.Context) {
					Action: demoCmdFunc
				},
			},
			{
				Name:  "remove",
				Usage: "Remove an existing template",
				Action: func(c *cli.Context) {
					Action: demoCmdFunc
				},
			},
		},
	}
}

func demoCmdFunc(c *cli.Context) {
	/*if c.String("demo-flag") == "" {
		util.Err("missing '--demo-flag'")
	}*/

	util.Out("Result:\n\targs: %v\n\tproviders: %v\n\tdemo-flag: %v\n\tdemo-bool: %v", c.Args().Get(0), c.GlobalStringSlice("providers"), c.String("demo-flag"), c.String("demo-bool"))
}
<commit_msg>Demo fix derp<commit_after>/*
Copyright 2015 Nodetemple <hostmaster@nodetemple.com>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package command

import (
	"github.com/codegangsta/cli"
	"github.com/nodetemple/nodetemple/nodectl/util"
)

func DemoCmd() cli.Command {
	return cli.Command{
		Name:  "demo",
		Usage: "A simple `hello world` demo",
		Description: "A simple `hello world` demo with output of flags, args, etc.",
		/*Flags: []cli.Flag{
			cli.StringFlag{Name: "demo-flag, d", Value: "", Usage: "Demo flag usage"},
			cli.BoolFlag{Name: "demo-bool", Usage: "Demo bool usage"},
		},
		Action: demoCmdFunc,*/
		Subcommands: []cli.Command{
			{
				Name:  "add",
				Usage: "Add a new template",
				Action: demoCmdFunc,
			},
			{
				Name:  "remove",
				Usage: "Remove an existing template",
				Action: demoCmdFunc,
			},
		},
	}
}

func demoCmdFunc(c *cli.Context) {
	/*if c.String("demo-flag") == "" {
		util.Err("missing '--demo-flag'")
	}*/

	util.Out("Result:\n\targs: %v\n\tproviders: %v\n\tdemo-flag: %v\n\tdemo-bool: %v", c.Args().Get(0), c.GlobalStringSlice("providers"), c.String("demo-flag"), c.String("demo-bool"))
}
<|endoftext|>


<commit_before>package nomad

import (
	"fmt"
	"testing"
	"time"

	"github.com/hashicorp/net-rpc-msgpackrpc"
	"github.com/hashicorp/nomad/nomad/mock"
	"github.com/hashicorp/nomad/nomad/structs"
	"github.com/hashicorp/nomad/testutil"
)

func TestInitializeHeartbeatTimers(t *testing.T) {
	s1 := testServer(t, nil)
	defer s1.Shutdown()
	testutil.WaitForLeader(t, s1.RPC)

	node := mock.Node()
	state := s1.fsm.State()
	err := state.UpsertNode(1, node)
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// Reset the heartbeat timers
	err = s1.initializeHeartbeatTimers()
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// Check that we have a timer
	_, ok := s1.heartbeatTimers[node.ID]
	if !ok {
		t.Fatalf("missing heartbeat timer")
	}
}

func TestResetHeartbeatTimer(t *testing.T) {
	s1 := testServer(t, nil)
	defer s1.Shutdown()
	testutil.WaitForLeader(t, s1.RPC)

	// Create a new timer
	ttl, err := s1.resetHeartbeatTimer("test")
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	if ttl < s1.config.MinHeartbeatTTL || ttl > 2*s1.config.MinHeartbeatTTL {
		t.Fatalf("bad: %#v", ttl)
	}

	// Check that we have a timer
	_, ok := s1.heartbeatTimers["test"]
	if !ok {
		t.Fatalf("missing heartbeat timer")
	}
}

func TestResetHeartbeatTimerLocked(t *testing.T) {
	s1 := testServer(t, nil)
	defer s1.Shutdown()
	testutil.WaitForLeader(t, s1.RPC)

	s1.heartbeatTimersLock.Lock()
	s1.resetHeartbeatTimerLocked("foo", 5*time.Millisecond)
	s1.heartbeatTimersLock.Unlock()

	if _, ok := s1.heartbeatTimers["foo"]; !ok {
		t.Fatalf("missing timer")
	}

	time.Sleep(10 * time.Millisecond)

	if _, ok := s1.heartbeatTimers["foo"]; ok {
		t.Fatalf("timer should be gone")
	}
}

func TestResetHeartbeatTimerLocked_Renew(t *testing.T) {
	s1 := testServer(t, nil)
	defer s1.Shutdown()
	testutil.WaitForLeader(t, s1.RPC)

	s1.heartbeatTimersLock.Lock()
	s1.resetHeartbeatTimerLocked("foo", 5*time.Millisecond)
	s1.heartbeatTimersLock.Unlock()

	if _, ok := s1.heartbeatTimers["foo"]; !ok {
		t.Fatalf("missing timer")
	}

	time.Sleep(2 * time.Millisecond)

	// Renew the heartbeat
	s1.heartbeatTimersLock.Lock()
	s1.resetHeartbeatTimerLocked("foo", 5*time.Millisecond)
	s1.heartbeatTimersLock.Unlock()
	renew := time.Now()

	// Watch for invalidation
	for time.Now().Sub(renew) < 20*time.Millisecond {
		s1.heartbeatTimersLock.Lock()
		_, ok := s1.heartbeatTimers["foo"]
		s1.heartbeatTimersLock.Unlock()
		if !ok {
			end := time.Now()
			if diff := end.Sub(renew); diff < 5*time.Millisecond {
				t.Fatalf("early invalidate %v", diff)
			}
			return
		}
		time.Sleep(time.Millisecond)
	}
	t.Fatalf("should have expired")
}

func TestInvalidateHeartbeat(t *testing.T) {
	s1 := testServer(t, nil)
	defer s1.Shutdown()
	testutil.WaitForLeader(t, s1.RPC)

	// Create a node
	node := mock.Node()
	state := s1.fsm.State()
	err := state.UpsertNode(1, node)
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// This should cause a status update
	s1.invalidateHeartbeat(node.ID)

	// Check it is updated
	out, err := state.NodeByID(node.ID)
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	if !out.TerminalStatus() {
		t.Fatalf("should update node: %#v", out)
	}
}

func TestClearHeartbeatTimer(t *testing.T) {
	s1 := testServer(t, nil)
	defer s1.Shutdown()
	testutil.WaitForLeader(t, s1.RPC)

	s1.heartbeatTimersLock.Lock()
	s1.resetHeartbeatTimerLocked("foo", 5*time.Millisecond)
	s1.heartbeatTimersLock.Unlock()

	err := s1.clearHeartbeatTimer("foo")
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	if _, ok := s1.heartbeatTimers["foo"]; ok {
		t.Fatalf("timer should be gone")
	}
}

func TestClearAllHeartbeatTimers(t *testing.T) {
	s1 := testServer(t, nil)
	defer s1.Shutdown()
	testutil.WaitForLeader(t, s1.RPC)

	s1.heartbeatTimersLock.Lock()
	s1.resetHeartbeatTimerLocked("foo", 10*time.Millisecond)
	s1.resetHeartbeatTimerLocked("bar", 10*time.Millisecond)
	s1.resetHeartbeatTimerLocked("baz", 10*time.Millisecond)
	s1.heartbeatTimersLock.Unlock()

	err := s1.clearAllHeartbeatTimers()
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	if len(s1.heartbeatTimers) != 0 {
		t.Fatalf("timers should be gone")
	}
}

func TestServer_HeartbeatTTL_Failover(t *testing.T) {
	s1 := testServer(t, nil)
	defer s1.Shutdown()

	s2 := testServer(t, func(c *Config) {
		c.DevDisableBootstrap = true
	})
	defer s2.Shutdown()

	s3 := testServer(t, func(c *Config) {
		c.DevDisableBootstrap = true
	})
	defer s3.Shutdown()
	servers := []*Server{s1, s2, s3}
	testJoin(t, s1, s2, s3)

	testutil.WaitForResult(func() (bool, error) {
		peers, _ := s1.raftPeers.Peers()
		return len(peers) == 3, nil
	}, func(err error) {
		t.Fatalf("should have 3 peers")
	})

	// Find the leader
	var leader *Server
	for _, s := range servers {
		// Check that s.heartbeatTimers is empty
		if len(s.heartbeatTimers) != 0 {
			t.Fatalf("should have no heartbeatTimers")
		}
		// Find the leader too
		if s.IsLeader() {
			leader = s
		}
	}
	if leader == nil {
		t.Fatalf("Should have a leader")
	}
	codec := rpcClient(t, leader)

	// Create the register request
	node := mock.Node()
	req := &structs.NodeRegisterRequest{
		Node:         node,
		WriteRequest: structs.WriteRequest{Region: "global"},
	}

	// Fetch the response
	var resp structs.GenericResponse
	if err := msgpackrpc.CallWithCodec(codec, "Node.Register", req, &resp); err != nil {
		t.Fatalf("err: %v", err)
	}

	// Check that heartbeatTimers has the heartbeat ID
	if _, ok := leader.heartbeatTimers[node.ID]; !ok {
		t.Fatalf("missing heartbeat timer")
	}

	// Shutdown the leader!
	leader.Shutdown()

	// heartbeatTimers should be cleared on leader shutdown
	if len(leader.heartbeatTimers) != 0 {
		t.Fatalf("heartbeat timers should be empty on the shutdown leader")
	}

	// Find the new leader
	testutil.WaitForResult(func() (bool, error) {
		leader = nil
		for _, s := range servers {
			if s.IsLeader() {
				leader = s
			}
		}
		if leader == nil {
			return false, fmt.Errorf("Should have a new leader")
		}

		// Ensure heartbeat timer is restored
		if _, ok := leader.heartbeatTimers[node.ID]; !ok {
			return false, fmt.Errorf("missing heartbeat timer")
		}

		return true, nil
	}, func(err error) {
		t.Fatalf("err: %s", err)
	})
}
<commit_msg>Loosen timing a bit on travis<commit_after>package nomad

import (
	"fmt"
	"testing"
	"time"

	"github.com/hashicorp/net-rpc-msgpackrpc"
	"github.com/hashicorp/nomad/nomad/mock"
	"github.com/hashicorp/nomad/nomad/structs"
	"github.com/hashicorp/nomad/testutil"
)

func TestInitializeHeartbeatTimers(t *testing.T) {
	s1 := testServer(t, nil)
	defer s1.Shutdown()
	testutil.WaitForLeader(t, s1.RPC)

	node := mock.Node()
	state := s1.fsm.State()
	err := state.UpsertNode(1, node)
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// Reset the heartbeat timers
	err = s1.initializeHeartbeatTimers()
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// Check that we have a timer
	_, ok := s1.heartbeatTimers[node.ID]
	if !ok {
		t.Fatalf("missing heartbeat timer")
	}
}

func TestResetHeartbeatTimer(t *testing.T) {
	s1 := testServer(t, nil)
	defer s1.Shutdown()
	testutil.WaitForLeader(t, s1.RPC)

	// Create a new timer
	ttl, err := s1.resetHeartbeatTimer("test")
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	if ttl < s1.config.MinHeartbeatTTL || ttl > 2*s1.config.MinHeartbeatTTL {
		t.Fatalf("bad: %#v", ttl)
	}

	// Check that we have a timer
	_, ok := s1.heartbeatTimers["test"]
	if !ok {
		t.Fatalf("missing heartbeat timer")
	}
}

func TestResetHeartbeatTimerLocked(t *testing.T) {
	s1 := testServer(t, nil)
	defer s1.Shutdown()
	testutil.WaitForLeader(t, s1.RPC)

	s1.heartbeatTimersLock.Lock()
	s1.resetHeartbeatTimerLocked("foo", 5*time.Millisecond)
	s1.heartbeatTimersLock.Unlock()

	if _, ok := s1.heartbeatTimers["foo"]; !ok {
		t.Fatalf("missing timer")
	}

	time.Sleep(time.Duration(testutil.TestMultiplier()*10) * time.Millisecond)

	if _, ok := s1.heartbeatTimers["foo"]; ok {
		t.Fatalf("timer should be gone")
	}
}

func TestResetHeartbeatTimerLocked_Renew(t *testing.T) {
	s1 := testServer(t, nil)
	defer s1.Shutdown()
	testutil.WaitForLeader(t, s1.RPC)

	s1.heartbeatTimersLock.Lock()
	s1.resetHeartbeatTimerLocked("foo", 5*time.Millisecond)
	s1.heartbeatTimersLock.Unlock()

	if _, ok := s1.heartbeatTimers["foo"]; !ok {
		t.Fatalf("missing timer")
	}

	time.Sleep(2 * time.Millisecond)

	// Renew the heartbeat
	s1.heartbeatTimersLock.Lock()
	s1.resetHeartbeatTimerLocked("foo", 5*time.Millisecond)
	s1.heartbeatTimersLock.Unlock()
	renew := time.Now()

	// Watch for invalidation
	for time.Now().Sub(renew) < time.Duration(testutil.TestMultiplier()*20)*time.Millisecond {
		s1.heartbeatTimersLock.Lock()
		_, ok := s1.heartbeatTimers["foo"]
		s1.heartbeatTimersLock.Unlock()
		if !ok {
			end := time.Now()
			if diff := end.Sub(renew); diff < 5*time.Millisecond {
				t.Fatalf("early invalidate %v", diff)
			}
			return
		}
		time.Sleep(time.Millisecond)
	}
	t.Fatalf("should have expired")
}

func TestInvalidateHeartbeat(t *testing.T) {
	s1 := testServer(t, nil)
	defer s1.Shutdown()
	testutil.WaitForLeader(t, s1.RPC)

	// Create a node
	node := mock.Node()
	state := s1.fsm.State()
	err := state.UpsertNode(1, node)
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// This should cause a status update
	s1.invalidateHeartbeat(node.ID)

	// Check it is updated
	out, err := state.NodeByID(node.ID)
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	if !out.TerminalStatus() {
		t.Fatalf("should update node: %#v", out)
	}
}

func TestClearHeartbeatTimer(t *testing.T) {
	s1 := testServer(t, nil)
	defer s1.Shutdown()
	testutil.WaitForLeader(t, s1.RPC)

	s1.heartbeatTimersLock.Lock()
	s1.resetHeartbeatTimerLocked("foo", 5*time.Millisecond)
	s1.heartbeatTimersLock.Unlock()

	err := s1.clearHeartbeatTimer("foo")
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	if _, ok := s1.heartbeatTimers["foo"]; ok {
		t.Fatalf("timer should be gone")
	}
}

func TestClearAllHeartbeatTimers(t *testing.T) {
	s1 := testServer(t, nil)
	defer s1.Shutdown()
	testutil.WaitForLeader(t, s1.RPC)

	s1.heartbeatTimersLock.Lock()
	s1.resetHeartbeatTimerLocked("foo", 10*time.Millisecond)
	s1.resetHeartbeatTimerLocked("bar", 10*time.Millisecond)
	s1.resetHeartbeatTimerLocked("baz", 10*time.Millisecond)
	s1.heartbeatTimersLock.Unlock()

	err := s1.clearAllHeartbeatTimers()
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	if len(s1.heartbeatTimers) != 0 {
		t.Fatalf("timers should be gone")
	}
}

func TestServer_HeartbeatTTL_Failover(t *testing.T) {
	s1 := testServer(t, nil)
	defer s1.Shutdown()

	s2 := testServer(t, func(c *Config) {
		c.DevDisableBootstrap = true
	})
	defer s2.Shutdown()

	s3 := testServer(t, func(c *Config) {
		c.DevDisableBootstrap = true
	})
	defer s3.Shutdown()
	servers := []*Server{s1, s2, s3}
	testJoin(t, s1, s2, s3)

	testutil.WaitForResult(func() (bool, error) {
		peers, _ := s1.raftPeers.Peers()
		return len(peers) == 3, nil
	}, func(err error) {
		t.Fatalf("should have 3 peers")
	})

	// Find the leader
	var leader *Server
	for _, s := range servers {
		// Check that s.heartbeatTimers is empty
		if len(s.heartbeatTimers) != 0 {
			t.Fatalf("should have no heartbeatTimers")
		}
		// Find the leader too
		if s.IsLeader() {
			leader = s
		}
	}
	if leader == nil {
		t.Fatalf("Should have a leader")
	}
	codec := rpcClient(t, leader)

	// Create the register request
	node := mock.Node()
	req := &structs.NodeRegisterRequest{
		Node:         node,
		WriteRequest: structs.WriteRequest{Region: "global"},
	}

	// Fetch the response
	var resp structs.GenericResponse
	if err := msgpackrpc.CallWithCodec(codec, "Node.Register", req, &resp); err != nil {
		t.Fatalf("err: %v", err)
	}

	// Check that heartbeatTimers has the heartbeat ID
	if _, ok := leader.heartbeatTimers[node.ID]; !ok {
		t.Fatalf("missing heartbeat timer")
	}

	// Shutdown the leader!
	leader.Shutdown()

	// heartbeatTimers should be cleared on leader shutdown
	if len(leader.heartbeatTimers) != 0 {
		t.Fatalf("heartbeat timers should be empty on the shutdown leader")
	}

	// Find the new leader
	testutil.WaitForResult(func() (bool, error) {
		leader = nil
		for _, s := range servers {
			if s.IsLeader() {
				leader = s
			}
		}
		if leader == nil {
			return false, fmt.Errorf("Should have a new leader")
		}

		// Ensure heartbeat timer is restored
		if _, ok := leader.heartbeatTimers[node.ID]; !ok {
			return false, fmt.Errorf("missing heartbeat timer")
		}

		return true, nil
	}, func(err error) {
		t.Fatalf("err: %s", err)
	})
}
<|endoftext|>


<commit_before>// +build functional

package cri_containerd

import (
	"context"
	"strings"
	"testing"
	"time"

	runtime "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
)

const (
	// Go specific timestamp format which needs to be used for parsing the timestamps
	// generated by the `dir` command.
	timestampFormat = "01/02/2006 15:04 PM"
	// These values come from the Dockerfile for the test nanoserver image
	testDirName                     = "testdir"
	testLinkName                    = "fakelink"
	testDirPath                     = "C:\\Users\\Public"
	imageWindowsNanoserverTestImage = "docker.io/mtbar131/hcsshim:nanoserver_test"
)

func Test_PullImageTimestamps(t *testing.T) {
	requireFeatures(t, featureWCOWHypervisor)

	client := newTestRuntimeClient(t)
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// startTimestamp must be saved BEFORE pulling the image
	// We need the timestamp in UTC because container timestamps are in UTC.
	startTimestamp := time.Now().UTC()

	// Remove existing image first
	removeImages(t, []string{imageWindowsNanoserverTestImage})

	// Sleep at least 1 minute before pulling the image. This is because if the pull
	// image operation finishes in the same minute as that of the startTimestamp
	// the time.Before function in go will return false and the test will not catch
	// the bug.
	time.Sleep(1 * time.Minute)

	// Now pull the image
	pullRequiredImages(t, []string{imageWindowsNanoserverTestImage})
	defer removeImages(t, []string{imageWindowsNanoserverTestImage})

	sandboxRequest := &runtime.RunPodSandboxRequest{
		Config: &runtime.PodSandboxConfig{
			Metadata: &runtime.PodSandboxMetadata{
				Name:      t.Name(),
				Uid:       "0",
				Namespace: testNamespace,
			},
		},
		RuntimeHandler: wcowHypervisorRuntimeHandler,
	}

	podID := runPodSandbox(t, client, ctx, sandboxRequest)
	defer removePodSandbox(t, client, ctx, podID)
	defer stopPodSandbox(t, client, ctx, podID)

	command := []string{
		"cmd",
		"/c",
		"ping",
		"-t",
		"127.0.0.1",
	}

	execCommand := []string{
		"cmd",
		"/c",
		"dir",
		testDirPath,
	}

	containerId := createContainerInSandbox(t, client, ctx, podID, t.Name()+"-Container", imageWindowsNanoserverTestImage, command, nil, nil, sandboxRequest.Config)
	defer removeContainer(t, client, ctx, containerId)

	startContainer(t, client, ctx, containerId)
	defer stopContainer(t, client, ctx, containerId)

	output, errorMsg, exitCode := execContainer(t, client, ctx, containerId, execCommand)

	if exitCode != 0 || len(errorMsg) > 0 {
		t.Fatalf("Failed to exec inside container: %s, exitcode: %v\n",
			errorMsg, exitCode)
	}

	lines := strings.Split(output, "\n")
	var fakelinkTimestamp, testdirTimestamp time.Time
	var err error
	for _, line := range lines {
		tokens := strings.Split(line, " ")
		if strings.Contains(line, testLinkName) {
			fakelinkTimestamp, err = time.Parse(timestampFormat, tokens[0]+" "+tokens[2]+" "+tokens[3])
			if err != nil {
				t.Fatalf("Failed to parse timestamp : %s\n", err)
			}
		} else if strings.Contains(line, testDirName) {
			testdirTimestamp, err = time.Parse(timestampFormat, tokens[0]+" "+tokens[2]+" "+tokens[3])
			if err != nil {
				t.Fatalf("Failed to parse timestamp : %s\n", err)
			}
		}
	}

	t.Logf("startTimestamp: %v, testdir last write timestamp: %v, fakelink last write timestamp: %v\n", startTimestamp, testdirTimestamp, fakelinkTimestamp)

	if startTimestamp.Before(testdirTimestamp) || startTimestamp.Before(fakelinkTimestamp) {
		t.Fatalf("Timestamps not in order. startTimestamp should be less than testdirTimestamp and fakelinkTimestamp")
	}
}
<commit_msg>Change image timestamp test to use 19H1 runtime class<commit_after>// +build functional

package cri_containerd

import (
	"context"
	"strings"
	"testing"
	"time"

	runtime "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
)

const (
	// Go specific timestamp format which needs to be used for parsing the timestamps
	// generated by the `dir` command.
	timestampFormat = "01/02/2006 15:04 PM"
	// These values come from the Dockerfile for the test nanoserver image
	testDirName                     = "testdir"
	testLinkName                    = "fakelink"
	testDirPath                     = "C:\\Users\\Public"
	imageWindowsNanoserverTestImage = "docker.io/mtbar131/hcsshim:nanoserver_test"
)

func Test_PullImageTimestamps(t *testing.T) {
	requireFeatures(t, featureWCOWHypervisor)

	client := newTestRuntimeClient(t)
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// startTimestamp must be saved BEFORE pulling the image
	// We need the timestamp in UTC because container timestamps are in UTC.
	startTimestamp := time.Now().UTC()

	// Remove existing image first
	removeImages(t, []string{imageWindowsNanoserverTestImage})

	// Sleep at least 1 minute before pulling the image. This is because if the pull
	// image operation finishes in the same minute as that of the startTimestamp
	// the time.Before function in go will return false and the test will not catch
	// the bug.
	time.Sleep(1 * time.Minute)

	// Now pull the image
	pullRequiredImages(t, []string{imageWindowsNanoserverTestImage})
	defer removeImages(t, []string{imageWindowsNanoserverTestImage})

	sandboxRequest := &runtime.RunPodSandboxRequest{
		Config: &runtime.PodSandboxConfig{
			Metadata: &runtime.PodSandboxMetadata{
				Name:      t.Name(),
				Uid:       "0",
				Namespace: testNamespace,
			},
		},
		RuntimeHandler: wcowHypervisor18362RuntimeHandler,
	}

	podID := runPodSandbox(t, client, ctx, sandboxRequest)
	defer removePodSandbox(t, client, ctx, podID)
	defer stopPodSandbox(t, client, ctx, podID)

	command := []string{
		"cmd",
		"/c",
		"ping",
		"-t",
		"127.0.0.1",
	}

	execCommand := []string{
		"cmd",
		"/c",
		"dir",
		testDirPath,
	}

	containerId := createContainerInSandbox(t, client, ctx, podID, t.Name()+"-Container", imageWindowsNanoserverTestImage, command, nil, nil, sandboxRequest.Config)
	defer removeContainer(t, client, ctx, containerId)

	startContainer(t, client, ctx, containerId)
	defer stopContainer(t, client, ctx, containerId)

	output, errorMsg, exitCode := execContainer(t, client, ctx, containerId, execCommand)

	if exitCode != 0 || len(errorMsg) > 0 {
		t.Fatalf("Failed to exec inside container: %s, exitcode: %v\n",
			errorMsg, exitCode)
	}

	lines := strings.Split(output, "\n")
	var fakelinkTimestamp, testdirTimestamp time.Time
	var err error
	for _, line := range lines {
		tokens := strings.Split(line, " ")
		if strings.Contains(line, testLinkName) {
			fakelinkTimestamp, err = time.Parse(timestampFormat, tokens[0]+" "+tokens[2]+" "+tokens[3])
			if err != nil {
				t.Fatalf("Failed to parse timestamp : %s\n", err)
			}
		} else if strings.Contains(line, testDirName) {
			testdirTimestamp, err = time.Parse(timestampFormat, tokens[0]+" "+tokens[2]+" "+tokens[3])
			if err != nil {
				t.Fatalf("Failed to parse timestamp : %s\n", err)
			}
		}
	}

	t.Logf("startTimestamp: %v, testdir last write timestamp: %v, fakelink last write timestamp: %v\n", startTimestamp, testdirTimestamp, fakelinkTimestamp)

	if startTimestamp.Before(testdirTimestamp) || startTimestamp.Before(fakelinkTimestamp) {
		t.Fatalf("Timestamps not in order. startTimestamp should be less than testdirTimestamp and fakelinkTimestamp")
	}
}
<|endoftext|>


<commit_before>package hwaflib

func (ctx *Context) Version() string {
	version := "20130820"
	return version
}

func (ctx *Context) Revision() string {
	revision := "ed359ef"
	return revision
}

// EOF

<commit_msg>version: 20130820<commit_after>package hwaflib

func (ctx *Context) Version() string {
	version := "20130820"
	return version
}

func (ctx *Context) Revision() string {
	revision := "4cdee1b"
	return revision
}

// EOF

<|endoftext|>


<commit_before>package database

import (
	"database/sql"

	"github.com/karousel/core/album"
	"github.com/karousel/core/collection"
	"github.com/karousel/core/file"
	"github.com/karousel/core/metadata"
	"github.com/karousel/core/photo"

	_ "github.com/lib/pq"
	gorp "gopkg.in/gorp.v1"
)

func NewDatabase(datasource string) (*gorp.DbMap, error) {
	var databaseMap *gorp.DbMap

	database, err := sql.Open("postgres", datasource)

	if err != nil {
		return databaseMap, err
	}

	databaseMap = &gorp.DbMap{Db: database, Dialect: gorp.PostgresDialect{}}

	collectionsTable := databaseMap.AddTableWithName(collection.Collection{}, "collections")
	collectionsTable.SetKeys(true, "id")
	collectionsTable.ColMap("name").SetNotNull(true)
	collectionsTable.ColMap("name").SetUnique(true)

	albumsTable := databaseMap.AddTableWithName(album.Album{}, "albums")
	albumsTable.SetKeys(true, "id")
	albumsTable.ColMap("collection_id").SetNotNull(true)
	albumsTable.ColMap("name").SetNotNull(true)
	albumsTable.ColMap("name").SetUnique(true)

	photosTable := databaseMap.AddTableWithName(photo.Photo{}, "photos")
	photosTable.SetKeys(true, "id")
	photosTable.ColMap("album_id").SetNotNull(true)
	photosTable.ColMap("name").SetNotNull(true)
	photosTable.ColMap("uploaded").SetNotNull(true)
	photosTable.ColMap("status").SetNotNull(true)

	filesTable := databaseMap.AddTableWithName(file.File{}, "files")
	filesTable.SetKeys(true, "id")
	filesTable.ColMap("parent_id").SetNotNull(true)
	filesTable.ColMap("checksum").SetNotNull(true)

	metadataTable := databaseMap.AddTableWithName(metadata.Metadata{}, "metadata")
	metadataTable.SetKeys(true, "id")
	metadataTable.ColMap("parent_id").SetNotNull(true)
	metadataTable.ColMap("raw").SetNotNull(true)

	err = databaseMap.CreateTablesIfNotExists()

	if err != nil {
		return databaseMap, err
	}

	return databaseMap, nil
}
<commit_msg>Added creation of the users table to the database package<commit_after>package database

import (
	"database/sql"

	"github.com/karousel/core/album"
	"github.com/karousel/core/collection"
	"github.com/karousel/core/file"
	"github.com/karousel/core/metadata"
	"github.com/karousel/core/photo"
	"github.com/karousel/core/user"

	_ "github.com/lib/pq"
	gorp "gopkg.in/gorp.v1"
)

func NewDatabase(datasource string) (*gorp.DbMap, error) {
	var databaseMap *gorp.DbMap

	database, err := sql.Open("postgres", datasource)

	if err != nil {
		return databaseMap, err
	}

	databaseMap = &gorp.DbMap{Db: database, Dialect: gorp.PostgresDialect{}}

	collectionsTable := databaseMap.AddTableWithName(collection.Collection{}, "collections")
	collectionsTable.SetKeys(true, "id")
	collectionsTable.ColMap("name").SetNotNull(true)
	collectionsTable.ColMap("name").SetUnique(true)

	albumsTable := databaseMap.AddTableWithName(album.Album{}, "albums")
	albumsTable.SetKeys(true, "id")
	albumsTable.ColMap("collection_id").SetNotNull(true)
	albumsTable.ColMap("name").SetNotNull(true)
	albumsTable.ColMap("name").SetUnique(true)

	photosTable := databaseMap.AddTableWithName(photo.Photo{}, "photos")
	photosTable.SetKeys(true, "id")
	photosTable.ColMap("album_id").SetNotNull(true)
	photosTable.ColMap("name").SetNotNull(true)
	photosTable.ColMap("uploaded").SetNotNull(true)
	photosTable.ColMap("status").SetNotNull(true)

	filesTable := databaseMap.AddTableWithName(file.File{}, "files")
	filesTable.SetKeys(true, "id")
	filesTable.ColMap("parent_id").SetNotNull(true)
	filesTable.ColMap("checksum").SetNotNull(true)

	metadataTable := databaseMap.AddTableWithName(metadata.Metadata{}, "metadata")
	metadataTable.SetKeys(true, "id")
	metadataTable.ColMap("parent_id").SetNotNull(true)
	metadataTable.ColMap("raw").SetNotNull(true)

	usersTable := databaseMap.AddTableWithName(user.User{}, "users")
	usersTable.SetKeys(true, "id")
	usersTable.ColMap("email_address").SetNotNull(true)
	usersTable.ColMap("email_address").SetUnique(true)
	usersTable.ColMap("password").SetNotNull(true)
	usersTable.ColMap("name").SetNotNull(true)
	usersTable.ColMap("is_administrator").SetNotNull(true)

	err = databaseMap.CreateTablesIfNotExists()

	if err != nil {
		return databaseMap, err
	}

	return databaseMap, nil
}
<|endoftext|>


<commit_before>package databases

import (
	"log"
	"strings"

	"labix.org/v2/mgo"
	"labix.org/v2/mgo/bson"
)

type MongoDB struct {
	Collection *mgo.Collection
	Session    *mgo.Session
}

func (mongo *MongoDB) Init(config Config) {
	pool := strings.Join(config.Addresses, ",")
	var err error
	mongo.Session, err = mgo.Dial(pool)
	if err != nil {
		log.Fatal(err)
	}
	mongo.Session.SetMode(mgo.Monotonic, true)
	mongo.Collection = mongo.Session.DB(config.Name).C(config.Table)
}

func (mongo *MongoDB) Shutdown() {
	mongo.Session.Close()
}

func (mongo *MongoDB) Create(key string, value map[string]interface{}) error {
	value["_id"] = key
	err := mongo.Collection.Insert(bson.M(value))
	return err
}

func (mongo *MongoDB) Read(key string) error {
	result := map[string]interface{}{}
	err := mongo.Collection.FindId(key).One(&result)
	return err
}

func (mongo *MongoDB) Update(key string, value map[string]interface{}) error {
	err := mongo.Collection.Update(bson.M{"_id": key}, bson.M(value))
	return err
}

func (mongo *MongoDB) Delete(key string) error {
	err := mongo.Collection.Remove(bson.M{"_id": key})
	return err
}

func (mongo *MongoDB) Query(key string, args []interface{}) error {
	view := args[0].(string)

	var q, s bson.M
	switch view {
	case "name_and_street_by_city":
		q = bson.M{
			"city.f.f": args[1],
		}
		s = bson.M{
			"name.f.f.f": 1,
			"street.f.f": 1,
		}
	case "name_and_email_by_county":
		q = bson.M{
			"county.f.f": args[1],
		}
		s = bson.M{
			"name.f.f.f": 1,
			"email.f.f":  1,
		}
	case "achievements_by_realm":
		q = bson.M{
			"realm.f": args[1],
		}
		s = bson.M{
			"achievements": 1,
		}
	case "name_by_coins":
		q = bson.M{
			"coins.f": bson.M{
				"$gt": args[1].(float64) * 0.5,
				"$lt": args[1].(float64),
			},
		}
		s = bson.M{
			"name.f.f.f": 1,
		}
	case "email_by_achievement_and_category":
		q = bson.M{
			"category": args[2].(int16),
			"achievements.0": bson.M{
				"$gt": 0,
				"$lt": args[1].([]int16)[0],
			},
		}
		s = bson.M{
			"email.f.f": 1,
		}
	case "street_by_year_and_coins":
		q = bson.M{
			"year": args[1],
			"coins.f": bson.M{
				"$gt": args[2].(float64),
				"$lt": 655.35,
			},
		}
		s = bson.M{
			"street.f.f": 1,
		}
	case "name_and_email_and_street_and_achievements_and_coins_by_city":
		q = bson.M{
			"city.f.f": args[1],
		}
		s = bson.M{
			"name.f.f.f":   1,
			"email.f.f":    1,
			"street.f.f":   1,
			"achievements": 1,
			"coins.f":      1,
		}
	case "street_and_name_and_email_and_achievement_and_coins_by_county":
		q = bson.M{
			"county.f.f": args[1],
		}
		s = bson.M{
			"street.f.f":   1,
			"name.f.f.f":   1,
			"email.f.f":    1,
			"achievements": bson.M{"$slice": 1},
			"coins.f":      1,
		}
	case "category_name_and_email_and_street_and_gmtime_and_year_by_country":
		q = bson.M{
			"country.f": args[1],
		}
		s = bson.M{
			"category":   1,
			"name.f.f.f": 1,
			"email.f.f":  1,
			"street.f.f": 1,
			"gmtime":     1,
			"year":       1,
		}
	case "body_by_city":
		q = bson.M{
			"city.f.f": args[1],
		}
		s = bson.M{
			"body": 1,
		}
	case "body_by_realm":
		q = bson.M{
			"realm.f": args[1],
		}
		s = bson.M{
			"body": 1,
		}
	case "body_by_country":
		q = bson.M{
			"country.f": args[1],
		}
		s = bson.M{
			"body": 1,
		}
	}

	result := []map[string]interface{}{}
	err := mongo.Collection.Find(q).Select(s).Limit(20).All(&result)
	return err
}
<commit_msg>set minimal range for achievements<commit_after>package databases

import (
	"log"
	"strings"

	"labix.org/v2/mgo"
	"labix.org/v2/mgo/bson"
)

type MongoDB struct {
	Collection *mgo.Collection
	Session    *mgo.Session
}

func (mongo *MongoDB) Init(config Config) {
	pool := strings.Join(config.Addresses, ",")
	var err error
	mongo.Session, err = mgo.Dial(pool)
	if err != nil {
		log.Fatal(err)
	}
	mongo.Session.SetMode(mgo.Monotonic, true)
	mongo.Collection = mongo.Session.DB(config.Name).C(config.Table)
}

func (mongo *MongoDB) Shutdown() {
	mongo.Session.Close()
}

func (mongo *MongoDB) Create(key string, value map[string]interface{}) error {
	value["_id"] = key
	err := mongo.Collection.Insert(bson.M(value))
	return err
}

func (mongo *MongoDB) Read(key string) error {
	result := map[string]interface{}{}
	err := mongo.Collection.FindId(key).One(&result)
	return err
}

func (mongo *MongoDB) Update(key string, value map[string]interface{}) error {
	err := mongo.Collection.Update(bson.M{"_id": key}, bson.M(value))
	return err
}

func (mongo *MongoDB) Delete(key string) error {
	err := mongo.Collection.Remove(bson.M{"_id": key})
	return err
}

func (mongo *MongoDB) Query(key string, args []interface{}) error {
	view := args[0].(string)

	var q, s bson.M
	switch view {
	case "name_and_street_by_city":
		q = bson.M{
			"city.f.f": args[1],
		}
		s = bson.M{
			"name.f.f.f": 1,
			"street.f.f": 1,
		}
	case "name_and_email_by_county":
		q = bson.M{
			"county.f.f": args[1],
		}
		s = bson.M{
			"name.f.f.f": 1,
			"email.f.f":  1,
		}
	case "achievements_by_realm":
		q = bson.M{
			"realm.f": args[1],
		}
		s = bson.M{
			"achievements": 1,
		}
	case "name_by_coins":
		q = bson.M{
			"coins.f": bson.M{
				"$gt": args[1].(float64) * 0.5,
				"$lt": args[1].(float64),
			},
		}
		s = bson.M{
			"name.f.f.f": 1,
		}
	case "email_by_achievement_and_category":
		q = bson.M{
			"category": args[2].(int16),
			"achievements.0": bson.M{
				"$gt": 0,
				"$lt": args[1].([]int16)[0] + 2,
			},
		}
		s = bson.M{
			"email.f.f": 1,
		}
	case "street_by_year_and_coins":
		q = bson.M{
			"year": args[1],
			"coins.f": bson.M{
				"$gt": args[2].(float64),
				"$lt": 655.35,
			},
		}
		s = bson.M{
			"street.f.f": 1,
		}
	case "name_and_email_and_street_and_achievements_and_coins_by_city":
		q = bson.M{
			"city.f.f": args[1],
		}
		s = bson.M{
			"name.f.f.f":   1,
			"email.f.f":    1,
			"street.f.f":   1,
			"achievements": 1,
			"coins.f":      1,
		}
	case "street_and_name_and_email_and_achievement_and_coins_by_county":
		q = bson.M{
			"county.f.f": args[1],
		}
		s = bson.M{
			"street.f.f":   1,
			"name.f.f.f":   1,
			"email.f.f":    1,
			"achievements": bson.M{"$slice": 1},
			"coins.f":      1,
		}
	case "category_name_and_email_and_street_and_gmtime_and_year_by_country":
		q = bson.M{
			"country.f": args[1],
		}
		s = bson.M{
			"category":   1,
			"name.f.f.f": 1,
			"email.f.f":  1,
			"street.f.f": 1,
			"gmtime":     1,
			"year":       1,
		}
	case "body_by_city":
		q = bson.M{
			"city.f.f": args[1],
		}
		s = bson.M{
			"body": 1,
		}
	case "body_by_realm":
		q = bson.M{
			"realm.f": args[1],
		}
		s = bson.M{
			"body": 1,
		}
	case "body_by_country":
		q = bson.M{
			"country.f": args[1],
		}
		s = bson.M{
			"body": 1,
		}
	}

	result := []map[string]interface{}{}
	err := mongo.Collection.Find(q).Select(s).Limit(20).All(&result)
	return err
}
<|endoftext|>