From 5f493920cd63666ac42445092aee18d5fa3974a3 Mon Sep 17 00:00:00 2001 From: Adam0Brien Date: Tue, 15 Aug 2023 12:21:47 +0100 Subject: [PATCH] base: Add mapper function and fix tests --- base/v0_6_exp/validate.go | 30 +++++++++++++++++++++++++----- base/v0_6_exp/validate_test.go | 25 ++++++++++++++----------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/base/v0_6_exp/validate.go b/base/v0_6_exp/validate.go index 715043a9..02d2ea0b 100644 --- a/base/v0_6_exp/validate.go +++ b/base/v0_6_exp/validate.go @@ -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" ) @@ -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++ @@ -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 diff --git a/base/v0_6_exp/validate_test.go b/base/v0_6_exp/validate_test.go index 78e72910..1d9e5438 100644 --- a/base/v0_6_exp/validate_test.go +++ b/base/v0_6_exp/validate_test.go @@ -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"), @@ -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"), @@ -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"), }, } @@ -139,6 +139,9 @@ func TestValidateResource(t *testing.T) { } } +func TestMapperFunction(t *testing.T) { + +} func TestValidateTree(t *testing.T) { tests := []struct { in Tree