From 2fa54107b2c84cabe948ace5d70836dd4be95799 Mon Sep 17 00:00:00 2001 From: Alexey Palazhchenko Date: Mon, 28 Jun 2021 14:40:40 +0000 Subject: [PATCH] chore: fix tests for disabled RBAC 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 --- cmd/talosctl/cmd/talos/version.go | 63 +++++++++++++++++++----------- internal/integration/cli/config.go | 32 ++++++++++----- 2 files changed, 64 insertions(+), 31 deletions(-) diff --git a/cmd/talosctl/cmd/talos/version.go b/cmd/talosctl/cmd/talos/version.go index 6c79ce3369..08ba81c3d7 100644 --- a/cmd/talosctl/cmd/talos/version.go +++ b/cmd/talosctl/cmd/talos/version.go @@ -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 @@ -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 @@ -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) } diff --git a/internal/integration/cli/config.go b/internal/integration/cli/config.go index d20d320520..07082a7a9c 100644 --- a/internal/integration/cli/config.go +++ b/internal/integration/cli/config.go @@ -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" ) @@ -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() @@ -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 @@ -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 @@ -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? @@ -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 {