|
| 1 | +package cliwrappers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + |
| 7 | + . "github.com/onsi/gomega" |
| 8 | + |
| 9 | + "github.com/konflux-ci/konflux-build-cli/pkg/cliwrappers" |
| 10 | + "github.com/konflux-ci/konflux-build-cli/testutil" |
| 11 | +) |
| 12 | + |
| 13 | +func setupSubscriptionManagerCli() (*cliwrappers.SubscriptionManagerCli, *mockExecutor) { |
| 14 | + executor := &mockExecutor{} |
| 15 | + smCli := &cliwrappers.SubscriptionManagerCli{Executor: executor} |
| 16 | + return smCli, executor |
| 17 | +} |
| 18 | + |
| 19 | +func TestSubscriptionManagerCli_Register(t *testing.T) { |
| 20 | + g := NewWithT(t) |
| 21 | + ensureRetryerDisabled(t) |
| 22 | + |
| 23 | + t.Run("should register with org and activation key", func(t *testing.T) { |
| 24 | + smCli, executor := setupSubscriptionManagerCli() |
| 25 | + var capturedArgs []string |
| 26 | + executor.executeFunc = func(cmd cliwrappers.Cmd) (string, string, int, error) { |
| 27 | + g.Expect(cmd.Name).To(Equal("subscription-manager")) |
| 28 | + capturedArgs = cmd.Args |
| 29 | + return "", "", 0, nil |
| 30 | + } |
| 31 | + |
| 32 | + params := &cliwrappers.SubscriptionManagerRegisterParams{ |
| 33 | + Org: "my-org", |
| 34 | + ActivationKey: "my-key", |
| 35 | + } |
| 36 | + |
| 37 | + err := smCli.Register(params) |
| 38 | + |
| 39 | + g.Expect(err).ToNot(HaveOccurred()) |
| 40 | + g.Expect(capturedArgs).To(Equal([]string{ |
| 41 | + "register", "--org", "my-org", "--activationkey", "my-key", |
| 42 | + })) |
| 43 | + }) |
| 44 | + |
| 45 | + t.Run("should include --force when Force is true", func(t *testing.T) { |
| 46 | + smCli, executor := setupSubscriptionManagerCli() |
| 47 | + var capturedArgs []string |
| 48 | + executor.executeFunc = func(cmd cliwrappers.Cmd) (string, string, int, error) { |
| 49 | + capturedArgs = cmd.Args |
| 50 | + return "", "", 0, nil |
| 51 | + } |
| 52 | + |
| 53 | + params := &cliwrappers.SubscriptionManagerRegisterParams{ |
| 54 | + Org: "my-org", |
| 55 | + ActivationKey: "my-key", |
| 56 | + Force: true, |
| 57 | + } |
| 58 | + |
| 59 | + err := smCli.Register(params) |
| 60 | + |
| 61 | + g.Expect(err).ToNot(HaveOccurred()) |
| 62 | + g.Expect(capturedArgs).To(Equal([]string{ |
| 63 | + "register", "--force", "--org", "my-org", "--activationkey", "my-key", |
| 64 | + })) |
| 65 | + }) |
| 66 | + |
| 67 | + t.Run("should return error when registration fails", func(t *testing.T) { |
| 68 | + smCli, executor := setupSubscriptionManagerCli() |
| 69 | + executor.executeFunc = func(cmd cliwrappers.Cmd) (string, string, int, error) { |
| 70 | + return "", "", 1, errors.New("command failed") |
| 71 | + } |
| 72 | + |
| 73 | + params := &cliwrappers.SubscriptionManagerRegisterParams{ |
| 74 | + Org: "my-org", |
| 75 | + ActivationKey: "my-key", |
| 76 | + } |
| 77 | + |
| 78 | + err := smCli.Register(params) |
| 79 | + |
| 80 | + g.Expect(err).To(HaveOccurred()) |
| 81 | + g.Expect(err.Error()).To(Equal("command failed")) |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +func TestSubscriptionManagerCli_Unregister(t *testing.T) { |
| 86 | + g := NewWithT(t) |
| 87 | + |
| 88 | + t.Run("should unregister", func(t *testing.T) { |
| 89 | + smCli, executor := setupSubscriptionManagerCli() |
| 90 | + var capturedArgs []string |
| 91 | + executor.executeFunc = func(cmd cliwrappers.Cmd) (string, string, int, error) { |
| 92 | + g.Expect(cmd.Name).To(Equal("subscription-manager")) |
| 93 | + capturedArgs = cmd.Args |
| 94 | + return "", "", 0, nil |
| 95 | + } |
| 96 | + |
| 97 | + smCli.Unregister() |
| 98 | + |
| 99 | + g.Expect(capturedArgs).To(Equal([]string{"unregister"})) |
| 100 | + }) |
| 101 | + |
| 102 | + t.Run("should log a warning on failure", func(t *testing.T) { |
| 103 | + smCli, executor := setupSubscriptionManagerCli() |
| 104 | + executor.executeFunc = func(cmd cliwrappers.Cmd) (string, string, int, error) { |
| 105 | + return "", "", 1, errors.New("unregister failed") |
| 106 | + } |
| 107 | + |
| 108 | + logOutput := testutil.CaptureLogOutput(smCli.Unregister) |
| 109 | + g.Expect(logOutput).To(ContainSubstring("subscription-manager unregister command failed")) |
| 110 | + }) |
| 111 | +} |
0 commit comments