Dragonfly2
95 строк · 2.2 Кб
1/*
2* Copyright 2022 The Dragonfly Authors
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17package config
18
19import (
20"testing"
21
22testifyassert "github.com/stretchr/testify/assert"
23)
24
25func Test_NewDfstore(t *testing.T) {
26tests := []struct {
27name string
28expect func(t *testing.T, cfg *DfstoreConfig)
29}{
30{
31name: "new dfstore configuration",
32expect: func(t *testing.T, cfg *DfstoreConfig) {
33assert := testifyassert.New(t)
34assert.Equal("http://127.0.0.1:65004", cfg.Endpoint)
35assert.Equal(3, cfg.MaxReplicas)
36},
37},
38}
39
40for _, tc := range tests {
41t.Run(tc.name, func(t *testing.T) {
42cfg := NewDfstore()
43tc.expect(t, cfg)
44})
45}
46}
47
48func TestDfstoreConfig_Validate(t *testing.T) {
49tests := []struct {
50name string
51cfg *DfstoreConfig
52expect func(t *testing.T, err error)
53}{
54{
55name: "normal dfsrore",
56cfg: &DfstoreConfig{
57Endpoint: "http://127.0.0.1:65004",
58MaxReplicas: 3,
59},
60expect: func(t *testing.T, err error) {
61assert := testifyassert.New(t)
62assert.Nil(err)
63},
64},
65{
66name: "dfstore without endpoint",
67cfg: &DfstoreConfig{
68Endpoint: "",
69MaxReplicas: 3,
70},
71expect: func(t *testing.T, err error) {
72assert := testifyassert.New(t)
73assert.EqualError(err, "dfstore requires parameter endpoint")
74},
75},
76{
77name: "dfstore with invalid endpoint",
78cfg: &DfstoreConfig{
79Endpoint: "127.0.0.1:65004",
80MaxReplicas: 3,
81},
82expect: func(t *testing.T, err error) {
83assert := testifyassert.New(t)
84assert.EqualError(err, "invalid endpoint: parse \"127.0.0.1:65004\": invalid URI for request")
85},
86},
87}
88
89for _, tc := range tests {
90t.Run(tc.name, func(t *testing.T) {
91err := tc.cfg.Validate()
92tc.expect(t, err)
93})
94}
95}
96