Skip to content

Commit

Permalink
filled out tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kqdeng committed Jun 22, 2020
1 parent 813f767 commit 67e0603
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 61 deletions.
46 changes: 43 additions & 3 deletions data_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ const (
{
"partitions": [
{
"size": "24Kb",
"size": "25Kb",
"label": "BIOS",
"number": 1
}
Expand Down Expand Up @@ -216,6 +216,46 @@ const (
}
}
}
`
cacherPartitionSizeInvalidSuffix = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": "3kmgt",
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
cacherPartitionSizeInvalidIntertwined = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": "12kb3",
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
tinkerbellDataModel = `
{
Expand Down Expand Up @@ -311,8 +351,8 @@ const (
"crypted_root_password": "$6$qViImWbWFfH/a4pq$s1bpFFXMpQj1eQbHWsruLy6/",
"operating_system_version": {
"distro": "ubuntu",
"version": "16.04",
"os_slug": "ubuntu_16_04"
"version": "18.04",
"os_slug": "ubuntu_18_04"
}
},
"custom":{
Expand Down
30 changes: 19 additions & 11 deletions grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type watchClient interface{}

type exportedHardware interface{}

// exportedHardwareCacher is the structure in which hegel returns to clients using the old cacher data model
// exposes only certain fields of the hardware data returned by cacher
type exportedHardwareCacher struct {
ID string `json:"id"`
Arch string `json:"arch"`
Expand All @@ -41,6 +43,8 @@ type exportedHardwareCacher struct {
BondingMode int `json:"bonding_mode"`
}

// exportedHardwareTinkerbell is the structure in which hegel returns to clients using the new tinkerbell data model
// exposes only certain fields of the hardware data returned by tinkerbell
type exportedHardwareTinkerbell struct {
ID string `json:"id"`
Metadata metadata `json:"metadata"`
Expand Down Expand Up @@ -116,7 +120,7 @@ type partition struct {
TypeGUID string `json:"typeGuid,omitempty"`
}

type RAID struct {
type raid struct {
Name string `json:"name"`
Level string `json:"level"`
Devices []string `json:"devices"`
Expand All @@ -125,7 +129,7 @@ type RAID struct {

type storage struct {
Disks []*disk `json:"disks,omitempty"`
RAID []*RAID `json:"raid,omitempty"`
RAID []*raid `json:"raid,omitempty"`
Filesystems []*filesystem `json:"filesystems,omitempty"`
}

Expand All @@ -147,23 +151,27 @@ func (ios *intOrString) UnmarshalJSON(b []byte) error {
return nil
}

// converts a string of "<size><suffix>" (e.g. "123kb") into its equivalent size in bytes
func convertSuffix(s string) (int, error) {
suffixes := map[string]int{"k": 1, "m": 2, "g": 3, "t": 4}
s = strings.ToLower(s)
i := strings.TrimFunc(s, func(r rune) bool {
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
i := strings.TrimFunc(s, func(r rune) bool { // trims from string s of anything not a number (e.g., "123 kb" -> "123" and "12k3b" -> "12k3")
return !unicode.IsNumber(r)
})
size, err := strconv.Atoi(i)
size, err := strconv.Atoi(i) // convert number portion of string s into an int
if err != nil {
return 0, err
return -1, err
}

suf := strings.TrimFunc(s, func(r rune) bool {
suf := strings.TrimFunc(s, func(r rune) bool { // trims from string s of anything not a letter (e.g., "123 kb" -> "kb")
return !unicode.IsLetter(r)
})
suf = strings.ReplaceAll(suf, "b", "")
res := size * int(math.Pow(1024, float64(suffixes[suf])))
return res, nil

if pow, ok := suffixes[suf]; ok { // checks if suf is a valid suffix
res := size * int(math.Pow(1024, pow))
return res, nil
}
return -1, errors.New("invalid suffix")
}

// exportedHardware transforms hardware that is returned from cacher/tink into what we want to expose to clients
Expand Down
118 changes: 72 additions & 46 deletions grpc_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"encoding/json"
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -32,6 +31,7 @@ func (hg hardwareGetterMock) ByIP(ctx context.Context, in getRequest, opts ...gr
}

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

Expand All @@ -48,29 +48,25 @@ func TestGetByIPCacher(t *testing.T) {
}
ehw, err := getByIP(context.Background(), hegelTestServer, test.remote)
if err != nil {
t.Fatal("Error in Finding Hardware", err)
if err.Error() != test.error {
t.Fatalf("unexpected error in getByIP, want: %v, got: %v\n", test.error, err.Error())
}
return
}
hw := exportedHardwareCacher{}
err = json.Unmarshal(ehw, &hw)
if err != nil {
t.Fatal("Error in unmarshalling hardware", err)
}

//instance := instance{}
//err = json.Unmarshal(hw.instance, &instance)
//if err != nil {
// t.Fatal("Error in unmarshalling hardware instance", err)
//}
//fmt.Println(instance)

if hw.State != test.state {
t.Fatalf("unexpected state, want: %v, got: %v\n", test.state, hw.State)
}
if hw.Facility != test.facility {
t.Fatalf("unexpected facility, want: %v, got: %v\n", test.facility, hw.Facility)
}
if len(hw.NetworkPorts) > 0 && hw.NetworkPorts[0]["data"].(map[string]interface{})["mac"] != test.mac {
t.Fatalf("unexpected mac, want: %v, got: %v\n", test.mac, hw.NetworkPorts[0]["data"])
t.Fatalf("unexpected mac, want: %v, got: %v\n", test.mac, hw.NetworkPorts[0]["data"].(map[string]interface{})["mac"])
}
if len(hw.Instance.Storage.Disks) > 0 {
if hw.Instance.Storage.Disks[0].Device != test.diskDevice {
Expand All @@ -79,8 +75,8 @@ func TestGetByIPCacher(t *testing.T) {
if hw.Instance.Storage.Disks[0].WipeTable != test.wipeTable {
t.Fatalf("unexpected storage disk wipe table, want: %v, got: %v\n", test.wipeTable, hw.Instance.Storage.Disks[0].WipeTable)
}
if int(hw.Instance.Storage.Disks[0].Paritions[0].Size) != test.partionSize {
t.Fatalf("unexpected storage disk partition size, want: %v, got: %v\n", test.partionSize, hw.Instance.Storage.Disks[0].Paritions[0].Size)
if int(hw.Instance.Storage.Disks[0].Paritions[0].Size) != test.partitionSize {
t.Fatalf("unexpected storage disk partition size, want: %v, got: %v\n", test.partitionSize, hw.Instance.Storage.Disks[0].Paritions[0].Size)
}
}
if len(hw.Instance.Storage.Filesystems) > 0 {
Expand All @@ -91,24 +87,12 @@ func TestGetByIPCacher(t *testing.T) {
t.Fatalf("unexpected storage filesystem mount format, want: %v, got: %v\n", test.filesystemFormat, hw.Instance.Storage.Filesystems[0].Mount.Format)
}
}
if hw.Instance.OS.Slug != test.osSlug {
t.Fatalf("unexpected os slug, want: %v, got: %v\n", test.osSlug, hw.Instance.OS.Slug)
if hw.Instance.OS.OsSlug != test.osSlug {
t.Fatalf("unexpected os slug, want: %v, got: %v\n", test.osSlug, hw.Instance.OS.OsSlug)
}
if hw.PlanSlug != test.planSlug {
t.Fatalf("unexpected plan slug, want: %v, got: %v\n", test.planSlug, hw.PlanSlug)
}

//fmt.Println(hw.State)
//fmt.Println(hw.Facility)
//fmt.Println(hw.Instance.Storage.Disks[0].Device)
//fmt.Println(hw.Instance.Storage.Disks[0].WipeTable)
fmt.Println(hw.Instance.Storage.Disks[0].Paritions[0].Size)
//fmt.Println(hw.Instance.Storage.Filesystems[0].Mount.Device)
//fmt.Println(hw.Instance.Storage.Filesystems[0].Mount.Format)
//fmt.Println(hw.Instance.OS.Slug)

//instance := reflect.ValueOf(hw.instance)
//fmt.Println(instance.MapIndex(reflect.ValueOf("storage")))
}
}

Expand All @@ -126,17 +110,47 @@ func TestGetByIPTinkerbell(t *testing.T) {
}
ehw, err := getByIP(context.Background(), hegelTestServer, test.remote)
if err != nil {
t.Fatal("Error in Finding Hardware", err)
if err.Error() != test.error {
t.Fatalf("unexpected error in getByIP, want: %v, got: %v\n", test.error, err.Error())
}
return
}
hw := exportedHardwareTinkerbell{}
err = json.Unmarshal(ehw, &hw)
if err != nil {
t.Fatal("Error in unmarshalling hardware", err)
}
if hw.Metadata.State != test.state {
t.Fatalf("unexpected state, want: %v, got: %v\n", test.state, hw.Metadata.State)
}
if hw.Metadata.BondingMode != test.bondingMode {
t.Fatalf("unexpected primary data mac, want: %v, got: %v\n", test.bondingMode, hw.Metadata.BondingMode)
t.Fatalf("unexpected bonding mode, want: %v, got: %v\n", test.bondingMode, hw.Metadata.BondingMode)
}
if len(hw.Metadata.Instance.Storage.Disks) > 0 {
if hw.Metadata.Instance.Storage.Disks[0].Device != test.diskDevice {
t.Fatalf("unexpected disk device, want: %v, got: %v\n", test.diskDevice, hw.Metadata.Instance.Storage.Disks[0].Device)
}
if hw.Metadata.Instance.Storage.Disks[0].WipeTable != test.wipeTable {
t.Fatalf("unexpected wipe table, want: %v, got: %v\n", test.wipeTable, hw.Metadata.Instance.Storage.Disks[0].WipeTable)
}
if int(hw.Metadata.Instance.Storage.Disks[0].Paritions[0].Size) != test.partitionSize {
t.Fatalf("unexpected partition size, want: %v, got: %v\n", test.partitionSize, hw.Metadata.Instance.Storage.Disks[0].Paritions[0].Size)
}
}
if len(hw.Metadata.Instance.Storage.Filesystems) > 0 {
if hw.Metadata.Instance.Storage.Filesystems[0].Mount.Device != test.filesystemDevice {
t.Fatalf("unexpected filesystem mount device, want: %v, got: %v\n", test.filesystemDevice, hw.Metadata.Instance.Storage.Filesystems[0].Mount.Device)
}
if hw.Metadata.Instance.Storage.Filesystems[0].Mount.Format != test.filesystemFormat {
t.Fatalf("unexpected filesystem mount format, want: %v, got: %v\n", test.filesystemFormat, hw.Metadata.Instance.Storage.Filesystems[0].Mount.Format)
}
}
if hw.Metadata.Instance.OS.OsSlug != test.osSlug {
t.Fatalf("unexpected os slug, want: %v, got: %v\n", test.osSlug, hw.Metadata.Instance.OS.OsSlug)
}
if hw.Metadata.Facility.(map[string]interface{})["plan_slug"] != test.planSlug {
t.Fatalf("unexpected os slug, want: %v, got: %v\n", test.planSlug, hw.Metadata.Facility.(map[string]interface{})["plan_slug"])
}
// TODO (kdeng3849) fill out the rest
}
}

Expand All @@ -147,12 +161,13 @@ var cacherGrpcTests = map[string]struct {
mac string
diskDevice string
wipeTable bool
partionSize int
partitionSize int
filesystemDevice string
filesystemFormat string
osSlug string
planSlug string
json string
error string
}{
"cacher": {
remote: "192.168.1.5",
Expand All @@ -161,35 +176,45 @@ var cacherGrpcTests = map[string]struct {
mac: "98:03:9b:48:de:bc",
diskDevice: "/dev/sda",
wipeTable: true,
partionSize: 4096,
partitionSize: 4096,
filesystemDevice: "/dev/sda3",
filesystemFormat: "ext4",
osSlug: "ubuntu_16_04",
planSlug: "t1.small.x86",
json: cacherDataModel,
},
"cacher_partition_size_int": {
partionSize: 4096,
json: cacherPartitionSizeInt,
partitionSize: 4096,
json: cacherPartitionSizeInt,
},
"cacher_partition_size_string": {
partionSize: 3333,
json: cacherPartitionSizeString,
partitionSize: 3333,
json: cacherPartitionSizeString,
},
"cacher_partition_size_whitespace": {
partionSize: 1234,
json: cacherPartitionSizeWhitespace,
partitionSize: 1234,
json: cacherPartitionSizeWhitespace,
},
"cacher_partition_size_k": {
partionSize: 24576,
json: cacherPartitionSizeK,
partitionSize: 24576,
json: cacherPartitionSizeK,
},
"cacher_partition_size_kb": {
partionSize: 24576,
json: cacherPartitionSizeKB,
partitionSize: 25600,
json: cacherPartitionSizeKB,
},
"cacher_partition_size_m": {
partionSize: 3145728,
json: cacherPartitionSizeM,
partitionSize: 3145728,
json: cacherPartitionSizeM,
},
"cacher_partition_size_invalid_suffix": {
partitionSize: -1,
json: cacherPartitionSizeInvalidSuffix,
error: "invalid suffix",
},
"cacher_partition_size_invalid_intertwined": {
json: cacherPartitionSizeInvalidIntertwined,
error: `strconv.Atoi: parsing "12kb3": invalid syntax`,
},
}

Expand All @@ -199,22 +224,23 @@ var tinkerbellGrpcTests = map[string]struct {
bondingMode int
diskDevice string
wipeTable bool
partionSize int
partitionSize int
filesystemDevice string
filesystemFormat string
osSlug string
planSlug string
json string
error string
}{
"tinkerbell": {
remote: "192.168.1.5",
bondingMode: 5,
diskDevice: "/dev/sda",
wipeTable: true,
partionSize: 4096,
partitionSize: 4096,
filesystemDevice: "/dev/sda3",
filesystemFormat: "ext4",
osSlug: "ubuntu_16_04",
osSlug: "ubuntu_18_04",
planSlug: "c2.medium.x86",
json: tinkerbellDataModel,
},
Expand Down
2 changes: 1 addition & 1 deletion http_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func getMetadata(w http.ResponseWriter, r *http.Request) {
ehw, err := fetcher.GetByIP(context.Background(), hegelServer, userIP)
if err != nil {
metrics.Errors.WithLabelValues("metadata", "lookup").Inc()
logger.Info("Error in Finding Hardware ", err)
logger.Info("Error in finding or exporting hxxardware ", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down

0 comments on commit 67e0603

Please sign in to comment.