Skip to content

Commit

Permalink
Merge branch 'main' into ttimonen/watch
Browse files Browse the repository at this point in the history
  • Loading branch information
matthyx authored Jul 19, 2024
2 parents 4563c43 + 798ee08 commit 381db20
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM --platform=$BUILDPLATFORM golang:1.22-bullseye as builder
FROM --platform=$BUILDPLATFORM golang:1.22-bullseye AS builder

ENV GO111MODULE=on CGO_ENABLED=0
WORKDIR /work
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,13 @@ func mergeEgressRulesByPorts(rules []softwarecomposition.NetworkPolicyEgressRule
}

for _, port := range rule.Ports {
key := PortProtocolKey{Port: *port.Port, Protocol: *port.Protocol}
key := PortProtocolKey{}
if port.Port != nil {
key.Port = *port.Port
}
if port.Protocol != nil {
key.Protocol = *port.Protocol
}
if _, exists := merged[key]; !exists {
keys = append(keys, key)
}
Expand Down
14 changes: 13 additions & 1 deletion pkg/registry/file/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -305,7 +306,18 @@ func (s *StorageImpl) get(ctx context.Context, key string, opts storage.GetOptio
decoder := gob.NewDecoder(payloadFile)
err = decoder.Decode(objPtr)
if err != nil {
logger.L().Ctx(ctx).Error("Get - json unmarshal failed", helpers.Error(err), helpers.String("key", key))
if errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, io.EOF) {
// irrecoverable error, delete both files
_ = s.appFs.Remove(makeMetadataPath(p))
_ = s.appFs.Remove(makePayloadPath(p))
logger.L().Debug("Get - gob unexpected EOF, removed files", helpers.String("key", key))
if opts.IgnoreNotFound {
return runtime.SetZeroValue(objPtr)
} else {
return storage.NewKeyNotFoundError(key, 0)
}
}
logger.L().Ctx(ctx).Error("Get - gob unmarshal failed", helpers.Error(err), helpers.String("key", key))
return err
}
return nil
Expand Down
24 changes: 20 additions & 4 deletions pkg/registry/file/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ func TestStorageImpl_Delete(t *testing.T) {
}
}

func isNotFoundError(_ assert.TestingT, err error, _ ...any) bool {
return storage.IsNotFound(err)
}

func TestStorageImpl_Get(t *testing.T) {
var emptyObj bytes.Buffer
_ = gob.NewEncoder(&emptyObj).Encode(v1beta1.SBOMSPDXv2p3{})
Expand All @@ -268,15 +272,15 @@ func TestStorageImpl_Get(t *testing.T) {
args args
content string
create bool
wantErr bool
wantErr assert.ErrorAssertionFunc
want runtime.Object
}{
{
name: "not found",
args: args{
key: "/spdx.softwarecomposition.kubescape.io/sbomspdxv2p3s/kubescape/toto",
},
wantErr: true,
wantErr: isNotFoundError,
},
{
name: "empty string",
Expand All @@ -285,7 +289,7 @@ func TestStorageImpl_Get(t *testing.T) {
objPtr: &v1beta1.SBOMSPDXv2p3{},
},
create: true,
wantErr: true,
wantErr: isNotFoundError,
},
{
name: "empty object",
Expand All @@ -295,6 +299,7 @@ func TestStorageImpl_Get(t *testing.T) {
},
content: emptyObj.String(),
create: true,
wantErr: assert.NoError,
want: &v1beta1.SBOMSPDXv2p3{},
},
{
Expand All @@ -305,12 +310,23 @@ func TestStorageImpl_Get(t *testing.T) {
},
content: realObj.String(),
create: true,
wantErr: assert.NoError,
want: &v1beta1.SBOMSPDXv2p3{
ObjectMeta: v1.ObjectMeta{
Name: "toto",
},
},
},
{
name: "truncated object",
args: args{
key: "/spdx.softwarecomposition.kubescape.io/sbomspdxv2p3s/kubescape/toto",
objPtr: &v1beta1.SBOMSPDXv2p3{},
},
content: string(realObj.Bytes()[10]),
create: true,
wantErr: isNotFoundError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -320,7 +336,7 @@ func TestStorageImpl_Get(t *testing.T) {
_ = afero.WriteFile(fs, path, []byte(tt.content), 0644)
}
s := NewStorageImpl(fs, DefaultStorageRoot)
if err := s.Get(context.TODO(), tt.args.key, tt.args.opts, tt.args.objPtr); (err != nil) != tt.wantErr {
if err := s.Get(context.TODO(), tt.args.key, tt.args.opts, tt.args.objPtr); !tt.wantErr(t, err) {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.want != nil {
Expand Down

0 comments on commit 381db20

Please sign in to comment.