github.com/pritambaral/docker@v1.4.2-0.20150120174542-b2fe1b3dd952/integration-cli/registry.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  	"testing"
    10  )
    11  
    12  const v2binary = "registry-v2"
    13  
    14  type testRegistryV2 struct {
    15  	cmd *exec.Cmd
    16  	dir string
    17  }
    18  
    19  func newTestRegistryV2(t *testing.T) (*testRegistryV2, error) {
    20  	template := `version: 0.1
    21  loglevel: debug
    22  storage:
    23      filesystem:
    24          rootdirectory: %s
    25  http:
    26      addr: %s`
    27  	tmp, err := ioutil.TempDir("", "registry-test-")
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	confPath := filepath.Join(tmp, "config.yaml")
    32  	config, err := os.Create(confPath)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	if _, err := fmt.Fprintf(config, template, tmp, privateRegistryURL); err != nil {
    37  		os.RemoveAll(tmp)
    38  		return nil, err
    39  	}
    40  
    41  	cmd := exec.Command(v2binary, confPath)
    42  	if err := cmd.Start(); err != nil {
    43  		os.RemoveAll(tmp)
    44  		if os.IsNotExist(err) {
    45  			t.Skip()
    46  		}
    47  		return nil, err
    48  	}
    49  	return &testRegistryV2{
    50  		cmd: cmd,
    51  		dir: tmp,
    52  	}, nil
    53  }
    54  
    55  func (r *testRegistryV2) Close() {
    56  	r.cmd.Process.Kill()
    57  	os.RemoveAll(r.dir)
    58  }