github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/pkg/cluster/master_test.go (about) 1 package cluster 2 3 import ( 4 "context" 5 model "github.com/cloudreve/Cloudreve/v3/models" 6 "github.com/cloudreve/Cloudreve/v3/pkg/aria2/rpc" 7 "github.com/cloudreve/Cloudreve/v3/pkg/serializer" 8 "github.com/cloudreve/Cloudreve/v3/pkg/util" 9 "github.com/stretchr/testify/assert" 10 "os" 11 "testing" 12 "time" 13 ) 14 15 func TestMasterNode_Init(t *testing.T) { 16 a := assert.New(t) 17 m := &MasterNode{} 18 m.Init(&model.Node{Status: model.NodeSuspend}) 19 a.Equal(model.NodeSuspend, m.DBModel().Status) 20 m.Init(&model.Node{Aria2Enabled: true}) 21 } 22 23 func TestMasterNode_DummyMethods(t *testing.T) { 24 a := assert.New(t) 25 m := &MasterNode{ 26 Model: &model.Node{}, 27 } 28 29 m.Model.ID = 5 30 a.Equal(m.Model.ID, m.ID()) 31 32 res, err := m.Ping(&serializer.NodePingReq{}) 33 a.NoError(err) 34 a.NotNil(res) 35 36 a.True(m.IsActive()) 37 a.True(m.IsMater()) 38 39 m.SubscribeStatusChange(func(isActive bool, id uint) {}) 40 } 41 42 func TestMasterNode_IsFeatureEnabled(t *testing.T) { 43 a := assert.New(t) 44 m := &MasterNode{ 45 Model: &model.Node{}, 46 } 47 48 a.False(m.IsFeatureEnabled("aria2")) 49 a.False(m.IsFeatureEnabled("random")) 50 m.Model.Aria2Enabled = true 51 a.True(m.IsFeatureEnabled("aria2")) 52 } 53 54 func TestMasterNode_AuthInstance(t *testing.T) { 55 a := assert.New(t) 56 m := &MasterNode{ 57 Model: &model.Node{}, 58 } 59 60 a.NotNil(m.MasterAuthInstance()) 61 a.NotNil(m.SlaveAuthInstance()) 62 } 63 64 func TestMasterNode_Kill(t *testing.T) { 65 m := &MasterNode{ 66 Model: &model.Node{}, 67 } 68 69 m.Kill() 70 71 caller, _ := rpc.New(context.Background(), "http://", "", 0, nil) 72 m.aria2RPC.Caller = caller 73 m.Kill() 74 } 75 76 func TestMasterNode_GetAria2Instance(t *testing.T) { 77 a := assert.New(t) 78 m := &MasterNode{ 79 Model: &model.Node{}, 80 aria2RPC: rpcService{}, 81 } 82 83 m.aria2RPC.parent = m 84 85 a.NotNil(m.GetAria2Instance()) 86 m.Model.Aria2Enabled = true 87 a.NotNil(m.GetAria2Instance()) 88 m.aria2RPC.Initialized = true 89 a.NotNil(m.GetAria2Instance()) 90 } 91 92 func TestRpcService_Init(t *testing.T) { 93 a := assert.New(t) 94 m := &MasterNode{ 95 Model: &model.Node{ 96 Aria2OptionsSerialized: model.Aria2Option{ 97 Options: "{", 98 }, 99 }, 100 aria2RPC: rpcService{}, 101 } 102 m.aria2RPC.parent = m 103 104 // failed to decode address 105 { 106 m.Model.Aria2OptionsSerialized.Server = string([]byte{0x7f}) 107 a.Error(m.aria2RPC.Init()) 108 } 109 110 // failed to decode options 111 { 112 m.Model.Aria2OptionsSerialized.Server = "" 113 a.Error(m.aria2RPC.Init()) 114 } 115 116 // failed to initialized 117 { 118 m.Model.Aria2OptionsSerialized.Server = "" 119 m.Model.Aria2OptionsSerialized.Options = "{}" 120 caller, _ := rpc.New(context.Background(), "http://", "", 0, nil) 121 m.aria2RPC.Caller = caller 122 a.Error(m.aria2RPC.Init()) 123 a.False(m.aria2RPC.Initialized) 124 } 125 } 126 127 func getTestRPCNode() *MasterNode { 128 m := &MasterNode{ 129 Model: &model.Node{ 130 Aria2OptionsSerialized: model.Aria2Option{}, 131 }, 132 aria2RPC: rpcService{ 133 options: &clientOptions{ 134 Options: map[string]interface{}{"1": "1"}, 135 }, 136 }, 137 } 138 m.aria2RPC.parent = m 139 caller, _ := rpc.New(context.Background(), "http://", "", 0, nil) 140 m.aria2RPC.Caller = caller 141 return m 142 } 143 144 func TestRpcService_CreateTask(t *testing.T) { 145 a := assert.New(t) 146 m := getTestRPCNode() 147 148 res, err := m.aria2RPC.CreateTask(&model.Download{}, map[string]interface{}{"1": "1"}) 149 a.Error(err) 150 a.Empty(res) 151 } 152 153 func TestRpcService_Status(t *testing.T) { 154 a := assert.New(t) 155 m := getTestRPCNode() 156 157 res, err := m.aria2RPC.Status(&model.Download{}) 158 a.Error(err) 159 a.Empty(res) 160 } 161 162 func TestRpcService_Cancel(t *testing.T) { 163 a := assert.New(t) 164 m := getTestRPCNode() 165 166 a.Error(m.aria2RPC.Cancel(&model.Download{})) 167 } 168 169 func TestRpcService_Select(t *testing.T) { 170 a := assert.New(t) 171 m := getTestRPCNode() 172 173 a.NotNil(m.aria2RPC.GetConfig()) 174 a.Error(m.aria2RPC.Select(&model.Download{}, []int{1, 2, 3})) 175 } 176 177 func TestRpcService_DeleteTempFile(t *testing.T) { 178 a := assert.New(t) 179 m := getTestRPCNode() 180 fdName := "TestRpcService_DeleteTempFile" 181 a.NoError(os.Mkdir(fdName, 0644)) 182 183 a.NoError(m.aria2RPC.DeleteTempFile(&model.Download{Parent: fdName})) 184 time.Sleep(500 * time.Millisecond) 185 a.False(util.Exists(fdName)) 186 }