From bcc6a4bbf2ce996a133062ab6e21a9d12128ce31 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Fri, 20 Jan 2017 13:30:49 +0800 Subject: [PATCH] update --- .../systemcfg/store/json/driver_json_test.go | 11 +- src/adminserver/systemcfg/systemcfg_test.go | 78 ++++++--- src/common/api/base.go | 10 -- src/common/api/base_test.go | 20 --- src/common/dao/config_test.go | 164 ------------------ src/ui/api/repository.go | 6 +- src/ui/api/target.go | 4 +- 7 files changed, 69 insertions(+), 224 deletions(-) diff --git a/src/adminserver/systemcfg/store/json/driver_json_test.go b/src/adminserver/systemcfg/store/json/driver_json_test.go index c551049f6..eef48ee36 100644 --- a/src/adminserver/systemcfg/store/json/driver_json_test.go +++ b/src/adminserver/systemcfg/store/json/driver_json_test.go @@ -34,6 +34,11 @@ func TestReadWrite(t *testing.T) { } }() + if store.Name() != "JSON" { + t.Errorf("unexpected name: %s != %s", store.Name(), "JSON") + return + } + config := &models.SystemCfg{ Authentication: &models.Authentication{ LDAP: &models.LDAP{}, @@ -43,10 +48,12 @@ func TestReadWrite(t *testing.T) { }, } if err := store.Write(config); err != nil { - t.Fatalf("failed to write configurations to json file: %v", err) + t.Errorf("failed to write configurations to json file: %v", err) + return } if _, err = store.Read(); err != nil { - t.Fatalf("failed to read configurations from json file: %v", err) + t.Errorf("failed to read configurations from json file: %v", err) + return } } diff --git a/src/adminserver/systemcfg/systemcfg_test.go b/src/adminserver/systemcfg/systemcfg_test.go index f3f4a7f1b..cc28c2b38 100644 --- a/src/adminserver/systemcfg/systemcfg_test.go +++ b/src/adminserver/systemcfg/systemcfg_test.go @@ -15,39 +15,31 @@ package systemcfg -/* import ( "os" "testing" + + comcfg "github.com/vmware/harbor/src/common/config" ) - +// test functions under adminserver/systemcfg func TestSystemcfg(t *testing.T) { key := "JSON_STORE_PATH" - tmpPath := "/tmp/config.json" - originalPath := os.Getenv(key) - defer func() { - if err := os.Remove(tmpPath); err != nil { - t.Errorf("failed to remove %s: %v", tmpPath, err) + path := "/tmp/config.json" + if _, err := os.Stat(path); err == nil { + if err := os.Remove(path); err != nil { + t.Fatalf("failed to remove %s: %v", path, err) } + } else if !os.IsNotExist(err) { + t.Fatalf("failed to check the existence of %s: %v", path, err) + } - if len(originalPath) == 0 { - if err := os.Unsetenv(key); err != nil { - t.Fatalf("failed to unset env %s: %v", key, err) - } - return - } - - if err := os.Setenv(key, originalPath); err != nil { - t.Fatalf("failed to set env %s: %v", key, err) - } - }() - - if err := os.Setenv(key, tmpPath); err != nil { + if err := os.Setenv(key, path); err != nil { t.Fatalf("failed to set env %s: %v", key, err) } m := map[string]string{ + "AUTH_MODE": comcfg.DBAuth, "LDAP_SCOPE": "1", "LDAP_TIMEOUT": "30", "MYSQL_PORT": "3306", @@ -58,8 +50,7 @@ func TestSystemcfg(t *testing.T) { for k, v := range m { if err := os.Setenv(k, v); err != nil { - t.Errorf("failed to set env %s: %v", k, err) - return + t.Fatalf("failed to set env %s: %v", k, err) } } @@ -67,5 +58,46 @@ func TestSystemcfg(t *testing.T) { t.Errorf("failed to initialize system configurations: %v", err) return } + defer func() { + if err := os.Remove(path); err != nil { + t.Fatalf("failed to remove %s: %v", path, err) + } + }() + + // run Init again to make sure it works well when the configuration file + // already exists + if err := Init(); err != nil { + t.Errorf("failed to initialize system configurations: %v", err) + return + } + + cfg, err := GetSystemCfg() + if err != nil { + t.Errorf("failed to get system configurations: %v", err) + return + } + + if cfg.Authentication.Mode != comcfg.DBAuth { + t.Errorf("unexpected auth mode: %s != %s", + cfg.Authentication.Mode, comcfg.DBAuth) + return + } + + cfg.Authentication.Mode = comcfg.LDAPAuth + if err = UpdateSystemCfg(cfg); err != nil { + t.Errorf("failed to update system configurations: %v", err) + return + } + + cfg, err = GetSystemCfg() + if err != nil { + t.Errorf("failed to get system configurations: %v", err) + return + } + + if cfg.Authentication.Mode != comcfg.LDAPAuth { + t.Errorf("unexpected auth mode: %s != %s", + cfg.Authentication.Mode, comcfg.DBAuth) + return + } } -*/ diff --git a/src/common/api/base.go b/src/common/api/base.go index 227327339..38635f76d 100644 --- a/src/common/api/base.go +++ b/src/common/api/base.go @@ -26,7 +26,6 @@ import ( "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/utils/log" "github.com/vmware/harbor/src/ui/auth" - "github.com/vmware/harbor/src/ui/config" "github.com/astaxie/beego" ) @@ -210,12 +209,3 @@ func (b *BaseAPI) GetPaginationParams() (page, pageSize int64) { return page, pageSize } - -// GetIsInsecure ... -func GetIsInsecure() (bool, error) { - verify, err := config.VerifyRemoteCert() - if err != nil { - return false, err - } - return !verify, nil -} diff --git a/src/common/api/base_test.go b/src/common/api/base_test.go index 0352b6e76..386e30e52 100644 --- a/src/common/api/base_test.go +++ b/src/common/api/base_test.go @@ -13,23 +13,3 @@ limitations under the License. */ package api - -/* -import ( - "github.com/vmware/harbor/src/common/config" - "os" - "testing" -) - -func TestGetIsInsecure(t *testing.T) { - os.Setenv("VERIFY_REMOTE_CERT", "off") - err := config.Reload() - if err != nil { - t.Errorf("Failed to load config, error: %v", err) - } - if !GetIsInsecure() { - t.Errorf("GetIsInsecure() should be true when VERIFY_REMOTE_CERT is off, in fact: false") - } - os.Unsetenv("VERIFY_REMOTE_CERT") -} -*/ diff --git a/src/common/dao/config_test.go b/src/common/dao/config_test.go index ce624e313..a874a6c6e 100644 --- a/src/common/dao/config_test.go +++ b/src/common/dao/config_test.go @@ -15,175 +15,12 @@ package dao -/* import ( "testing" "github.com/vmware/harbor/src/common/models" ) -func deleteConfigByKey(key string) error { - if _, err := GetOrmer().Raw("delete from properties where k = ?", key). - Exec(); err != nil { - return err - } - return nil -} - -func TestGetConfigByKey(t *testing.T) { - cfg := &models.Config{ - Key: "key", - Value: "value", - } - - if err := InsertConfig(cfg); err != nil { - t.Fatalf("failed to insert configuration into table: %v", err) - } - defer func(key string) { - if err := deleteConfigByKey(key); err != nil { - t.Fatalf("failed to delete configuration %s: %v", key, err) - } - }(cfg.Key) - - config, err := GetConfigByKey(cfg.Key) - if err != nil { - t.Fatalf("failed to get configuration by key %s: %v", cfg.Key, err) - } - - if config == nil { - t.Fatal("configuration is nil") - } - - if config.Value != cfg.Value { - t.Fatalf("unexpected value: %s != %s", config.Value, cfg.Value) - } -} - -func TestListConfigs(t *testing.T) { - configs, err := ListConfigs() - if err != nil { - t.Fatalf("failed to list configurations: %v", err) - } - size := len(configs) - - cfg := &models.Config{ - Key: "key", - Value: "value", - } - if err := InsertConfig(cfg); err != nil { - t.Fatalf("failed to insert configuration into table: %v", err) - } - defer func(key string) { - if err := deleteConfigByKey(key); err != nil { - t.Fatalf("failed to delete configuration %s: %v", key, err) - } - }(cfg.Key) - - configs, err = ListConfigs() - if err != nil { - t.Fatalf("failed to list configurations: %v", err) - } - - if size+1 != len(configs) { - t.Fatalf("unexpected length of configurations: %d != %d", len(configs), size+1) - } -} - -func TestInsertConfig(t *testing.T) { - cfg := &models.Config{ - Key: "key1", - Value: "value1", - } - - if err := InsertConfig(cfg); err != nil { - t.Fatalf("failed to insert configuration into table: %v", err) - } - defer func(key string) { - if err := deleteConfigByKey(key); err != nil { - t.Fatalf("failed to delete configuration %s: %v", key, err) - } - }(cfg.Key) - - config, err := GetConfigByKey(cfg.Key) - if err != nil { - t.Fatalf("failed to get configuration by key %s: %v", cfg.Key, err) - } - if config == nil { - t.Fatal("configuration is nil") - } - - if config.Value != cfg.Value { - t.Fatalf("unexpected value: %s != %s", config.Value, cfg.Value) - } -} - -func TestUpdateConfig(t *testing.T) { - cfg := &models.Config{ - Key: "key", - Value: "value", - } - - if err := InsertConfig(cfg); err != nil { - t.Fatalf("failed to insert configuration into table: %v", err) - } - defer func(key string) { - if err := deleteConfigByKey(key); err != nil { - t.Fatalf("failed to delete configuration %s: %v", key, err) - } - }(cfg.Key) - - newCfg := &models.Config{ - Key: "key", - Value: "new_value", - } - if err := UpdateConfig(newCfg); err != nil { - t.Fatalf("failed to update configuration: %v", err) - } - - config, err := GetConfigByKey(cfg.Key) - if err != nil { - t.Fatalf("failed to get configuration by key %s: %v", cfg.Key, err) - } - - if config == nil { - t.Fatal("configuration is nil") - } - - if config.Value != newCfg.Value { - t.Fatalf("unexpected value: %s != %s", config.Value, newCfg.Value) - } -} - -func TestInsertOrUpdateConfigs(t *testing.T) { - cfg1 := &models.Config{ - Key: "key1", - Value: "value1", - } - - if err := InsertConfig(cfg1); err != nil { - t.Fatalf("failed to insert configuration into table: %v", err) - } - defer func(key string) { - if err := deleteConfigByKey(key); err != nil { - t.Fatalf("failed to delete configuration %s: %v", key, err) - } - }(cfg1.Key) - - cfg2 := &models.Config{ - Key: "key2", - Value: "value2", - } - - if err := InsertOrUpdateConfigs([]*models.Config{cfg1, cfg2}); err != nil { - t.Fatalf("failed to insert or update configurations: %v", err) - } - defer func(key string) { - if err := deleteConfigByKey(key); err != nil { - t.Fatalf("failed to delete configuration %s: %v", key, err) - } - }(cfg2.Key) -} - func TestAuthModeCanBeModified(t *testing.T) { c, err := GetOrmer().QueryTable(&models.User{}).Count() if err != nil { @@ -233,4 +70,3 @@ func TestAuthModeCanBeModified(t *testing.T) { } } } -*/ diff --git a/src/ui/api/repository.go b/src/ui/api/repository.go index 5eca32805..b229c0e96 100644 --- a/src/ui/api/repository.go +++ b/src/ui/api/repository.go @@ -366,14 +366,14 @@ func (ra *RepositoryAPI) initRepositoryClient(repoName string) (r *registry.Repo return nil, err } - insecure, err := api.GetIsInsecure() + verify, err := config.VerifyRemoteCert() if err != nil { return nil, err } username, password, ok := ra.Ctx.Request.BasicAuth() if ok { - return newRepositoryClient(endpoint, insecure, username, password, + return newRepositoryClient(endpoint, !verify, username, password, repoName, "repository", repoName, "pull", "push", "*") } @@ -382,7 +382,7 @@ func (ra *RepositoryAPI) initRepositoryClient(repoName string) (r *registry.Repo return nil, err } - return cache.NewRepositoryClient(endpoint, insecure, username, repoName, + return cache.NewRepositoryClient(endpoint, !verify, username, repoName, "repository", repoName, "pull", "push", "*") } diff --git a/src/ui/api/target.go b/src/ui/api/target.go index c1f88d2ba..becda414e 100644 --- a/src/ui/api/target.go +++ b/src/ui/api/target.go @@ -102,12 +102,12 @@ func (t *TargetAPI) Ping() { password = t.GetString("password") } - insecure, err := api.GetIsInsecure() + verify, err := config.VerifyRemoteCert() if err != nil { log.Errorf("failed to check whether insecure or not: %v", err) t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) } - registry, err := newRegistryClient(endpoint, insecure, username, password, + registry, err := newRegistryClient(endpoint, !verify, username, password, "", "", "") if err != nil { // timeout, dns resolve error, connection refused, etc.