309 lines
6.4 KiB
Go
309 lines
6.4 KiB
Go
|
package instruction
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestCopyInvalidSyntax(t *testing.T) {
|
||
|
tests := []string{
|
||
|
"one-arg",
|
||
|
"src dst extra-arg",
|
||
|
}
|
||
|
|
||
|
for i, test := range tests {
|
||
|
inst := new(copy)
|
||
|
err := inst.Build(test)
|
||
|
if err != ErrInvalidSyntax {
|
||
|
t.Errorf("[%d] expected error <%s>, got <%s>", i, ErrInvalidSyntax, err)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
func TestCopyBuildArgs(t *testing.T) {
|
||
|
tests := []string{
|
||
|
"source destination",
|
||
|
"\tsource destination",
|
||
|
" source destination",
|
||
|
"source\t destination",
|
||
|
"source destination",
|
||
|
"source \tdestination",
|
||
|
"source destination",
|
||
|
"source destination\t",
|
||
|
"source\t\tdestination\t",
|
||
|
"source \t\t destination\t",
|
||
|
"source\t \tdestination\t",
|
||
|
}
|
||
|
|
||
|
for i, test := range tests {
|
||
|
inst := new(copy)
|
||
|
err := inst.Build(test)
|
||
|
if err != nil {
|
||
|
t.Errorf("[%d] unexpected error <%s>", i, err)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
if inst.Src != "source" {
|
||
|
t.Errorf("[%d] expected 'source', got '%s'", i, inst.Src)
|
||
|
continue
|
||
|
}
|
||
|
if inst.Dst != "destination" {
|
||
|
t.Errorf("[%d] expected 'source', got '%s'", i, inst.Dst)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestCopySourceNotExist(t *testing.T) {
|
||
|
defer os.RemoveAll("/tmp/destination")
|
||
|
raw := "/tmp/source /tmp/destination"
|
||
|
|
||
|
ctx, err := CreateContext("apt-get", "")
|
||
|
if err != nil {
|
||
|
t.Fatalf("cannot create context")
|
||
|
}
|
||
|
|
||
|
for i := 0; i < 2; i++ {
|
||
|
inst := new(copy)
|
||
|
err := inst.Build(raw)
|
||
|
if err != nil {
|
||
|
t.Fatalf("[%d] unexpected error <%s>", i, err)
|
||
|
}
|
||
|
|
||
|
if i == 0 {
|
||
|
_, err = inst.Exec(*ctx)
|
||
|
} else {
|
||
|
_, err = inst.DryRun(*ctx)
|
||
|
}
|
||
|
if err == nil {
|
||
|
t.Fatalf("[%d] expected error", i)
|
||
|
}
|
||
|
|
||
|
ce, ok := err.(*FileError)
|
||
|
if !ok {
|
||
|
t.Fatalf("[%d] expected error of type <*FileError>", i)
|
||
|
}
|
||
|
if ce.Reason != "cannot find file" || ce.File != "/tmp/source" {
|
||
|
t.Fatalf("[%d] expected error <%s '%s'> got <%s '%s'>", i, "cannot find file", "/tmp/source", ce.Reason, ce.File)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestCopySourceIsDir(t *testing.T) {
|
||
|
src, dst := "/tmp/sourcedir", "/tmp/destinationdir"
|
||
|
raw := fmt.Sprintf("%s %s", src, dst)
|
||
|
defer os.RemoveAll(src)
|
||
|
defer os.RemoveAll(dst)
|
||
|
|
||
|
ctx, err := CreateContext("apt-get", "")
|
||
|
if err != nil {
|
||
|
t.Fatalf("cannot create context")
|
||
|
}
|
||
|
|
||
|
for i := 0; i < 2; i++ {
|
||
|
// 1. create directory
|
||
|
os.RemoveAll(src)
|
||
|
if err := os.MkdirAll(src, os.FileMode(0777)); err != nil {
|
||
|
t.Fatalf("[%d] cannot create test directory | %s", i, err)
|
||
|
}
|
||
|
|
||
|
inst := new(copy)
|
||
|
err := inst.Build(raw)
|
||
|
if err != nil {
|
||
|
t.Fatalf("[%d] unexpected error <%s>", i, err)
|
||
|
}
|
||
|
|
||
|
if i == 0 {
|
||
|
_, err = inst.Exec(*ctx)
|
||
|
} else {
|
||
|
_, err = inst.DryRun(*ctx)
|
||
|
}
|
||
|
if err != nil {
|
||
|
t.Fatalf("[%d] unexpected error <%s>", i, err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
func TestCopyInvalidDestination(t *testing.T) {
|
||
|
src, dst := "/tmp/source", "/tmp/missing-directory/invalid-destination"
|
||
|
raw := fmt.Sprintf("%s %s", src, dst)
|
||
|
defer os.RemoveAll(src)
|
||
|
defer os.RemoveAll(dst)
|
||
|
|
||
|
ctx, err := CreateContext("apt-get", "")
|
||
|
if err != nil {
|
||
|
t.Fatalf("cannot create context")
|
||
|
}
|
||
|
|
||
|
for i := 0; i < 2; i++ {
|
||
|
|
||
|
os.RemoveAll(src)
|
||
|
// 1. create directory
|
||
|
if err := os.MkdirAll(src, os.FileMode(0777)); err != nil {
|
||
|
t.Fatalf("[%d] cannot create test directory | %s", i, err)
|
||
|
}
|
||
|
inst := new(copy)
|
||
|
err := inst.Build(raw)
|
||
|
if err != nil {
|
||
|
t.Fatalf("[%d] unexpected error <%s>", i, err)
|
||
|
}
|
||
|
|
||
|
if i == 0 {
|
||
|
_, err = inst.Exec(*ctx)
|
||
|
} else {
|
||
|
_, err = inst.DryRun(*ctx)
|
||
|
}
|
||
|
if err == nil {
|
||
|
t.Fatalf("[%d] expected error", i)
|
||
|
}
|
||
|
|
||
|
ce, ok := err.(*FileError)
|
||
|
if !ok {
|
||
|
t.Fatalf("[%d] expected error of type <*FileError>", i)
|
||
|
}
|
||
|
if ce.Reason != "cannot copy to" || ce.File != dst {
|
||
|
t.Fatalf("[%d] expected error <%s '%s'> got <%s '%s'>", i, "cannot copy to", dst, ce.Reason, ce.File)
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
func TestCopySourceIsFile(t *testing.T) {
|
||
|
src, dst := "/tmp/source", "/tmp/destination-file"
|
||
|
raw := fmt.Sprintf("%s %s", src, dst)
|
||
|
defer os.RemoveAll(src)
|
||
|
defer os.RemoveAll(dst)
|
||
|
|
||
|
ctx, err := CreateContext("apt-get", "")
|
||
|
if err != nil {
|
||
|
t.Fatalf("cannot create context")
|
||
|
}
|
||
|
|
||
|
for i := 0; i < 2; i++ {
|
||
|
|
||
|
os.RemoveAll(src)
|
||
|
|
||
|
// 1. create directory
|
||
|
fd, err := os.Create(src)
|
||
|
if err != nil {
|
||
|
t.Fatalf("[%d] cannot create test file | %s", i, err)
|
||
|
}
|
||
|
fd.Close()
|
||
|
|
||
|
inst := new(copy)
|
||
|
err = inst.Build(raw)
|
||
|
if err != nil {
|
||
|
t.Fatalf("[%d] unexpected error <%s>", i, err)
|
||
|
}
|
||
|
|
||
|
if i == 0 {
|
||
|
_, err = inst.Exec(*ctx)
|
||
|
} else {
|
||
|
_, err = inst.DryRun(*ctx)
|
||
|
}
|
||
|
if err != nil {
|
||
|
t.Fatalf("[%d] unexpected error <%s>", i, err)
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestCopySourceIsFileDestinationExists(t *testing.T) {
|
||
|
src, dst := "/tmp/source", "/tmp/destination-file"
|
||
|
raw := fmt.Sprintf("%s %s", src, dst)
|
||
|
defer os.RemoveAll(src)
|
||
|
defer os.RemoveAll(dst)
|
||
|
|
||
|
ctx, err := CreateContext("apt-get", "")
|
||
|
if err != nil {
|
||
|
t.Fatalf("cannot create context")
|
||
|
}
|
||
|
|
||
|
for i := 0; i < 2; i++ {
|
||
|
|
||
|
os.RemoveAll(src)
|
||
|
os.RemoveAll(dst)
|
||
|
|
||
|
// 1. create source
|
||
|
fd, err := os.Create(src)
|
||
|
if err != nil {
|
||
|
t.Fatalf("[%d] cannot create test file | %s", i, err)
|
||
|
}
|
||
|
fd.Close()
|
||
|
// 1. create destination
|
||
|
fd, err = os.Create(src)
|
||
|
if err != nil {
|
||
|
t.Fatalf("[%d] cannot create test file | %s", i, err)
|
||
|
}
|
||
|
fd.Close()
|
||
|
|
||
|
inst := new(copy)
|
||
|
err = inst.Build(raw)
|
||
|
if err != nil {
|
||
|
t.Fatalf("[%d] unexpected error <%s>", i, err)
|
||
|
}
|
||
|
|
||
|
if i == 0 {
|
||
|
_, err = inst.Exec(*ctx)
|
||
|
} else {
|
||
|
_, err = inst.DryRun(*ctx)
|
||
|
}
|
||
|
if err != nil {
|
||
|
t.Fatalf("[%d] unexpected error <%s>", i, err)
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
func TestCopyCannotWriteDestination(t *testing.T) {
|
||
|
src, dst := "/tmp/source", "/tmp/destination-perm"
|
||
|
raw := fmt.Sprintf("%s %s", src, dst)
|
||
|
defer os.RemoveAll(src)
|
||
|
defer os.RemoveAll(dst)
|
||
|
|
||
|
ctx, err := CreateContext("apt-get", "")
|
||
|
if err != nil {
|
||
|
t.Fatalf("cannot create context")
|
||
|
}
|
||
|
|
||
|
for i := 0; i < 2; i++ {
|
||
|
|
||
|
os.RemoveAll(src)
|
||
|
if err := os.RemoveAll(dst); err != nil {
|
||
|
t.Fatalf("[%d] cannot remove destination file\n", i)
|
||
|
}
|
||
|
|
||
|
// 1. create source
|
||
|
fd, err := os.Create(src)
|
||
|
if err != nil {
|
||
|
t.Fatalf("[%d] cannot create test file | %s", i, err)
|
||
|
}
|
||
|
fd.Close()
|
||
|
// 1. create destination
|
||
|
fd, err = os.Create(src)
|
||
|
if err != nil {
|
||
|
t.Fatalf("[%d] cannot create test file | %s", i, err)
|
||
|
}
|
||
|
err = fd.Chmod(os.FileMode(0555))
|
||
|
if err != nil {
|
||
|
t.Fatalf("[%d] cannot set permissions | %s", i, err)
|
||
|
}
|
||
|
fd.Close()
|
||
|
|
||
|
inst := new(copy)
|
||
|
err = inst.Build(raw)
|
||
|
if err != nil {
|
||
|
t.Fatalf("[%d] unexpected error <%s>", i, err)
|
||
|
}
|
||
|
|
||
|
if i == 0 {
|
||
|
_, err = inst.Exec(*ctx)
|
||
|
} else {
|
||
|
_, err = inst.DryRun(*ctx)
|
||
|
}
|
||
|
if err != nil {
|
||
|
t.Fatalf("[%d] unexpected error <%s>", i, err)
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|