debian-forge-composer/internal/mocks/rpmrepo/rpmrepo.go
Achilleas Koutsou d0da8fd122 dnfjson: add package tests
The rpmrepo mock contains code to be used for testing depsolving.  It
creates a file server that serves the metadata in test/data/testrepo and
can be used as a repository for depsolve tests.

The dnfjson tests perform a single depsolve with an expected response.
The chain depsolve tests perform multiple depsolves that should produce
the same expected response:
- Single transaction using the ChainDepsove() function
- Two transactions for the same packages split in two with no extra
  repositories
- Two transactions for the same packages split in two with the main
  repository redefined

dnfjsontest: squash
2022-06-01 11:36:52 +01:00

47 lines
1,000 B
Go

package rpmrepo
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"github.com/osbuild/osbuild-composer/internal/rpmmd"
)
type testRepoServer struct {
Server *httptest.Server
RepoConfig rpmmd.RepoConfig
}
func NewTestServer() *testRepoServer {
server := httptest.NewServer(http.FileServer(http.Dir("../../test/data/testrepo/")))
testrepo := rpmmd.RepoConfig{
Name: "cs9-baseos",
BaseURL: server.URL,
CheckGPG: false,
IgnoreSSL: true,
RHSM: false,
}
return &testRepoServer{Server: server, RepoConfig: testrepo}
}
func (trs *testRepoServer) Close() {
trs.Server.Close()
}
// WriteConfig writes the repository config to the file defined by the given
// path. Assumes the location already exists.
func (trs *testRepoServer) WriteConfig(path string) {
fp, err := os.Create(path)
if err != nil {
panic(err)
}
data, err := json.Marshal(trs.RepoConfig)
if err != nil {
panic(err)
}
if _, err := fp.Write(data); err != nil {
panic(err)
}
}