<commit_before>package docker

import (
	"bytes"
	"fmt"
	"github.com/dotcloud/docker/devmapper"
	"github.com/dotcloud/docker/utils"
	"io"
	"io/ioutil"
	"log"
	"net"
	"os"
	"path/filepath"
	"runtime"
	"strconv"
	"strings"
	"sync"
	"syscall"
	"testing"
	"time"
)

const (
	unitTestImageName        = "docker-test-image"
	unitTestImageID          = "83599e29c455eb719f77d799bc7c51521b9551972f5a850d7ad265bc1b5292f6" // 1.0
	unitTestNetworkBridge    = "testdockbr0"
	unitTestStoreBase        = "/var/lib/docker/unit-tests"
	unitTestStoreDevicesBase = "/var/lib/docker/unit-tests-devices"
	testDaemonAddr           = "127.0.0.1:4270"
	testDaemonProto          = "tcp"
)

var (
	globalRuntime   *Runtime
	startFds        int
	startGoroutines int
)

func nuke(runtime *Runtime) error {
	var wg sync.WaitGroup
	for _, container := range runtime.List() {
		wg.Add(1)
		go func(c *Container) {
			c.Kill()
			wg.Done()
		}(container)
	}
	wg.Wait()
	runtime.networkManager.Close()

	for _, container := range runtime.List() {
		container.EnsureUnmounted()
	}
	return os.RemoveAll(runtime.root)
}

func cleanup(runtime *Runtime) error {
	for _, container := range runtime.List() {
		container.Kill()
		runtime.Destroy(container)
	}
	images, err := runtime.graph.Map()
	if err != nil {
		return err
	}
	for _, image := range images {
		if image.ID != unitTestImageID {
			runtime.DeleteImage(image.ID)
		}
	}
	return nil
}

func cleanupLast(runtime *Runtime) error {
	cleanup(runtime)
	runtime.deviceSet.Shutdown()
	return nil
}

func layerArchive(tarfile string) (io.Reader, error) {
	// FIXME: need to close f somewhere
	f, err := os.Open(tarfile)
	if err != nil {
		return nil, err
	}
	return f, nil
}

// Remove any leftover device mapper devices from earlier runs of the unit tests
func removeDev(name string) {
	path := filepath.Join("/dev/mapper", name)
	fd, err := syscall.Open(path, syscall.O_RDONLY, 07777)
	if err != nil {
		if err == syscall.ENXIO {
			// No device for this node, just remove it
			os.Remove(path)
			return
		}
	} else {
		syscall.Close(fd)
	}
	if err := devmapper.RemoveDevice(name); err != nil {
		panic(fmt.Errorf("Unable to remove existing device %s: %s", name, err))
	}
}

func cleanupDevMapper() {
	infos, _ := ioutil.ReadDir("/dev/mapper")
	if infos != nil {
		hasPool := false
		for _, info := range infos {
			name := info.Name()
			if strings.HasPrefix(name, "docker-unit-tests-devices-") {
				if name == "docker-unit-tests-devices-pool" {
					hasPool = true
				} else {
					removeDev(name)
				}
			}
			// We need to remove the pool last as the other devices block it
			if hasPool {
				removeDev("docker-unit-tests-devices-pool")
			}
		}
	}
}

func init() {
	os.Setenv("TEST", "1")
<commit_msg>Clean up better from previous unit-test runs<commit_after>package docker

import (
	"bytes"
	"fmt"
	"github.com/dotcloud/docker/devmapper"
	"github.com/dotcloud/docker/utils"
	"io"
	"io/ioutil"
	"log"
	"net"
	"os"
	"path/filepath"
	"runtime"
	"strconv"
	"strings"
	"sync"
	"syscall"
	"testing"
	"time"
)

const (
	unitTestImageName        = "docker-test-image"
	unitTestImageID          = "83599e29c455eb719f77d799bc7c51521b9551972f5a850d7ad265bc1b5292f6" // 1.0
	unitTestNetworkBridge    = "testdockbr0"
	unitTestStoreBase        = "/var/lib/docker/unit-tests"
	unitTestStoreDevicesBase = "/var/lib/docker/unit-tests-devices"
	testDaemonAddr           = "127.0.0.1:4270"
	testDaemonProto          = "tcp"
)

var (
	globalRuntime   *Runtime
	startFds        int
	startGoroutines int
)

func nuke(runtime *Runtime) error {
	var wg sync.WaitGroup
	for _, container := range runtime.List() {
		wg.Add(1)
		go func(c *Container) {
			c.Kill()
			wg.Done()
		}(container)
	}
	wg.Wait()
	runtime.networkManager.Close()

	for _, container := range runtime.List() {
		container.EnsureUnmounted()
	}
	return os.RemoveAll(runtime.root)
}

func cleanup(runtime *Runtime) error {
	for _, container := range runtime.List() {
		container.Kill()
		runtime.Destroy(container)
	}
	images, err := runtime.graph.Map()
	if err != nil {
		return err
	}
	for _, image := range images {
		if image.ID != unitTestImageID {
			runtime.DeleteImage(image.ID)
		}
	}
	return nil
}

func cleanupLast(runtime *Runtime) error {
	cleanup(runtime)
	runtime.deviceSet.Shutdown()
	return nil
}

func layerArchive(tarfile string) (io.Reader, error) {
	// FIXME: need to close f somewhere
	f, err := os.Open(tarfile)
	if err != nil {
		return nil, err
	}
	return f, nil
}

// Remove any leftover device mapper devices from earlier runs of the unit tests
func removeDev(name string) {
	path := filepath.Join("/dev/mapper", name)
	fd, err := syscall.Open(path, syscall.O_RDONLY, 07777)
	if err != nil {
		if err == syscall.ENXIO {
			// No device for this node, just remove it
			os.Remove(path)
			return
		}
	} else {
		syscall.Close(fd)
	}
	if err := devmapper.RemoveDevice(name); err != nil {
		log.Fatalf("Unable to remove device %s needed to get a freash unit test environment", name)
	}
}

func cleanupDevMapper() {
	// Unmount any leftover mounts from previous unit test runs
	if data, err := ioutil.ReadFile("/proc/mounts"); err == nil {
		for _, line := range strings.Split(string(data), "\n") {
			cols := strings.Split(line, " ")
			if len(cols) >= 2 && strings.HasPrefix(cols[0], "/dev/mapper/docker-unit-tests-devices") {
				err = syscall.Unmount(cols[1], 0)
				if err != nil {
					log.Fatalf("Unable to unmount %s needed to get a freash unit test environment: %s", cols[1], err)
				}
			}
		}
	}

	// Remove any leftover devmapper devices from previous unit run tests
	infos, _ := ioutil.ReadDir("/dev/mapper")
	if infos != nil {
		hasPool := false
		for _, info := range infos {
			name := info.Name()
			if strings.HasPrefix(name, "docker-unit-tests-devices-") {
				if name == "docker-unit-tests-devices-pool" {
					hasPool = true
				} else {
					removeDev(name)
				}
			}
			// We need to remove the pool last as the other devices block it
			if hasPool {
				removeDev("docker-unit-tests-devices-pool")
			}
		}
	}
}

func init() {
	os.Setenv("TEST", "1")

{'fraction_non_alphanumeric': 0.1359670781893004, 'fraction_numerical': 0.022222222222222223, 'mean_word_length': 3.2849083215796897, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '456798', 'n_tokens_mistral': 2488, 'n_tokens_neox': 2217, 'n_words': 529}