<commit_before>package docker

import (
	"os"
	"testing"
)

func TestPull(t* testing.T) {
	runtime, err := newTestRuntime()
	if err != nil {
		t.Fatal(err)
	}
	defer nuke(runtime)

	err = runtime.graph.PullRepository(os.Stdout, "busybox", "", runtime.repositories, nil)
	if err != nil {
		t.Fatal(err)
	}
	img, err := runtime.repositories.LookupImage("busybox")
	if err != nil {
		t.Fatal(err)
	}

	// Try to run something on this image to make sure the layer's been downloaded properly.
	config, err := ParseRun([]string{img.Id, "echo", "Hello World"}, os.Stdout, runtime.capabilities)
	if err != nil {
		t.Fatal(err)
	}

	b := NewBuilder(runtime)
	container, err := b.Create(config)
	if err != nil {
		t.Fatal(err)
	}
	if err := container.Start(); err != nil {
		t.Fatal(err)
	}

	if status := container.Wait(); status != 0 {
		t.Fatalf("Expected status code 0, found %d instead", status)
	}
}

func TestPullTag(t* testing.T) {
	runtime, err := newTestRuntime()
	if err != nil {
		t.Fatal(err)
	}
	defer nuke(runtime)

	err = runtime.graph.PullRepository(os.Stdout, "ubuntu", "12.04", runtime.repositories, nil)
	if err != nil {
		t.Fatal(err)
	}
	_, err = runtime.repositories.LookupImage("ubuntu:12.04")
	if err != nil {
		t.Fatal(err)
	}

	img2, err := runtime.repositories.LookupImage("ubuntu:12.10")
	if img2 != nil {
		t.Fatalf("Expected nil image but found %v instead", img2.Id)
	}
}<commit_msg>Added test for PushRepository<commit_after>package docker

import (
	"crypto/rand"
	"encoding/hex"
	"github.com/dotcloud/docker/auth"
	"os"
	"path"
	"testing"
)

func TestPull(t* testing.T) {
	runtime, err := newTestRuntime()
	if err != nil {
		t.Fatal(err)
	}
	defer nuke(runtime)

	err = runtime.graph.PullRepository(os.Stdout, "busybox", "", runtime.repositories, nil)
	if err != nil {
		t.Fatal(err)
	}
	img, err := runtime.repositories.LookupImage("busybox")
	if err != nil {
		t.Fatal(err)
	}

	// Try to run something on this image to make sure the layer's been downloaded properly.
	config, err := ParseRun([]string{img.Id, "echo", "Hello World"}, os.Stdout, runtime.capabilities)
	if err != nil {
		t.Fatal(err)
	}

	b := NewBuilder(runtime)
	container, err := b.Create(config)
	if err != nil {
		t.Fatal(err)
	}
	if err := container.Start(); err != nil {
		t.Fatal(err)
	}

	if status := container.Wait(); status != 0 {
		t.Fatalf("Expected status code 0, found %d instead", status)
	}
}

func TestPullTag(t* testing.T) {
	runtime, err := newTestRuntime()
	if err != nil {
		t.Fatal(err)
	}
	defer nuke(runtime)

	err = runtime.graph.PullRepository(os.Stdout, "ubuntu", "12.04", runtime.repositories, nil)
	if err != nil {
		t.Fatal(err)
	}
	_, err = runtime.repositories.LookupImage("ubuntu:12.04")
	if err != nil {
		t.Fatal(err)
	}

	img2, err := runtime.repositories.LookupImage("ubuntu:12.10")
	if img2 != nil {
		t.Fatalf("Expected nil image but found %v instead", img2.Id)
	}
}

func login(runtime *Runtime) error {
	authConfig := auth.NewAuthConfig("unittester", "surlautrerivejetattendrai", "<EMAIL>", runtime.root)
	runtime.authConfig = authConfig
	_, err := auth.Login(authConfig)
	return err
}

func TestPush(t* testing.T) {
	os.Setenv("DOCKER_INDEX_URL", "https://indexstaging-docker.dotcloud.com")
	runtime, err := newTestRuntime()
	if err != nil {
		t.Fatal(err)
	}
	defer nuke(runtime)

	err = login(runtime)
	if err != nil {
		t.Fatal(err)
	}

	err = runtime.graph.PullRepository(os.Stdout, "joffrey/busybox", "", runtime.repositories, nil)
	if err != nil {
		t.Fatal(err)
	}
	tokenBuffer := make([]byte, 16)
	_, err = rand.Read(tokenBuffer)
	if err != nil {
		t.Fatal(err)
	}
	token := hex.EncodeToString(tokenBuffer)
	config, err := ParseRun([]string{"joffrey/busybox", "touch", "/" + token}, os.Stdout, runtime.capabilities)
	if err != nil {
		t.Fatal(err)
	}

	b := NewBuilder(runtime)
	container, err := b.Create(config)
	if err != nil {
		t.Fatal(err)
	}
	if err := container.Start(); err != nil {
		t.Fatal(err)
	}

	if status := container.Wait(); status != 0 {
		t.Fatalf("Expected status code 0, found %d instead", status)
	}

	img, err := b.Commit(container, "unittester/" + token, "", "", "", nil)
	if err != nil {
		t.Fatal(err)
	}

	repo := runtime.repositories.Repositories["unittester/"+token]
	err = runtime.graph.PushRepository(os.Stdout, "unittester/" + token, repo, runtime.authConfig)
	if err != nil {
		t.Fatal(err)
	}

	// Remove image so we can pull it again
	if err := runtime.graph.Delete(img.Id); err != nil {
		t.Fatal(err)
	}

	err = runtime.graph.PullRepository(os.Stdout, "unittester/" + token, "", runtime.repositories, runtime.authConfig)
	if err != nil {
		t.Fatal(err)
	}

	layerPath, err := img.layer()
	if err != nil {
		t.Fatal(err)
	}

	if _, err := os.Stat(path.Join(layerPath, token)); err != nil {
		t.Fatalf("Error while trying to retrieve token file: %v", err)
	}
}
{'fraction_non_alphanumeric': 0.1671833023351932, 'fraction_numerical': 0.007852862161603637, 'mean_word_length': 3.864321608040201, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '61225', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1790, 'n_words': 429}