diff --git a/farmerbot/internal/farmerbot_test.go b/farmerbot/internal/farmerbot_test.go index e455b8e4e..1c45bebec 100644 --- a/farmerbot/internal/farmerbot_test.go +++ b/farmerbot/internal/farmerbot_test.go @@ -54,8 +54,8 @@ func TestFarmerbot(t *testing.T) { assert.NoError(t, err) farmerbot.state = state - oldNode1 := farmerbot.nodes[1] - oldNode2 := farmerbot.nodes[2] + oldNode1 := farmerbot.nodes[0] + oldNode2 := farmerbot.nodes[1] t.Run("invalid identity", func(t *testing.T) { _, err := NewFarmerBot(ctx, Config{}, "dev", "invalid", peer.KeyTypeSr25519) @@ -86,8 +86,8 @@ func TestFarmerbot(t *testing.T) { oldNode1.powerState = off oldNode2.powerState = off - state.addNode(oldNode1) - state.addNode(oldNode2) + state.updateNode(oldNode1) + state.updateNode(oldNode2) farmerbot.state = state mockRMBAndSubstrateCalls(ctx, sub, rmb, inputs, false, true, resources, []string{}, false, false) @@ -101,8 +101,8 @@ func TestFarmerbot(t *testing.T) { t.Run("test iterateOnNodes: update nodes (periodic wake up: failed to set off node)", func(t *testing.T) { oldNode1.powerState = off oldNode2.powerState = off - state.addNode(oldNode1) - state.addNode(oldNode2) + state.updateNode(oldNode1) + state.updateNode(oldNode2) farmerbot.state = state mockRMBAndSubstrateCalls(ctx, sub, rmb, inputs, false, true, resources, []string{}, false, false) diff --git a/farmerbot/internal/find_node_test.go b/farmerbot/internal/find_node_test.go index 2ff359489..b4fa99055 100644 --- a/farmerbot/internal/find_node_test.go +++ b/farmerbot/internal/find_node_test.go @@ -45,15 +45,15 @@ func TestFindNode(t *testing.T) { assert.NoError(t, err) farmerbot.state = state - node := farmerbot.nodes[1] + node := farmerbot.nodes[0] node.dedicated = false - farmerbot.nodes[1] = node - node2 := farmerbot.nodes[2] + farmerbot.nodes[0] = node + node2 := farmerbot.nodes[1] node2.dedicated = false - farmerbot.nodes[2] = node2 + farmerbot.nodes[1] = node2 - oldNode1 := farmerbot.nodes[1] - oldNode2 := farmerbot.nodes[2] + oldNode1 := farmerbot.nodes[0] + oldNode2 := farmerbot.nodes[1] oldFarm := farmerbot.farm nodeOptions := NodeFilterOption{ @@ -67,65 +67,71 @@ func TestFindNode(t *testing.T) { t.Run("test valid find node: found an ON node", func(t *testing.T) { nodeID, err := farmerbot.findNode(sub, nodeOptions) assert.NoError(t, err) - assert.Contains(t, farmerbot.nodes, nodeID) - farmerbot.addNode(oldNode1) - farmerbot.addNode(oldNode2) + _, node, err := farmerbot.getNode(nodeID) + assert.NoError(t, err) + assert.Contains(t, farmerbot.nodes, node) + + assert.NoError(t, farmerbot.updateNode(oldNode1)) + assert.NoError(t, farmerbot.updateNode(oldNode2)) farmerbot.farm = oldFarm }) t.Run("test valid find node: found an ON node, trying to power off fails because resources is claimed", func(t *testing.T) { nodeID, err := farmerbot.findNode(sub, nodeOptions) assert.NoError(t, err) - assert.Contains(t, farmerbot.nodes, nodeID) + + _, node, err := farmerbot.getNode(nodeID) + assert.NoError(t, err) + assert.Contains(t, farmerbot.nodes, node) err = farmerbot.powerOff(sub, nodeID) assert.Error(t, err) - farmerbot.addNode(oldNode1) - farmerbot.addNode(oldNode2) + assert.NoError(t, farmerbot.updateNode(oldNode1)) + assert.NoError(t, farmerbot.updateNode(oldNode2)) farmerbot.farm = oldFarm }) t.Run("test valid find node: found an ON node (first is OFF)", func(t *testing.T) { - node := farmerbot.nodes[1] + node := farmerbot.nodes[0] node.powerState = off - farmerbot.nodes[1] = node + farmerbot.nodes[0] = node nodeID, err := farmerbot.findNode(sub, nodeOptions) assert.NoError(t, err) - assert.Equal(t, nodeID, uint32(farmerbot.nodes[2].ID)) + assert.Equal(t, nodeID, uint32(farmerbot.nodes[1].ID)) - farmerbot.addNode(oldNode1) - farmerbot.addNode(oldNode2) + assert.NoError(t, farmerbot.updateNode(oldNode1)) + assert.NoError(t, farmerbot.updateNode(oldNode2)) farmerbot.farm = oldFarm }) t.Run("test valid find node: node is rented (second node is found)", func(t *testing.T) { - node := farmerbot.nodes[1] + node := farmerbot.nodes[0] node.hasActiveRentContract = true - farmerbot.nodes[1] = node + farmerbot.nodes[0] = node nodeID, err := farmerbot.findNode(sub, nodeOptions) assert.NoError(t, err) assert.Contains(t, farmerbot.config.IncludedNodes, nodeID) - farmerbot.addNode(oldNode1) - farmerbot.addNode(oldNode2) + assert.NoError(t, farmerbot.updateNode(oldNode1)) + assert.NoError(t, farmerbot.updateNode(oldNode2)) farmerbot.farm = oldFarm }) t.Run("test valid find node: node is dedicated so node is found", func(t *testing.T) { - node := farmerbot.nodes[1] + node := farmerbot.nodes[0] node.dedicated = true - farmerbot.nodes[1] = node + farmerbot.nodes[0] = node nodeID, err := farmerbot.findNode(sub, nodeOptions) assert.NoError(t, err) assert.Contains(t, farmerbot.config.IncludedNodes, nodeID) - farmerbot.addNode(oldNode1) - farmerbot.addNode(oldNode2) + assert.NoError(t, farmerbot.updateNode(oldNode1)) + assert.NoError(t, farmerbot.updateNode(oldNode2)) farmerbot.farm = oldFarm }) @@ -134,26 +140,26 @@ func TestFindNode(t *testing.T) { assert.NoError(t, err) assert.Contains(t, farmerbot.config.IncludedNodes, nodeID) - farmerbot.addNode(oldNode1) - farmerbot.addNode(oldNode2) + assert.NoError(t, farmerbot.updateNode(oldNode1)) + assert.NoError(t, farmerbot.updateNode(oldNode2)) farmerbot.farm = oldFarm }) t.Run("test valid find node: no gpus with specified device/vendor in first node (second is found)", func(t *testing.T) { - node2 := farmerbot.nodes[2] + node2 := farmerbot.nodes[1] node2.gpus = []zos.GPU{ { Device: "device", Vendor: "vendor", }, } - farmerbot.nodes[2] = node2 + farmerbot.nodes[1] = node2 nodeID, err := farmerbot.findNode(sub, NodeFilterOption{GPUVendors: []string{"vendor"}, GPUDevices: []string{"device"}}) assert.NoError(t, err) - assert.Equal(t, nodeID, uint32(farmerbot.nodes[2].ID)) + assert.Equal(t, nodeID, uint32(farmerbot.nodes[1].ID)) - farmerbot.addNode(oldNode2) + assert.NoError(t, farmerbot.updateNode(oldNode2)) }) t.Run("test invalid find node: no gpus in nodes", func(t *testing.T) { @@ -162,20 +168,20 @@ func TestFindNode(t *testing.T) { }) t.Run("test invalid find node: found an OFF node but change power failed", func(t *testing.T) { - node := farmerbot.nodes[1] + node := farmerbot.nodes[0] node.powerState = off - node2 := farmerbot.nodes[2] + node2 := farmerbot.nodes[1] node2.powerState = off - farmerbot.nodes[1] = node - farmerbot.nodes[2] = node2 + farmerbot.nodes[0] = node + farmerbot.nodes[1] = node2 sub.EXPECT().SetNodePowerTarget(farmerbot.identity, gomock.Any(), true).Return(types.Hash{}, fmt.Errorf("error")) _, err := farmerbot.findNode(sub, nodeOptions) assert.Error(t, err) - farmerbot.addNode(oldNode1) - farmerbot.addNode(oldNode2) + assert.NoError(t, farmerbot.updateNode(oldNode1)) + assert.NoError(t, farmerbot.updateNode(oldNode2)) }) t.Run("test invalid find node: no enough public ips", func(t *testing.T) { @@ -203,38 +209,38 @@ func TestFindNode(t *testing.T) { }) t.Run("test valid find node: nodes are dedicated and used, no nodes found", func(t *testing.T) { - node := farmerbot.nodes[1] + node := farmerbot.nodes[0] node.dedicated = true - farmerbot.nodes[1] = node - node2 := farmerbot.nodes[2] + farmerbot.nodes[0] = node + node2 := farmerbot.nodes[1] node2.dedicated = true - farmerbot.nodes[2] = node2 + farmerbot.nodes[1] = node2 _, err := farmerbot.findNode(sub, NodeFilterOption{}) assert.Error(t, err) - farmerbot.addNode(oldNode1) - farmerbot.addNode(oldNode2) + assert.NoError(t, farmerbot.updateNode(oldNode1)) + assert.NoError(t, farmerbot.updateNode(oldNode2)) farmerbot.farm = oldFarm }) t.Run("test invalid find node: node is excluded", func(t *testing.T) { - _, err := farmerbot.findNode(sub, NodeFilterOption{NodesExcluded: []uint32{uint32(farmerbot.nodes[1].ID), uint32(farmerbot.nodes[2].ID)}}) + _, err := farmerbot.findNode(sub, NodeFilterOption{NodesExcluded: []uint32{uint32(farmerbot.nodes[0].ID), uint32(farmerbot.nodes[1].ID)}}) assert.Error(t, err) }) t.Run("test invalid find node: node cannot claim resources", func(t *testing.T) { - node := farmerbot.nodes[1] + node := farmerbot.nodes[0] node.resources.total = capacity{} - node2 := farmerbot.nodes[2] + node2 := farmerbot.nodes[1] node2.resources.total = capacity{} - farmerbot.nodes[1] = node - farmerbot.nodes[2] = node2 + farmerbot.nodes[0] = node + farmerbot.nodes[1] = node2 _, err := farmerbot.findNode(sub, nodeOptions) assert.Error(t, err) - farmerbot.addNode(oldNode1) - farmerbot.addNode(oldNode2) + assert.NoError(t, farmerbot.updateNode(oldNode1)) + assert.NoError(t, farmerbot.updateNode(oldNode2)) }) } diff --git a/farmerbot/internal/power_test.go b/farmerbot/internal/power_test.go index b5b8172cd..a8b41f45c 100644 --- a/farmerbot/internal/power_test.go +++ b/farmerbot/internal/power_test.go @@ -28,23 +28,31 @@ func TestPowerLargeScale(t *testing.T) { IncludedNodes: []uint32{1, 2, 3, 4, 5, 6, 7}, } - farmerbot, err := NewFarmerBot(ctx, inputs, "dev", aliceSeed, peer.KeyTypeSr25519) - assert.Error(t, err) - // mock state resources := gridtypes.Capacity{HRU: 1, SRU: 1, CRU: 1, MRU: 1} mockRMBAndSubstrateCalls(ctx, sub, rmb, inputs, true, false, resources, []string{}, false, false) state, err := newState(ctx, sub, rmb, inputs, farmTwinID) assert.NoError(t, err) - farmerbot.state = state + + farmerbot := FarmerBot{ + state: state, + substrateManager: nil, + gridProxyClient: nil, + rmbNodeClient: rmb, + network: "dev", + mnemonicOrSeed: aliceSeed, + keyType: peer.KeyTypeSr25519, + identity: nil, + twinID: 0, + } t.Run("test valid power off: all nodes will be off except one node", func(t *testing.T) { - for i := range farmerbot.nodes { - if i == 1 { + for _, node := range farmerbot.nodes { + if node.ID == 7 { continue } - sub.EXPECT().SetNodePowerTarget(farmerbot.identity, gomock.Any(), false).Return(types.Hash{}, nil) + sub.EXPECT().SetNodePowerTarget(farmerbot.identity, uint32(node.ID), false).Return(types.Hash{}, nil) } err = farmerbot.manageNodesPower(sub) @@ -55,16 +63,16 @@ func TestPowerLargeScale(t *testing.T) { }) t.Run("test valid power off (all are off except 2): all nodes will be off except one node", func(t *testing.T) { - for i, node := range farmerbot.nodes { + for _, node := range farmerbot.nodes { node.lastTimePowerStateChanged = time.Now().Add(-periodicWakeUpDuration - time.Minute) - if i == 1 || i == 2 { + if node.ID == 1 || node.ID == 2 { node.powerState = on - farmerbot.addNode(node) + assert.NoError(t, state.updateNode(node)) continue } node.powerState = off - farmerbot.addNode(node) + assert.NoError(t, state.updateNode(node)) } sub.EXPECT().SetNodePowerTarget(farmerbot.identity, gomock.Any(), false).Return(types.Hash{}, nil) @@ -108,36 +116,36 @@ func TestPower(t *testing.T) { assert.NoError(t, err) farmerbot.state = state - oldNode1 := farmerbot.nodes[1] - oldNode2 := farmerbot.nodes[2] + oldNode1 := farmerbot.nodes[0] + oldNode2 := farmerbot.nodes[1] t.Run("test valid power on: already on", func(t *testing.T) { - err = farmerbot.powerOn(sub, uint32(state.nodes[1].ID)) + err = farmerbot.powerOn(sub, uint32(state.nodes[0].ID)) assert.NoError(t, err) }) t.Run("test valid power on: already waking up", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.powerState = wakingUp - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) - err = farmerbot.powerOn(sub, uint32(state.nodes[1].ID)) + err = farmerbot.powerOn(sub, uint32(state.nodes[0].ID)) assert.NoError(t, err) - state.addNode(oldNode1) + assert.NoError(t, state.updateNode(oldNode1)) }) t.Run("test valid power on", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.powerState = off - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) - sub.EXPECT().SetNodePowerTarget(farmerbot.identity, uint32(state.nodes[1].ID), true).Return(types.Hash{}, nil) + sub.EXPECT().SetNodePowerTarget(farmerbot.identity, uint32(state.nodes[0].ID), true).Return(types.Hash{}, nil) - err = farmerbot.powerOn(sub, uint32(state.nodes[1].ID)) + err = farmerbot.powerOn(sub, uint32(state.nodes[0].ID)) assert.NoError(t, err) - state.addNode(oldNode1) + assert.NoError(t, state.updateNode(oldNode1)) }) t.Run("test invalid power on: node not found", func(t *testing.T) { @@ -146,125 +154,125 @@ func TestPower(t *testing.T) { }) t.Run("test invalid power on: set node failed", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.powerState = off - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) - sub.EXPECT().SetNodePowerTarget(farmerbot.identity, uint32(state.nodes[1].ID), true).Return(types.Hash{}, errors.New("error")) + sub.EXPECT().SetNodePowerTarget(farmerbot.identity, uint32(state.nodes[0].ID), true).Return(types.Hash{}, errors.New("error")) - err = farmerbot.powerOn(sub, uint32(state.nodes[1].ID)) + err = farmerbot.powerOn(sub, uint32(state.nodes[0].ID)) assert.Error(t, err) - state.addNode(oldNode1) + assert.NoError(t, state.updateNode(oldNode1)) }) t.Run("test valid power off: already off", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.powerState = off - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) - err = farmerbot.powerOff(sub, uint32(state.nodes[1].ID)) + err = farmerbot.powerOff(sub, uint32(state.nodes[0].ID)) assert.NoError(t, err) - state.addNode(oldNode1) + assert.NoError(t, state.updateNode(oldNode1)) }) t.Run("test valid power off: already shutting down", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.powerState = shuttingDown - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) - err = farmerbot.powerOff(sub, uint32(state.nodes[1].ID)) + err = farmerbot.powerOff(sub, uint32(state.nodes[0].ID)) assert.NoError(t, err) - state.addNode(oldNode1) + assert.NoError(t, state.updateNode(oldNode1)) }) t.Run("test valid power off", func(t *testing.T) { - sub.EXPECT().SetNodePowerTarget(farmerbot.identity, uint32(state.nodes[1].ID), false).Return(types.Hash{}, nil) + sub.EXPECT().SetNodePowerTarget(farmerbot.identity, uint32(state.nodes[0].ID), false).Return(types.Hash{}, nil) - err = farmerbot.powerOff(sub, uint32(state.nodes[1].ID)) + err = farmerbot.powerOff(sub, uint32(state.nodes[0].ID)) assert.NoError(t, err) - state.addNode(oldNode1) + assert.NoError(t, state.updateNode(oldNode1)) }) t.Run("test invalid power off: one node is on and cannot be off", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.powerState = off - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) - err = farmerbot.powerOff(sub, uint32(state.nodes[2].ID)) + err = farmerbot.powerOff(sub, uint32(state.nodes[1].ID)) assert.Error(t, err) - state.addNode(oldNode1) - state.addNode(oldNode2) + assert.NoError(t, state.updateNode(oldNode1)) + assert.NoError(t, state.updateNode(oldNode2)) }) t.Run("test invalid power off: node is set to never shutdown", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.neverShutDown = true - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) - err = farmerbot.powerOff(sub, uint32(state.nodes[1].ID)) + err = farmerbot.powerOff(sub, uint32(state.nodes[0].ID)) assert.Error(t, err) - state.addNode(oldNode1) + assert.NoError(t, state.updateNode(oldNode1)) }) t.Run("test invalid power off: node has public config", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.PublicConfig.HasValue = true - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) - err = farmerbot.powerOff(sub, uint32(state.nodes[1].ID)) + err = farmerbot.powerOff(sub, uint32(state.nodes[0].ID)) assert.Error(t, err) - state.addNode(oldNode1) + assert.NoError(t, state.updateNode(oldNode1)) }) t.Run("test invalid power off: node has rent contract", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.hasActiveRentContract = true - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) - err = farmerbot.powerOff(sub, uint32(state.nodes[1].ID)) + err = farmerbot.powerOff(sub, uint32(state.nodes[0].ID)) assert.Error(t, err) - state.addNode(oldNode1) + assert.NoError(t, state.updateNode(oldNode1)) }) t.Run("test invalid power off: node has active contracts", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.hasActiveContracts = true - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) - err = farmerbot.powerOff(sub, uint32(state.nodes[1].ID)) + err = farmerbot.powerOff(sub, uint32(state.nodes[0].ID)) assert.Error(t, err) - state.addNode(oldNode1) + assert.NoError(t, state.updateNode(oldNode1)) }) t.Run("test invalid power off: node power changed", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.lastTimePowerStateChanged = time.Now() - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) - err = farmerbot.powerOff(sub, uint32(state.nodes[1].ID)) + err = farmerbot.powerOff(sub, uint32(state.nodes[0].ID)) assert.Error(t, err) - state.addNode(oldNode1) + assert.NoError(t, state.updateNode(oldNode1)) }) t.Run("test invalid power off: node has used resources", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.resources.used = testNode.resources.total - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) - err = farmerbot.powerOff(sub, uint32(state.nodes[1].ID)) + err = farmerbot.powerOff(sub, uint32(state.nodes[0].ID)) assert.Error(t, err) - state.addNode(oldNode1) + assert.NoError(t, state.updateNode(oldNode1)) }) t.Run("test invalid power off: node not found", func(t *testing.T) { @@ -273,35 +281,35 @@ func TestPower(t *testing.T) { }) t.Run("test invalid power off: set node power failed", func(t *testing.T) { - sub.EXPECT().SetNodePowerTarget(farmerbot.identity, uint32(state.nodes[1].ID), false).Return(types.Hash{}, errors.New("error")) + sub.EXPECT().SetNodePowerTarget(farmerbot.identity, uint32(state.nodes[0].ID), false).Return(types.Hash{}, errors.New("error")) sub.EXPECT().GetPowerTarget(gomock.Any()).Return(substrate.NodePower{}, nil) - err = farmerbot.powerOff(sub, uint32(state.nodes[1].ID)) + err = farmerbot.powerOff(sub, uint32(state.nodes[0].ID)) assert.Error(t, err) - assert.Equal(t, state.nodes[1].powerState, on) - state.addNode(oldNode1) + assert.Equal(t, state.nodes[0].powerState, on) + assert.NoError(t, state.updateNode(oldNode1)) }) t.Run("test invalid power off: set and get node power failed", func(t *testing.T) { - sub.EXPECT().SetNodePowerTarget(farmerbot.identity, uint32(state.nodes[1].ID), false).Return(types.Hash{}, errors.New("error")) + sub.EXPECT().SetNodePowerTarget(farmerbot.identity, uint32(state.nodes[0].ID), false).Return(types.Hash{}, errors.New("error")) sub.EXPECT().GetPowerTarget(gomock.Any()).Return(substrate.NodePower{}, errors.New("error")) - err = farmerbot.powerOff(sub, uint32(state.nodes[1].ID)) + err = farmerbot.powerOff(sub, uint32(state.nodes[0].ID)) assert.Error(t, err) - assert.Equal(t, state.nodes[1].powerState, on) - state.addNode(oldNode1) + assert.Equal(t, state.nodes[0].powerState, on) + assert.NoError(t, state.updateNode(oldNode1)) }) t.Run("test invalid power off: set node power failed but target is changed", func(t *testing.T) { - sub.EXPECT().SetNodePowerTarget(farmerbot.identity, uint32(state.nodes[1].ID), false).Return(types.Hash{}, errors.New("error")) - sub.EXPECT().GetPowerTarget(uint32(state.nodes[1].ID)).Return(substrate.NodePower{ + sub.EXPECT().SetNodePowerTarget(farmerbot.identity, uint32(state.nodes[0].ID), false).Return(types.Hash{}, errors.New("error")) + sub.EXPECT().GetPowerTarget(uint32(state.nodes[0].ID)).Return(substrate.NodePower{ Target: substrate.Power{IsDown: true}, }, nil) - err = farmerbot.powerOff(sub, uint32(state.nodes[1].ID)) + err = farmerbot.powerOff(sub, uint32(state.nodes[0].ID)) assert.Error(t, err) - assert.Equal(t, state.nodes[1].powerState, shuttingDown) - state.addNode(oldNode1) + assert.Equal(t, state.nodes[0].powerState, shuttingDown) + assert.NoError(t, state.updateNode(oldNode1)) }) t.Run("test power management: a node to shutdown (failed set the first node)", func(t *testing.T) { @@ -312,8 +320,8 @@ func TestPower(t *testing.T) { err = farmerbot.manageNodesPower(sub) assert.NoError(t, err) - state.addNode(oldNode1) - state.addNode(oldNode2) + assert.NoError(t, state.updateNode(oldNode1)) + assert.NoError(t, state.updateNode(oldNode2)) }) t.Run("test power management: a node to shutdown (failed set the first node but power target changes)", func(t *testing.T) { @@ -325,121 +333,121 @@ func TestPower(t *testing.T) { err = farmerbot.manageNodesPower(sub) assert.NoError(t, err) - state.addNode(oldNode1) - state.addNode(oldNode2) + assert.NoError(t, state.updateNode(oldNode1)) + assert.NoError(t, state.updateNode(oldNode2)) }) t.Run("test power management: nothing to shut down", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.powerState = off - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) err = farmerbot.manageNodesPower(sub) assert.NoError(t, err) - state.addNode(oldNode1) + assert.NoError(t, state.updateNode(oldNode1)) }) t.Run("test power management: cannot shutdown public config", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.PublicConfig.HasValue = true - state.addNode(testNode) - testNode = state.nodes[2] + assert.NoError(t, state.updateNode(testNode)) + testNode = state.nodes[1] testNode.PublicConfig.HasValue = true - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) err = farmerbot.manageNodesPower(sub) assert.NoError(t, err) - state.addNode(oldNode1) - state.addNode(oldNode2) + assert.NoError(t, state.updateNode(oldNode1)) + assert.NoError(t, state.updateNode(oldNode2)) }) t.Run("test power management: node is waking up", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.powerState = wakingUp - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) err = farmerbot.manageNodesPower(sub) assert.NoError(t, err) - state.addNode(oldNode1) + assert.NoError(t, state.updateNode(oldNode1)) }) t.Run("test power management: a node to wake up (node 1 is used and node 2 is off)", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.resources.used = testNode.resources.total - state.addNode(testNode) - testNode = state.nodes[2] + assert.NoError(t, state.updateNode(testNode)) + testNode = state.nodes[1] testNode.powerState = off - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) - sub.EXPECT().SetNodePowerTarget(farmerbot.identity, uint32(state.nodes[2].ID), true).Return(types.Hash{}, nil) + sub.EXPECT().SetNodePowerTarget(farmerbot.identity, uint32(state.nodes[1].ID), true).Return(types.Hash{}, nil) err = farmerbot.manageNodesPower(sub) assert.NoError(t, err) - state.addNode(oldNode1) - state.addNode(oldNode2) + assert.NoError(t, state.updateNode(oldNode1)) + assert.NoError(t, state.updateNode(oldNode2)) }) t.Run("test power management: a node to wake up (node 1 has rent contract)", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.hasActiveRentContract = true - state.addNode(testNode) - testNode = state.nodes[2] + assert.NoError(t, state.updateNode(testNode)) + testNode = state.nodes[1] testNode.powerState = off - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) - sub.EXPECT().SetNodePowerTarget(farmerbot.identity, uint32(state.nodes[2].ID), true).Return(types.Hash{}, nil) + sub.EXPECT().SetNodePowerTarget(farmerbot.identity, uint32(state.nodes[1].ID), true).Return(types.Hash{}, nil) err = farmerbot.manageNodesPower(sub) assert.NoError(t, err) - state.addNode(oldNode1) - state.addNode(oldNode2) + assert.NoError(t, state.updateNode(oldNode1)) + assert.NoError(t, state.updateNode(oldNode2)) }) t.Run("test invalid power management: no nodes to wake up (usage is high)", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.resources.used = testNode.resources.total - state.addNode(testNode) - testNode = state.nodes[2] + assert.NoError(t, state.updateNode(testNode)) + testNode = state.nodes[1] testNode.resources.used = testNode.resources.total - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) err = farmerbot.manageNodesPower(sub) assert.Error(t, err) - state.addNode(oldNode1) - state.addNode(oldNode2) + assert.NoError(t, state.updateNode(oldNode1)) + assert.NoError(t, state.updateNode(oldNode2)) }) t.Run("test valid power management: second node has no resources (usage is low)", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.resources.used = testNode.resources.total testNode.resources.used.cru = 0 - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) err = farmerbot.manageNodesPower(sub) assert.NoError(t, err) - state.addNode(oldNode1) - state.addNode(oldNode2) + assert.NoError(t, state.updateNode(oldNode1)) + assert.NoError(t, state.updateNode(oldNode2)) }) t.Run("test power management: total resources is 0 (nothing happens)", func(t *testing.T) { - testNode := state.nodes[1] + testNode := state.nodes[0] testNode.resources.total = capacity{} - state.addNode(testNode) - testNode = state.nodes[2] + assert.NoError(t, state.updateNode(testNode)) + testNode = state.nodes[1] testNode.resources.total = capacity{} - state.addNode(testNode) + assert.NoError(t, state.updateNode(testNode)) err = farmerbot.manageNodesPower(sub) assert.NoError(t, err) - state.addNode(oldNode1) - state.addNode(oldNode2) + assert.NoError(t, state.updateNode(oldNode1)) + assert.NoError(t, state.updateNode(oldNode2)) }) } diff --git a/farmerbot/internal/state_test.go b/farmerbot/internal/state_test.go index 02adc0d5f..5caac0502 100644 --- a/farmerbot/internal/state_test.go +++ b/farmerbot/internal/state_test.go @@ -136,10 +136,10 @@ func TestSetConfig(t *testing.T) { state, err := newState(ctx, sub, rmb, inputs, farmTwinID) assert.NoError(t, err) assert.Equal(t, uint32(state.farm.ID), uint32(1)) + assert.True(t, state.nodes[0].dedicated) assert.True(t, state.nodes[1].dedicated) - assert.True(t, state.nodes[2].dedicated) - assert.Equal(t, uint32(state.nodes[1].ID), uint32(1)) - assert.Equal(t, uint32(state.nodes[2].ID), uint32(2)) + assert.Equal(t, uint32(state.nodes[0].ID), uint32(1)) + assert.Equal(t, uint32(state.nodes[1].ID), uint32(2)) now := time.Now() assert.Equal(t, state.config.Power.PeriodicWakeUpStart.PeriodicWakeUpTime().Hour(), now.Hour()) @@ -241,7 +241,7 @@ func TestSetConfig(t *testing.T) { state, err := newState(ctx, sub, rmb, inputs, farmTwinID) assert.NoError(t, err) - assert.False(t, state.nodes[1].hasActiveRentContract) + assert.False(t, state.nodes[0].hasActiveRentContract) }) t.Run("test valid excluded node", func(t *testing.T) { @@ -317,9 +317,9 @@ func TestSetConfig(t *testing.T) { func TestStateModel(t *testing.T) { state := state{ - nodes: map[uint32]node{1: { + nodes: []node{{ Node: substrate.Node{ID: 1}, - }, 2: { + }, { Node: substrate.Node{ID: 2}, }}, } @@ -327,7 +327,7 @@ func TestStateModel(t *testing.T) { t.Run("test update node", func(t *testing.T) { err := state.updateNode(node{Node: substrate.Node{ID: 1, TwinID: 1}}) assert.NoError(t, err) - assert.Equal(t, uint32(state.nodes[1].TwinID), uint32(1)) + assert.Equal(t, uint32(state.nodes[0].TwinID), uint32(1)) }) t.Run("test update node (not found)", func(t *testing.T) { @@ -351,11 +351,12 @@ func TestStateModel(t *testing.T) { }) t.Run("test update node", func(t *testing.T) { - state.addNode(node{Node: substrate.Node{ID: 1, TwinID: 1}}) - assert.Equal(t, uint32(state.nodes[1].TwinID), uint32(1)) + state.updateNode(node{Node: substrate.Node{ID: 1, TwinID: 1}}) + assert.Equal(t, uint32(state.nodes[0].TwinID), uint32(1)) state.deleteNode(1) - assert.Empty(t, state.nodes[1]) + _, _, err := state.getNode(1) + assert.Error(t, err) }) }