Skip to content

Commit

Permalink
fix: manifest v2 compose unmarshall (#2309)
Browse files Browse the repository at this point in the history
Signed-off-by: Javier López Barba <javier@okteto.com>
  • Loading branch information
jLopezbarb committed Mar 4, 2022
1 parent ddfa08e commit e27ade8
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/model/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,13 @@ type DeployInfo struct {

// ComposeInfo represents information about compose file
type ComposeInfo struct {
Manifest []string `json:"manifest,omitempty" yaml:"manifest,omitempty"`
Stack *Stack `json:"-" yaml:"-"`
Manifest ManifestList `json:"manifest,omitempty" yaml:"manifest,omitempty"`
Stack *Stack `json:"-" yaml:"-"`
}

// ManifestList is a list containing all the compose files
type ManifestList []string

// DeployCommand represents a command to be executed
type DeployCommand struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Expand Down
42 changes: 42 additions & 0 deletions pkg/model/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,48 @@ func (d *DeployInfo) UnmarshalYAML(unmarshal func(interface{}) error) error {
return nil
}

// UnmarshalYAML Implements the Unmarshaler interface of the yaml pkg.
func (c *ComposeInfo) UnmarshalYAML(unmarshal func(interface{}) error) error {
var rawString string
err := unmarshal(&rawString)
if err == nil {
c.Manifest = []string{rawString}
return nil
}
var rawListString []string
err = unmarshal(&rawListString)
if err == nil {
c.Manifest = rawListString
return nil
}

type composeInfoRaw ComposeInfo
var compose composeInfoRaw
err = unmarshal(&compose)
if err != nil {
return err
}
*c = ComposeInfo(compose)
return nil
}

// UnmarshalYAML Implements the Unmarshaler interface of the yaml pkg.
func (m *ManifestList) UnmarshalYAML(unmarshal func(interface{}) error) error {
var rawString string
err := unmarshal(&rawString)
if err == nil {
*m = ManifestList{rawString}
return nil
}
var rawListString []string
err = unmarshal(&rawListString)
if err != nil {
return err
}
*m = ManifestList(rawListString)
return nil
}

type devRaw Dev

func (d *devRaw) UnmarshalYAML(unmarshal func(interface{}) error) error {
Expand Down
107 changes: 107 additions & 0 deletions pkg/model/serializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1933,6 +1933,113 @@ func TestDeployInfoMarshalling(t *testing.T) {
}
}

func TestComposeInfoUnmarshalling(t *testing.T) {
tests := []struct {
name string
composeInfoManifest []byte
expected *ComposeInfo
}{
{
name: "list of compose",
composeInfoManifest: []byte(`
- docker-compose.yml
- docker-compose.dev.yml`),
expected: &ComposeInfo{
Manifest: []string{
"docker-compose.yml",
"docker-compose.dev.yml",
},
},
},
{
name: "a docker compose",
composeInfoManifest: []byte(`docker-compose.yml`),
expected: &ComposeInfo{
Manifest: []string{
"docker-compose.yml",
},
},
},
{
name: "extended notation one compose",
composeInfoManifest: []byte(`manifest: docker-compose.yml`),
expected: &ComposeInfo{
Manifest: []string{
"docker-compose.yml",
},
},
},
{
name: "extended notation one compose",
composeInfoManifest: []byte(`manifest:
- docker-compose.yml
- docker-compose.dev.yml`),
expected: &ComposeInfo{
Manifest: []string{
"docker-compose.yml",
"docker-compose.dev.yml",
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := &ComposeInfo{}

err := yaml.UnmarshalStrict(tt.composeInfoManifest, &result)
if err != nil {
t.Fatalf("Not expecting error but got %s", err)
}

if !assert.Equal(t, tt.expected, result) {
t.Fatal("Failed")
}
})
}
}

func TestManifestListUnmarshalling(t *testing.T) {
tests := []struct {
name string
manifestListManifest []byte
expected *ManifestList
}{
{
name: "list of compose",
manifestListManifest: []byte(`
- docker-compose.yml
- docker-compose.dev.yml`),
expected: &ManifestList{
"docker-compose.yml",
"docker-compose.dev.yml",
},
},
{
name: "a docker compose",
manifestListManifest: []byte(`docker-compose.yml`),
expected: &ManifestList{
"docker-compose.yml",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := &ManifestList{}

err := yaml.UnmarshalStrict(tt.manifestListManifest, &result)
if err != nil {
t.Fatalf("Not expecting error but got %s", err)
}

if !assert.Equal(t, tt.expected, result) {
t.Fatal("Failed")
}
})
}
}

func TestManifestBuildUnmarshalling(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit e27ade8

Please sign in to comment.