All checks were successful
Test / Create distribution (push) Successful in 33s
Test / Sandbox (push) Successful in 1m53s
Test / Hakurei (push) Successful in 3m18s
Test / Sandbox (race detector) (push) Successful in 3m40s
Test / Hpkg (push) Successful in 3m35s
Test / Hakurei (race detector) (push) Successful in 5m19s
Test / Flake checks (push) Successful in 1m39s
These are very useful in many packages containing relatively large amount of code making calls to difficult or impossible to stub functions. Signed-off-by: Ophestra <cat@gensokyo.uk>
134 lines
4.1 KiB
Go
134 lines
4.1 KiB
Go
package container
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"hakurei.app/container/stub"
|
|
)
|
|
|
|
func TestTmpfileOp(t *testing.T) {
|
|
const sampleDataString = `chronos:x:65534:65534:Hakurei:/var/empty:/bin/zsh`
|
|
var (
|
|
samplePath = MustAbs("/etc/passwd")
|
|
sampleData = []byte(sampleDataString)
|
|
)
|
|
|
|
checkOpBehaviour(t, []opBehaviourTestCase{
|
|
{"createTemp", &Params{ParentPerm: 0700}, &TmpfileOp{
|
|
Path: samplePath,
|
|
Data: sampleData,
|
|
}, nil, nil, []stub.Call{
|
|
{"createTemp", stub.ExpectArgs{"/", "tmp.*"}, newCheckedFile(t, "tmp.32768", sampleDataString, nil), stub.UniqueError(5)},
|
|
}, stub.UniqueError(5)},
|
|
|
|
{"Write", &Params{ParentPerm: 0700}, &TmpfileOp{
|
|
Path: samplePath,
|
|
Data: sampleData,
|
|
}, nil, nil, []stub.Call{
|
|
{"createTemp", stub.ExpectArgs{"/", "tmp.*"}, writeErrOsFile{stub.UniqueError(4)}, nil},
|
|
}, stub.UniqueError(4)},
|
|
|
|
{"Close", &Params{ParentPerm: 0700}, &TmpfileOp{
|
|
Path: samplePath,
|
|
Data: sampleData,
|
|
}, nil, nil, []stub.Call{
|
|
{"createTemp", stub.ExpectArgs{"/", "tmp.*"}, newCheckedFile(t, "tmp.32768", sampleDataString, stub.UniqueError(3)), nil},
|
|
}, stub.UniqueError(3)},
|
|
|
|
{"ensureFile", &Params{ParentPerm: 0700}, &TmpfileOp{
|
|
Path: samplePath,
|
|
Data: sampleData,
|
|
}, nil, nil, []stub.Call{
|
|
{"createTemp", stub.ExpectArgs{"/", "tmp.*"}, newCheckedFile(t, "tmp.32768", sampleDataString, nil), nil},
|
|
{"ensureFile", stub.ExpectArgs{"/sysroot/etc/passwd", os.FileMode(0444), os.FileMode(0700)}, nil, stub.UniqueError(2)},
|
|
}, stub.UniqueError(2)},
|
|
|
|
{"bindMount", &Params{ParentPerm: 0700}, &TmpfileOp{
|
|
Path: samplePath,
|
|
Data: sampleData,
|
|
}, nil, nil, []stub.Call{
|
|
{"createTemp", stub.ExpectArgs{"/", "tmp.*"}, newCheckedFile(t, "tmp.32768", sampleDataString, nil), nil},
|
|
{"ensureFile", stub.ExpectArgs{"/sysroot/etc/passwd", os.FileMode(0444), os.FileMode(0700)}, nil, nil},
|
|
{"bindMount", stub.ExpectArgs{"tmp.32768", "/sysroot/etc/passwd", uintptr(0x5), false}, nil, stub.UniqueError(1)},
|
|
}, stub.UniqueError(1)},
|
|
|
|
{"remove", &Params{ParentPerm: 0700}, &TmpfileOp{
|
|
Path: samplePath,
|
|
Data: sampleData,
|
|
}, nil, nil, []stub.Call{
|
|
{"createTemp", stub.ExpectArgs{"/", "tmp.*"}, newCheckedFile(t, "tmp.32768", sampleDataString, nil), nil},
|
|
{"ensureFile", stub.ExpectArgs{"/sysroot/etc/passwd", os.FileMode(0444), os.FileMode(0700)}, nil, nil},
|
|
{"bindMount", stub.ExpectArgs{"tmp.32768", "/sysroot/etc/passwd", uintptr(0x5), false}, nil, nil},
|
|
{"remove", stub.ExpectArgs{"tmp.32768"}, nil, stub.UniqueError(0)},
|
|
}, stub.UniqueError(0)},
|
|
|
|
{"success", &Params{ParentPerm: 0700}, &TmpfileOp{
|
|
Path: samplePath,
|
|
Data: sampleData,
|
|
}, nil, nil, []stub.Call{
|
|
{"createTemp", stub.ExpectArgs{"/", "tmp.*"}, newCheckedFile(t, "tmp.32768", sampleDataString, nil), nil},
|
|
{"ensureFile", stub.ExpectArgs{"/sysroot/etc/passwd", os.FileMode(0444), os.FileMode(0700)}, nil, nil},
|
|
{"bindMount", stub.ExpectArgs{"tmp.32768", "/sysroot/etc/passwd", uintptr(0x5), false}, nil, nil},
|
|
{"remove", stub.ExpectArgs{"tmp.32768"}, nil, nil},
|
|
}, nil},
|
|
})
|
|
|
|
checkOpsValid(t, []opValidTestCase{
|
|
{"nil", (*TmpfileOp)(nil), false},
|
|
{"zero", new(TmpfileOp), false},
|
|
{"valid", &TmpfileOp{Path: samplePath}, true},
|
|
})
|
|
|
|
checkOpsBuilder(t, []opsBuilderTestCase{
|
|
{"noref", new(Ops).Place(samplePath, sampleData), Ops{
|
|
&TmpfileOp{
|
|
Path: samplePath,
|
|
Data: sampleData,
|
|
},
|
|
}},
|
|
|
|
{"ref", new(Ops).PlaceP(samplePath, new(*[]byte)), Ops{
|
|
&TmpfileOp{
|
|
Path: samplePath,
|
|
Data: []byte{},
|
|
},
|
|
}},
|
|
})
|
|
|
|
checkOpIs(t, []opIsTestCase{
|
|
{"zero", new(TmpfileOp), new(TmpfileOp), false},
|
|
|
|
{"differs path", &TmpfileOp{
|
|
Path: MustAbs("/etc/group"),
|
|
Data: sampleData,
|
|
}, &TmpfileOp{
|
|
Path: samplePath,
|
|
Data: sampleData,
|
|
}, false},
|
|
|
|
{"differs data", &TmpfileOp{
|
|
Path: samplePath,
|
|
Data: append(sampleData, 0),
|
|
}, &TmpfileOp{
|
|
Path: samplePath,
|
|
Data: sampleData,
|
|
}, false},
|
|
|
|
{"equals", &TmpfileOp{
|
|
Path: samplePath,
|
|
Data: sampleData,
|
|
}, &TmpfileOp{
|
|
Path: samplePath,
|
|
Data: sampleData,
|
|
}, true},
|
|
})
|
|
|
|
checkOpMeta(t, []opMetaTestCase{
|
|
{"passwd", &TmpfileOp{
|
|
Path: samplePath,
|
|
Data: sampleData,
|
|
}, "placing", `tmpfile "/etc/passwd" (49 bytes)`},
|
|
})
|
|
}
|