Преглед на файлове

Update tests with new cookies for registry

Guillaume J. Charmes преди 12 години
родител
ревизия
b76d6120ac
променени са 4 файла, в които са добавени 34 реда и са изтрити 19 реда
  1. 9 2
      api.go
  2. 9 10
      api_test.go
  3. 15 4
      auth/auth.go
  4. 1 3
      runtime_test.go

+ 9 - 2
api.go

@@ -61,9 +61,13 @@ func getBoolParam(value string) (bool, 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: return specific error code if config file missing?
 	authConfig, err := auth.LoadConfig(srv.runtime.root)
 	if err != nil {
-		return err
+		if err != auth.ErrConfigFileMissing {
+			return err
+		}
+		authConfig = &auth.AuthConfig{}
 	}
 	b, err := json.Marshal(&auth.AuthConfig{Username: authConfig.Username, Email: authConfig.Email})
 	if err != nil {
@@ -82,7 +86,10 @@ func postAuth(srv *Server, version float64, w http.ResponseWriter, r *http.Reque
 
 	authConfig, err := auth.LoadConfig(srv.runtime.root)
 	if err != nil {
-		return err
+		if err != auth.ErrConfigFileMissing {
+			return err
+		}
+		authConfig = &auth.AuthConfig{}
 	}
 	if config.Username == authConfig.Username {
 		config.Password = authConfig.Password

+ 9 - 10
api_test.go

@@ -26,8 +26,7 @@ func TestGetAuth(t *testing.T) {
 	defer nuke(runtime)
 
 	srv := &Server{
-		runtime:  runtime,
-		registry: registry.NewRegistry(runtime.root),
+		runtime: runtime,
 	}
 
 	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)
 	}
 
-	newAuthConfig := srv.registry.GetAuthConfig(false)
+	newAuthConfig := registry.NewRegistry(runtime.root).GetAuthConfig(false)
 	if newAuthConfig.Username != authConfig.Username ||
 		newAuthConfig.Email != authConfig.Email {
 		t.Fatalf("The auth configuration hasn't been set correctly")
@@ -247,8 +246,7 @@ func TestGetImagesSearch(t *testing.T) {
 	defer nuke(runtime)
 
 	srv := &Server{
-		runtime:  runtime,
-		registry: registry.NewRegistry(runtime.root),
+		runtime: runtime,
 	}
 
 	r := httptest.NewRecorder()
@@ -504,15 +502,16 @@ func TestPostAuth(t *testing.T) {
 	defer nuke(runtime)
 
 	srv := &Server{
-		runtime:  runtime,
-		registry: registry.NewRegistry(runtime.root),
+		runtime: runtime,
 	}
 
-	authConfigOrig := &auth.AuthConfig{
+	config := &auth.AuthConfig{
 		Username: "utest",
 		Email:    "utest@yopmail.com",
 	}
-	srv.registry.ResetClient(authConfigOrig)
+
+	authStr := auth.EncodeAuth(config)
+	auth.SaveConfig(runtime.root, authStr, config.Email)
 
 	r := httptest.NewRecorder()
 	if err := getAuth(srv, API_VERSION, r, nil, nil); err != nil {
@@ -524,7 +523,7 @@ func TestPostAuth(t *testing.T) {
 		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.")
 	}
 }

+ 15 - 4
auth/auth.go

@@ -3,6 +3,7 @@ package auth
 import (
 	"encoding/base64"
 	"encoding/json"
+	"errors"
 	"fmt"
 	"io/ioutil"
 	"net/http"
@@ -17,6 +18,12 @@ const CONFIGFILE = ".dockercfg"
 // the registry server we want to login against
 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 {
 	Username string `json:"username"`
 	Password string `json:"password"`
@@ -75,7 +82,7 @@ func DecodeAuth(authStr string) (*AuthConfig, error) {
 func LoadConfig(rootPath string) (*AuthConfig, error) {
 	confFile := path.Join(rootPath, CONFIGFILE)
 	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)
 	if err != nil {
@@ -97,7 +104,7 @@ func LoadConfig(rootPath string) (*AuthConfig, error) {
 }
 
 // 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)
 	if len(email) == 0 {
 		os.Remove(confFile)
@@ -161,7 +168,9 @@ func Login(authConfig *AuthConfig) (string, error) {
 				status = "Login Succeeded\n"
 				storeConfig = true
 			} 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")
 			} else {
 				return "", fmt.Errorf("Login: %s (Code: %d; Headers: %s)", body,
@@ -175,7 +184,9 @@ func Login(authConfig *AuthConfig) (string, error) {
 	}
 	if storeConfig {
 		authStr := EncodeAuth(authConfig)
-		saveConfig(authConfig.rootPath, authStr, authConfig.Email)
+		if err := SaveConfig(authConfig.rootPath, authStr, authConfig.Email); err != nil {
+			return "", err
+		}
 	}
 	return status, nil
 }

+ 1 - 3
runtime_test.go

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