registry.go 1.3 KB

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