github.com/grubernaut/docker@v1.6.0-rc2/integration-cli/registry.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"os"
     8  	"os/exec"
     9  	"path/filepath"
    10  	"testing"
    11  )
    12  
    13  const v2binary = "registry-v2"
    14  
    15  type testRegistryV2 struct {
    16  	cmd *exec.Cmd
    17  	dir string
    18  }
    19  
    20  func newTestRegistryV2(t *testing.T) (*testRegistryV2, error) {
    21  	template := `version: 0.1
    22  loglevel: debug
    23  storage:
    24      filesystem:
    25          rootdirectory: %s
    26  http:
    27      addr: %s`
    28  	tmp, err := ioutil.TempDir("", "registry-test-")
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	confPath := filepath.Join(tmp, "config.yaml")
    33  	config, err := os.Create(confPath)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	if _, err := fmt.Fprintf(config, template, tmp, privateRegistryURL); err != nil {
    38  		os.RemoveAll(tmp)
    39  		return nil, err
    40  	}
    41  
    42  	cmd := exec.Command(v2binary, confPath)
    43  	if err := cmd.Start(); err != nil {
    44  		os.RemoveAll(tmp)
    45  		if os.IsNotExist(err) {
    46  			t.Skip()
    47  		}
    48  		return nil, err
    49  	}
    50  	return &testRegistryV2{
    51  		cmd: cmd,
    52  		dir: tmp,
    53  	}, nil
    54  }
    55  
    56  func (t *testRegistryV2) Ping() error {
    57  	// We always ping through HTTP for our test registry.
    58  	resp, err := http.Get(fmt.Sprintf("http://%s/v2/", privateRegistryURL))
    59  	if err != nil {
    60  		return err
    61  	}
    62  	if resp.StatusCode != 200 {
    63  		return fmt.Errorf("registry ping replied with an unexpected status code %d", resp.StatusCode)
    64  	}
    65  	return nil
    66  }
    67  
    68  func (r *testRegistryV2) Close() {
    69  	r.cmd.Process.Kill()
    70  	os.RemoveAll(r.dir)
    71  }