mirror of
https://github.com/apricote/hcloud-upload-image.git
synced 2026-02-06 17:57:06 +00:00
fix: upload from local image generates broken command (#98)
While adding support for qcow2 images in #69 I broke support for local images. Building a shell pipeline through string concatenation is not a good idea... The specific issue was fixed and I also moved building the shell pipeline to a separate function and added unit tests for all cases, so it should be easier to spot these issues in the future. Closes #97
This commit is contained in:
parent
28bf5380f3
commit
420dcf94c9
3 changed files with 173 additions and 49 deletions
|
|
@ -1,33 +1,110 @@
|
|||
package hcloudimages_test
|
||||
package hcloudimages
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"github.com/hetznercloud/hcloud-go/v2/hcloud"
|
||||
|
||||
"github.com/apricote/hcloud-upload-image/hcloudimages"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func ExampleClient_Upload() {
|
||||
client := hcloudimages.NewClient(
|
||||
hcloud.NewClient(hcloud.WithToken("<your token>")),
|
||||
)
|
||||
|
||||
imageURL, err := url.Parse("https://example.com/disk-image.raw.bz2")
|
||||
func mustParseURL(s string) *url.URL {
|
||||
u, err := url.Parse(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
image, err := client.Upload(context.TODO(), hcloudimages.UploadOptions{
|
||||
ImageURL: imageURL,
|
||||
ImageCompression: hcloudimages.CompressionBZ2,
|
||||
Architecture: hcloud.ArchitectureX86,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("Uploaded Image: %d", image.ID)
|
||||
return u
|
||||
}
|
||||
|
||||
func TestAssembleCommand(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
options UploadOptions
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "local raw",
|
||||
options: UploadOptions{},
|
||||
want: "bash -c 'set -euo pipefail && dd of=/dev/sda bs=4M && sync'",
|
||||
},
|
||||
{
|
||||
name: "remote raw",
|
||||
options: UploadOptions{
|
||||
ImageURL: mustParseURL("https://example.com/image.xz"),
|
||||
},
|
||||
want: "bash -c 'set -euo pipefail && wget --no-verbose -O - \"https://example.com/image.xz\" | dd of=/dev/sda bs=4M && sync'",
|
||||
},
|
||||
{
|
||||
name: "local xz",
|
||||
options: UploadOptions{
|
||||
ImageCompression: CompressionXZ,
|
||||
},
|
||||
want: "bash -c 'set -euo pipefail && xz -cd | dd of=/dev/sda bs=4M && sync'",
|
||||
},
|
||||
{
|
||||
name: "remote xz",
|
||||
options: UploadOptions{
|
||||
ImageURL: mustParseURL("https://example.com/image.xz"),
|
||||
ImageCompression: CompressionXZ,
|
||||
},
|
||||
want: "bash -c 'set -euo pipefail && wget --no-verbose -O - \"https://example.com/image.xz\" | xz -cd | dd of=/dev/sda bs=4M && sync'",
|
||||
},
|
||||
{
|
||||
name: "local bz2",
|
||||
options: UploadOptions{
|
||||
ImageCompression: CompressionBZ2,
|
||||
},
|
||||
want: "bash -c 'set -euo pipefail && bzip2 -cd | dd of=/dev/sda bs=4M && sync'",
|
||||
},
|
||||
{
|
||||
name: "remote bz2",
|
||||
options: UploadOptions{
|
||||
ImageURL: mustParseURL("https://example.com/image.bz2"),
|
||||
ImageCompression: CompressionXZ,
|
||||
},
|
||||
want: "bash -c 'set -euo pipefail && wget --no-verbose -O - \"https://example.com/image.bz2\" | xz -cd | dd of=/dev/sda bs=4M && sync'",
|
||||
},
|
||||
{
|
||||
name: "local qcow2",
|
||||
options: UploadOptions{
|
||||
ImageFormat: FormatQCOW2,
|
||||
},
|
||||
want: "bash -c 'set -euo pipefail && tee image.qcow2 > /dev/null && qemu-img dd -f qcow2 -O raw if=image.qcow2 of=/dev/sda bs=4M && sync'",
|
||||
},
|
||||
{
|
||||
name: "remote qcow2",
|
||||
options: UploadOptions{
|
||||
ImageURL: mustParseURL("https://example.com/image.qcow2"),
|
||||
ImageFormat: FormatQCOW2,
|
||||
},
|
||||
want: "bash -c 'set -euo pipefail && wget --no-verbose -O - \"https://example.com/image.qcow2\" | tee image.qcow2 > /dev/null && qemu-img dd -f qcow2 -O raw if=image.qcow2 of=/dev/sda bs=4M && sync'",
|
||||
},
|
||||
|
||||
{
|
||||
name: "unknown compression",
|
||||
options: UploadOptions{
|
||||
ImageCompression: "noodle",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
|
||||
{
|
||||
name: "unknown format",
|
||||
options: UploadOptions{
|
||||
ImageFormat: "poodle",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := assembleCommand(tt.options)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("assembleCommand() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("assembleCommand() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue