Skip to content

Commit

Permalink
Included tests for findSoftware
Browse files Browse the repository at this point in the history
Signed-off-by: nathannaveen <42319948+nathannaveen@users.noreply.github.com>
  • Loading branch information
nathannaveen committed Feb 23, 2024
1 parent 15cbe51 commit 60c48e2
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/testing/backend/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ var skipMatrix = map[string]map[string]bool{
"TestVulnerability": {arango: true, ent: true},
// ent: query by id fails, Query_greater_than_-_no_score_value fails
"TestIngestVulnMetadata": {ent: true},

"TestFindSoftware": {ent: true, redis: true, arango: true},
}

type backend interface {
Expand Down
143 changes: 143 additions & 0 deletions internal/testing/backend/search_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
//go:build integration

package backend_test

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/guacsec/guac/internal/testing/testdata"
"github.com/guacsec/guac/pkg/assembler/graphql/model"
)

func TestFindSoftware(t *testing.T) {
b := setupTest(t)
tests := []struct {
name string
searchText string
want []model.PackageSourceOrArtifact
wantErr bool
InPkg []*model.PkgInputSpec
InSrc []*model.SourceInputSpec
InArt []*model.ArtifactInputSpec
}{
{
name: "default package",
InPkg: []*model.PkgInputSpec{testdata.P1},
searchText: "tensorflow",
want: []model.PackageSourceOrArtifact{
model.Package{
Type: "pypi",
Namespaces: []*model.PackageNamespace{{
Names: []*model.PackageName{{
Name: "tensorflow",
Versions: []*model.PackageVersion{{
Version: "",
Qualifiers: []*model.PackageQualifier{},
}},
}},
},
},
}},
},
{
name: "package no match",
InPkg: []*model.PkgInputSpec{testdata.P1},
searchText: "invalid",
want: []model.PackageSourceOrArtifact{},
},
{
name: "default artifact",
InArt: []*model.ArtifactInputSpec{
{
Algorithm: "sha256",
Digest: "testdigest",
},
},
searchText: "test",
want: []model.PackageSourceOrArtifact{
&model.Artifact{
Algorithm: "sha256",
Digest: "testdigest",
},
},
},
{
name: "artifact no match",
InArt: []*model.ArtifactInputSpec{
{
Algorithm: "sha256",
Digest: "testdigest",
},
},
searchText: "invalid",
want: []model.PackageSourceOrArtifact{},
},
{
name: "default source",
InSrc: []*model.SourceInputSpec{
{
Type: "git",
Namespace: "github.com/jeff",
Name: "myrepo",
},
},
searchText: "jeff",
want: []model.PackageSourceOrArtifact{
model.Source{
Type: "git",
Namespaces: []*model.SourceNamespace{{
Namespace: "github.com/jeff",
Names: []*model.SourceName{{
Name: "myrepo",
}},
}},
},
},
},
{
name: "source no match",
InSrc: []*model.SourceInputSpec{
{
Type: "git",
Namespace: "github.com/jeff",
Name: "myrepo",
},
},
searchText: "invalid",
want: []model.PackageSourceOrArtifact{},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := context.Background()

for _, p := range test.InPkg {
if _, err := b.IngestPackage(ctx, model.IDorPkgInput{PackageInput: p}); err != nil {
t.Fatalf("Could not ingest package: %v", err)
}
}
for _, s := range test.InSrc {
if _, err := b.IngestSource(ctx, model.IDorSourceInput{SourceInput: s}); err != nil {
t.Fatalf("Could not ingest source: %v", err)
}
}
for _, a := range test.InArt {
if _, err := b.IngestArtifact(ctx, &model.IDorArtifactInput{ArtifactInput: a}); err != nil {
t.Fatalf("Could not ingest artifact: %v", err)
}
}

got, err := b.FindSoftware(ctx, test.searchText)
if (err != nil) != test.wantErr {
t.Errorf("FindSoftware() error = %v, wantErr %v", err, test.wantErr)
return
}

if diff := cmp.Diff(test.want, got, commonOpts); diff != "" {
t.Errorf("FindSoftware() Unexpected results. (-want +got):\n%s", diff)
}
})
}
}

0 comments on commit 60c48e2

Please sign in to comment.