-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Move inventory helpers to internal package
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
- Loading branch information
1 parent
cfe3539
commit 6a51fa7
Showing
5 changed files
with
151 additions
and
21 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
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,93 @@ | ||
/* | ||
Copyright 2022 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package inventory | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
"sigs.k8s.io/cli-utils/pkg/object" | ||
|
||
"github.com/fluxcd/pkg/ssa" | ||
) | ||
|
||
func Test_Inventory(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
set1, err := readManifest("testdata/inventory1.yaml") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
inv1 := New() | ||
err = AddChangeSet(inv1, set1) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
|
||
set2, err := readManifest("testdata/inventory2.yaml") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
inv2 := New() | ||
err = AddChangeSet(inv2, set2) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
|
||
t.Run("lists objects in inventory", func(t *testing.T) { | ||
unList, err := List(inv1) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(len(unList)).To(BeIdenticalTo(len(inv1.Entries))) | ||
|
||
mList, err := ListMetadata(inv1) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(len(mList)).To(BeIdenticalTo(len(inv1.Entries))) | ||
}) | ||
|
||
t.Run("diff objects in inventory", func(t *testing.T) { | ||
unList, err := Diff(inv2, inv1) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(len(unList)).To(BeIdenticalTo(1)) | ||
g.Expect(unList[0].GetName()).To(BeIdenticalTo("test2")) | ||
}) | ||
} | ||
|
||
func readManifest(manifest string) (*ssa.ChangeSet, error) { | ||
data, err := os.ReadFile(manifest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
objects, err := ssa.ReadObjects(strings.NewReader(string(data))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cs := ssa.NewChangeSet() | ||
|
||
for _, o := range objects { | ||
cse := ssa.ChangeSetEntry{ | ||
ObjMetadata: object.UnstructuredToObjMetadata(o), | ||
GroupVersion: o.GroupVersionKind().Version, | ||
Subject: ssa.FmtUnstructured(o), | ||
Action: string(ssa.CreatedAction), | ||
} | ||
cs.Add(cse) | ||
} | ||
|
||
return cs, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: test | ||
--- | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: test1 | ||
namespace: test | ||
data: | ||
key: value1 |
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,22 @@ | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: test | ||
--- | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: test1 | ||
namespace: test | ||
data: | ||
key: value1 | ||
--- | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: test2 | ||
namespace: test | ||
data: | ||
key: value2 | ||
|
||
|