Skip to content

Commit

Permalink
Added support for the VirtualNode type
Browse files Browse the repository at this point in the history
- Switched to use hms-xname xnametypes for xname validation instead of
  the hms-base package
- Updated hms-base and hms-hmetcd versions

CASMHMS-6074
  • Loading branch information
shunr-hpe committed Sep 21, 2023
1 parent 58587a5 commit e1f04ce
Show file tree
Hide file tree
Showing 39 changed files with 871 additions and 876 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.19.1
1.20.0
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.20.0] - 2023-09-08

### Added

- Added support for the VirtualNode type

## [1.19.1] - 2023-01-11

### Changed
Expand Down
4 changes: 2 additions & 2 deletions cmd/hbtd/hbtd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// (C) Copyright [2018-2021] Hewlett Packard Enterprise Development LP
// (C) Copyright [2018-2021,2023] Hewlett Packard Enterprise Development LP
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -52,7 +52,7 @@ import (
"syscall"
"time"

"github.com/Cray-HPE/hms-base"
"github.com/Cray-HPE/hms-base/v2"
"github.com/Cray-HPE/hms-hmetcd"
"github.com/Cray-HPE/hms-msgbus"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/hbtd/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"net/http"
"time"

"github.com/Cray-HPE/hms-base"
"github.com/Cray-HPE/hms-base/v2"
)

// HealthResponse - used to report service health stats
Expand Down
12 changes: 7 additions & 5 deletions cmd/hbtd/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/Cray-HPE/hms-base"
"io/ioutil"
"net/http"
"os"
Expand All @@ -37,7 +36,10 @@ import (
"sync"
"time"

"github.com/Cray-HPE/hms-hmetcd"
base "github.com/Cray-HPE/hms-base/v2"
"github.com/Cray-HPE/hms-xname/xnametypes"

hmetcd "github.com/Cray-HPE/hms-hmetcd"
"github.com/gorilla/mux"
)

Expand Down Expand Up @@ -973,7 +975,7 @@ func hbRcv(w http.ResponseWriter, r *http.Request) {

//Check to be sure that certain fields' values are valid.

if base.GetHMSType(jdata.Component) == base.HMSTypeInvalid {
if xnametypes.GetHMSType(jdata.Component) == xnametypes.HMSTypeInvalid {
hbtdPrintf("Invalid XName in heartbeat JSON: %s\n", jdata.Component)
pdet := base.NewProblemDetails("about:blank",
"Invalid Request",
Expand Down Expand Up @@ -1015,7 +1017,7 @@ func hbRcvXName(w http.ResponseWriter, r *http.Request) {
if xname == "" {
//Should NOT happen, but if so, grab the end of the URL
toks := strings.Split(r.URL.Path, "/")
xn := base.NormalizeHMSCompID(toks[len(toks)-1])
xn := xnametypes.NormalizeHMSCompID(toks[len(toks)-1])
if xn == "" {
//Enforce valid XName
hbtdPrintf("ERROR: request is not a POST.\n")
Expand Down Expand Up @@ -1321,7 +1323,7 @@ func hbStateSingle(w http.ResponseWriter, r *http.Request) {
var rspSingle hbSingleStateRsp

vars := mux.Vars(r)
targ := base.NormalizeHMSCompID(vars["xname"])
targ := xnametypes.NormalizeHMSCompID(vars["xname"])
errinst := URL_HB_STATE + "/" + targ
now := time.Now().Unix()

Expand Down
148 changes: 76 additions & 72 deletions cmd/hbtd/track_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/Cray-HPE/hms-base"
"github.com/Cray-HPE/hms-hmetcd"
"github.com/gorilla/mux"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand All @@ -40,6 +37,10 @@ import (
"sync"
"testing"
"time"

base "github.com/Cray-HPE/hms-base/v2"
hmetcd "github.com/Cray-HPE/hms-hmetcd"
"github.com/gorilla/mux"
)

var SMRMutex sync.Mutex
Expand Down Expand Up @@ -517,6 +518,58 @@ func hb_cmp(t *testing.T, cmp string, ts string, status string) {
}
}

func heartbeatBody(xname, status, timestamp string) *bytes.Buffer {
body := `{"Component":"` + xname +
`","Hostname":"` + "nid0001.us.cray.com" +
`","NID":"` + "0001" +
`","Status":"` + status +
`","Timestamp":"` + timestamp + `"}`
return bytes.NewBufferString(body)
}

func postHeartbeat(t *testing.T, requestBody *bytes.Buffer, expectedReturnCode int) {
url := "http://localhost:8080/hmi/v1/heartbeat"
req, err := http.NewRequest("POST", url, requestBody)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(hbRcv)
handler.ServeHTTP(rr, req)

// Check the return code
if rr.Code != expectedReturnCode {
t.Errorf("Wrong http code: expected: %v, actual: %v, url: %s, requestBody: %v", expectedReturnCode, rr.Code, url, requestBody)
}
}

func heartbeatToXnameBody(status, timestamp string) *bytes.Buffer {
body := `{"Status":"` + status + `","Timestamp":"` + timestamp + `"}`
return bytes.NewBufferString(body)
}

func postHeartbeatToXname(t *testing.T, xname string, requestBody *bytes.Buffer, expectedReturnCode int) {
url := "http://localhost:8080/hmi/v1/heartbeat/" + xname
req, err := http.NewRequest("POST", url, requestBody)

if err != nil {
t.Fatal(err)
}

// Set up to grab the "responses"
rr := httptest.NewRecorder()
handlerXName := http.HandlerFunc(hbRcvXName)

// Mock up the second operation
handlerXName.ServeHTTP(rr, req)

// Check the return code
if rr.Code != expectedReturnCode {
t.Errorf("Wrong http code: expected: %v, actual: %v, url: %s, requestBody: %v", expectedReturnCode, rr.Code, url, requestBody)
}
}

// Test entry point for hb_rcv(), which is the HB HTTP request handler.
// We will fake out an HTTP request object and record the response.
// We will also examine the newly-created components in the HB tracking
Expand All @@ -535,51 +588,30 @@ func TestHb_rcv(t *testing.T) {
}

//Slop together JSON payloads for 2 heartbeating nodes

req1_hb := bytes.NewBufferString(`{"Component":"x1c2s2b0n3","Hostname":"nid0001.us.cray.com","NID":"0001","Status":"OK","Timestamp":"Jan 1, 0000"}`)
req2_hb := bytes.NewBufferString(`{"Status":"OK","Timestamp":"Jan 2, 1000"}`)

// Create 2 fake HTTP POSTs with the HB data in it
req1, err1 := http.NewRequest("POST", "http://localhost:8080/hmi/v1/heartbeat", req1_hb)

if err1 != nil {
t.Fatal(err1)
xnames := []string{"x1c2s2b0n3", "x1c2s2b0n3v4"}
timestamps := []string{"Jan 1, 0000", "Jan 2, 0000"}
for i := range xnames {
postHeartbeat(t,
heartbeatBody(xnames[i], "OK", timestamps[i]),
http.StatusOK)
}

req2, err2 := http.NewRequest("POST", "http://localhost:8080/hmi/v1/heartbeat/x2c3s4b0n5", req2_hb)

if err2 != nil {
t.Fatal(err2)
xnames2 := []string{"x1c2s2b0n4", "x1c2s2b0n4v5"}
timestamps2 := []string{"Jan 3, 0000", "Jan 4, 0000"}
for i := range xnames2 {
postHeartbeatToXname(t,
xnames2[i],
heartbeatToXnameBody("OK", timestamps2[i]),
http.StatusOK)
}

// Set up to grab the "responses"
rr := httptest.NewRecorder()
handler := http.HandlerFunc(hbRcv)
handlerXName := http.HandlerFunc(hbRcvXName)

// Mock up the first operation
handler.ServeHTTP(rr, req1)

// Check the return code
if rr.Code != http.StatusOK {
t.Errorf("HTTP handler returned bad error code, got %v, want %v",
rr.Code, http.StatusOK)
for i := range xnames {
hb_cmp(t, xnames[i], timestamps[i], "OK")
}

// Mock up the second operation
handlerXName.ServeHTTP(rr, req2)

// Check the return code
if rr.Code != http.StatusOK {
t.Errorf("HTTP handler returned bad error code, got %v, want %v",
rr.Code, http.StatusOK)
for i := range xnames2 {
hb_cmp(t, xnames2[i], timestamps2[i], "OK")
}

//Now check the hbmap to see if it has entries for both nodes

hb_cmp(t, "x1c2s2b0n3", "Jan 1, 0000", "OK")
hb_cmp(t, "x2c3s4b0n5", "Jan 2, 1000", "OK")

//Now check some error conditions. First, a non-POST request

req_e1, err_e1 := http.NewRequest("GET", "http://localhost:8080/hmi/v1/heartbeat", nil)
Expand All @@ -599,7 +631,7 @@ func TestHb_rcv(t *testing.T) {
}

//Next we'll give it JSON with an invalid data type

// the Component is an int instead of a string
req_e1_data := bytes.NewBufferString(`{"Component":1234,"Hostname":"nid0001.us.cray.com","NID":"0001","Status":"OK","Timestamp":"Jan 1, 0000"}`)
req_e1, err_e1 = http.NewRequest("POST", "http://localhost:8080/hmi/v1/heartbeat", req_e1_data)

Expand Down Expand Up @@ -636,21 +668,7 @@ func TestHb_rcv(t *testing.T) {

//Send a HB with an invalid component XName

req_e1_data = bytes.NewBufferString(`{"Component":"xxyyzz","Hostname":"nid0001.us.cray.com","NID":"0001","Status":"OK","Timestamp":"Jan 1, 0000"}`)
req_e1, err_e1 = http.NewRequest("POST", "http://localhost:8080/hmi/v1/heartbeat", req_e1_data)

if err_e1 != nil {
t.Fatal(err_e1)
}
rr_e1 = httptest.NewRecorder()
handler_e1 = http.HandlerFunc(hbRcv)
handler_e1.ServeHTTP(rr_e1, req_e1)

// Check the return code
if rr_e1.Code != http.StatusBadRequest {
t.Errorf("HTTP handler returned bad error code, got %v, want %v",
rr_e1.Code, http.StatusBadRequest)
}
postHeartbeat(t, heartbeatBody("xxyyzz", "OK", "Jan 1, 0000"), http.StatusBadRequest)

//Send a HB with a NID that's numerically invalid

Expand All @@ -672,21 +690,7 @@ func TestHb_rcv(t *testing.T) {

//Send a HB to an existing key

req_e1_data = bytes.NewBufferString(`{"Component":"x1c2s2b0n3","Hostname":"nid0001.us.cray.com","NID":"0001","Status":"OK","Timestamp":"Jan 1, 0000"}`)
req_e1, err_e1 = http.NewRequest("POST", "http://localhost:8080/hmi/v1/heartbeat", req_e1_data)

if err_e1 != nil {
t.Fatal(err_e1)
}
rr_e1 = httptest.NewRecorder()
handler_e1 = http.HandlerFunc(hbRcv)
handler_e1.ServeHTTP(rr_e1, req_e1)

// Check the return code
if rr_e1.Code != http.StatusOK {
t.Errorf("HTTP handler returned bad error code, got %v, want %v",
rr_e1.Code, http.StatusOK)
}
postHeartbeat(t, heartbeatBody(xnames[0], "OK", "Jan 1, 0000"), http.StatusOK)

time.Sleep(1 * time.Second)
t.Logf(" ==> FINISHED HEARTBEAT HTTP OPERATIONS TEST\n")
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ module github.com/Cray-HPE/hms-hbtd
go 1.16

require (
github.com/Cray-HPE/hms-base v1.15.0
github.com/Cray-HPE/hms-hmetcd v1.10.2
github.com/Cray-HPE/hms-base/v2 v2.0.1
github.com/Cray-HPE/hms-hmetcd v1.10.3
github.com/Cray-HPE/hms-msgbus v1.11.0
github.com/Cray-HPE/hms-xname v1.3.0
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.4.1 // indirect
)
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Cray-HPE/hms-base v1.15.0 h1:VaQj6cY9OAs6rcD1miE133eqQnRTkEcX1lnwJA/SUuU=
github.com/Cray-HPE/hms-base v1.15.0/go.mod h1:+G8KFLPtanLC5lQ602hrf3MDfLTmIXedTavVCOdz5XA=
github.com/Cray-HPE/hms-hmetcd v1.10.2 h1:gtCi9r3IB9OasjtJ/WgjjeJzCcUsQTEv7yYphSb8tTI=
github.com/Cray-HPE/hms-hmetcd v1.10.2/go.mod h1:cL/imOEh++ErJ1x30HEeo1a38gxBAJgviA8V4j8Wrus=
github.com/Cray-HPE/hms-base/v2 v2.0.1 h1:xHQWmoOlmA/UU0zDWjHxCK/qOF9WMLr7MrSpFAxLXWY=
github.com/Cray-HPE/hms-base/v2 v2.0.1/go.mod h1:Mq+Ao3q4YtNZJZ1ly9wnEIKyvc3+QaA1B/xpiUpyhzQ=
github.com/Cray-HPE/hms-hmetcd v1.10.3 h1:bn+MzRcHp1CzLcm/VScouwiLB46EiLR6FddCEyanhqw=
github.com/Cray-HPE/hms-hmetcd v1.10.3/go.mod h1:cL/imOEh++ErJ1x30HEeo1a38gxBAJgviA8V4j8Wrus=
github.com/Cray-HPE/hms-msgbus v1.11.0 h1:w5ThMJ8D5pmuvXhc817rJDG4TTixEfvtcDi3tR6eOUM=
github.com/Cray-HPE/hms-msgbus v1.11.0/go.mod h1:cxn+lUOq3tpY3+KdFml6L56ZQo8sqN2VoZ6gGxds6o8=
github.com/Cray-HPE/hms-xname v1.3.0 h1:DQmetMniubqcaL6Cxarz9+7KFfWGSEizIhfPHIgC3Gw=
github.com/Cray-HPE/hms-xname v1.3.0/go.mod h1:XKdjQSzoTps5KDOE8yWojBTAWASGaS6LfRrVDxwTQO8=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
1 change: 0 additions & 1 deletion vendor/github.com/Cray-HPE/hms-base/.version

This file was deleted.

45 changes: 0 additions & 45 deletions vendor/github.com/Cray-HPE/hms-base/Dockerfile

This file was deleted.

Loading

0 comments on commit e1f04ce

Please sign in to comment.