REWORD ADDING TESTS

This commit is contained in:
Philippe Caseiro 2022-07-01 15:44:07 +02:00
parent 1dfc9b8a24
commit 58e71d6ad1
4 changed files with 52 additions and 16 deletions

View File

@ -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 {

View File

@ -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())
}
}

View File

@ -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,13 +90,17 @@ func (hr *APKRepository) Delete() error {
} }
func (hr *APKRepository) Manage() error { func (hr *APKRepository) Manage() error {
if hr.Enabled { if utils.DryRun {
if err := hr.Add(); err != nil { return nil
return err
}
log.Println("\tUpdating apk repositories")
return hr.Update()
} else { } else {
return hr.Delete() if hr.Enabled {
if err := hr.Add(); err != nil {
return err
}
log.Println("\tUpdating apk repositories")
return hr.Update()
} else {
return hr.Delete()
}
} }
} }

View File

@ -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,12 +24,19 @@ 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) {
var stdOut bytes.Buffer if DryRun {
var stdErr bytes.Buffer stdOut := []byte(fmt.Sprintf("CMD %s\n", name))
stdErr := []byte("STDERR\n")
cmd := exec.Command(name, arg...) return stdOut, stdErr, nil
cmd.Stderr = &stdErr } else {
cmd.Stdout = &stdOut var stdOut bytes.Buffer
err := cmd.Run() var stdErr bytes.Buffer
return stdOut.Bytes(), stdErr.Bytes(), err
cmd := exec.Command(name, arg...)
cmd.Stderr = &stdErr
cmd.Stdout = &stdOut
err := cmd.Run()
return stdOut.Bytes(), stdErr.Bytes(), err
}
} }