Skip to content

Commit

Permalink
base: Add mapper function and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam0Brien committed Aug 22, 2023
1 parent 1198407 commit 5f49392
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
30 changes: 25 additions & 5 deletions base/v0_6_exp/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ package v0_6_exp
import (
baseutil "github.com/coreos/butane/base/util"
"github.com/coreos/butane/config/common"
exp "github.com/coreos/ignition/v2/config/v3_5_experimental"

"github.com/coreos/ignition/v2/config/shared/errors"
"github.com/coreos/ignition/v2/config/util"
exp "github.com/coreos/ignition/v2/config/v3_5_experimental"
"github.com/coreos/vcontext/path"
"github.com/coreos/vcontext/report"
)
Expand All @@ -28,6 +28,7 @@ func (rs Resource) Validate(c path.ContextPath) (r report.Report) {
var field string
sources := 0
var config string
var butaneReport report.Report
switch {
case rs.Local != nil:
sources++
Expand All @@ -47,15 +48,34 @@ func (rs Resource) Validate(c path.ContextPath) (r report.Report) {
if field == "local" || field == "inline" {
_, report, err := exp.Parse([]byte(config))
if len(report.Entries) > 0 {
r.Merge(report)
} else {
r.AddOnWarn(c.Append("ignition", "config", "merge", field), err)
butaneReport = MapIgnitionReportToButane(report)
// convert report into butane specific report then merge it
r.Merge(butaneReport) // report needs to be butane specific
}
if err != nil {
if err == errors.ErrUnknownVersion {
r.AddOnError(c.Append(field), common.ErrTooManyResourceSources)
}
}
}
}
return
}

func MapIgnitionReportToButane(ignitionReport report.Report) report.Report {
var butaneRep report.Report
for _, entry := range ignitionReport.Entries {
butaneEntry := report.Entry{
Kind: entry.Kind,
Message: entry.Message,
Context: entry.Context,
Marker: entry.Marker,
}
butaneRep.Entries = append(butaneRep.Entries, butaneEntry)
}
return butaneRep
}

func (fs Filesystem) Validate(c path.ContextPath) (r report.Report) {
if !util.IsTrue(fs.WithMountUnit) {
return
Expand Down
25 changes: 14 additions & 11 deletions base/v0_6_exp/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestValidateResource(t *testing.T) {
// inline specified
{
Resource{
Inline: util.StrToPtr("hello"),
// Inline: util.StrToPtr("hello"),
Compression: util.StrToPtr("gzip"),
Verification: Verification{
Hash: util.StrToPtr("this isn't validated"),
Expand All @@ -64,7 +64,7 @@ func TestValidateResource(t *testing.T) {
// local specified
{
Resource{
Local: util.StrToPtr("hello"),
// Local: util.StrToPtr("hello"),
Compression: util.StrToPtr("gzip"),
Verification: Verification{
Hash: util.StrToPtr("this isn't validated"),
Expand All @@ -77,54 +77,54 @@ func TestValidateResource(t *testing.T) {
{
Resource{
Source: util.StrToPtr("data:,hello"),
Inline: util.StrToPtr("hello"),
Inline: util.StrToPtr("{\"ignition\": {\"version\": \"3.5.0\"}}"),
Compression: util.StrToPtr("gzip"),
Verification: Verification{
Hash: util.StrToPtr("this isn't validated"),
},
},
common.ErrTooManyResourceSources,
path.New("yaml", "source"),
path.New("yaml", "inline"),
},
// source + local, invalid
{
Resource{
Source: util.StrToPtr("data:,hello"),
Local: util.StrToPtr("hello"),
Local: util.StrToPtr("{\"ignition\": {\"version\": \"3.5.0\"}}"),
Compression: util.StrToPtr("gzip"),
Verification: Verification{
Hash: util.StrToPtr("this isn't validated"),
},
},
common.ErrTooManyResourceSources,
path.New("yaml", "source"),
path.New("yaml", "local"),
},
// inline + local, invalid
{
Resource{
Inline: util.StrToPtr("hello"),
Local: util.StrToPtr("hello"),
Local: util.StrToPtr("{\"ignition\": {\"version\": \"3.5.0\"}}"),
Compression: util.StrToPtr("gzip"),
Verification: Verification{
Hash: util.StrToPtr("this isn't validated"),
},
},
common.ErrTooManyResourceSources,
path.New("yaml", "inline"),
path.New("yaml", "local"),
},
// source + inline + local, invalid
{
Resource{
Source: util.StrToPtr("data:,hello"),
Inline: util.StrToPtr("hello"),
Local: util.StrToPtr("hello"),
Inline: util.StrToPtr("{\"ignition\": {\"version\": \"3.5.0\"}}"),
Local: util.StrToPtr("{\"ignition\": {\"version\": \"3.5.0\"}}"),
Compression: util.StrToPtr("gzip"),
Verification: Verification{
Hash: util.StrToPtr("this isn't validated"),
},
},
common.ErrTooManyResourceSources,
path.New("yaml", "source"),
path.New("yaml", "local"),
},
}

Expand All @@ -139,6 +139,9 @@ func TestValidateResource(t *testing.T) {
}
}

func TestMapperFunction(t *testing.T) {

}
func TestValidateTree(t *testing.T) {
tests := []struct {
in Tree
Expand Down

0 comments on commit 5f49392

Please sign in to comment.