api tests: add store fixtures

Prior this commit there wasn't an easy to populate the store. The only way
was to call the weldr API or store methods. This design made testing of
various edges quite hard.

This commit adds store fixtures - an easy way how to define store state
before each test case.

In addition, the fixtures were refactored so that new instances are created
prior each test. Before this change the tests were in some cases dependant
on each other.
This commit is contained in:
Ondřej Budai 2019-11-27 20:26:28 +01:00 committed by Tom Gundersen
parent a00a0caa70
commit 66c57a05b7
3 changed files with 164 additions and 102 deletions

View file

@ -2,10 +2,16 @@ package rpmmd_mock
import ( import (
"fmt" "fmt"
"github.com/google/uuid"
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/rpmmd" "github.com/osbuild/osbuild-composer/internal/rpmmd"
"github.com/osbuild/osbuild-composer/internal/store"
"sort"
"time" "time"
) )
type FixtureGenerator func() Fixture
func generatePackageList() rpmmd.PackageList { func generatePackageList() rpmmd.PackageList {
baseTime, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z") baseTime, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
@ -37,72 +43,140 @@ func generatePackageList() rpmmd.PackageList {
packageList = append(packageList, basePackage, secondBuild) packageList = append(packageList, basePackage, secondBuild)
} }
sort.Slice(packageList, func(i, j int) bool {
return packageList[i].Name < packageList[j].Name
})
return packageList return packageList
} }
var basePackageList = fetchPackageList{ func createBaseStoreFixture() *store.Store {
generatePackageList(), var bName = "test"
nil, var b = blueprint.Blueprint{Name: bName, Version: "0.0.0"}
var date = time.Date(2019, 11, 27, 13, 19, 0, 0, time.FixedZone("UTC+1", 60*60))
s := store.New(nil)
s.Blueprints[bName] = b
s.Composes = map[uuid.UUID]store.Compose{
uuid.MustParse("e65f76f8-b0d9-4974-9dd7-745ae80b4721"): store.Compose{
QueueStatus: "WAITING",
Blueprint: &b,
OutputType: "tar",
Targets: nil,
JobCreated: date,
},
uuid.MustParse("e65f76f8-b0d9-4974-9dd7-745ae80b4722"): store.Compose{
QueueStatus: "RUNNING",
Blueprint: &b,
OutputType: "tar",
Targets: nil,
JobCreated: date,
JobStarted: date,
},
uuid.MustParse("e65f76f8-b0d9-4974-9dd7-745ae80b4723"): store.Compose{
QueueStatus: "FINISHED",
Blueprint: &b,
OutputType: "tar",
Targets: nil,
JobCreated: date,
JobStarted: date,
JobFinished: date,
},
uuid.MustParse("e65f76f8-b0d9-4974-9dd7-745ae80b4724"): store.Compose{
QueueStatus: "FAILED",
Blueprint: &b,
OutputType: "tar",
Targets: nil,
JobCreated: date,
JobStarted: date,
JobFinished: date,
},
}
return s
} }
var BaseFixture = Fixture{ func BaseFixture() Fixture {
basePackageList, return Fixture{
depsolve{ fetchPackageList{
[]rpmmd.PackageSpec{ generatePackageList(),
{ nil,
Name: "dep-package1", },
Epoch: 0, depsolve{
Version: "1.33", []rpmmd.PackageSpec{
Release: "2.fc30", {
Arch: "x86_64", Name: "dep-package1",
Epoch: 0,
Version: "1.33",
Release: "2.fc30",
Arch: "x86_64",
},
{
Name: "dep-package2",
Epoch: 0,
Version: "2.9",
Release: "1.fc30",
Arch: "x86_64",
},
}, },
{ nil,
Name: "dep-package2", },
Epoch: 0, createBaseStoreFixture(),
Version: "2.9", }
Release: "1.fc30", }
Arch: "x86_64",
func NonExistingPackage() Fixture {
return Fixture{
fetchPackageList{
generatePackageList(),
nil,
},
depsolve{
nil,
&rpmmd.DNFError{
Kind: "MarkingErrors",
Reason: "Error occurred when marking packages for installation: Problems in request:\nmissing packages: fash",
}, },
}, },
nil, createBaseStoreFixture(),
}, }
} }
var NonExistingPackage = Fixture{ func BadDepsolve() Fixture {
basePackageList, return Fixture{
depsolve{ fetchPackageList{
nil, generatePackageList(),
&rpmmd.DNFError{ nil,
Kind: "MarkingErrors",
Reason: "Error occurred when marking packages for installation: Problems in request:\nmissing packages: fash",
}, },
}, depsolve{
nil,
&rpmmd.DNFError{
Kind: "DepsolveError",
Reason: "There was a problem depsolving ['go2rpm']: \n Problem: conflicting requests\n - nothing provides askalono-cli needed by go2rpm-1-4.fc31.noarch",
},
},
createBaseStoreFixture(),
}
} }
var BadDepsolve = Fixture{ func BadFetch() Fixture {
basePackageList, return Fixture{
depsolve{ fetchPackageList{
nil, ret: nil,
&rpmmd.DNFError{ err: &rpmmd.DNFError{
Kind: "DepsolveError", Kind: "FetchError",
Reason: "There was a problem depsolving ['go2rpm']: \n Problem: conflicting requests\n - nothing provides askalono-cli needed by go2rpm-1-4.fc31.noarch", Reason: "There was a problem when fetching packages.",
},
}, },
}, depsolve{
} nil,
&rpmmd.DNFError{
var BadFetch = Fixture{ Kind: "DepsolveError",
fetchPackageList{ Reason: "There was a problem depsolving ['go2rpm']: \n Problem: conflicting requests\n - nothing provides askalono-cli needed by go2rpm-1-4.fc31.noarch",
ret: nil, },
err: &rpmmd.DNFError{ },
Kind: "FetchError", createBaseStoreFixture(),
Reason: "There was a problem when fetching packages.", }
},
},
depsolve{
nil,
&rpmmd.DNFError{
Kind: "DepsolveError",
Reason: "There was a problem depsolving ['go2rpm']: \n Problem: conflicting requests\n - nothing provides askalono-cli needed by go2rpm-1-4.fc31.noarch",
},
},
} }

View file

@ -1,6 +1,9 @@
package rpmmd_mock package rpmmd_mock
import "github.com/osbuild/osbuild-composer/internal/rpmmd" import (
"github.com/osbuild/osbuild-composer/internal/rpmmd"
"github.com/osbuild/osbuild-composer/internal/store"
)
type fetchPackageList struct { type fetchPackageList struct {
ret rpmmd.PackageList ret rpmmd.PackageList
@ -14,6 +17,7 @@ type depsolve struct {
type Fixture struct { type Fixture struct {
fetchPackageList fetchPackageList
depsolve depsolve
*store.Store
} }
type rpmmdMock struct { type rpmmdMock struct {

File diff suppressed because one or more lines are too long