2015-01-12 16:26:49 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-01-31 14:28:11 -05:00
|
|
|
"net/http"
|
2015-01-12 16:26:49 -05:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2015-01-12 16:26:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const v2binary = "registry-v2"
|
|
|
|
|
|
|
|
type testRegistryV2 struct {
|
|
|
|
cmd *exec.Cmd
|
|
|
|
dir string
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func newTestRegistryV2(c *check.C) (*testRegistryV2, error) {
|
2015-01-12 16:26:49 -05:00
|
|
|
template := `version: 0.1
|
|
|
|
loglevel: debug
|
|
|
|
storage:
|
|
|
|
filesystem:
|
|
|
|
rootdirectory: %s
|
|
|
|
http:
|
2015-01-13 13:46:32 -05:00
|
|
|
addr: %s`
|
2015-01-12 16:26:49 -05: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 13:46:32 -05:00
|
|
|
if _, err := fmt.Fprintf(config, template, tmp, privateRegistryURL); err != nil {
|
2015-01-12 16:26:49 -05: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 12:46:47 -04:00
|
|
|
c.Skip(err.Error())
|
2015-01-12 16:26:49 -05:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &testRegistryV2{
|
|
|
|
cmd: cmd,
|
|
|
|
dir: tmp,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-01-31 14:28:11 -05: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-02 17:53:20 -05:00
|
|
|
return fmt.Errorf("registry ping replied with an unexpected status code %d", resp.StatusCode)
|
2015-01-31 14:28:11 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-12 16:26:49 -05:00
|
|
|
func (r *testRegistryV2) Close() {
|
|
|
|
r.cmd.Process.Kill()
|
|
|
|
os.RemoveAll(r.dir)
|
|
|
|
}
|