Skip to content

Commit 23f4f69

Browse files
authored
Add configured nodes count (PR #8, fixes issue #5)
* Add count of configured nodes to response * Test new number of configured nodes response property * Add custom docker build script for a feature branch
1 parent 4dc06d6 commit 23f4f69

File tree

4 files changed

+131
-8
lines changed

4 files changed

+131
-8
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Deploy dev images to GHCR
2+
3+
on:
4+
push:
5+
branches:
6+
- 'feature/configured-nodes-count'
7+
8+
jobs:
9+
push-store-image:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: 'Checkout GitHub Action'
13+
uses: actions/checkout@main
14+
15+
- name: 'Login to GitHub Container Registry'
16+
uses: docker/login-action@v1
17+
with:
18+
registry: ghcr.io
19+
username: ${{github.actor}}
20+
password: ${{secrets.GITHUB_TOKEN}}
21+
22+
- name: 'Build Inventory Image'
23+
run: |
24+
docker build . --tag ghcr.io/qubic/qubic-nodes:feature-configured-nodes
25+
docker push ghcr.io/qubic/qubic-nodes:feature-configured-nodes

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ require (
66
github.com/ardanlabs/conf v1.5.0
77
github.com/pkg/errors v0.9.1
88
github.com/qubic/go-node-connector v0.7.0
9+
github.com/stretchr/testify v1.2.2
910
)
1011

1112
require (
1213
github.com/cloudflare/circl v1.3.7 // indirect
14+
github.com/davecgh/go-spew v1.1.1 // indirect
1315
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
16+
github.com/pmezard/go-difflib v1.0.0 // indirect
1417
github.com/silenceper/pool v1.0.0 // indirect
1518
github.com/sirupsen/logrus v1.4.2 // indirect
1619
golang.org/x/sys v0.15.0 // indirect

web/handler.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ type RequestHandler struct {
1414
}
1515

1616
type statusResponse struct {
17-
MaxTick uint32 `json:"max_tick"`
18-
LastUpdate int64 `json:"last_update"`
19-
ReliableNodes []nodeResponse `json:"reliable_nodes"`
20-
MostReliableNode nodeResponse `json:"most_reliable_node"`
17+
MaxTick uint32 `json:"max_tick"`
18+
LastUpdate int64 `json:"last_update"`
19+
NumberOfConfiguredNodes int `json:"number_of_configured_nodes"`
20+
ReliableNodes []nodeResponse `json:"reliable_nodes"`
21+
MostReliableNode nodeResponse `json:"most_reliable_node"`
2122
}
2223

2324
type nodeResponse struct {
@@ -68,10 +69,11 @@ func (h *RequestHandler) HandleStatus(writer http.ResponseWriter, _ *http.Reques
6869
}
6970

7071
response := statusResponse{
71-
MaxTick: containerResponse.MaxTick,
72-
LastUpdate: containerResponse.LastUpdate,
73-
ReliableNodes: reliableNodes,
74-
MostReliableNode: mostReliableResponse,
72+
MaxTick: containerResponse.MaxTick,
73+
LastUpdate: containerResponse.LastUpdate,
74+
NumberOfConfiguredNodes: len(h.Container.Addresses),
75+
ReliableNodes: reliableNodes,
76+
MostReliableNode: mostReliableResponse,
7577
}
7678

7779
data, err := json.Marshal(response)

web/handler_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package web
2+
3+
import (
4+
"github.com/qubic/go-qubic-nodes/node"
5+
"github.com/stretchr/testify/assert"
6+
"io"
7+
"net/http"
8+
"net/http/httptest"
9+
"testing"
10+
)
11+
12+
func TestHandler_whenStatus_thenReturnNumberOfConfiguredNodes(t *testing.T) {
13+
14+
var node1 = node.Node{
15+
Address: "1.2.3.4",
16+
Port: "12345",
17+
Peers: []string{"2.3.4.5", "3.4.5.6"},
18+
LastTick: 123,
19+
LastUpdate: 1500000000,
20+
LastUpdateSuccess: true,
21+
}
22+
23+
var node2 = node.Node{
24+
Address: "2.3.4.5",
25+
Port: "12345",
26+
Peers: []string{"3.4.5.6", "1.2.3.4"},
27+
LastTick: 122,
28+
LastUpdate: 1500000000,
29+
LastUpdateSuccess: true,
30+
}
31+
32+
var container = node.Container{
33+
34+
Addresses: []string{node1.Address, node2.Address},
35+
Port: "12345",
36+
TickErrorThreshold: 3,
37+
ReliableTickRange: 4,
38+
OnlineNodes: nil,
39+
MaxTick: 123,
40+
LastUpdate: 1500000000,
41+
ReliableNodes: []*node.Node{&node1},
42+
MostReliableNode: &node1,
43+
}
44+
45+
handler := RequestHandler{
46+
Container: &container,
47+
}
48+
49+
expectedResponse := `{
50+
"max_tick": 123,
51+
"last_update": 1500000000,
52+
"number_of_configured_nodes": 2,
53+
"reliable_nodes": [
54+
{
55+
"address": "1.2.3.4",
56+
"port": "12345",
57+
"peers": [
58+
"2.3.4.5",
59+
"3.4.5.6"
60+
],
61+
"last_tick": 123,
62+
"last_update": 1500000000
63+
}
64+
],
65+
"most_reliable_node": {
66+
"address": "1.2.3.4",
67+
"port": "12345",
68+
"peers": [
69+
"2.3.4.5",
70+
"3.4.5.6"
71+
],
72+
"last_tick": 123,
73+
"last_update": 1500000000
74+
}
75+
}`
76+
77+
resp := makeStatusCall(handler)
78+
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"), "Unexpected content type header")
79+
assert.Equal(t, 200, resp.StatusCode, "Unexpected http status")
80+
data, err := io.ReadAll(resp.Body)
81+
assert.NoError(t, err)
82+
assert.JSONEq(t, expectedResponse, string(data))
83+
}
84+
85+
func makeStatusCall(handler RequestHandler) *http.Response {
86+
rec := httptest.NewRecorder()
87+
handler.HandleStatus(rec, nil)
88+
resp := rec.Result()
89+
defer func(Body io.ReadCloser) {
90+
_ = Body.Close()
91+
}(resp.Body)
92+
return resp
93+
}

0 commit comments

Comments
 (0)