Skip to content

Commit

Permalink
Feature: Adds support for Version parsing and comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjones-cisco committed Oct 28, 2016
1 parent 60befe7 commit c36c7d6
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 42 deletions.
14 changes: 0 additions & 14 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,6 @@
(line 62) (kenjones): Add support for Groups


### ``tosca_namespace_alias.go``
(line 43) (kenjones): Leverage https://github.com/blang/semver to provide Version implementation details.

(line 45) Version.GetMajor

(line 52) Version.GetMinor

(line 59) Version.GetFixVersion

(line 66) Version.GetQualifier

(line 73) Version.GetBuildVersion


### ``tosca_reusable_modeling_definitions.go``
(line 21) : Implement ArtifactDefinition struct

Expand Down
6 changes: 4 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import:
- godoc/vfs/zipfs
- package: gopkg.in/yaml.v2
- package: github.com/kenjones-cisco/mergo
- package: github.com/blang/semver
version: ^3.3.0
testImport:
- package: github.com/davecgh/go-spew
version: ^1.0.0
Expand Down
2 changes: 1 addition & 1 deletion service_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import "github.com/kenjones-cisco/mergo"
// ServiceTemplateDefinition is the meta structure containing an entire tosca document as described in
// http://docs.oasis-open.org/tosca/TOSCA-Simple-Profile-YAML/v1.0/csd03/TOSCA-Simple-Profile-YAML-v1.0-csd03.html
type ServiceTemplateDefinition struct {
DefinitionsVersion Version `yaml:"tosca_definitions_version" json:"tosca_definitions_version"` // A.9.3.1 tosca_definitions_version
DefinitionsVersion string `yaml:"tosca_definitions_version" json:"tosca_definitions_version"` // A.9.3.1 tosca_definitions_version
Metadata Metadata `yaml:"metadata,omitempty" json:"metadata"`
Description string `yaml:"description,omitempty" json:"description,omitempty"`
DslDefinitions interface{} `yaml:"dsl_definitions,omitempty" json:"dsl_definitions,omitempty"` // Declares optional DSL-specific definitions and conventions. For example, in YAML, this allows defining reusable YAML macros (i.e., YAML alias anchors) for use throughout the TOSCA Service Template.
Expand Down
182 changes: 182 additions & 0 deletions service_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"

"github.com/davecgh/go-spew/spew"
"gopkg.in/yaml.v2"
)

func TestFlattenNodeType(t *testing.T) {
Expand Down Expand Up @@ -291,6 +293,186 @@ func TestParseCsar(t *testing.T) {
}
}

func isExpectedType(t reflect.Type, k reflect.Kind) bool {
return t.Kind() == k
}

func toBytes(s string) []byte {
r := strings.NewReader(s)
b, _ := ioutil.ReadAll(r)
return b
}

func TestVersion(t *testing.T) {
fname := "./tests/custom_types/custom_policy_types.yaml"
var s ServiceTemplateDefinition
o, err := os.Open(fname)
if err != nil {
t.Fatal(err)
}
err = s.Parse(o)
if err != nil {
t.Log("Error in processing", fname)
t.Fatal(err)
}

for name, p := range s.PolicyTypes {
major := p.Version.GetMajor()
if !isExpectedType(reflect.TypeOf(major), reflect.Int) {
t.Log(name, "has invalid Major version component:", major)
t.Fail()
}

minor := p.Version.GetMinor()
if !isExpectedType(reflect.TypeOf(minor), reflect.Int) {
t.Log(name, "has invalid Minor version component:", minor)
t.Fail()
}

fixv := p.Version.GetFixVersion()
if !isExpectedType(reflect.TypeOf(fixv), reflect.Int) {
t.Log(name, "has invalid Fix version component:", fixv)
t.Fail()
}

rel := p.Version.GetQualifier()
if !isExpectedType(reflect.TypeOf(rel), reflect.String) {
t.Log(name, "has invalid Qualifier version component:", rel)
t.Fail()
}

build := p.Version.GetBuildVersion()
if !isExpectedType(reflect.TypeOf(build), reflect.Int) {
t.Log(name, "has invalid Build version component:", build)
t.Fail()
}
}

var v Version
var str = "1.0.0.alpha-10"
var data = toBytes(str)
if err = yaml.Unmarshal(data, &v); err != nil {
t.Log(err)
t.Fail()
}
if v.GetMajor() != 1 {
t.Log(v.String(), "not parsed correctly")
t.Fail()
}
if v.GetMinor() != 0 {
t.Log(v.String(), "not parsed correctly")
t.Fail()
}
if v.GetFixVersion() != 0 {
t.Log(v.String(), "not parsed correctly")
t.Fail()
}
if v.GetQualifier() != "alpha" {
t.Log(v.String(), "not parsed correctly", v.GetQualifier())
t.Fail()
}
if v.GetBuildVersion() != 10 {
t.Log(v.String(), "not parsed correctly", v.GetBuildVersion())
t.Fail()
}

str = "1.0.alpha-9"
data = toBytes(str)
if err = yaml.Unmarshal(data, &v); err != nil {
t.Log(err)
t.Fail()
}
if v.GetMajor() != 1 {
t.Log(v.String(), "not parsed correctly")
t.Fail()
}
if v.GetMinor() != 0 {
t.Log(v.String(), "not parsed correctly")
t.Fail()
}
if v.GetFixVersion() != 0 {
t.Log(v.String(), "not parsed correctly")
t.Fail()
}
if v.GetQualifier() != "alpha" {
t.Log(v.String(), "not parsed correctly", v.GetQualifier())
t.Fail()
}
if v.GetBuildVersion() != 9 {
t.Log(v.String(), "not parsed correctly", v.GetBuildVersion())
t.Fail()
}

str = "1.0"
data = toBytes(str)
if err = yaml.Unmarshal(data, &v); err != nil {
t.Log(err)
t.Fail()
}
if v.GetMajor() != 1 {
t.Log(v.String(), "not parsed correctly")
t.Fail()
}
if v.GetMinor() != 0 {
t.Log(v.String(), "not parsed correctly")
t.Fail()
}
if v.GetFixVersion() != 0 {
t.Log(v.String(), "not parsed correctly")
t.Fail()
}
if v.GetQualifier() != "" {
t.Log(v.String(), "not parsed correctly", v.GetQualifier())
t.Fail()
}
if v.GetBuildVersion() != 0 {
t.Log(v.String(), "not parsed correctly", v.GetBuildVersion())
t.Fail()
}

str = "1"
data = toBytes(str)
if err = yaml.Unmarshal(data, &v); err != nil {
t.Log(err)
t.Fail()
}
if v.GetMajor() != 1 {
t.Log(v.String(), "not parsed correctly")
t.Fail()
}
if v.GetMinor() != 0 {
t.Log(v.String(), "not parsed correctly")
t.Fail()
}
if v.GetFixVersion() != 0 {
t.Log(v.String(), "not parsed correctly")
t.Fail()
}
if v.GetQualifier() != "" {
t.Log(v.String(), "not parsed correctly", v.GetQualifier())
t.Fail()
}
if v.GetBuildVersion() != 0 {
t.Log(v.String(), "not parsed correctly", v.GetBuildVersion())
t.Fail()
}

str = "test"
data = toBytes(str)
if err = yaml.Unmarshal(data, &v); err == nil {
t.Log(str, "is not a valid version but parsed successfully")
t.Fail()
}

str = "version: 1"
data = toBytes(str)
if err = yaml.Unmarshal(data, &v); err == nil {
t.Log(str, "is not a valid version but parsed successfully")
t.Fail()
}

}

func TestEvaluate(t *testing.T) {
fname := "./tests/tosca_web_application.yaml"
var s ServiceTemplateDefinition
Expand Down
2 changes: 2 additions & 0 deletions tests/custom_types/custom_policy_types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ description: >
policy_types:
my.policies.types.Performance:
derived_from: tosca.policies.Performance
version: 1.0.0
description: |
The definition that is used to declare the performance requirements
of nodes or groups of nodes.
Expand Down Expand Up @@ -38,6 +39,7 @@ policy_types:

my.policies.types.UpdateScalingConstraints:
derived_from: tosca.policies.Update
version: 1.0.0-beta
description: The definition that is used to govern scaling of nodes or groups of nodes.
properties:
min_instances:
Expand Down
Loading

0 comments on commit c36c7d6

Please sign in to comment.