start adding some tests
This commit is contained in:
parent
048e062102
commit
d00e78174c
5
go.mod
5
go.mod
@ -2,7 +2,10 @@ module git.iamthefij.com/iamthefij/restic-scheduler
|
|||||||
|
|
||||||
go 1.17
|
go 1.17
|
||||||
|
|
||||||
require github.com/hashicorp/hcl/v2 v2.11.1
|
require (
|
||||||
|
github.com/go-test/deep v1.0.8
|
||||||
|
github.com/hashicorp/hcl/v2 v2.11.1
|
||||||
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/agext/levenshtein v1.2.1 // indirect
|
github.com/agext/levenshtein v1.2.1 // indirect
|
||||||
|
3
go.sum
3
go.sum
@ -7,8 +7,9 @@ github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6
|
|||||||
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
|
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
|
|
||||||
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
|
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
|
||||||
|
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
|
||||||
|
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
|
||||||
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
||||||
|
28
restic.go
28
restic.go
@ -42,8 +42,12 @@ func (glo ResticGlobalOpts) ToArgs() (args []string) {
|
|||||||
args = append(args, "--cache-dir", glo.CacheDir)
|
args = append(args, "--cache-dir", glo.CacheDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
if glo.CleanupCache {
|
if glo.PasswordFile != "" {
|
||||||
args = append(args, "--cleanup-cache")
|
args = append(args, "--password-file", glo.PasswordFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
if glo.TLSClientCertFile != "" {
|
||||||
|
args = append(args, "--tls-client-cert", glo.TLSClientCertFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
if glo.LimitDownload > 0 {
|
if glo.LimitDownload > 0 {
|
||||||
@ -54,6 +58,14 @@ func (glo ResticGlobalOpts) ToArgs() (args []string) {
|
|||||||
args = append(args, "--limit-upload", fmt.Sprint(glo.LimitUpload))
|
args = append(args, "--limit-upload", fmt.Sprint(glo.LimitUpload))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if glo.VerboseLevel > 0 {
|
||||||
|
args = append(args, "--verbose", fmt.Sprint(glo.VerboseLevel))
|
||||||
|
}
|
||||||
|
|
||||||
|
if glo.CleanupCache {
|
||||||
|
args = append(args, "--cleanup-cache")
|
||||||
|
}
|
||||||
|
|
||||||
if glo.NoCache {
|
if glo.NoCache {
|
||||||
args = append(args, "--no-cache")
|
args = append(args, "--no-cache")
|
||||||
}
|
}
|
||||||
@ -62,18 +74,6 @@ func (glo ResticGlobalOpts) ToArgs() (args []string) {
|
|||||||
args = append(args, "--no-lock")
|
args = append(args, "--no-lock")
|
||||||
}
|
}
|
||||||
|
|
||||||
if glo.PasswordFile != "" {
|
|
||||||
args = append(args, "--password-file", glo.PasswordFile)
|
|
||||||
}
|
|
||||||
|
|
||||||
if glo.TLSClientCertFile != "" {
|
|
||||||
args = append(args, "--tls-client-cert", glo.TLSClientCertFile)
|
|
||||||
}
|
|
||||||
|
|
||||||
if glo.VerboseLevel > 0 {
|
|
||||||
args = append(args, "--verbose", fmt.Sprint(glo.VerboseLevel))
|
|
||||||
}
|
|
||||||
|
|
||||||
return args
|
return args
|
||||||
}
|
}
|
||||||
|
|
||||||
|
85
restic_test.go
Normal file
85
restic_test.go
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
package main_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
main "git.iamthefij.com/iamthefij/restic-scheduler"
|
||||||
|
"github.com/go-test/deep"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGlobalOptions(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
args := main.ResticGlobalOpts{
|
||||||
|
CaCertFile: "file",
|
||||||
|
CacheDir: "directory",
|
||||||
|
PasswordFile: "file",
|
||||||
|
TLSClientCertFile: "file",
|
||||||
|
LimitDownload: 1,
|
||||||
|
LimitUpload: 1,
|
||||||
|
VerboseLevel: 1,
|
||||||
|
CleanupCache: true,
|
||||||
|
NoCache: true,
|
||||||
|
NoLock: true,
|
||||||
|
}.ToArgs()
|
||||||
|
|
||||||
|
expected := []string{
|
||||||
|
"--cacert", "file",
|
||||||
|
"--cache-dir", "directory",
|
||||||
|
"--password-file", "file",
|
||||||
|
"--tls-client-cert", "file",
|
||||||
|
"--limit-download", "1",
|
||||||
|
"--limit-upload", "1",
|
||||||
|
"--verbose", "1",
|
||||||
|
"--cleanup-cache",
|
||||||
|
"--no-cache",
|
||||||
|
"--no-lock",
|
||||||
|
}
|
||||||
|
|
||||||
|
if diff := deep.Equal(args, expected); diff != nil {
|
||||||
|
t.Errorf("args didn't match %v", diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildEnv(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
cmd main.ResticCmd
|
||||||
|
expected []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "No Env",
|
||||||
|
cmd: main.ResticCmd{}, // nolint:exhaustivestruct
|
||||||
|
expected: os.Environ(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "SetEnv",
|
||||||
|
cmd: main.ResticCmd{ // nolint:exhaustivestruct
|
||||||
|
Env: map[string]string{"TestKey": "Value"},
|
||||||
|
},
|
||||||
|
expected: append(os.Environ(), "TestKey=Value"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "SetEnv",
|
||||||
|
cmd: main.ResticCmd{ // nolint:exhaustivestruct
|
||||||
|
Passphrase: "Shhhhhhhh!!",
|
||||||
|
},
|
||||||
|
expected: append(os.Environ(), "RESTIC_PASSWORD=Shhhhhhhh!!"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
c := c
|
||||||
|
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
if diff := deep.Equal(c.expected, c.cmd.BuildEnv()); diff != nil {
|
||||||
|
t.Error(diff)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user