2015-01-13 00:26:49 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-01-31 22:28:11 +03:00
|
|
|
"net/http"
|
2015-01-13 00:26:49 +03:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2015-04-18 19:46:47 +03:00
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2015-01-13 00:26:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const v2binary = "registry-v2"
|
|
|
|
|
|
|
|
type testRegistryV2 struct {
|
|
|
|
cmd *exec.Cmd
|
|
|
|
dir string
|
|
|
|
}
|
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func newTestRegistryV2(c *check.C) (*testRegistryV2, error) {
|
2015-01-13 00:26:49 +03:00
|
|
|
template := `version: 0.1
|
|
|
|
loglevel: debug
|
|
|
|
storage:
|
|
|
|
filesystem:
|
|
|
|
rootdirectory: %s
|
|
|
|
http:
|
2015-01-13 21:46:32 +03:00
|
|
|
addr: %s`
|
2015-01-13 00:26:49 +03:00
|
|
|
tmp, err := ioutil.TempDir("", "registry-test-")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
confPath := filepath.Join(tmp, "config.yaml")
|
|
|
|
config, err := os.Create(confPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-13 21:46:32 +03:00
|
|
|
if _, err := fmt.Fprintf(config, template, tmp, privateRegistryURL); err != nil {
|
2015-01-13 00:26:49 +03:00
|
|
|
os.RemoveAll(tmp)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(v2binary, confPath)
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
os.RemoveAll(tmp)
|
|
|
|
if os.IsNotExist(err) {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Skip(err.Error())
|
2015-01-13 00:26:49 +03:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &testRegistryV2{
|
|
|
|
cmd: cmd,
|
|
|
|
dir: tmp,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-01-31 22:28:11 +03:00
|
|
|
func (t *testRegistryV2) Ping() error {
|
|
|
|
// We always ping through HTTP for our test registry.
|
|
|
|
resp, err := http.Get(fmt.Sprintf("http://%s/v2/", privateRegistryURL))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
2015-02-03 01:53:20 +03:00
|
|
|
return fmt.Errorf("registry ping replied with an unexpected status code %d", resp.StatusCode)
|
2015-01-31 22:28:11 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-13 00:26:49 +03:00
|
|
|
func (r *testRegistryV2) Close() {
|
|
|
|
r.cmd.Process.Kill()
|
|
|
|
os.RemoveAll(r.dir)
|
|
|
|
}
|