Merge branch 'development' into RefactorFlags

This commit is contained in:
Christian Goll
2023-04-11 14:17:13 +02:00
committed by GitHub
34 changed files with 2342 additions and 629 deletions

View File

@@ -8,6 +8,8 @@ import (
"os"
"strings"
apinode "github.com/hpcng/warewulf/internal/pkg/api/node"
apiprofile "github.com/hpcng/warewulf/internal/pkg/api/profile"
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
apiutil "github.com/hpcng/warewulf/internal/pkg/api/util"
@@ -109,11 +111,17 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
yes := apiutil.ConfirmationPrompt(fmt.Sprintf("Are you sure you want to modify %d nodes", len(modifiedProfileMap)))
if yes {
err = apiprofile.ProfileDelete(&wwapiv1.NodeDeleteParameter{NodeNames: pList, Force: true})
if err != nil {
wwlog.Verbose("Problem deleting nodes before modification %s")
}
buffer, _ = yaml.Marshal(modifiedProfileMap)
err = apiprofile.ProfileAddFromYaml(&wwapiv1.NodeYaml{NodeConfMapYaml: string(buffer)})
newHash := apinode.Hash()
err = apiprofile.ProfileAddFromYaml(&wwapiv1.NodeAddParameter{
NodeConfYaml: string(buffer),
Hash: newHash.Hash,
})
if err != nil {
wwlog.Error("Got following problem when writing back yaml: %s", err)
os.Exit(1)

View File

@@ -1,25 +1,33 @@
package list
import (
"fmt"
"strings"
"github.com/hpcng/warewulf/internal/app/wwctl/helper"
apiprofile "github.com/hpcng/warewulf/internal/pkg/api/profile"
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/spf13/cobra"
)
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
req := wwapiv1.GetProfileList{
ShowAll: ShowAll,
Profiles: args,
}
profileInfo, err := apiprofile.ProfileList(&req)
if err != nil {
func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err error) {
return func(cmd *cobra.Command, args []string) (err error) {
req := wwapiv1.GetProfileList{
ShowAll: vars.showAll,
Profiles: args,
}
profileInfo, err := apiprofile.ProfileList(&req)
if err != nil {
return
}
if len(profileInfo.Output) > 0 {
ph := helper.NewPrintHelper(strings.Split(profileInfo.Output[0], "="))
for _, val := range profileInfo.Output[1:] {
ph.Append(strings.Split(val, "="))
}
ph.Render()
}
return
}
for _, str := range profileInfo.Output {
fmt.Printf("%s\n", str)
}
return
}

View File

@@ -0,0 +1,84 @@
package list
import (
"bytes"
"io"
"os"
"strings"
"testing"
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/hpcng/warewulf/internal/pkg/warewulfconf"
"github.com/hpcng/warewulf/internal/pkg/warewulfd"
"github.com/stretchr/testify/assert"
)
func Test_List(t *testing.T) {
tests := []struct {
name string
args []string
stdout string
inDb string
}{
{
name: "profile list test",
args: []string{},
stdout: `PROFILE NAME COMMENT/DESCRIPTION
default --`,
inDb: `WW_INTERNAL: 43
nodeprofiles:
default: {}
nodes:
n01:
profiles:
- default
`,
},
}
conf_yml := `
WW_INTERNAL: 0
`
conf := warewulfconf.New()
err := conf.Read([]byte(conf_yml))
assert.NoError(t, err)
warewulfd.SetNoDaemon()
for _, tt := range tests {
_, err = node.TestNew([]byte(tt.inDb))
assert.NoError(t, err)
t.Logf("Running test: %s\n", tt.name)
t.Run(tt.name, func(t *testing.T) {
baseCmd := GetCommand()
baseCmd.SetArgs(tt.args)
baseCmd.SetOut(nil)
baseCmd.SetErr(nil)
stdoutR, stdoutW, _ := os.Pipe()
oriout := os.Stdout
os.Stdout = stdoutW
err = baseCmd.Execute()
if err != nil {
t.Errorf("Received error when running command, err: %v", err)
t.FailNow()
}
stdoutC := make(chan string)
go func() {
var buf bytes.Buffer
_, _ = io.Copy(&buf, stdoutR)
stdoutC <- buf.String()
}()
stdoutW.Close()
os.Stdout = oriout
stdout := <-stdoutC
stdout = strings.TrimSpace(stdout)
stdout = strings.ReplaceAll(stdout, " ", "")
assert.NotEmpty(t, stdout, "os.stdout should not be empty")
tt.stdout = strings.ReplaceAll(strings.TrimSpace(tt.stdout), " ", "")
if stdout != strings.ReplaceAll(strings.TrimSpace(tt.stdout), " ", "") {
t.Errorf("Got wrong output, got:\n '%s'\n, but want:\n '%s'\n", stdout, tt.stdout)
t.FailNow()
}
})
}
}

View File

@@ -2,23 +2,22 @@ package list
import "github.com/spf13/cobra"
var (
baseCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Use: "list [OPTIONS] [PROFILE ...]",
Short: "List profiles and configurations",
Long: "This command will display configurations for PROFILE.",
RunE: CobraRunE,
Aliases: []string{"ls"},
}
ShowAll bool
)
func init() {
baseCmd.PersistentFlags().BoolVarP(&ShowAll, "all", "a", false, "Show all node configurations")
type variables struct {
showAll bool
}
// GetRootCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command {
vars := variables{}
baseCmd := &cobra.Command{
DisableFlagsInUseLine: true,
Use: "list [OPTIONS] [PROFILE ...]",
Short: "List profiles and configurations",
Long: "This command will display configurations for PROFILE.",
RunE: CobraRunE(&vars),
Aliases: []string{"ls"},
}
baseCmd.PersistentFlags().BoolVarP(&vars.showAll, "all", "a", false, "Show all node configurations")
return baseCmd
}