Skip to content

Commit

Permalink
ensure annotations exist before adding checksum
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <[email protected]>
  • Loading branch information
matthyx committed Nov 19, 2024
1 parent 9170df9 commit cfacdbc
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 52 deletions.
4 changes: 2 additions & 2 deletions pkg/registry/file/configurationscansummarystorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestConfigurationScanSummaryStorage_Get(t *testing.T) {
configScanSummaryStorage := NewConfigurationScanSummaryStorage(realStorage)

if tt.create {
wlObj := &softwarecomposition.WorkloadConfigurationScanSummary{ObjectMeta: v1.ObjectMeta{Annotations: map[string]string{}}}
wlObj := &softwarecomposition.WorkloadConfigurationScanSummary{}
_ = realStorage.Create(context.TODO(), "/spdx.softwarecomposition.kubescape.io/workloadconfigurationscansummaries/kubescape/toto", wlObj, nil, 0)
}

Expand Down Expand Up @@ -186,7 +186,7 @@ func TestConfigurationScanSummaryStorage_GetList(t *testing.T) {
configScanSummaryStorage := NewConfigurationScanSummaryStorage(realStorage)

if tt.create {
wlObj := &softwarecomposition.WorkloadConfigurationScanSummary{ObjectMeta: v1.ObjectMeta{Annotations: map[string]string{}}}
wlObj := &softwarecomposition.WorkloadConfigurationScanSummary{}
_ = realStorage.Create(context.TODO(), "/spdx.softwarecomposition.kubescape.io/workloadconfigurationscansummaries/kubescape/toto", wlObj, nil, 0)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/registry/file/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ func (s *StorageImpl) writeFiles(key string, obj runtime.Object, metaOut runtime
return fmt.Errorf("calculate checksum: %w", err)
}
// add checksum to metadata
metadata.(metav1.Object).GetAnnotations()[helpersv1.SyncChecksumMetadataKey] = checksum
if anno := metadata.(metav1.Object).GetAnnotations(); anno == nil {
metadata.(metav1.Object).SetAnnotations(map[string]string{helpersv1.SyncChecksumMetadataKey: checksum})
} else {
anno[helpersv1.SyncChecksumMetadataKey] = checksum
}
// write metadata
metadataEncoder := json.NewEncoder(metadataFile)
if err := metadataEncoder.Encode(metadata); err != nil {
Expand Down
25 changes: 8 additions & 17 deletions pkg/registry/file/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ func TestStorageImpl_Create(t *testing.T) {
key: "/spdx.softwarecomposition.kubescape.io/sbomsyfts/kubescape/toto",
obj: &v1beta1.SBOMSyft{
ObjectMeta: v1.ObjectMeta{
Name: "toto",
Annotations: map[string]string{},
Name: "toto",
},
},
},
Expand All @@ -131,7 +130,6 @@ func TestStorageImpl_Create(t *testing.T) {
key: "/spdx.softwarecomposition.kubescape.io/sbomsyfts/kubescape/toto",
obj: &v1beta1.SBOMSyft{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{},
Name: "toto",
ManagedFields: []v1.ManagedFieldsEntry{{Manager: "node-agent"}},
},
Expand Down Expand Up @@ -360,23 +358,20 @@ func TestStorageImpl_GetList(t *testing.T) {
objs := map[string]runtime.Object{
"/spdx.softwarecomposition.kubescape.io/sbomsyfts/kubescape/toto": &v1beta1.SBOMSyft{
ObjectMeta: v1.ObjectMeta{
Name: "toto",
Namespace: "kubescape",
Annotations: map[string]string{},
Name: "toto",
Namespace: "kubescape",
},
},
"/spdx.softwarecomposition.kubescape.io/sbomsyfts/kubescape/titi": &v1beta1.SBOMSyft{
ObjectMeta: v1.ObjectMeta{
Name: "titi",
Namespace: "kubescape",
Annotations: map[string]string{},
Name: "titi",
Namespace: "kubescape",
},
},
"/spdx.softwarecomposition.kubescape.io/sbomsyfts/other/tata": &v1beta1.SBOMSyft{
ObjectMeta: v1.ObjectMeta{
Name: "tata",
Namespace: "other",
Annotations: map[string]string{},
Name: "tata",
Namespace: "other",
},
},
}
Expand Down Expand Up @@ -437,8 +432,7 @@ func TestStorageImpl_GuaranteedUpdate(t *testing.T) {
count := 0
toto := &v1beta1.SBOMSyft{
ObjectMeta: v1.ObjectMeta{
Name: "toto",
Annotations: map[string]string{},
Name: "toto",
},
Spec: v1beta1.SBOMSyftSpec{
Metadata: v1beta1.SPDXMeta{
Expand All @@ -452,7 +446,6 @@ func TestStorageImpl_GuaranteedUpdate(t *testing.T) {
ObjectMeta: v1.ObjectMeta{
Name: "toto",
ResourceVersion: "1",
Annotations: map[string]string{},
},
Spec: v1beta1.SBOMSyftSpec{
Metadata: v1beta1.SPDXMeta{
Expand All @@ -466,7 +459,6 @@ func TestStorageImpl_GuaranteedUpdate(t *testing.T) {
ObjectMeta: v1.ObjectMeta{
Name: "toto",
ResourceVersion: "3",
Annotations: map[string]string{},
},
Spec: v1beta1.SBOMSyftSpec{
Metadata: v1beta1.SPDXMeta{
Expand All @@ -480,7 +472,6 @@ func TestStorageImpl_GuaranteedUpdate(t *testing.T) {
ObjectMeta: v1.ObjectMeta{
Name: "toto",
ResourceVersion: "1",
Annotations: map[string]string{},
},
Spec: v1beta1.SBOMSyftSpec{
Metadata: v1beta1.SPDXMeta{
Expand Down
32 changes: 7 additions & 25 deletions pkg/registry/file/vulnerabilitysummarystorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,7 @@ func TestVulnSummaryStorageImpl_GetList(t *testing.T) {
},
},
keyCreatedObj: []string{"/spdx.softwarecomposition.kubescape.io/vulnerabilitymanifestsummaries/any/any"},
createdObj: []*softwarecomposition.VulnerabilityManifestSummary{{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{},
},
}},
createdObj: []*softwarecomposition.VulnerabilityManifestSummary{{}},
},
createObj: true,
wantErr: false,
Expand Down Expand Up @@ -234,9 +230,8 @@ func TestVulnSummaryStorageImpl_GetList(t *testing.T) {
createdObj: []*softwarecomposition.VulnerabilityManifestSummary{
{
ObjectMeta: v1.ObjectMeta{
Name: "any",
Namespace: "any",
Annotations: map[string]string{},
Name: "any",
Namespace: "any",
},
Spec: softwarecomposition.VulnerabilityManifestSummarySpec{
Severities: softwarecomposition.SeveritySummary{
Expand All @@ -249,9 +244,8 @@ func TestVulnSummaryStorageImpl_GetList(t *testing.T) {
},
{
ObjectMeta: v1.ObjectMeta{
Name: "any",
Namespace: "many",
Annotations: map[string]string{},
Name: "any",
Namespace: "many",
},
Spec: softwarecomposition.VulnerabilityManifestSummarySpec{
Severities: softwarecomposition.SeveritySummary{
Expand Down Expand Up @@ -402,11 +396,7 @@ func TestVulnSummaryStorageImpl_Get(t *testing.T) {
},
},
keyCreatedObj: []string{"/spdx.softwarecomposition.kubescape.io/vulnerabilitymanifestsummaries/any/any"},
createdObj: []*softwarecomposition.VulnerabilityManifestSummary{{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{},
},
}},
createdObj: []*softwarecomposition.VulnerabilityManifestSummary{{}},
},
createObj: true, wantErr: false,
},
Expand Down Expand Up @@ -434,15 +424,7 @@ func TestVulnSummaryStorageImpl_Get(t *testing.T) {
},
},
keyCreatedObj: []string{"/spdx.softwarecomposition.kubescape.io/vulnerabilitymanifestsummaries/any/any", "/spdx.softwarecomposition.kubescape.io/vulnerabilitymanifestsummaries/any/many"},
createdObj: []*softwarecomposition.VulnerabilityManifestSummary{{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{},
},
}, {
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{},
},
}},
createdObj: []*softwarecomposition.VulnerabilityManifestSummary{{}, {}},
},
createObj: true, wantErr: false,
},
Expand Down
14 changes: 7 additions & 7 deletions pkg/registry/file/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ func TestFilesystemStorageWatchPublishing(t *testing.T) {
name: "Create should publish to the appropriate single channel",
start: map[string]int{keyK: 1},
inputObjects: map[string]*v1beta1.SBOMSyft{
keyK + "/some-sbom": {ObjectMeta: v1.ObjectMeta{Name: "some-sbom", Annotations: map[string]string{}}},
keyK + "/some-sbom": {ObjectMeta: v1.ObjectMeta{Name: "some-sbom"}},
},
want: map[string][]watch.Event{keyK: {{Type: watch.Added, Object: obj}}},
}, {
name: "Create should publish to all watchers on the relevant key",
start: map[string]int{keyK: 3},
inputObjects: map[string]*v1beta1.SBOMSyft{
keyK + "/some-sbom": {ObjectMeta: v1.ObjectMeta{Name: "some-sbom", Annotations: map[string]string{}}},
keyK + "/some-sbom": {ObjectMeta: v1.ObjectMeta{Name: "some-sbom"}},
},
want: map[string][]watch.Event{keyK: {
{Type: watch.Added, Object: obj},
Expand All @@ -135,19 +135,19 @@ func TestFilesystemStorageWatchPublishing(t *testing.T) {
name: "Creating on key different than the watch should produce no event",
start: map[string]int{keyK: 3, keyN: 1},
inputObjects: map[string]*v1beta1.SBOMSyft{
keyN + "/some-sbom": {ObjectMeta: v1.ObjectMeta{Name: "some-sbom", Annotations: map[string]string{}}},
keyN + "/some-sbom": {ObjectMeta: v1.ObjectMeta{Name: "some-sbom"}},
},
}, {
name: "Creating on key not being watched should produce no events",
start: map[string]int{keyK: 1},
inputObjects: map[string]*v1beta1.SBOMSyft{
keyN + "/some-sbom": {ObjectMeta: v1.ObjectMeta{Name: "some-sbom", Annotations: map[string]string{}}},
keyN + "/some-sbom": {ObjectMeta: v1.ObjectMeta{Name: "some-sbom"}},
},
}, {
name: "Sending to stopped watch should not produce an event",
start: map[string]int{keyN: 3},
inputObjects: map[string]*v1beta1.SBOMSyft{
keyN + "/some-sbom": {ObjectMeta: v1.ObjectMeta{Name: "some-sbom", Annotations: map[string]string{}}},
keyN + "/some-sbom": {ObjectMeta: v1.ObjectMeta{Name: "some-sbom"}},
},
stopBefore: map[string]int{keyN: 1},
want: map[string][]watch.Event{keyN: {
Expand All @@ -158,7 +158,7 @@ func TestFilesystemStorageWatchPublishing(t *testing.T) {
name: "Stopping watch after send shouldn't deadlock",
start: map[string]int{keyN: 3},
inputObjects: map[string]*v1beta1.SBOMSyft{
keyN + "/some-sbom": {ObjectMeta: v1.ObjectMeta{Name: "some-sbom", Annotations: map[string]string{}}},
keyN + "/some-sbom": {ObjectMeta: v1.ObjectMeta{Name: "some-sbom"}},
},
stopAfter: map[string]int{keyN: 0},
want: map[string][]watch.Event{keyN: {
Expand All @@ -170,7 +170,7 @@ func TestFilesystemStorageWatchPublishing(t *testing.T) {
name: "Stopping watch twice is ok",
start: map[string]int{keyN: 3},
inputObjects: map[string]*v1beta1.SBOMSyft{
keyN + "/some-sbom": {ObjectMeta: v1.ObjectMeta{Name: "some-sbom", Annotations: map[string]string{}}},
keyN + "/some-sbom": {ObjectMeta: v1.ObjectMeta{Name: "some-sbom"}},
},
stopBefore: map[string]int{keyN: 1},
stopAfter: map[string]int{keyN: 1},
Expand Down

0 comments on commit cfacdbc

Please sign in to comment.