Skip to content

Commit

Permalink
fix tests to use slice of nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
rawdaGastan committed May 8, 2024
1 parent ff0493b commit 85f58fd
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 193 deletions.
12 changes: 6 additions & 6 deletions farmerbot/internal/farmerbot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -86,8 +86,8 @@ func TestFarmerbot(t *testing.T) {

oldNode1.powerState = off
oldNode2.powerState = off
state.addNode(oldNode1)
state.addNode(oldNode2)
state.updateNode(oldNode1)

Check failure on line 89 in farmerbot/internal/farmerbot_test.go

View workflow job for this annotation

GitHub Actions / Run Lint (farmerbot)

Error return value of `state.updateNode` is not checked (errcheck)
state.updateNode(oldNode2)

Check failure on line 90 in farmerbot/internal/farmerbot_test.go

View workflow job for this annotation

GitHub Actions / Run Lint (farmerbot)

Error return value of `state.updateNode` is not checked (errcheck)
farmerbot.state = state

mockRMBAndSubstrateCalls(ctx, sub, rmb, inputs, false, true, resources, []string{}, false, false)
Expand All @@ -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)

Check failure on line 104 in farmerbot/internal/farmerbot_test.go

View workflow job for this annotation

GitHub Actions / Run Lint (farmerbot)

Error return value of `state.updateNode` is not checked (errcheck)
state.updateNode(oldNode2)
farmerbot.state = state

mockRMBAndSubstrateCalls(ctx, sub, rmb, inputs, false, true, resources, []string{}, false, false)
Expand Down
106 changes: 56 additions & 50 deletions farmerbot/internal/find_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
})

Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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))
})
}
Loading

0 comments on commit 85f58fd

Please sign in to comment.