Update tests with new cookies for registry

This commit is contained in:
Guillaume J. Charmes 2013-05-28 17:35:10 -07:00
parent cd0de83917
commit b76d6120ac
4 changed files with 34 additions and 19 deletions

7
api.go
View File

@ -61,10 +61,14 @@ func getBoolParam(value string) (bool, error) {
func getAuth(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error { func getAuth(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
// FIXME: Handle multiple login at once // FIXME: Handle multiple login at once
// FIXME: return specific error code if config file missing?
authConfig, err := auth.LoadConfig(srv.runtime.root) authConfig, err := auth.LoadConfig(srv.runtime.root)
if err != nil { if err != nil {
if err != auth.ErrConfigFileMissing {
return err return err
} }
authConfig = &auth.AuthConfig{}
}
b, err := json.Marshal(&auth.AuthConfig{Username: authConfig.Username, Email: authConfig.Email}) b, err := json.Marshal(&auth.AuthConfig{Username: authConfig.Username, Email: authConfig.Email})
if err != nil { if err != nil {
return err return err
@ -82,8 +86,11 @@ func postAuth(srv *Server, version float64, w http.ResponseWriter, r *http.Reque
authConfig, err := auth.LoadConfig(srv.runtime.root) authConfig, err := auth.LoadConfig(srv.runtime.root)
if err != nil { if err != nil {
if err != auth.ErrConfigFileMissing {
return err return err
} }
authConfig = &auth.AuthConfig{}
}
if config.Username == authConfig.Username { if config.Username == authConfig.Username {
config.Password = authConfig.Password config.Password = authConfig.Password
} }

View File

@ -27,7 +27,6 @@ func TestGetAuth(t *testing.T) {
srv := &Server{ srv := &Server{
runtime: runtime, runtime: runtime,
registry: registry.NewRegistry(runtime.root),
} }
r := httptest.NewRecorder() r := httptest.NewRecorder()
@ -56,7 +55,7 @@ func TestGetAuth(t *testing.T) {
t.Fatalf("%d OK or 0 expected, received %d\n", http.StatusOK, r.Code) t.Fatalf("%d OK or 0 expected, received %d\n", http.StatusOK, r.Code)
} }
newAuthConfig := srv.registry.GetAuthConfig(false) newAuthConfig := registry.NewRegistry(runtime.root).GetAuthConfig(false)
if newAuthConfig.Username != authConfig.Username || if newAuthConfig.Username != authConfig.Username ||
newAuthConfig.Email != authConfig.Email { newAuthConfig.Email != authConfig.Email {
t.Fatalf("The auth configuration hasn't been set correctly") t.Fatalf("The auth configuration hasn't been set correctly")
@ -248,7 +247,6 @@ func TestGetImagesSearch(t *testing.T) {
srv := &Server{ srv := &Server{
runtime: runtime, runtime: runtime,
registry: registry.NewRegistry(runtime.root),
} }
r := httptest.NewRecorder() r := httptest.NewRecorder()
@ -505,14 +503,15 @@ func TestPostAuth(t *testing.T) {
srv := &Server{ srv := &Server{
runtime: runtime, runtime: runtime,
registry: registry.NewRegistry(runtime.root),
} }
authConfigOrig := &auth.AuthConfig{ config := &auth.AuthConfig{
Username: "utest", Username: "utest",
Email: "utest@yopmail.com", Email: "utest@yopmail.com",
} }
srv.registry.ResetClient(authConfigOrig)
authStr := auth.EncodeAuth(config)
auth.SaveConfig(runtime.root, authStr, config.Email)
r := httptest.NewRecorder() r := httptest.NewRecorder()
if err := getAuth(srv, API_VERSION, r, nil, nil); err != nil { if err := getAuth(srv, API_VERSION, r, nil, nil); err != nil {
@ -524,7 +523,7 @@ func TestPostAuth(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if authConfig.Username != authConfigOrig.Username || authConfig.Email != authConfigOrig.Email { if authConfig.Username != config.Username || authConfig.Email != config.Email {
t.Errorf("The retrieve auth mismatch with the one set.") t.Errorf("The retrieve auth mismatch with the one set.")
} }
} }

View File

@ -3,6 +3,7 @@ package auth
import ( import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -17,6 +18,12 @@ const CONFIGFILE = ".dockercfg"
// the registry server we want to login against // the registry server we want to login against
const INDEX_SERVER = "https://index.docker.io/v1" const INDEX_SERVER = "https://index.docker.io/v1"
//const INDEX_SERVER = "http://indexstaging-docker.dotcloud.com/"
var (
ErrConfigFileMissing error = errors.New("The Auth config file is missing")
)
type AuthConfig struct { type AuthConfig struct {
Username string `json:"username"` Username string `json:"username"`
Password string `json:"password"` Password string `json:"password"`
@ -75,7 +82,7 @@ func DecodeAuth(authStr string) (*AuthConfig, error) {
func LoadConfig(rootPath string) (*AuthConfig, error) { func LoadConfig(rootPath string) (*AuthConfig, error) {
confFile := path.Join(rootPath, CONFIGFILE) confFile := path.Join(rootPath, CONFIGFILE)
if _, err := os.Stat(confFile); err != nil { if _, err := os.Stat(confFile); err != nil {
return &AuthConfig{}, fmt.Errorf("The Auth config file is missing") return nil, ErrConfigFileMissing
} }
b, err := ioutil.ReadFile(confFile) b, err := ioutil.ReadFile(confFile)
if err != nil { if err != nil {
@ -97,7 +104,7 @@ func LoadConfig(rootPath string) (*AuthConfig, error) {
} }
// save the auth config // save the auth config
func saveConfig(rootPath, authStr string, email string) error { func SaveConfig(rootPath, authStr string, email string) error {
confFile := path.Join(rootPath, CONFIGFILE) confFile := path.Join(rootPath, CONFIGFILE)
if len(email) == 0 { if len(email) == 0 {
os.Remove(confFile) os.Remove(confFile)
@ -161,7 +168,9 @@ func Login(authConfig *AuthConfig) (string, error) {
status = "Login Succeeded\n" status = "Login Succeeded\n"
storeConfig = true storeConfig = true
} else if resp.StatusCode == 401 { } else if resp.StatusCode == 401 {
saveConfig(authConfig.rootPath, "", "") if err := SaveConfig(authConfig.rootPath, "", ""); err != nil {
return "", err
}
return "", fmt.Errorf("Wrong login/password, please try again") return "", fmt.Errorf("Wrong login/password, please try again")
} else { } else {
return "", fmt.Errorf("Login: %s (Code: %d; Headers: %s)", body, return "", fmt.Errorf("Login: %s (Code: %d; Headers: %s)", body,
@ -175,7 +184,9 @@ func Login(authConfig *AuthConfig) (string, error) {
} }
if storeConfig { if storeConfig {
authStr := EncodeAuth(authConfig) authStr := EncodeAuth(authConfig)
saveConfig(authConfig.rootPath, authStr, authConfig.Email) if err := SaveConfig(authConfig.rootPath, authStr, authConfig.Email); err != nil {
return "", err
}
} }
return status, nil return status, nil
} }

View File

@ -2,7 +2,6 @@ package docker
import ( import (
"fmt" "fmt"
"github.com/dotcloud/docker/registry"
"github.com/dotcloud/docker/utils" "github.com/dotcloud/docker/utils"
"io" "io"
"io/ioutil" "io/ioutil"
@ -72,7 +71,6 @@ func init() {
// Create the "Server" // Create the "Server"
srv := &Server{ srv := &Server{
runtime: runtime, runtime: runtime,
registry: registry.NewRegistry(runtime.root),
} }
// Retrieve the Image // Retrieve the Image
if err := srv.ImagePull(unitTestImageName, "", "", os.Stdout, false); err != nil { if err := srv.ImagePull(unitTestImageName, "", "", os.Stdout, false); err != nil {