Dragonfly2
86 строк · 2.7 Кб
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 announcer
18
19import (
20"testing"
21
22"github.com/stretchr/testify/assert"
23"go.uber.org/mock/gomock"
24
25"d7y.io/dragonfly/v2/client/config"
26configmocks "d7y.io/dragonfly/v2/client/config/mocks"
27managerclientmocks "d7y.io/dragonfly/v2/pkg/rpc/manager/client/mocks"
28schedulerclientmocks "d7y.io/dragonfly/v2/pkg/rpc/scheduler/client/mocks"
29)
30
31func TestAnnouncer_New(t *testing.T) {
32tests := []struct {
33name string
34config *config.DaemonOption
35hostID string
36deamonPort int32
37deamonDownloadPort int32
38expect func(t *testing.T, announcer Announcer)
39}{
40{
41name: "new announcer",
42config: &config.DaemonOption{},
43hostID: "foo",
44deamonPort: 8000,
45deamonDownloadPort: 8001,
46expect: func(t *testing.T, a Announcer) {
47assert := assert.New(t)
48instance := a.(*announcer)
49assert.NotNil(instance.config)
50assert.Equal(instance.hostID, "foo")
51assert.Equal(instance.daemonPort, int32(8000))
52assert.Equal(instance.daemonDownloadPort, int32(8001))
53assert.NotNil(instance.managerClient)
54assert.NotNil(instance.schedulerClient)
55},
56},
57{
58name: "new announcer with empty value",
59config: nil,
60hostID: "",
61deamonPort: 0,
62deamonDownloadPort: 0,
63expect: func(t *testing.T, a Announcer) {
64assert := assert.New(t)
65instance := a.(*announcer)
66assert.Nil(instance.config)
67assert.Equal(instance.hostID, "")
68assert.Equal(instance.daemonPort, int32(0))
69assert.Equal(instance.daemonDownloadPort, int32(0))
70assert.NotNil(instance.managerClient)
71assert.NotNil(instance.schedulerClient)
72},
73},
74}
75
76for _, tc := range tests {
77t.Run(tc.name, func(t *testing.T) {
78ctl := gomock.NewController(t)
79defer ctl.Finish()
80mockManagerClient := managerclientmocks.NewMockV1(ctl)
81mockSchedulerClient := schedulerclientmocks.NewMockV1(ctl)
82mockDynconfig := configmocks.NewMockDynconfig(ctl)
83tc.expect(t, New(tc.config, mockDynconfig, tc.hostID, tc.deamonPort, tc.deamonDownloadPort, mockSchedulerClient, WithManagerClient(mockManagerClient)))
84})
85}
86}
87