|
@@ -18,28 +18,55 @@ const (
|
|
)
|
|
)
|
|
|
|
|
|
type testRegistryV2 struct {
|
|
type testRegistryV2 struct {
|
|
- cmd *exec.Cmd
|
|
|
|
- dir string
|
|
|
|
|
|
+ cmd *exec.Cmd
|
|
|
|
+ dir string
|
|
|
|
+ username string
|
|
|
|
+ password string
|
|
|
|
+ email string
|
|
}
|
|
}
|
|
|
|
|
|
-func newTestRegistryV2(c *check.C, schema1 bool) (*testRegistryV2, error) {
|
|
|
|
|
|
+func newTestRegistryV2(c *check.C, schema1, auth bool) (*testRegistryV2, error) {
|
|
|
|
+ tmp, err := ioutil.TempDir("", "registry-test-")
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
template := `version: 0.1
|
|
template := `version: 0.1
|
|
loglevel: debug
|
|
loglevel: debug
|
|
storage:
|
|
storage:
|
|
filesystem:
|
|
filesystem:
|
|
rootdirectory: %s
|
|
rootdirectory: %s
|
|
http:
|
|
http:
|
|
- addr: %s`
|
|
|
|
- tmp, err := ioutil.TempDir("", "registry-test-")
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
|
|
+ addr: %s
|
|
|
|
+%s`
|
|
|
|
+ var (
|
|
|
|
+ htpasswd string
|
|
|
|
+ username string
|
|
|
|
+ password string
|
|
|
|
+ email string
|
|
|
|
+ )
|
|
|
|
+ if auth {
|
|
|
|
+ htpasswdPath := filepath.Join(tmp, "htpasswd")
|
|
|
|
+ // generated with: htpasswd -Bbn testuser testpassword
|
|
|
|
+ userpasswd := "testuser:$2y$05$sBsSqk0OpSD1uTZkHXc4FeJ0Z70wLQdAX/82UiHuQOKbNbBrzs63m"
|
|
|
|
+ username = "testuser"
|
|
|
|
+ password = "testpassword"
|
|
|
|
+ email = "test@test.org"
|
|
|
|
+ if err := ioutil.WriteFile(htpasswdPath, []byte(userpasswd), os.FileMode(0644)); err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ htpasswd = fmt.Sprintf(`auth:
|
|
|
|
+ htpasswd:
|
|
|
|
+ realm: basic-realm
|
|
|
|
+ path: %s
|
|
|
|
+`, htpasswdPath)
|
|
}
|
|
}
|
|
|
|
+
|
|
confPath := filepath.Join(tmp, "config.yaml")
|
|
confPath := filepath.Join(tmp, "config.yaml")
|
|
config, err := os.Create(confPath)
|
|
config, err := os.Create(confPath)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
- if _, err := fmt.Fprintf(config, template, tmp, privateRegistryURL); err != nil {
|
|
|
|
|
|
+ if _, err := fmt.Fprintf(config, template, tmp, privateRegistryURL, htpasswd); err != nil {
|
|
os.RemoveAll(tmp)
|
|
os.RemoveAll(tmp)
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
@@ -57,8 +84,11 @@ http:
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
return &testRegistryV2{
|
|
return &testRegistryV2{
|
|
- cmd: cmd,
|
|
|
|
- dir: tmp,
|
|
|
|
|
|
+ cmd: cmd,
|
|
|
|
+ dir: tmp,
|
|
|
|
+ username: username,
|
|
|
|
+ password: password,
|
|
|
|
+ email: email,
|
|
}, nil
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
|
|
@@ -68,7 +98,14 @@ func (t *testRegistryV2) Ping() error {
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
- if resp.StatusCode != http.StatusOK {
|
|
|
|
|
|
+ resp.Body.Close()
|
|
|
|
+
|
|
|
|
+ fail := resp.StatusCode != http.StatusOK
|
|
|
|
+ if t.username != "" {
|
|
|
|
+ // unauthorized is a _good_ status when pinging v2/ and it needs auth
|
|
|
|
+ fail = fail && resp.StatusCode != http.StatusUnauthorized
|
|
|
|
+ }
|
|
|
|
+ if fail {
|
|
return fmt.Errorf("registry ping replied with an unexpected status code %d", resp.StatusCode)
|
|
return fmt.Errorf("registry ping replied with an unexpected status code %d", resp.StatusCode)
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|