feat(test): adding unit tests
This commit is contained in:
parent
1dfc9b8a24
commit
d9379f7e33
|
@ -11,6 +11,7 @@ func main() {
|
||||||
Config string `arg:"-c,--config,env:CONFIG" help:"Configuration values file or directory path" default:"./data/config"`
|
Config string `arg:"-c,--config,env:CONFIG" help:"Configuration values file or directory path" default:"./data/config"`
|
||||||
TemplateDirectory string `arg:"-t,--template-dir,env:TEMPLATE_DIR" help:"Template directory path" default:"./data/templates"`
|
TemplateDirectory string `arg:"-t,--template-dir,env:TEMPLATE_DIR" help:"Template directory path" default:"./data/templates"`
|
||||||
RootDirectory string `arg:"-r,--root-dir,env:ROOT_DIR" help:"Generate files with this root instead of /" default:"/"`
|
RootDirectory string `arg:"-r,--root-dir,env:ROOT_DIR" help:"Generate files with this root instead of /" default:"/"`
|
||||||
|
DryRun bool `arg:"-d,--dry-run,env:DRY_RUN" help:"Dry run do not really complete actions" default:"false"`
|
||||||
}
|
}
|
||||||
|
|
||||||
arg.MustParse(&args)
|
arg.MustParse(&args)
|
||||||
|
@ -21,7 +22,7 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if err = hostConfig.ManageServices(); err != nil {
|
if err = hostConfig.ManageServices(args.DryRun); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
|
||||||
"github.com/imdario/mergo"
|
"github.com/imdario/mergo"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -80,10 +81,14 @@ func (tc *TemplaterConfig) New(confpath string, templateDir string, rootDir stri
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process the services contained in the configuration "object"
|
// Process the services contained in the configuration "object"
|
||||||
func (tc *TemplaterConfig) ManageServices() error {
|
func (tc *TemplaterConfig) ManageServices(dryRun bool) error {
|
||||||
// Get global vars to add on each service
|
// Get global vars to add on each service
|
||||||
gbls := tc.GlobalService.Vars
|
gbls := tc.GlobalService.Vars
|
||||||
|
|
||||||
|
if dryRun {
|
||||||
|
utils.DryRun = dryRun
|
||||||
|
}
|
||||||
|
|
||||||
for name, svr := range tc.Services {
|
for name, svr := range tc.Services {
|
||||||
err := mergo.Merge(&svr.Vars, gbls)
|
err := mergo.Merge(&svr.Vars, gbls)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
package templater
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestManageService(t *testing.T) {
|
||||||
|
var hostConfig TemplaterConfig
|
||||||
|
|
||||||
|
err := hostConfig.New("../../data/config/loki-stack.json", "../../data/templates/", "/tmp/testing")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
err = hostConfig.ManageServices(true)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf(err.Error())
|
||||||
|
}
|
||||||
|
}
|
|
@ -78,7 +78,7 @@ func (hr *APKRepository) Update() error {
|
||||||
|
|
||||||
// FIXME
|
// FIXME
|
||||||
func (hr *APKRepository) Delete() error {
|
func (hr *APKRepository) Delete() error {
|
||||||
fileBytes, err := ioutil.ReadFile("/etc/apk/repositories")
|
fileBytes, err := ioutil.ReadFile(APKConfigFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -90,6 +90,9 @@ func (hr *APKRepository) Delete() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hr *APKRepository) Manage() error {
|
func (hr *APKRepository) Manage() error {
|
||||||
|
if utils.DryRun {
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
if hr.Enabled {
|
if hr.Enabled {
|
||||||
if err := hr.Add(); err != nil {
|
if err := hr.Add(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -99,4 +102,5 @@ func (hr *APKRepository) Manage() error {
|
||||||
} else {
|
} else {
|
||||||
return hr.Delete()
|
return hr.Delete()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,14 @@ package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
"github.com/hashicorp/hcl/v2"
|
"github.com/hashicorp/hcl/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var DryRun = false
|
||||||
|
|
||||||
func CheckErr(e error) {
|
func CheckErr(e error) {
|
||||||
if e != nil {
|
if e != nil {
|
||||||
panic(e)
|
panic(e)
|
||||||
|
@ -21,6 +24,12 @@ func CheckDiags(diag hcl.Diagnostics) {
|
||||||
|
|
||||||
// Execute a system command ...
|
// Execute a system command ...
|
||||||
func RunSystemCommand(name string, arg ...string) ([]byte, []byte, error) {
|
func RunSystemCommand(name string, arg ...string) ([]byte, []byte, error) {
|
||||||
|
if DryRun {
|
||||||
|
stdOut := []byte(fmt.Sprintf("CMD %s\n", name))
|
||||||
|
stdErr := []byte("STDERR\n")
|
||||||
|
|
||||||
|
return stdOut, stdErr, nil
|
||||||
|
} else {
|
||||||
var stdOut bytes.Buffer
|
var stdOut bytes.Buffer
|
||||||
var stdErr bytes.Buffer
|
var stdErr bytes.Buffer
|
||||||
|
|
||||||
|
@ -29,4 +38,5 @@ func RunSystemCommand(name string, arg ...string) ([]byte, []byte, error) {
|
||||||
cmd.Stdout = &stdOut
|
cmd.Stdout = &stdOut
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
return stdOut.Bytes(), stdErr.Bytes(), err
|
return stdOut.Bytes(), stdErr.Bytes(), err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue