Skip to content

Commit

Permalink
test(pkg/distribution): add initial index entry signature unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Massimiliano Giovagnoli <[email protected]>
  • Loading branch information
maxgio92 committed Aug 23, 2023
1 parent 8b2779b commit 3cb64a4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 deletions.
3 changes: 1 addition & 2 deletions build/registry/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ require (
github.com/onsi/gomega v1.27.8
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.7.0
golang.org/x/tools v0.9.3
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/klog/v2 v2.100.1
Expand Down Expand Up @@ -74,13 +73,13 @@ require (
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/net v0.11.0 // indirect
golang.org/x/oauth2 v0.9.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/term v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
golang.org/x/tools v0.9.3 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
Expand Down
1 change: 0 additions & 1 deletion build/registry/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
8 changes: 4 additions & 4 deletions build/registry/pkg/distribution/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const (
GHOrg = "falcosecurity"
)

func pluginToIndexEntry(p registry.Plugin, registry, repo string) *index.Entry {
func PluginToIndexEntry(p registry.Plugin, registry, repo string) *index.Entry {
return &index.Entry{
Name: p.Name,
Type: string(falcoctloci.Plugin),
Expand All @@ -56,7 +56,7 @@ func pluginToIndexEntry(p registry.Plugin, registry, repo string) *index.Entry {
}
}

func pluginRulesToIndexEntry(p registry.Plugin, registry, repo string) *index.Entry {
func PluginRulesToIndexEntry(p registry.Plugin, registry, repo string) *index.Entry {
return &index.Entry{
Name: p.Name + common.RulesArtifactSuffix,
Type: string(falcoctloci.Rulesfile),
Expand Down Expand Up @@ -90,13 +90,13 @@ func upsertIndex(r *registry.Registry, ociArtifacts map[string]string, indexPath
tokens := strings.Split(refPlugin, "/")
ociRegistry := tokens[0]
ociRepo := filepath.Join(tokens[1:]...)
i.Upsert(pluginToIndexEntry(p, ociRegistry, ociRepo))
i.Upsert(PluginToIndexEntry(p, ociRegistry, ociRepo))
}
if refRulesfile, ok := ociArtifacts[p.Name+common.RulesArtifactSuffix]; ok {
tokens := strings.Split(refRulesfile, "/")
ociRegistry := tokens[0]
ociRepo := filepath.Join(tokens[1:]...)
i.Upsert(pluginRulesToIndexEntry(p, ociRegistry, ociRepo))
i.Upsert(PluginRulesToIndexEntry(p, ociRegistry, ociRepo))
}
}

Expand Down
61 changes: 61 additions & 0 deletions build/registry/pkg/distribution/index_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright (C) 2022 The Falco 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 distribution_test

import (
"reflect"
"testing"

"github.com/falcosecurity/falcoctl/pkg/index"

"github.com/falcosecurity/plugins/build/registry/pkg/distribution"
"github.com/falcosecurity/plugins/build/registry/pkg/registry"
)

func TestPluginToIndexEntrySignature(t *testing.T) {
t.Parallel()

signature := &index.Signature{
Cosign: &index.CosignSignature{},
}

expected := signature

p := registry.Plugin{Signature: signature}

entry := distribution.PluginToIndexEntry(p, "", "")
if !reflect.DeepEqual(entry.Signature, expected) {
t.Fatalf("Index entry signature: expected %#v, got %v", expected, entry.Signature)
}
}

func TestPluginRulesToIndexEntrySignature(t *testing.T) {
t.Parallel()

signature := &index.Signature{
Cosign: &index.CosignSignature{},
}

expected := signature

p := registry.Plugin{Signature: signature}

entry := distribution.PluginRulesToIndexEntry(p, "", "")
if !reflect.DeepEqual(entry.Signature, expected) {
t.Fatalf("Index entry signature: expected %#v, got %v", expected, entry.Signature)
}
}
12 changes: 7 additions & 5 deletions build/registry/pkg/distribution/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ limitations under the License.
package distribution_test

import (
"github.com/falcosecurity/plugins/build/registry/pkg/distribution"
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"os"

"github.com/falcosecurity/plugins/build/registry/pkg/distribution"
)

const (
Expand All @@ -29,7 +31,7 @@ const (
registryFile = "testdata/registry.yaml"
wrongRegistryFile = "testdata/wrong-registry.yaml"
registryUser = "falcosecurity"
registry = "ghcr.io"
registryName = "ghcr.io"
)

var _ = Describe("Update index", func() {
Expand All @@ -39,7 +41,7 @@ var _ = Describe("Update index", func() {
Context("with registry file", func() {
BeforeEach(func() {
os.Setenv("REGISTRY_USER", registryUser)
os.Setenv("REGISTRY", registry)
os.Setenv("REGISTRY", registryName)
})
Context("with index file", func() {
BeforeEach(func() {
Expand All @@ -61,7 +63,7 @@ var _ = Describe("Update index", func() {
Context("without registry file", func() {
BeforeEach(func() {
os.Setenv("REGISTRY_USER", registryUser)
os.Setenv("REGISTRY", registry)
os.Setenv("REGISTRY", registryName)
})
Context("with index file", func() {
BeforeEach(func() {
Expand Down

0 comments on commit 3cb64a4

Please sign in to comment.