Skip to content

Commit

Permalink
feat: find manifests by filter
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickkirschen committed Jun 3, 2024
1 parent a7bba6d commit 430fa3c
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 45 deletions.
6 changes: 3 additions & 3 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func MyListener(action manifesto.Action, manifest *manifesto.Manifest) error {
}

func main() {
m := manifesto.ParseFile("example/my-manifest.yaml", &MySpec{}, &MySpec{})
m1 := manifesto.ParseFile("example/my-manifest-1.yaml", &MySpec{}, &MySpec{})

pool := manifesto.CreatePool()
pool.Listen(MyListener)
pool.Apply(m)
pool.Apply(m1)

m3, _ := pool.GetByKey(m.CreateKey())
m3, _ := pool.GetByKey(m1.CreateKey())
m3.Error("Houston, we have a problem!")
pool.Delete(m3.CreateKey())
}
2 changes: 1 addition & 1 deletion example/my-manifest.yaml → example/my-manifest-1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: example.com/v1alpha1
kind: MyManifest

metadata:
name: my-manifest
name: my-manifest-1

spec:
message: hello, world
8 changes: 8 additions & 0 deletions example/my-manifest-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: example.com/v1alpha1
kind: MyManifest

metadata:
name: my-manifest-2

spec:
message: bye, world
11 changes: 11 additions & 0 deletions manifesto.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ func (pool *Pool) GetByKey(key ManifestKey) (*Manifest, bool) {
return manifest, ok
}

// Find goes through all existing manifests and filters for a testing function.
func (pool *Pool) Find(test func(*Manifest) bool) []*Manifest {
manifests := make([]*Manifest, 0)
for _, manifest := range pool.manifests {
if test(manifest) {
manifests = append(manifests, manifest)
}
}
return manifests
}

// ParseFile reads a JSON/YAML file and returns the parsed Manifest.
func ParseFile(filename string, spec any, status any) *Manifest {
content, err := os.ReadFile(filename)
Expand Down
111 changes: 70 additions & 41 deletions manifesto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,103 @@ import (
"testing"
)

const myManifest = `apiVersion: example.com/v1alpha1
kind: MyManifest
metadata:
name: my-manifest-1
spec:
message: hello, world
`

func wantMyManifest(manifest *Manifest) bool {
spec := manifest.Spec.(*MySpec)
return manifest.Metadata.Name == "my-manifest-1" && spec.Message == "hello, world"
}

type MySpec struct {
Message string `yaml:"message" json:"message"`
}

func TestParseFile(t *testing.T) {
manifest := ParseFile("example/my-manifest.yaml", &MySpec{}, &MySpec{})
spec := manifest.Spec.(*MySpec)

want := manifest.Metadata.Name == "my-manifest" && spec.Message == "hello, world"
manifest := ParseFile("example/my-manifest-1.yaml", &MySpec{}, &MySpec{})

if !want {
if !wantMyManifest(manifest) {
t.Fatalf("Unable to parse manifest from file")
}
}

func TestParseReader(t *testing.T) {
r := io.NopCloser(strings.NewReader(`apiVersion: example.com/v1alpha1
kind: MyManifest
metadata:
name: my-manifest
spec:
message: hello, world
`))
r := io.NopCloser(strings.NewReader(myManifest))
manifest := ParseReader(r, &MySpec{}, &MySpec{})

spec := manifest.Spec.(*MySpec)

want := manifest.Metadata.Name == "my-manifest" && spec.Message == "hello, world"

if !want {
if !wantMyManifest(manifest) {
t.Fatalf("Unable to parse manifest from reader")
}
}

func TestParseString(t *testing.T) {
s := `apiVersion: example.com/v1alpha1
kind: MyManifest
metadata:
name: my-manifest
spec:
message: hello, world
`
manifest := ParseString(s, &MySpec{}, &MySpec{})

spec := manifest.Spec.(*MySpec)

want := manifest.Metadata.Name == "my-manifest" && spec.Message == "hello, world"
manifest := ParseString(myManifest, &MySpec{}, &MySpec{})

if !want {
if !wantMyManifest(manifest) {
t.Fatalf("Unable to parse manifest from string")
}
}

func TestParseBytes(t *testing.T) {
b := []byte(`apiVersion: example.com/v1alpha1
kind: MyManifest
metadata:
name: my-manifest
spec:
message: hello, world
`)
b := []byte(myManifest)
manifest := ParseBytes(b, &MySpec{}, &MySpec{})

spec := manifest.Spec.(*MySpec)
if !wantMyManifest(manifest) {
t.Fatalf("Unable to parse manifest from bytes")
}
}

want := manifest.Metadata.Name == "my-manifest" && spec.Message == "hello, world"
func TestFind(t *testing.T) {
m1 := ParseFile("example/my-manifest-1.yaml", &MySpec{}, &MySpec{})
m2 := ParseFile("example/my-manifest-2.yaml", &MySpec{}, &MySpec{})

if !want {
t.Fatalf("Unable to parse manifest from bytes")
pool := CreatePool()
err1 := pool.Apply(m1)
err2 := pool.Apply(m2)

if len(err1) != 0 || len(err2) != 0 {
t.Fatalf("Unable to apply manifests: %s, %s", err1, err2)
}

manifests := pool.Find(
func(m *Manifest) bool {
spec := m.Spec.(*MySpec)
return m.ApiVersion == "example.com/v1alpha1" && m.Kind == "MyManifest" && strings.Contains(spec.Message, "world")
})

if len(manifests) != 2 {
t.Fatalf("Unable to find manifests: found %d manifests", len(manifests))
}
}

func TestDelete(t *testing.T) {
manifest := ParseString(myManifest, &MySpec{}, &MySpec{})

if !wantMyManifest(manifest) {
t.Fatalf("Unable to parse manifest from string")
}

pool := CreatePool()
err := pool.Apply(manifest)

if len(err) != 0 {
t.Fatalf("Unable to apply manifest: %s", err)
}

key := manifest.CreateKey()
_, ok := pool.GetByKey(key)
if !ok {
t.Fatalf("Manifest does not exist after insertion")
}

pool.Delete(key)
_, ok = pool.GetByKey(key)
if ok {
t.Fatalf("Manifest does exist after deletion")
}
}

0 comments on commit 430fa3c

Please sign in to comment.