-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Improve bind imports, support lower case 'source' in yaml, add g…
…o test Entries stacker.yaml's "binds" are of type Bind. When imported from yaml, support was present for these three things: 1. ascii art specifying source and dest ("source -> dest") 2. source only ("source") 3. map with 'Source' and 'Dest'. Support for '3' is dropped, and replaced by map with 'source' and 'dest'. 'Source' and 'Dest' are inconsistent with the rest of stacker.yaml. Note that importing from json still does support capital letter variants as the golang json library is case insensitive. Improvements to the bind yaml and json support generally rely more heavily on the library to do the lifting. Signed-off-by: Scott Moser <smoser@brickies.net>
- Loading branch information
Showing
3 changed files
with
202 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package types | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
func TestUnmarshalYamlAndJSON(t *testing.T) { | ||
assert := assert.New(t) | ||
tables := []struct { | ||
desc string | ||
yblob string | ||
jblob string | ||
expected Binds | ||
errstr string | ||
}{ | ||
{desc: "proper array of source/dest bind allowed", | ||
yblob: "- source: src1\n dest: dest1\n", | ||
jblob: `[{"source": "src1", "dest": "dest1"}]`, | ||
expected: Binds{ | ||
Bind{Source: "src1", Dest: "dest1"}, | ||
}}, | ||
{desc: "array of bind ascii art", | ||
yblob: "- src1 -> dest1\n- src2 -> dest2", | ||
jblob: `["src1 -> dest1", "src2 -> dest2"]`, | ||
expected: Binds{ | ||
Bind{Source: "src1", Dest: "dest1"}, | ||
Bind{Source: "src2", Dest: "dest2"}, | ||
}}, | ||
{desc: "example mixed valid ascii art and dict", | ||
yblob: "- src1 -> dest1\n- source: src2\n dest: dest2\n", | ||
jblob: `["src1 -> dest1", {"source": "src2", "dest": "dest2"}]`, | ||
expected: Binds{ | ||
Bind{Source: "src1", Dest: "dest1"}, | ||
Bind{Source: "src2", Dest: "dest2"}, | ||
}}, | ||
// golang encoding/json is case insensitive | ||
{desc: "capital Source/Dest is not allowed as yaml", | ||
yblob: "- Source: src1\n Dest: dest1\n", | ||
expected: Binds{}, | ||
errstr: "xpected 'bind'"}, | ||
{desc: "source is required", | ||
yblob: "- Dest: dest1\n", | ||
jblob: `[{"Dest": "dest1"}]`, | ||
expected: Binds{}, | ||
errstr: "xpected 'bind'"}, | ||
{desc: "must be an array", | ||
yblob: "source: src1\ndest: dest1\n", | ||
jblob: `{"source": "src1", "dest": "dest1"}`, | ||
expected: Binds{}, | ||
errstr: "unmarshal"}, | ||
} | ||
var err error | ||
found := Binds{} | ||
for _, t := range tables { | ||
err = yaml.Unmarshal([]byte(t.yblob), &found) | ||
if t.errstr == "" { | ||
if !assert.NoError(err, t.desc) { | ||
continue | ||
} | ||
assert.Equal(t.expected, found) | ||
} else { | ||
assert.ErrorContains(err, t.errstr, t.desc) | ||
} | ||
} | ||
|
||
for _, t := range tables { | ||
if t.jblob == "" { | ||
continue | ||
} | ||
err = json.Unmarshal([]byte(t.jblob), &found) | ||
if t.errstr == "" { | ||
if !assert.NoError(err, t.desc) { | ||
continue | ||
} | ||
assert.Equal(t.expected, found) | ||
} else { | ||
assert.ErrorContains(err, t.errstr, t.desc) | ||
} | ||
} | ||
} | ||
|
||
func TestUnmarshalJSON(t *testing.T) { | ||
assert := assert.New(t) | ||
tables := []struct { | ||
desc string | ||
jblob string | ||
expected Binds | ||
errstr string | ||
}{ | ||
{desc: "proper array of source/dest bind allowed", | ||
jblob: `[{"source": "src1", "dest": "dest1"}]`, | ||
expected: Binds{ | ||
Bind{Source: "src1", Dest: "dest1"}, | ||
}}, | ||
/* | ||
{desc: "array of bind ascii art", | ||
jblob: "- src1 -> dest1\n- src2 -> dest2", | ||
expected: Binds{ | ||
Bind{Source: "src1", Dest: "dest1"}, | ||
Bind{Source: "src2", Dest: "dest2"}, | ||
}}, | ||
{desc: "example mixed valid ascii art and dict", | ||
jblob: "- src1 -> dest1\n- source: src2\n dest: dest2\n", | ||
expected: Binds{ | ||
Bind{Source: "src1", Dest: "dest1"}, | ||
Bind{Source: "src2", Dest: "dest2"}, | ||
}}, | ||
{desc: "capital Source/Dest is not allowed", | ||
jblob: "- Source: src1\n Dest: dest1\n", | ||
expected: Binds{}, | ||
errstr: "xpected 'bind'"}, | ||
{desc: "source is required", | ||
jblob: "- Dest: dest1\n", | ||
expected: Binds{}, | ||
errstr: "xpected 'bind'"}, | ||
{desc: "must be an array", | ||
jblob: "source: src1\ndest: dest1\n", | ||
expected: Binds{}, | ||
errstr: "unmarshal"}, | ||
*/ | ||
} | ||
var err error | ||
found := Binds{} | ||
for _, t := range tables { | ||
err = json.Unmarshal([]byte(t.jblob), &found) | ||
if t.errstr == "" { | ||
if !assert.NoError(err, t.desc) { | ||
continue | ||
} | ||
assert.Equal(t.expected, found) | ||
} else { | ||
assert.ErrorContains(err, t.errstr, t.desc) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters