Skip to content

Commit

Permalink
restructured test files; cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
kqdeng committed Jun 22, 2020
1 parent 209f9dc commit ac55146
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 88 deletions.
10 changes: 5 additions & 5 deletions grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,18 @@ func (ios *intOrString) UnmarshalJSON(b []byte) error {
return nil
}

// converts a string of "<size><suffix>" (e.g. "123kb") into its equivalent size in bytes
// convertSuffix converts a string of "<size><suffix>" (e.g. "123kb") into its equivalent size in bytes
func convertSuffix(s string) (int, error) {
if s == "" { // return 0 if empty string
if s == "" {
return 0, nil
}

suffixes := map[string]float64{"": 0, "b": 0, "k": 1, "kb": 1, "m": 2, "mb": 2, "g": 3, "gb": 3, "t": 4, "tb": 4}
s = strings.ToLower(s) // converts string s to all lowercase
s = strings.ToLower(s)
i := strings.TrimFunc(s, func(r rune) bool { // trims both ends of string s of anything not a number (e.g., "123 kb" -> "123" and "12k3b" -> "12k3")
return !unicode.IsNumber(r)
})
size, err := strconv.Atoi(i) // convert number portion of string s into an int
size, err := strconv.Atoi(i)
if err != nil {
return -1, err
}
Expand All @@ -172,7 +172,7 @@ func convertSuffix(s string) (int, error) {
return !unicode.IsLetter(r)
})

if pow, ok := suffixes[suf]; ok { // checks if suf is a valid suffix
if pow, ok := suffixes[suf]; ok {
res := size * int(math.Pow(1024, pow))
return res, nil
}
Expand Down
34 changes: 2 additions & 32 deletions grpc_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,17 @@ import (
"encoding/json"
"os"
"testing"

"github.com/packethost/cacher/protos/cacher"
"google.golang.org/grpc"
)

type hardwareGetterMock struct {
hardwareResp string
}

func (hg hardwareGetterMock) ByIP(ctx context.Context, in getRequest, opts ...grpc.CallOption) (hardware, error) {
var hw hardware
dataModelVersion := os.Getenv("DATA_MODEL_VERSION")
switch dataModelVersion {
case "1":
err := json.Unmarshal([]byte(hg.hardwareResp), &hw)
if err != nil {
return nil, err
}
default:
hw = cacher.Hardware{JSON: hg.hardwareResp}
}

return hw, nil
}

func (hg hardwareGetterMock) Watch(ctx context.Context, in getRequest, opts ...grpc.CallOption) (watchClient, error) {
// TODO (kdeng3849)
return nil, nil
}

func TestGetByIPCacher(t *testing.T) {
t.Log("DATA_MODEL_VERSION (empty to use cacher):", os.Getenv("DATA_MODEL_VERSION"))

for name, test := range cacherGrpcTests {
t.Log(name)

var hgm hardwareGetter = hardwareGetterMock{test.json}
hegelTestServer := &server{
log: logger,
hardwareClient: hgm,
hardwareClient: hardwareGetterMock{test.json},
}
ehw, err := getByIP(context.Background(), hegelTestServer, test.remote)
if err != nil {
Expand Down Expand Up @@ -104,10 +75,9 @@ func TestGetByIPTinkerbell(t *testing.T) {
for name, test := range tinkerbellGrpcTests {
t.Log(name)

var hgm hardwareGetter = hardwareGetterMock{test.json}
hegelTestServer := &server{
log: logger,
hardwareClient: hgm,
hardwareClient: hardwareGetterMock{test.json},
}
ehw, err := getByIP(context.Background(), hegelTestServer, test.remote)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions http_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ func getMetadata(w http.ResponseWriter, r *http.Request) {
if userIP != "" {
metrics.MetadataRequests.Inc()
logger.With("userIP", userIP).Info("Actual IP is : ")
ehw, err := fetcher.GetByIP(context.Background(), hegelServer, userIP)
ehw, err := getByIP(context.Background(), hegelServer, userIP)
if err != nil {
metrics.Errors.WithLabelValues("metadata", "lookup").Inc()
logger.Info("Error in finding or exporting hxxardware ", err)
logger.Info("Error in finding or exporting hardware ", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down
23 changes: 6 additions & 17 deletions http_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,12 @@ import (
"net/http/httptest"
"os"
"testing"

"github.com/packethost/hegel/metrics"
"github.com/packethost/pkg/log"
)

func TestMain(m *testing.M) {
fetcher = dataFetcherMock{}

os.Setenv("PACKET_ENV", "test")
os.Setenv("PACKET_VERSION", "ignored")
os.Setenv("ROLLBAR_TOKEN", "ignored")

l, _ := log.Init("github.com/packethost/hegel")
logger = l.Package("main")
metrics.Init(l)

os.Exit(m.Run())
}

func TestGetMetadataCacher(t *testing.T) {
for name, test := range cacherTests {
t.Log(name)
hegelServer.hardwareClient = hardwareGetterMock{test.json}

os.Setenv("DATA_MODEL_VERSION", "")

Expand Down Expand Up @@ -68,6 +52,7 @@ func TestGetMetadataTinkerbell(t *testing.T) {

for name, test := range tinkerbellTests {
t.Log(name)
hegelServer.hardwareClient = hardwareGetterMock{test.json}

req, err := http.NewRequest("GET", "/metadata", nil)
if err != nil {
Expand Down Expand Up @@ -105,22 +90,26 @@ var cacherTests = map[string]struct {
id string
remote string
planSlug string
json string
}{
"cacher": {
id: "8978e7d4-1a55-4845-8a66-a5259236b104",
remote: "192.168.1.5",
planSlug: "t1.small.x86",
json: cacherDataModel,
},
}

var tinkerbellTests = map[string]struct {
id string
remote string
bondingMode int
json string
}{
"tinkerbell": {
id: "fde7c87c-d154-447e-9fce-7eb7bdec90c0",
remote: "192.168.1.5",
bondingMode: 5,
json: tinkerbellDataModel,
},
}
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ type hardwareGetter interface {
Watch(ctx context.Context, in getRequest, opts ...grpc.CallOption) (watchClient, error)
}

type getRequest interface{}
type hardware interface{}

type hardwareGetterCacher struct {
client cacher.CacherClient
}

type hardwareGetterTinkerbell struct {
client tink.HardwareServiceClient
}

type getRequest interface{}
type hardware interface{}

func (hg hardwareGetterCacher) ByIP(ctx context.Context, in getRequest, opts ...grpc.CallOption) (hardware, error) {
hw, err := hg.client.ByIP(ctx, in.(*cacher.GetRequest), opts...)
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"os"
"testing"

"github.com/packethost/hegel/metrics"
"github.com/packethost/pkg/log"
)

func TestMain(m *testing.M) {
hegelServer = &server{
log: logger,
}
os.Setenv("PACKET_ENV", "test")
os.Setenv("PACKET_VERSION", "ignored")
os.Setenv("ROLLBAR_TOKEN", "ignored")

l, _ := log.Init("github.com/packethost/hegel")
logger = l.Package("main")
metrics.Init(l)

os.Exit(m.Run())
}
58 changes: 29 additions & 29 deletions data_fetch.go → mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,38 @@ package main

import (
"context"
"encoding/json"
"os"

"github.com/packethost/cacher/protos/cacher"
"google.golang.org/grpc"
)

var fetcher dataFetch
// hardwareGetterMock is a mock implentation of the
type hardwareGetterMock struct {
hardwareResp string
}

func (hg hardwareGetterMock) ByIP(ctx context.Context, in getRequest, opts ...grpc.CallOption) (hardware, error) {
var hw hardware
dataModelVersion := os.Getenv("DATA_MODEL_VERSION")
switch dataModelVersion {
case "1":
err := json.Unmarshal([]byte(hg.hardwareResp), &hw)
if err != nil {
return nil, err
}
default:
hw = cacher.Hardware{JSON: hg.hardwareResp}
}

return hw, nil
}

func (hg hardwareGetterMock) Watch(ctx context.Context, in getRequest, opts ...grpc.CallOption) (watchClient, error) {
// TODO (kdeng3849)
return nil, nil
}

const (
cacherDataModel = `
Expand Down Expand Up @@ -568,31 +596,3 @@ const (
}
`
)

type dataFetch interface {
GetByIP(ctx context.Context, s *server, userIP string) ([]byte, error)
}

type dataFetcher struct{}
type dataFetcherMock struct{}

func (d dataFetcher) GetByIP(ctx context.Context, s *server, userIP string) ([]byte, error) {
return getByIP(ctx, s, userIP)
}

func (d dataFetcherMock) GetByIP(ctx context.Context, s *server, userIP string) ([]byte, error) {
var hw []byte
dataModelVersion := os.Getenv("DATA_MODEL_VERSION")
switch dataModelVersion {
case "1":
hw = []byte(tinkerbellDataModel)
default:
hw = []byte(cacherDataModel)
}

return exportHardware(hw)
}

func init() {
fetcher = dataFetcher{}
}

0 comments on commit ac55146

Please sign in to comment.