Skip to content

Commit

Permalink
Fix test cases.
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Fiehe <[email protected]>
  • Loading branch information
Christoph Fiehe committed Oct 11, 2024
1 parent c0a11d5 commit 8db03d0
Show file tree
Hide file tree
Showing 28 changed files with 852 additions and 228 deletions.
57 changes: 34 additions & 23 deletions api/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func apiPublishList(c *gin.Context) {
func apiPublishShow(c *gin.Context) {
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
distribution := slashEscape(c.Params.ByName("distribution"))

collectionFactory := context.NewCollectionFactory()
collection := collectionFactory.PublishedRepoCollection()
Expand Down Expand Up @@ -179,7 +179,7 @@ type publishedRepoCreateParams struct {
// @Failure 500 {object} Error "Internal Error"
// @Router /api/publish/{prefix} [post]
func apiPublishRepoOrSnapshot(c *gin.Context) {
param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)

var b publishedRepoCreateParams
Expand Down Expand Up @@ -346,7 +346,7 @@ func apiPublishRepoOrSnapshot(c *gin.Context) {
func apiPublishUpdateSwitch(c *gin.Context) {
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := utils.SanitizePath(c.Params.ByName("distribution"))
distribution := slashEscape(c.Params.ByName("distribution"))

var b struct {
ForceOverwrite bool
Expand Down Expand Up @@ -484,7 +484,7 @@ func apiPublishDrop(c *gin.Context) {

param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
distribution := slashEscape(c.Params.ByName("distribution"))

collectionFactory := context.NewCollectionFactory()
collection := collectionFactory.PublishedRepoCollection()
Expand Down Expand Up @@ -515,9 +515,9 @@ func apiPublishSourcesCreate(c *gin.Context) {
b sourceParams
)

param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
distribution := slashEscape(c.Params.ByName("distribution"))

collectionFactory := context.NewCollectionFactory()
collection := collectionFactory.PublishedRepoCollection()
Expand Down Expand Up @@ -560,15 +560,15 @@ func apiPublishSourcesCreate(c *gin.Context) {
return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("unable to save to DB: %s", err)
}

Check warning on line 561 in api/publish.go

View check run for this annotation

Codecov / codecov/patch

api/publish.go#L560-L561

Added lines #L560 - L561 were not covered by tests

return &task.ProcessReturnValue{Code: http.StatusOK, Value: published}, nil
return &task.ProcessReturnValue{Code: http.StatusCreated, Value: published}, nil
})
}

// @Router /api/publish/{prefix}/{distribution}/sources [get]
func apiPublishSourcesList(c *gin.Context) {
param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
distribution := slashEscape(c.Params.ByName("distribution"))

collectionFactory := context.NewCollectionFactory()
collection := collectionFactory.PublishedRepoCollection()
Expand Down Expand Up @@ -601,9 +601,9 @@ func apiPublishSourcesUpdate(c *gin.Context) {
b []sourceParams
)

param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
distribution := slashEscape(c.Params.ByName("distribution"))

collectionFactory := context.NewCollectionFactory()
collection := collectionFactory.PublishedRepoCollection()
Expand Down Expand Up @@ -648,26 +648,37 @@ func apiPublishSourcesUpdate(c *gin.Context) {

// @Router /api/publish/{prefix}/{distribution}/sources [delete]
func apiPublishSourcesDelete(c *gin.Context) {
param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
distribution := slashEscape(c.Params.ByName("distribution"))

collectionFactory := context.NewCollectionFactory()
collection := collectionFactory.PublishedRepoCollection()

published, err := collection.ByStoragePrefixDistribution(storage, prefix, distribution)
if err != nil {
AbortWithJSONError(c, http.StatusNotFound, fmt.Errorf("unable to show: %s", err))
AbortWithJSONError(c, http.StatusNotFound, fmt.Errorf("unable to delete: %s", err))
return
}

Check warning on line 662 in api/publish.go

View check run for this annotation

Codecov / codecov/patch

api/publish.go#L660-L662

Added lines #L660 - L662 were not covered by tests

err = collection.LoadComplete(published, collectionFactory)
if err != nil {
AbortWithJSONError(c, http.StatusInternalServerError, fmt.Errorf("unable to show: %s", err))
AbortWithJSONError(c, http.StatusInternalServerError, fmt.Errorf("unable to delete: %s", err))
return
}

Check warning on line 668 in api/publish.go

View check run for this annotation

Codecov / codecov/patch

api/publish.go#L666-L668

Added lines #L666 - L668 were not covered by tests

published.DropRevision()

resources := []string{string(published.Key())}
taskName := fmt.Sprintf("Update published %s repository %s/%s", published.SourceKind, published.StoragePrefix(), published.Distribution)
maybeRunTaskInBackground(c, taskName, resources, func(out aptly.Progress, _ *task.Detail) (*task.ProcessReturnValue, error) {
err = collection.Update(published)
if err != nil {
return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("unable to save to DB: %s", err)
}

Check warning on line 678 in api/publish.go

View check run for this annotation

Codecov / codecov/patch

api/publish.go#L677-L678

Added lines #L677 - L678 were not covered by tests

return &task.ProcessReturnValue{Code: http.StatusOK, Value: published}, nil
})
}

// @Router /api/publish/{prefix}/{distribution}/sources/{component} [put]
Expand All @@ -677,10 +688,10 @@ func apiPublishSourceUpdate(c *gin.Context) {
b sourceParams
)

param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
component := parseEscapedPath(c.Params.ByName("component"))
distribution := slashEscape(c.Params.ByName("distribution"))
component := slashEscape(c.Params.ByName("component"))

collectionFactory := context.NewCollectionFactory()
collection := collectionFactory.PublishedRepoCollection()
Expand Down Expand Up @@ -737,10 +748,10 @@ func apiPublishSourceUpdate(c *gin.Context) {
func apiPublishSourceDelete(c *gin.Context) {
var err error

param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
component := parseEscapedPath(c.Params.ByName("component"))
distribution := slashEscape(c.Params.ByName("distribution"))
component := slashEscape(c.Params.ByName("component"))

collectionFactory := context.NewCollectionFactory()
collection := collectionFactory.PublishedRepoCollection()
Expand Down Expand Up @@ -776,9 +787,9 @@ func apiPublishSourceDelete(c *gin.Context) {

// @Router /api/publish/{prefix}/{distribution}/update [post]
func apiPublishUpdate(c *gin.Context) {
param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
distribution := slashEscape(c.Params.ByName("distribution"))

var b struct {
AcquireByHash *bool
Expand Down
7 changes: 5 additions & 2 deletions cmd/publish_source_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,19 @@ func aptlyPublishSourceAdd(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to save to DB: %s", err)
}

Check warning on line 57 in cmd/publish_source_add.go

View check run for this annotation

Codecov / codecov/patch

cmd/publish_source_add.go#L56-L57

Added lines #L56 - L57 were not covered by tests

context.Progress().Printf("\nYou can run 'aptly publish update %s %s' to update the content of the published repository.\n",
distribution, published.StoragePrefix())

return err
}

func makeCmdPublishSourceAdd() *commander.Command {
cmd := &commander.Command{
Run: aptlyPublishSourceAdd,
UsageLine: "add <distribution> <source>",
Short: "add package source to published repository",
Short: "add source to staged source list of published repository",
Long: `
The command adds (in place) one or multiple package sources to a published repository.
The command adds sources to the staged source list of the published repository.
The flag -component is mandatory. Use a comma-separated list of components, if
multiple components should be modified. The number of given components must be
Expand Down
6 changes: 3 additions & 3 deletions cmd/publish_source_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func makeCmdPublishSourceDrop() *commander.Command {
cmd := &commander.Command{
Run: aptlyPublishSourceDrop,
UsageLine: "drop <distribution>",
Short: "drops revision of published repository",
Short: "drops staged source changes of published repository",
Long: `
Command drops revision of a published repository.
Command drops the staged source changes of the published repository.
Example:
$ aptly publish revision drop wheezy
$ aptly publish source drop wheezy
`,
Flag: *flag.NewFlagSet("aptly-publish-revision-create", flag.ExitOnError),
}
Expand Down
9 changes: 6 additions & 3 deletions cmd/publish_source_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func aptlyPublishSourceRemove(cmd *commander.Command, args []string) error {
for _, component := range components {
name, exists := sources[component]
if !exists {
return fmt.Errorf("unable to remove: Component %q is not part of revision", component)
return fmt.Errorf("unable to remove: component %q does not exist", component)
}
context.Progress().Printf("Removing component %q with source %q [%s]...\n", component, name, published.SourceKind)

Expand All @@ -54,16 +54,19 @@ func aptlyPublishSourceRemove(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to save to DB: %s", err)
}

Check warning on line 55 in cmd/publish_source_remove.go

View check run for this annotation

Codecov / codecov/patch

cmd/publish_source_remove.go#L54-L55

Added lines #L54 - L55 were not covered by tests

context.Progress().Printf("\nYou can run 'aptly publish update %s %s' to update the content of the published repository.\n",
distribution, published.StoragePrefix())

return err
}

func makeCmdPublishSourceRemove() *commander.Command {
cmd := &commander.Command{
Run: aptlyPublishSourceRemove,
UsageLine: "remove <distribution> [[<endpoint>:]<prefix>] <source>",
Short: "remove package source to published repository",
Short: "remove source from staged source list of published repository",
Long: `
The command removes one or multiple components from a published repository.
The command removes sources from the staged source list of the published repository.
The flag -component is mandatory. Use a comma-separated list of components, if
multiple components should be removed, e.g.:
Expand Down
7 changes: 5 additions & 2 deletions cmd/publish_source_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,19 @@ func aptlyPublishSourceUpdate(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to save to DB: %s", err)
}

Check warning on line 57 in cmd/publish_source_update.go

View check run for this annotation

Codecov / codecov/patch

cmd/publish_source_update.go#L56-L57

Added lines #L56 - L57 were not covered by tests

context.Progress().Printf("\nYou can run 'aptly publish update %s %s' to update the content of the published repository.\n",
distribution, published.StoragePrefix())

return err
}

func makeCmdPublishSourceUpdate() *commander.Command {
cmd := &commander.Command{
Run: aptlyPublishSourceUpdate,
UsageLine: "update <distribution> <source>",
Short: "update package source to published repository",
Short: "update source in staged source list of published repository",
Long: `
The command updates one or multiple components in a published repository.
The command updates sources in the staged source list of the published repository.
The flag -component is mandatory. Use a comma-separated list of components, if
multiple components should be modified. The number of given components must be
Expand Down
55 changes: 18 additions & 37 deletions cmd/publish_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func aptlyPublishSwitch(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to switch: %s", err)
}

if published.SourceKind != deb.SourceSnapshot {
return fmt.Errorf("unable to switch: not a published snapshot repository")
}

err = collectionFactory.PublishedRepoCollection().LoadComplete(published, collectionFactory)
if err != nil {
return fmt.Errorf("unable to switch: %s", err)

Check warning on line 52 in cmd/publish_switch.go

View check run for this annotation

Codecov / codecov/patch

cmd/publish_switch.go#L52

Added line #L52 was not covered by tests
Expand All @@ -57,46 +61,23 @@ func aptlyPublishSwitch(cmd *commander.Command, args []string) error {
return fmt.Errorf("mismatch in number of components (%d) and snapshots (%d)", len(components), len(names))
}

if published.SourceKind == deb.SourceLocalRepo {
localRepoCollection := collectionFactory.LocalRepoCollection()
for i, component := range components {
if !utils.StrSliceHasItem(publishedComponents, component) {
return fmt.Errorf("unable to switch: component %s does not exist in published repository", component)
}

localRepo, err := localRepoCollection.ByName(names[i])
if err != nil {
return fmt.Errorf("unable to switch: %s", err)
}

err = localRepoCollection.LoadComplete(localRepo)
if err != nil {
return fmt.Errorf("unable to switch: %s", err)
}
snapshotCollection := collectionFactory.SnapshotCollection()
for i, component := range components {
if !utils.StrSliceHasItem(publishedComponents, component) {
return fmt.Errorf("unable to switch: component %s does not exist in published repository", component)
}

published.UpdateLocalRepo(component, localRepo)
snapshot, err := snapshotCollection.ByName(names[i])
if err != nil {
return fmt.Errorf("unable to switch: %s", err)
}
} else if published.SourceKind == deb.SourceSnapshot {
snapshotCollection := collectionFactory.SnapshotCollection()
for i, component := range components {
if !utils.StrSliceHasItem(publishedComponents, component) {
return fmt.Errorf("unable to switch: component %s does not exist in published repository", component)
}

snapshot, err := snapshotCollection.ByName(names[i])
if err != nil {
return fmt.Errorf("unable to switch: %s", err)
}

err = snapshotCollection.LoadComplete(snapshot)
if err != nil {
return fmt.Errorf("unable to switch: %s", err)
}

published.UpdateSnapshot(component, snapshot)

err = snapshotCollection.LoadComplete(snapshot)
if err != nil {
return fmt.Errorf("unable to switch: %s", err)
}
} else {
return fmt.Errorf("unknown published repository type")

published.UpdateSnapshot(component, snapshot)
}

signer, err := getSigner(context.Flags())
Expand Down
4 changes: 3 additions & 1 deletion system/t06_publish/PublishSourceAdd1Test_gold
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Test
Adding component "test" with source "snap2" [snapshot]...

You can run 'aptly publish update maverick .' to update the content of the published repository.
5 changes: 4 additions & 1 deletion system/t06_publish/PublishSourceAdd2Test_gold
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
Test
Adding component "test" with source "snap2" [snapshot]...
Adding component "other-test" with source "snap3" [snapshot]...

You can run 'aptly publish update maverick .' to update the content of the published repository.
2 changes: 1 addition & 1 deletion system/t06_publish/PublishSourceAdd3Test_gold
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Test
ERROR: unable to add: component "main" has already been added
2 changes: 1 addition & 1 deletion system/t06_publish/PublishSourceDrop1Test_gold
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Test
Source changes have been removed successfully.
4 changes: 3 additions & 1 deletion system/t06_publish/PublishSourceList1Test_gold
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Test
Sources:
main: snap1 [snapshot]
test: snap2 [snapshot]
10 changes: 10 additions & 0 deletions system/t06_publish/PublishSourceList2Test_gold
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"Component": "main",
"Name": "snap1"
},
{
"Component": "test",
"Name": "snap2"
}
]
1 change: 1 addition & 0 deletions system/t06_publish/PublishSourceList3Test_gold
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ERROR: unable to list: no source changes exist
4 changes: 3 additions & 1 deletion system/t06_publish/PublishSourceRemove1Test_gold
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Test
Removing component "test" with source "snap2" [snapshot]...

You can run 'aptly publish update maverick .' to update the content of the published repository.
5 changes: 4 additions & 1 deletion system/t06_publish/PublishSourceRemove2Test_gold
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
Test
Removing component "test" with source "snap2" [snapshot]...
Removing component "other-test" with source "snap3" [snapshot]...

You can run 'aptly publish update maverick .' to update the content of the published repository.
2 changes: 1 addition & 1 deletion system/t06_publish/PublishSourceRemove3Test_gold
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Test
ERROR: unable to remove: component "not-existent" does not exist
4 changes: 3 additions & 1 deletion system/t06_publish/PublishSourceUpdate1Test_gold
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Test
Updating component "main" with source "snap2" [snapshot]...

You can run 'aptly publish update maverick .' to update the content of the published repository.
5 changes: 4 additions & 1 deletion system/t06_publish/PublishSourceUpdate2Test_gold
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
Test
Updating component "main" with source "snap2" [snapshot]...
Updating component "test" with source "snap3" [snapshot]...

You can run 'aptly publish update maverick .' to update the content of the published repository.
2 changes: 1 addition & 1 deletion system/t06_publish/PublishSourceUpdate3Test_gold
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Test
ERROR: unable to update: component "not-existent" does not exist
2 changes: 1 addition & 1 deletion system/t06_publish/PublishSwitch6Test_gold
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ERROR: unable to switch: local repo with name snap1 not found
ERROR: unable to switch: not a published snapshot repository
8 changes: 8 additions & 0 deletions system/t06_publish/PublishUpdate15Test_gold
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Loading packages...
Generating metadata files and linking package files...
Finalizing metadata files...
Signing file 'Release' with gpg, please enter your passphrase when prompted:
Clearsigning file 'Release' with gpg, please enter your passphrase when prompted:
Cleaning up prefix "." components ...

Published snapshot repository ./maverick (origin: LP-PPA-gladky-anton-gnuplot) [i386] publishes {main: [snap1]: Snapshot from mirror [gnuplot-maverick]: http://ppa.launchpad.net/gladky-anton/gnuplot/ubuntu/ maverick}, {other-test: [snap3]: Created as empty}, {test: [snap2]: Created as empty} has been successfully updated.
Loading

0 comments on commit 8db03d0

Please sign in to comment.