Skip to content

Commit

Permalink
chore: fix tests for disabled RBAC
Browse files Browse the repository at this point in the history
This commit also introduces a hidden `--json` flag for `talosctl version` command
that is not supported and should be re-worked at #907.

Refs #3852.

Signed-off-by: Alexey Palazhchenko <alexey.palazhchenko@gmail.com>
  • Loading branch information
AlekSi authored and talos-bot committed Jun 28, 2021
1 parent 78583ba commit 2fa5410
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 31 deletions.
63 changes: 41 additions & 22 deletions cmd/talosctl/cmd/talos/version.go
Expand Up @@ -12,37 +12,42 @@ import (
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"
"google.golang.org/protobuf/encoding/protojson"

"github.com/talos-systems/talos/pkg/cli"
"github.com/talos-systems/talos/pkg/machinery/client"
"github.com/talos-systems/talos/pkg/version"
)

var (
// versionCmdFlags represents the `talosctl version` command's flags.
var versionCmdFlags struct {
clientOnly bool
shortVersion bool
)
json bool
}

// versionCmd represents the version command.
// versionCmd represents the `talosctl version` command.
var versionCmd = &cobra.Command{
Use: "version",
Short: "Prints the version",
Long: ``,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("Client:")
if shortVersion {
version.PrintShortVersion()
} else {
version.PrintLongVersion()
}
if !versionCmdFlags.json {
fmt.Println("Client:")
if versionCmdFlags.shortVersion {
version.PrintShortVersion()
} else {
version.PrintLongVersion()
}

// Exit early if we're only looking for client version
if clientOnly {
return nil
}
// Exit early if we're only looking for client version
if versionCmdFlags.clientOnly {
return nil
}

fmt.Println("Server:")
fmt.Println("Server:")
}

return WithClient(func(ctx context.Context, c *client.Client) error {
var remotePeer peer.Peer
Expand All @@ -64,16 +69,26 @@ var versionCmd = &cobra.Command{
node = msg.Metadata.Hostname
}

fmt.Printf("\t%s: %s\n", "NODE", node)
if !versionCmdFlags.json {
fmt.Printf("\t%s: %s\n", "NODE", node)

version.PrintLongVersionFromExisting(msg.Version)

version.PrintLongVersionFromExisting(msg.Version)
var enabledFeatures []string
if msg.Features.GetRbac() {
enabledFeatures = append(enabledFeatures, "RBAC")
}

var enabledFeatures []string
if msg.Features.GetRbac() {
enabledFeatures = append(enabledFeatures, "RBAC")
fmt.Printf("\tEnabled: %s\n", strings.Join(enabledFeatures, ", "))

continue
}

fmt.Printf("\tEnabled: %s\n", strings.Join(enabledFeatures, ", "))
b, err := protojson.Marshal(msg)
if err != nil {
return err
}
fmt.Printf("%s\n", b)
}

return nil
Expand All @@ -82,8 +97,12 @@ var versionCmd = &cobra.Command{
}

func init() {
versionCmd.Flags().BoolVar(&shortVersion, "short", false, "Print the short version")
versionCmd.Flags().BoolVar(&clientOnly, "client", false, "Print client version only")
versionCmd.Flags().BoolVar(&versionCmdFlags.shortVersion, "short", false, "Print the short version")
versionCmd.Flags().BoolVar(&versionCmdFlags.clientOnly, "client", false, "Print client version only")

// TODO remove when https://github.com/talos-systems/talos/issues/907 is implemented
versionCmd.Flags().BoolVar(&versionCmdFlags.json, "json", false, "")
cli.Should(versionCmd.Flags().MarkHidden("json"))

addCommand(versionCmd)
}
32 changes: 23 additions & 9 deletions internal/integration/cli/config.go
Expand Up @@ -11,7 +11,10 @@ import (
"regexp"
"strings"

"google.golang.org/protobuf/encoding/protojson"

"github.com/talos-systems/talos/internal/integration/base"
machineapi "github.com/talos-systems/talos/pkg/machinery/api/machine"
clientconfig "github.com/talos-systems/talos/pkg/machinery/client/config"
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine"
)
Expand Down Expand Up @@ -65,12 +68,13 @@ func (suite *TalosconfigSuite) TestMerge() {

// TestNew checks `talosctl config new`.
func (suite *TalosconfigSuite) TestNew() {
stdout := suite.RunCLI([]string{"version"})
stdout := suite.RunCLI([]string{"version", "--json"})

// TODO: fix test (https://github.com/talos-systems/talos/issues/3852)
if !strings.Contains(stdout, "Enabled: RBAC") {
suite.T().Skip("skipping this test for now")
}
var v machineapi.Version
err := protojson.Unmarshal([]byte(stdout), &v)
suite.Require().NoError(err)

rbacEnabled := v.Features.GetRbac()

tempDir := suite.T().TempDir()

Expand All @@ -80,7 +84,7 @@ func (suite *TalosconfigSuite) TestNew() {
suite.RunCLI([]string{"--nodes", node, "config", "new", "--roles", "os:reader", readerConfig},
base.StdoutEmpty())

// commands that work for both admin and reader
// commands that work for both admin and reader, with and without RBAC
for _, tt := range []struct {
args []string
opts []base.RunOption
Expand All @@ -103,7 +107,7 @@ func (suite *TalosconfigSuite) TestNew() {
})
}

// commands that work for admin, but not for reader
// commands that work for admin, but not for reader (when RBAC is enabled)
for _, tt := range []struct {
args []string
adminOpts []base.RunOption
Expand Down Expand Up @@ -134,7 +138,7 @@ func (suite *TalosconfigSuite) TestNew() {
},
},
{
args: []string{"kubeconfig", tempDir},
args: []string{"kubeconfig", "--force", tempDir},
adminOpts: []base.RunOption{base.StdoutEmpty()},
readerOpts: []base.RunOption{
base.ShouldFail(), // why this one fails, but not others?
Expand All @@ -152,10 +156,20 @@ func (suite *TalosconfigSuite) TestNew() {
suite.RunCLI(args, tt.adminOpts...)

args = append([]string{"--talosconfig", readerConfig}, args...)
suite.RunCLI(args, tt.readerOpts...)
if rbacEnabled {
suite.RunCLI(args, tt.readerOpts...)
} else {
// check that it works the same way as for admin with reader's config
suite.RunCLI(args, tt.adminOpts...)
}
})
}

// do not test destructive command with disabled RBAC
if !rbacEnabled {
return
}

// destructive commands that don't work for reader
// (and that we don't test for admin because they are destructive)
for _, tt := range []struct {
Expand Down

0 comments on commit 2fa5410

Please sign in to comment.