Skip to content

Commit

Permalink
Ent: error management when closing Ent client during tests (#1478)
Browse files Browse the repository at this point in the history
* error management when closing Ent client

Signed-off-by: mrizzi <[email protected]>

* Ent - Removed go-txdb from tests

Signed-off-by: mrizzi <[email protected]>

---------

Signed-off-by: mrizzi <[email protected]>
  • Loading branch information
mrizzi authored Nov 13, 2023
1 parent 3c8609f commit 5521770
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 244 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ require (
entgo.io/ent v0.12.4
github.com/99designs/gqlgen v0.17.39
github.com/CycloneDX/cyclonedx-go v0.7.2
github.com/DATA-DOG/go-txdb v0.1.7
github.com/Khan/genqlient v0.6.0
github.com/Masterminds/semver v1.5.0
github.com/arangodb/go-driver v1.6.0
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ github.com/CycloneDX/cyclonedx-go v0.7.2 h1:kKQ0t1dPOlugSIYVOMiMtFqeXI2wp/f5DBId
github.com/CycloneDX/cyclonedx-go v0.7.2/go.mod h1:K2bA+324+Og0X84fA8HhN2X066K7Bxz4rpMQ4ZhjtSk=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/DATA-DOG/go-txdb v0.1.7 h1:ibr3YvD3SKI4oBPbXbmzsn7eCPlg9oFdDdFtsWCvy7Q=
github.com/DATA-DOG/go-txdb v0.1.7/go.mod h1:l06JaBQdV+y4aWAmDmWj4NwfnJknEXBxg8d4B8sJzXA=
github.com/Khan/genqlient v0.6.0 h1:Bwb1170ekuNIVIwTJEqvO8y7RxBxXu639VJOkKSrwAk=
github.com/Khan/genqlient v0.6.0/go.mod h1:rvChwWVTqXhiapdhLDV4bp9tz/Xvtewwkon4DpWWCRM=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
Expand Down Expand Up @@ -266,8 +264,6 @@ github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-openapi/inflect v0.19.0 h1:9jCH9scKIbHeV9m12SmPilScz6krDxKRasNNSNPXu/4=
github.com/go-openapi/inflect v0.19.0/go.mod h1:lHpZVlpIQqLyKwJ4N+YSc9hchQy/i12fJykb83CRBH4=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
Expand Down
6 changes: 3 additions & 3 deletions pkg/assembler/backends/ent/backend/hashequal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ func (s *Suite) TestIngestHashEquals() {
},
Query: &model.HashEqualSpec{
Artifacts: []*model.ArtifactSpec{{
ID: ptrfrom.String("9"),
ID: ptrfrom.String("3"),
}},
},
ExpHE: []*model.HashEqual{
Expand All @@ -667,7 +667,7 @@ func (s *Suite) TestIngestHashEquals() {
},
Query: &model.HashEqualSpec{
Artifacts: []*model.ArtifactSpec{{
ID: ptrfrom.String("10"),
ID: ptrfrom.String("1"),
}},
},
ExpHE: []*model.HashEqual{
Expand Down Expand Up @@ -748,7 +748,7 @@ func (s *Suite) TestIngestHashEquals() {
Digest: ptrfrom.String("7A8F47318E4676DACB0142AFA0B83029CD7BEFD9"),
},
{
ID: ptrfrom.String("21"),
ID: ptrfrom.String("3"),
},
},
},
Expand Down
36 changes: 19 additions & 17 deletions pkg/assembler/backends/ent/backend/neighbors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,26 @@ func (s *Suite) TestNode() {
}

func (s *Suite) TestNodes() {
be, err := GetBackend(s.Client)
s.Require().NoError(err)
s.Run("HappyPath", func() {
be, err := GetBackend(s.Client)
s.Require().NoError(err)

v, err := be.IngestArtifact(s.Ctx, a1)
s.Require().NoError(err)
v, err := be.IngestArtifact(s.Ctx, a1)
s.Require().NoError(err)

p, err := be.IngestPackage(s.Ctx, *p4)
s.Require().NoError(err)
p, err := be.IngestPackage(s.Ctx, *p4)
s.Require().NoError(err)

nodes, err := be.Nodes(s.Ctx, []string{v.ID, p.ID, p.Namespaces[0].Names[0].Versions[0].ID})
s.Require().NoError(err)
if diff := cmp.Diff(a1out, nodes[0], ignoreID, ignoreEmptySlices); diff != "" {
s.T().Errorf("Unexpected results. (-want +got):\n%s", diff)
}
if diff := cmp.Diff(p4outNamespace, nodes[1], ignoreID, ignoreEmptySlices); diff != "" {
s.T().Errorf("Unexpected results. (-want +got):\n%s", diff)
}
if diff := cmp.Diff(p4out, nodes[2], ignoreID, ignoreEmptySlices); diff != "" {
s.T().Errorf("Unexpected results. (-want +got):\n%s", diff)
}
nodes, err := be.Nodes(s.Ctx, []string{v.ID, p.ID, p.Namespaces[0].Names[0].Versions[0].ID})
s.Require().NoError(err)
if diff := cmp.Diff(a1out, nodes[0], ignoreID, ignoreEmptySlices); diff != "" {
s.T().Errorf("Unexpected results. (-want +got):\n%s", diff)
}
if diff := cmp.Diff(p4outNamespace, nodes[1], ignoreID, ignoreEmptySlices); diff != "" {
s.T().Errorf("Unexpected results. (-want +got):\n%s", diff)
}
if diff := cmp.Diff(p4out, nodes[2], ignoreID, ignoreEmptySlices); diff != "" {
s.T().Errorf("Unexpected results. (-want +got):\n%s", diff)
}
})
}
54 changes: 28 additions & 26 deletions pkg/assembler/backends/ent/backend/occurrence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,35 +210,37 @@ var s2out = &model.Source{
}

func (s *Suite) TestOccurrenceHappyPath() {
be, err := GetBackend(s.Client)
s.Require().NoError(err)
s.Run("HappyPath", func() {
be, err := GetBackend(s.Client)
s.Require().NoError(err)

_, err = be.IngestPackage(s.Ctx, *p1)
s.Require().NoError(err)
_, err = be.IngestPackage(s.Ctx, *p1)
s.Require().NoError(err)

_, err = be.IngestArtifact(s.Ctx, a1)
s.Require().NoError(err)
_, err = be.IngestArtifact(s.Ctx, a1)
s.Require().NoError(err)

occ, err := be.IngestOccurrence(s.Ctx,
model.PackageOrSourceInput{
Package: p1,
},
*a1,
model.IsOccurrenceInputSpec{
Justification: "test justification",
},
)
s.Require().NoError(err)
s.Require().NotNil(occ)
s.Equal("test justification", occ.Justification)
s.Equal(a1.Digest, occ.Artifact.Digest)

if pkgSrc, ok := occ.Subject.(*model.Package); ok && pkgSrc != nil {
s.Equal(p1.Type, pkgSrc.Type)
s.NotEmpty(pkgSrc.Namespaces[0].Names[0].Versions[0].ID)
} else {
s.Failf("fail", "subject is not a package, got %T", occ.Subject)
}
occ, err := be.IngestOccurrence(s.Ctx,
model.PackageOrSourceInput{
Package: p1,
},
*a1,
model.IsOccurrenceInputSpec{
Justification: "test justification",
},
)
s.Require().NoError(err)
s.Require().NotNil(occ)
s.Equal("test justification", occ.Justification)
s.Equal(a1.Digest, occ.Artifact.Digest)

if pkgSrc, ok := occ.Subject.(*model.Package); ok && pkgSrc != nil {
s.Equal(p1.Type, pkgSrc.Type)
s.NotEmpty(pkgSrc.Namespaces[0].Names[0].Versions[0].ID)
} else {
s.Failf("fail", "subject is not a package, got %T", occ.Subject)
}
})
}

func (s *Suite) TestOccurrence() {
Expand Down
85 changes: 59 additions & 26 deletions pkg/assembler/backends/ent/backend/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,14 @@ func (s *Suite) Test_get_package_helpers() {
{Key: "a", Value: "b"},
},
}
p2Spec := model.PkgInputSpec{
Type: "apk",
Namespace: ptr("test"),
Name: "alpine",
Version: ptr("1.0.0"),
Subpath: ptr("subpath"),
}

_, err := WithinTX(s.Ctx, s.Client, func(ctx context.Context) (*ent.PackageVersion, error) {
return upsertPackage(s.Ctx, ent.TxFromContext(ctx), p2Spec)
s.Run("HappyPath", func() {
ingestP2(s)
ingestP1(s)
})
s.Require().NoError(err)
pkgVersionID, err := WithinTX(s.Ctx, s.Client, func(ctx context.Context) (*ent.PackageVersion, error) {
return upsertPackage(s.Ctx, ent.TxFromContext(ctx), p1Spec)
})
s.Require().NoError(err)
s.Require().NotNil(pkgVersionID)

s.Run("getPkgName", func() {
ingestP2(s)
ingestP1(s)
pkgName, err := getPkgName(s.Ctx, s.Client, model.PkgInputSpec{
Type: "apk",
Namespace: ptr("test"),
Expand All @@ -113,12 +102,16 @@ func (s *Suite) Test_get_package_helpers() {
})

s.Run("getPkgVersion", func() {
ingestP2(s)
ingestP1(s)
pkgVersion, err := getPkgVersion(s.Ctx, s.Client, p1Spec)
s.Require().NoError(err)
s.Require().NotNil(pkgVersion)
})

s.Run("pkgTreeFromVersion", func() {
ingestP2(s)
ingestP1(s)
pkgVersion, err := getPkgVersion(s.Ctx, s.Client, p1Spec)
s.Require().NoError(err)
pkgTree, err := pkgTreeFromVersion(s.Ctx, pkgVersion)
Expand All @@ -134,6 +127,21 @@ func (s *Suite) Test_get_package_helpers() {
})
}

func ingestP2(s *Suite) {
p2Spec := model.PkgInputSpec{
Type: "apk",
Namespace: ptr("test"),
Name: "alpine",
Version: ptr("1.0.0"),
Subpath: ptr("subpath"),
}

_, err := WithinTX(s.Ctx, s.Client, func(ctx context.Context) (*ent.PackageVersion, error) {
return upsertPackage(s.Ctx, ent.TxFromContext(ctx), p2Spec)
})
s.Require().NoError(err)
}

func (s *Suite) TestEmptyQualifiersPredicate() {
spec := model.PkgInputSpec{
Type: "apk",
Expand All @@ -147,24 +155,26 @@ func (s *Suite) TestEmptyQualifiersPredicate() {
},
}

pkg, err := WithinTX(s.Ctx, s.Client, func(ctx context.Context) (*ent.PackageVersion, error) {
return upsertPackage(s.Ctx, ent.TxFromContext(ctx), spec)
s.Run("HappyPath", func() {
ingestP1(s)
})
s.Require().NoError(err)
s.Require().NotNil(pkg)

// Ingest twice to ensure upserts are working
pkg, err = WithinTX(s.Ctx, s.Client, func(ctx context.Context) (*ent.PackageVersion, error) {
return upsertPackage(s.Ctx, ent.TxFromContext(ctx), spec)
s.Run("Ingest twice", func() {
ingestP1(s)
// Ingest twice to ensure upserts are working
pkg, err := WithinTX(s.Ctx, s.Client, func(ctx context.Context) (*ent.PackageVersion, error) {
return upsertPackage(s.Ctx, ent.TxFromContext(ctx), spec)
})
s.Require().NoError(err)
s.Require().NotNil(pkg)
})
s.Require().NoError(err)
s.Require().NotNil(pkg)

s.Run("Empty keys", func() {
ingestP1(s)
s.Empty(s.Client.PackageVersion.Query().Where(packageversion.QualifiersIsEmpty()).AllX(s.Ctx))
})

s.Run("No Qualifiers", func() {
ingestP1(s)
spec.Qualifiers = nil
pkg, err := WithinTX(s.Ctx, s.Client, func(ctx context.Context) (*ent.PackageVersion, error) {
return upsertPackage(s.Ctx, ent.TxFromContext(ctx), spec)
Expand All @@ -176,23 +186,27 @@ func (s *Suite) TestEmptyQualifiersPredicate() {
})

s.Run("Single key", func() {
ingestP1(s)
versions := s.Client.PackageVersion.Query().Where(packageversion.QualifiersWithKeys("arch", "a")).AllX(s.Ctx)
s.NotEmpty(versions)
})

s.Run("Multiple keys", func() {
ingestP1(s)
versions := s.Client.PackageVersion.Query().Where(packageversion.QualifiersContains("arch", "arm64")).AllX(s.Ctx)
s.NotEmpty(versions)
})

s.Run("Using spec - Null value", func() {
ingestP1(s)
versions := s.Client.PackageVersion.Query().Where(
packageversion.QualifiersMatch([]*model.PackageQualifierSpec{{Key: "arch"}}, false),
).AllX(s.Ctx)
s.NotEmpty(versions)
})

s.Run("Using spec - Multiple", func() {
ingestP1(s)
versions := s.Client.PackageVersion.Query().Where(
packageversion.QualifiersMatch([]*model.PackageQualifierSpec{
{Key: "arch"},
Expand All @@ -204,6 +218,25 @@ func (s *Suite) TestEmptyQualifiersPredicate() {

}

func ingestP1(s *Suite) {
p1Spec := model.PkgInputSpec{
Type: "apk",
Namespace: ptr("test"),
Name: "alpine",
Version: ptr("1.0.0"),
Subpath: ptr("subpath"),
Qualifiers: []*model.PackageQualifierInputSpec{
{Key: "arch", Value: "arm64"},
{Key: "a", Value: "b"},
},
}
pkg, err := WithinTX(s.Ctx, s.Client, func(ctx context.Context) (*ent.PackageVersion, error) {
return upsertPackage(s.Ctx, ent.TxFromContext(ctx), p1Spec)
})
s.Require().NoError(err)
s.Require().NotNil(pkg)
}

func (s *Suite) Test_IngestPackages() {
ctx := s.Ctx
tests := []struct {
Expand Down
Loading

0 comments on commit 5521770

Please sign in to comment.