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