Skip to content

Commit

Permalink
CSAR parsing ok
Browse files Browse the repository at this point in the history
  • Loading branch information
owulveryck committed Jan 5, 2016
1 parent 945da4b commit 929dfbe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
28 changes: 9 additions & 19 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,20 @@ func (t *ServiceTemplateDefinition) ParseCsar(zipfile string) error {
}

rc, err := zip.OpenReader(zipfile)
defer rc.Close()
if err != nil {
log.Fatalf("%s: %s\n", zipfile, err)

return err
}
defer rc.Close()
fs := zipfs.New(rc, zipfile)
out, err := vfs.ReadFile(fs, "/TOSCA-Metadata/TOSCA.meta")
if err != nil {
log.Fatal(err)

return err
}
var m meta
err = yaml.Unmarshal(out, &m)
if err != nil {
log.Println("Cannont unmarshal", string(out))
return err
}
log.Println(m.EntryDefinition)
dirname := fmt.Sprintf("/%v", filepath.Dir(m.EntryDefinition))
base := filepath.Base(m.EntryDefinition)
ns := vfs.NameSpace{}
Expand All @@ -141,20 +137,17 @@ func (t *ServiceTemplateDefinition) ParseCsar(zipfile string) error {
// Unmarshal the data in an interface
err = yaml.Unmarshal(data, &std)
if err != nil {
log.Println("Cannont unmarshal", string(data))
return err
}
// Import de normative types by default
for _, normType := range []string{"interface_types", "relationship_types", "node_types", "capability_types"} {
data, err := Asset(normType)
if err != nil {
log.Panic("Normative type not found")
return err
}
var tt ServiceTemplateDefinition
err = yaml.Unmarshal(data, &tt)
if err != nil {
log.Println("Cannont unmarshal", string(data))
return err
}
std = merge(std, tt)
Expand All @@ -174,7 +167,6 @@ func (t *ServiceTemplateDefinition) ParseCsar(zipfile string) error {
// Unmarshal the data in an interface
err = yaml.Unmarshal(data, &tt)
if err != nil {
log.Println("Cannont unmarshal", string(data))
return err
}
std = merge(std, tt)
Expand Down Expand Up @@ -229,38 +221,36 @@ func (t *ServiceTemplateDefinition) Parse(r io.Reader) error {
case "http":
res, err := http.Get(u.String())
if err != nil {
log.Fatal(err)
return err

}
r, err = ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)

return err
}
case "https":
res, err := http.Get(u.String())
if err != nil {
log.Fatal(err)
return err

}
r, err = ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)

return err
}
default:
r, err = ioutil.ReadFile(im)
if err != nil {
log.Fatal(err)
return err
}
}
var tt ServiceTemplateDefinition

err = yaml.Unmarshal(r, &tt)
if err != nil {
log.Fatal(err)
return err
}
std = merge(std, tt)
}
Expand Down
39 changes: 26 additions & 13 deletions service_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestParse(t *testing.T) {
for _, f := range files {
if !f.IsDir() {
fname := fmt.Sprintf("./tests/%v", f.Name())
if filepath.Ext(fname) == "yaml" {
if filepath.Ext(fname) == ".yaml" {
var s ServiceTemplateDefinition
o, err := os.Open(fname)
if err != nil {
Expand All @@ -30,19 +30,32 @@ func TestParse(t *testing.T) {
}
}
func TestParseCsar(t *testing.T) {
files, _ := ioutil.ReadDir("./tests")
for _, f := range files {
if !f.IsDir() {
fname := fmt.Sprintf("./tests/%v", f.Name())
if filepath.Ext(fname) == "zip" {
var s ServiceTemplateDefinition
err := s.ParseCsar(fname)
if err != nil {
t.Fatal(err)
}
}
}

testsko := []string{
"tests/csar_metadata_not_yaml.zip",
"tests/csar_wordpress_invalid_import_path.zip",
"tests/csar_wrong_metadata_file.zip",
"tests/csar_not_zip.zip",
}
testsok := []string{
"tests/csar_elk.zip",
"tests/csar_hello_world.zip",
"tests/csar_single_instance_wordpress.zip",
"tests/csar_wordpress_invalid_script_url.zip",
}
for _, f := range testsko {
var s ServiceTemplateDefinition
err := s.ParseCsar(f)
if err == nil {
t.Fatalf("Error, %v passed the test and should have failed", f)
}
}
for _, f := range testsok {
var s ServiceTemplateDefinition
err := s.ParseCsar(f)
if err != nil {
t.Fatalf("%v failed with error %v", f, err)
}
}
}

Expand Down

0 comments on commit 929dfbe

Please sign in to comment.