Repository: dragonflyoss/Dragonfly2 (go)

Issue:
Add config test
### Feature request:
- Add scheduler config' `CDNDirPath` attribute test.
- Add manager config test.
<!-- Please describe the feature request and why you would like to have it -->

### Use case:
N/A
<!-- Please add a concrete use case to demonstrate how such a feature would add value for the user. -->

### UI Example:
N/A
<!-- If this is about a new command or command line options, please let us know how you would add it to UI (in the code block below). -->


Pull request:
test: manager and scheduler config
Signed-off-by: Gaius <gaius.qi@gmail.com>

<!--- Provide a general summary of your changes in the Title above -->

## Description
- Add scheduler config' `CDNDirPath` attribute test.
- Add manager config test.

<!--- Describe your changes in detail -->

## Related Issue
Fix #391
<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context
N/A
<!--- Why is this change required? What problem does it solve? -->

## Screenshots (if appropriate):

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Documentation Update (if none of the other choices apply)

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [x] Code compiles correctly.
- [x] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have read the **CONTRIBUTING** document.
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.


Patch:
diff --git a/manager/config/config.go b/manager/config/config.go
index bb5a0ff4..946e0407 100644
--- a/manager/config/config.go
+++ b/manager/config/config.go
@@ -34,7 +34,7 @@ type MysqlConfig struct {
 
 type RedisConfig struct {
 	Host     string `yaml:"host" mapstructure:"host"`
-	Port     string `yaml:"port" mapstructure:"port"`
+	Port     int    `yaml:"port" mapstructure:"port"`
 	Password string `yaml:"password" mapstructure:"password"`
 	DB       int    `yaml:"db" mapstructure:"db"`
 }
diff --git a/manager/database/database.go b/manager/database/database.go
index ea8a97db..29caae2b 100644
--- a/manager/database/database.go
+++ b/manager/database/database.go
@@ -30,7 +30,7 @@ func New(cfg *config.Config) (*Database, error) {
 
 func NewRedis(cfg *config.RedisConfig) *redis.Client {
 	return redis.NewClient(&redis.Options{
-		Addr:     fmt.Sprintf("%s:%s", cfg.Host, cfg.Port),
+		Addr:     fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
 		Password: cfg.Password,
 		DB:       cfg.DB,
 	})


Test patch:
diff --git a/manager/config/config_test.go b/manager/config/config_test.go
new file mode 100644
index 00000000..0e7754d0
--- /dev/null
+++ b/manager/config/config_test.go
@@ -0,0 +1,76 @@
+/*
+ *     Copyright 2020 The Dragonfly Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package config
+
+import (
+	"io/ioutil"
+	"testing"
+
+	"github.com/mitchellh/mapstructure"
+	testifyassert "github.com/stretchr/testify/assert"
+	"gopkg.in/yaml.v3"
+)
+
+func TestManagerConfig_Load(t *testing.T) {
+	assert := testifyassert.New(t)
+
+	config := &Config{
+		Server: &ServerConfig{
+			GRPC: &TCPListenConfig{
+				Listen: "127.0.0.1",
+				PortRange: TCPListenPortRange{
+					Start: 65003,
+					End:   65003,
+				},
+			},
+			REST: &RestConfig{
+				Addr: ":8080",
+			},
+		},
+		Database: &DatabaseConfig{
+			Mysql: &MysqlConfig{
+				User:     "foo",
+				Password: "foo",
+				Host:     "foo",
+				Port:     3306,
+				DBName:   "foo",
+			},
+			Redis: &RedisConfig{
+				Host:     "bar",
+				Password: "bar",
+				Port:     6379,
+				DB:       0,
+			},
+		},
+		Cache: &CacheConfig{
+			Redis: &RedisCacheConfig{
+				TTL: 1000,
+			},
+			Local: &LocalCacheConfig{
+				Size: 10000,
+				TTL:  1000,
+			},
+		},
+	}
+
+	managerConfigYAML := &Config{}
+	contentYAML, _ := ioutil.ReadFile("./testdata/manager.yaml")
+	var dataYAML map[string]interface{}
+	yaml.Unmarshal(contentYAML, &dataYAML)
+	mapstructure.Decode(dataYAML, &managerConfigYAML)
+	assert.EqualValues(config, managerConfigYAML)
+}
diff --git a/manager/config/testdata/manager.yaml b/manager/config/testdata/manager.yaml
new file mode 100644
index 00000000..69aa1efe
--- /dev/null
+++ b/manager/config/testdata/manager.yaml
@@ -0,0 +1,28 @@
+server:
+  grpc:
+    listen: 127.0.0.1
+    port:
+      start: 65003
+      end: 65003
+  rest:
+    addr: :8080
+
+database:
+  mysql:
+    user: foo
+    password: foo
+    host: foo
+    port: 3306
+    dbname: foo
+  redis:
+    password: bar
+    host: bar
+    port: 6379
+    db: 0
+
+cache:
+  redis:
+    ttl: 1000
+  local:
+    size: 10000
+    ttl: 1000
diff --git a/scheduler/config/config_test.go b/scheduler/config/config_test.go
index 5a1c17be..dbec5ba0 100644
--- a/scheduler/config/config_test.go
+++ b/scheduler/config/config_test.go
@@ -47,6 +47,7 @@ func TestSchedulerConfig_Load(t *testing.T) {
 			CachePath:  "bar",
 			ExpireTime: 1000,
 			Addr:       "127.0.0.1:8002",
+			CDNDirPath: "tmp",
 		},
 		Scheduler: SchedulerConfig{
 			ABTest:     true,
diff --git a/scheduler/config/testdata/scheduler.yaml b/scheduler/config/testdata/scheduler.yaml
index 727c161f..54a4838b 100644
--- a/scheduler/config/testdata/scheduler.yaml
+++ b/scheduler/config/testdata/scheduler.yaml
@@ -4,6 +4,7 @@ dynconfig:
   path: foo
   cachePath: bar
   addr: 127.0.0.1:8002
+  cdnDirPath: tmp
 scheduler:
   abtest: true
   ascheduler: "a-scheduler"