-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using path now and checking the right versions for the right snapshots
- Loading branch information
Showing
17 changed files
with
2,240 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
package matches | ||
|
||
import "context" | ||
|
||
type ContextKey string | ||
|
||
// UpdateSnapshotKey defines a signal to the snapshot watcher to update the snapshot its checking. | ||
var UpdateSnapshotKey = ContextKey("update-snapshot") | ||
|
||
// Matcher that can assert information given a CRD and a payload configuration of the matcher. | ||
type Matcher interface { | ||
Match(sourceTemplateLocation string, payload []byte) error | ||
Match(ctx context.Context, crdLocation string, payload []byte) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,100 @@ | ||
package matchsnapshot | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/Skarlso/crd-to-sample-yaml/pkg/matches" | ||
"k8s.io/apimachinery/pkg/util/yaml" | ||
|
||
"github.com/Skarlso/crd-to-sample-yaml/pkg/matches" | ||
"github.com/Skarlso/crd-to-sample-yaml/pkg/tests" | ||
) | ||
|
||
const MatcherName = "matchSnapshot" | ||
|
||
type Config struct { | ||
Name string `yaml:"name"` | ||
Path string `yaml:"path"` | ||
Minimal bool `yaml:"minimal"` | ||
} | ||
type Matcher struct { | ||
Updater Updater | ||
} | ||
type Matcher struct{} | ||
|
||
func (m *Matcher) Match(sourceTemplateLocation string, payload []byte) error { | ||
content, err := os.ReadFile(sourceTemplateLocation) | ||
if err != nil { | ||
return fmt.Errorf("failed to read source template: %w", err) | ||
} | ||
func init() { | ||
tests.Register(&Matcher{ | ||
Updater: &Update{}, | ||
}, MatcherName) | ||
} | ||
|
||
func (m *Matcher) Match(ctx context.Context, crdLocation string, payload []byte) error { | ||
c := Config{} | ||
if err := yaml.Unmarshal(payload, &c); err != nil { | ||
return err | ||
} | ||
|
||
snapshot, err := os.ReadFile(c.Name) | ||
// we only create the snapshots if update is requested, otherwise, | ||
// we just loop check existing snapshots | ||
if v := ctx.Value(matches.UpdateSnapshotKey); v != nil { | ||
if err := m.Updater.Update(crdLocation, c.Path, c.Minimal); err != nil { | ||
return fmt.Errorf("failed to update snapshot at %s: %w", c.Path, err) | ||
} | ||
} | ||
|
||
var snapshots []string | ||
err := filepath.Walk(c.Path, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// skip reading folders | ||
if info.IsDir() { | ||
return nil | ||
} | ||
|
||
// make sure we only check the snapshots that belong to this crd being checked. | ||
if strings.Contains(filepath.Base(path), filepath.Base(crdLocation)) { | ||
if filepath.Ext(path) == ".yaml" { | ||
if c.Minimal { | ||
// only check files that have the min extension. | ||
if strings.HasSuffix(filepath.Base(path), ".min.yaml") { | ||
snapshots = append(snapshots, path) | ||
} | ||
} else if !strings.HasSuffix(filepath.Base(path), ".min.yaml") { | ||
// only add the file if it specifically does NOT contain the min extension. | ||
snapshots = append(snapshots, path) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
content, err := os.ReadFile(crdLocation) | ||
if err != nil { | ||
return fmt.Errorf("failed to read snapshot template: %w", err) | ||
return fmt.Errorf("failed to read source template: %w", err) | ||
} | ||
|
||
return matches.Validate(content, snapshot) | ||
} | ||
// gather all the errors for all the files | ||
var validationErrors error | ||
for _, s := range snapshots { | ||
// one snapshot will contain a single version and the validation | ||
// will know which version to check against | ||
snapshotContent, err := os.ReadFile(s) | ||
if err != nil { | ||
return fmt.Errorf("failed to read snapshot template: %w", err) | ||
} | ||
|
||
func init() { | ||
tests.Register(&Matcher{}, MatcherName) | ||
if err := matches.Validate(content, snapshotContent); err != nil { | ||
validationErrors = errors.Join(validationErrors, err) | ||
} | ||
} | ||
|
||
return validationErrors | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package matchsnapshot | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/Skarlso/crd-to-sample-yaml/pkg" | ||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" | ||
"k8s.io/apimachinery/pkg/util/yaml" | ||
) | ||
|
||
const ( | ||
perm = 0o644 | ||
) | ||
|
||
type Updater interface { | ||
Update(sourceTemplateLocation string, targetSnapshot string, minimal bool) error | ||
} | ||
|
||
type Update struct{} | ||
|
||
// Update any given files in the snapshots. | ||
func (u *Update) Update(sourceTemplateLocation string, targetSnapshotLocation string, minimal bool) error { | ||
sourceTemplate, err := os.ReadFile(sourceTemplateLocation) | ||
if err != nil { | ||
return err | ||
} | ||
baseName := strings.Trim(filepath.Base(sourceTemplateLocation), filepath.Ext(sourceTemplateLocation)) | ||
|
||
crd := &v1beta1.CustomResourceDefinition{} | ||
if err := yaml.Unmarshal(sourceTemplate, crd); err != nil { | ||
return fmt.Errorf("failed to unmarshal into custom resource definition: %w", err) | ||
} | ||
|
||
for _, version := range crd.Spec.Versions { | ||
name := baseName + "-" + version.Name + ".yaml" | ||
if minimal { | ||
name = baseName + "-" + version.Name + ".min.yaml" | ||
} | ||
file, err := os.OpenFile(filepath.Join(targetSnapshotLocation, name), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) | ||
if err != nil { | ||
return fmt.Errorf("failed to open file %s: %w", filepath.Join(targetSnapshotLocation, name), err) | ||
} | ||
|
||
defer file.Close() | ||
|
||
parser := pkg.NewParser(crd.Spec.Group, crd.Spec.Names.Kind, false, minimal) | ||
if err := parser.ParseProperties(version.Name, file, version.Schema.OpenAPIV3Schema.Properties, pkg.RootRequiredFields); err != nil { | ||
return fmt.Errorf("failed to parse properties: %w", err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.