Skip to content

Commit

Permalink
added more edge cases for storage disk partition size field
Browse files Browse the repository at this point in the history
  • Loading branch information
kqdeng committed Jun 22, 2020
1 parent 67e0603 commit 209f9dc
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 20 deletions.
208 changes: 204 additions & 4 deletions data_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,26 @@ const (
}
}
}
`
cacherPartitionSizeStringLeadingZeros = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": "007",
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
cacherPartitionSizeWhitespace = `
{
Expand All @@ -146,7 +166,67 @@ const (
{
"partitions": [
{
"size": " 1234 ",
"size": " \t 1234\n ",
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
cacherPartitionSizeInterceptingWhitespace = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": "12\tmb",
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
cacherPartitionSizeBLower = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": "1000000b",
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
cacherPartitionSizeBUpper = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": "1000000B",
"label": "BIOS",
"number": 1
}
Expand Down Expand Up @@ -177,7 +257,27 @@ const (
}
}
`
cacherPartitionSizeKB = `
cacherPartitionSizeKBLower = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": "24kb",
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
cacherPartitionSizeKBUpper = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
Expand All @@ -186,7 +286,27 @@ const (
{
"partitions": [
{
"size": "25Kb",
"size": "24KB",
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
cacherPartitionSizeKBMixed = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": "24Kb",
"label": "BIOS",
"number": 1
}
Expand Down Expand Up @@ -216,6 +336,26 @@ const (
}
}
}
`
cacherPartitionSizeTB = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": "2TB",
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
cacherPartitionSizeInvalidSuffix = `
{
Expand All @@ -226,7 +366,7 @@ const (
{
"partitions": [
{
"size": "3kmgt",
"size": "3kmgtb",
"label": "BIOS",
"number": 1
}
Expand Down Expand Up @@ -256,6 +396,66 @@ const (
}
}
}
`
cacherPartitionSizeInvalidIntertwined2 = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": "k123b",
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
cacherPartitionSizeEmpty = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": "",
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
cacherPartitionSizeReversedPlacement = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": "b10",
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
tinkerbellDataModel = `
{
Expand Down
10 changes: 8 additions & 2 deletions grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ type storage struct {

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))
Expand All @@ -153,17 +154,21 @@ func (ios *intOrString) UnmarshalJSON(b []byte) error {

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

suf := strings.TrimFunc(s, func(r rune) bool { // trims from string s of anything not a letter (e.g., "123 kb" -> "kb")
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)
})

Expand Down Expand Up @@ -193,6 +198,7 @@ func exportHardware(hw []byte) ([]byte, error) {
return json.Marshal(exported)
}

// UnmarshalJSON implements the json.Unmarshaler interface for custom unmarshalling of exportedHardwareCacher
func (eh *exportedHardwareCacher) UnmarshalJSON(b []byte) error {
type ehj exportedHardwareCacher
var tmp ehj
Expand Down
Loading

0 comments on commit 209f9dc

Please sign in to comment.