Skip to content

Commit

Permalink
size could be string or int, and should not be modified
Browse files Browse the repository at this point in the history
sgdisk is the tool used in osie to build partitions. sgdisk
supports multiple methods to define the size of those partitions
and therefore this value should not be modified.

Signed-off-by: Mike Mason <mason@packet.com>
  • Loading branch information
mikemrm committed Aug 20, 2020
1 parent 98ffd13 commit 80d8895
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 419 deletions.
52 changes: 1 addition & 51 deletions grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ import (
"context"
"encoding/json"
"io"
"math"
"net"
"os"
"strconv"
"strings"
"time"
"unicode"

"github.com/itchyny/gojq"
"github.com/tinkerbell/tink/util"
Expand Down Expand Up @@ -104,7 +100,7 @@ type filesystemOptions struct {
type partition struct {
Label string `json:"label"`
Number int `json:"number"`
Size intOrString `json:"size"`
Size interface{} `json:"size"`
Start int `json:"start,omitempty"`
TypeGUID string `json:"typeGuid,omitempty"`
}
Expand All @@ -122,52 +118,6 @@ type storage struct {
Filesystems []*filesystem `json:"filesystems,omitempty"`
}

type intOrString int

// UnmarshalJSON implements the json.Unmarshaler interface for custom unmarshalling of IntOrString
func (ios *intOrString) UnmarshalJSON(b []byte) error {
if b[0] != '"' {
return json.Unmarshal(b, (*int)(ios))
}
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
i, err := convertSuffix(s)
if err != nil {
return err
}
*ios = intOrString(i)
return nil
}

// 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, 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)
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)
if err != nil {
return -1, err
}

suf := strings.TrimFunc(s, func(r rune) bool { // trims both ends of string s of anything not a letter (e.g., "123 kb" -> "kb")
return !unicode.IsLetter(r)
})

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

// exportedHardware transforms hardware that is returned from cacher into what we want to expose to clients
func exportHardware(hw []byte) ([]byte, error) {
exported := &exportedHardwareCacher{}
Expand Down
74 changes: 6 additions & 68 deletions grpc_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"encoding/json"
"fmt"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -53,7 +54,7 @@ func TestGetByIPCacher(t *testing.T) {
t.Fatalf("unexpected storage disk wipe table, want: %v, got: %v\n", test.wipeTable, hw.Instance.Storage.Disks[0].WipeTable)
}
t.Log("want:", test.partitionSize, " got:", hw.Instance.Storage.Disks[0].Paritions[0].Size)
if int(hw.Instance.Storage.Disks[0].Paritions[0].Size) != test.partitionSize {
if fmt.Sprintf("%v", hw.Instance.Storage.Disks[0].Paritions[0].Size) != fmt.Sprintf("%v", test.partitionSize) {
t.Fatalf("unexpected storage disk partition size, want: %v, got: %v\n", test.partitionSize, hw.Instance.Storage.Disks[0].Paritions[0].Size)
}
}
Expand Down Expand Up @@ -126,7 +127,7 @@ func TestGetByIPTinkerbell(t *testing.T) {
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 hw.Metadata.Instance.Storage.Disks[0].Partitions[0].Size != test.partitionSize {
if fmt.Sprintf("%v", hw.Metadata.Instance.Storage.Disks[0].Partitions[0].Size) != fmt.Sprintf("%v", test.partitionSize) {
t.Fatalf("unexpected partition size, want: %v, got: %v\n", test.partitionSize, hw.Metadata.Instance.Storage.Disks[0].Partitions[0].Size)
}
}
Expand Down Expand Up @@ -175,7 +176,7 @@ var cacherGrpcTests = map[string]struct {
mac string
diskDevice string
wipeTable bool
partitionSize int
partitionSize interface{}
filesystemDevice string
filesystemFormat string
osSlug string
Expand Down Expand Up @@ -204,73 +205,10 @@ var cacherGrpcTests = map[string]struct {
partitionSize: 3333,
json: cacherPartitionSizeString,
},
"cacher_partition_size_string_leading_zeros": { // "007"
partitionSize: 7,
json: cacherPartitionSizeStringLeadingZeros,
},
"cacher_partition_size_whitespace": { // " \t 1234\n "
partitionSize: 1234,
json: cacherPartitionSizeWhitespace,
},
"cacher_partition_size_intercepting_whitespace": { // "12\tmb"
partitionSize: 12582912,
json: cacherPartitionSizeInterceptingWhitespace,
},
"cacher_partition_size_b_lower": { // "1000000b"
partitionSize: 1000000,
partitionSize: "1000000b",
json: cacherPartitionSizeBLower,
},
"cacher_partition_size_b_upper": { // "1000000B"
partitionSize: 1000000,
json: cacherPartitionSizeBUpper,
},
"cacher_partition_size_k": { // "24K"
partitionSize: 24576,
json: cacherPartitionSizeK,
},
"cacher_partition_size_kb_lower": { // "24kb"
partitionSize: 24576,
json: cacherPartitionSizeKBLower,
},
"cacher_partition_size_kb_upper": { // "24KB"
partitionSize: 24576,
json: cacherPartitionSizeKBUpper,
},
"cacher_partition_size_kb_mixed": { // "24Kb"
partitionSize: 24576,
json: cacherPartitionSizeKBMixed,
},
"cacher_partition_size_m": { // "3m"
partitionSize: 3145728,
json: cacherPartitionSizeM,
},
"cacher_partition_size_t": { // "2TB"
partitionSize: 2199023255552,
json: cacherPartitionSizeTB,
},
"cacher_partition_size_invalid_suffix": { // "3kmgtb"
partitionSize: -1,
json: cacherPartitionSizeInvalidSuffix,
error: "invalid suffix",
},
"cacher_partition_size_invalid_intertwined": { // "12kb3"
partitionSize: -1,
json: cacherPartitionSizeInvalidIntertwined,
error: `strconv.Atoi: parsing "12kb3": invalid syntax`,
},
"cacher_partition_size_invalid_intertwined_2": { // "k123b"
partitionSize: -1,
json: cacherPartitionSizeInvalidIntertwined2,
error: "invalid suffix",
},
"cacher_partition_size_empty": { // ""
partitionSize: 0,
json: cacherPartitionSizeEmpty,
},
"cacher_partition_size_reverse_placement": { // "mb10" // (kdeng3849) should we allow this?
partitionSize: 10485760,
json: cacherPartitionSizeReversedPlacement,
},
}

// test cases for TestGetByIPTinkerbell
Expand All @@ -280,7 +218,7 @@ var tinkerbellGrpcTests = map[string]struct {
bondingMode int64
diskDevice string
wipeTable bool
partitionSize int64
partitionSize interface{}
filesystemDevice string
filesystemFormat string
osSlug string
Expand Down
Loading

0 comments on commit 80d8895

Please sign in to comment.