-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathrun_requirements_test.go
More file actions
124 lines (109 loc) · 4.95 KB
/
run_requirements_test.go
File metadata and controls
124 lines (109 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package cmd
import (
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
ccloudv1 "github.com/confluentinc/ccloud-sdk-go-v1-public"
"github.com/confluentinc/cli/v4/pkg/config"
testserver "github.com/confluentinc/cli/v4/test/test-server"
)
var (
noContextCfg = new(config.Config)
regularOrgContextState = &config.ContextState{Auth: &config.AuthConfig{Organization: testserver.RegularOrg}}
endOfFreeTrialSuspendedOrgContextState = &config.ContextState{Auth: &config.AuthConfig{Organization: testserver.SuspendedOrg(ccloudv1.SuspensionEventType_SUSPENSION_EVENT_END_OF_FREE_TRIAL)}}
pauseTrialSuspendedOrgContextState = &config.ContextState{Auth: &config.AuthConfig{Organization: testserver.SuspendedOrg(ccloudv1.SuspensionEventType_SUSPENSION_EVENT_PAUSE_TRIAL)}}
normalSuspendedOrgContextState = &config.ContextState{Auth: &config.AuthConfig{Organization: testserver.SuspendedOrg(ccloudv1.SuspensionEventType_SUSPENSION_EVENT_CUSTOMER_INITIATED_ORG_DEACTIVATION)}}
cloudCfg = func(contextState *config.ContextState) *config.Config {
return &config.Config{
Contexts: map[string]*config.Context{"cloud": {
PlatformName: testserver.TestCloudUrl.String(),
State: contextState,
}},
CurrentContext: "cloud",
IsTest: true,
}
}
apiKeyCloudCfg = &config.Config{
Contexts: map[string]*config.Context{"cloud": {
PlatformName: testserver.TestCloudUrl.String(),
Credential: &config.Credential{CredentialType: config.APIKey},
State: regularOrgContextState,
}},
CurrentContext: "cloud",
IsTest: true,
}
nonAPIKeyCloudCfg = &config.Config{
Contexts: map[string]*config.Context{"cloud": {
PlatformName: testserver.TestCloudUrl.String(),
Credential: &config.Credential{CredentialType: config.Username},
State: regularOrgContextState,
}},
CurrentContext: "cloud",
IsTest: true,
}
onPremCfg = &config.Config{
Contexts: map[string]*config.Context{"on-prem": {
Credential: new(config.Credential),
PlatformName: "https://example.com",
State: &config.ContextState{AuthToken: "token"},
}},
CurrentContext: "on-prem",
}
)
func TestErrIfMissingRunRequirement_NoError(t *testing.T) {
for _, test := range []struct {
req string
cfg *config.Config
}{
{RequireCloudLogin, cloudCfg(regularOrgContextState)},
{RequireCloudLoginAllowFreeTrialEnded, cloudCfg(regularOrgContextState)},
{RequireCloudLoginAllowFreeTrialEnded, cloudCfg(endOfFreeTrialSuspendedOrgContextState)},
{RequireCloudLoginOrOnPremLogin, cloudCfg(regularOrgContextState)},
{RequireCloudLoginOrOnPremLogin, onPremCfg},
{RequireNonAPIKeyCloudLogin, nonAPIKeyCloudCfg},
{RequireNonAPIKeyCloudLoginOrOnPremLogin, nonAPIKeyCloudCfg},
{RequireNonAPIKeyCloudLoginOrOnPremLogin, onPremCfg},
{RequireOnPremLogin, onPremCfg},
} {
cmd := &cobra.Command{Annotations: map[string]string{RunRequirement: test.req}}
err := ErrIfMissingRunRequirement(cmd, test.cfg)
require.NoError(t, err)
}
}
func TestErrIfMissingRunRequirement_Error(t *testing.T) {
for _, test := range []struct {
req string
cfg *config.Config
err error
}{
{RequireCloudLogin, onPremCfg, config.RequireCloudLoginErr},
{RequireCloudLogin, cloudCfg(endOfFreeTrialSuspendedOrgContextState), config.RequireCloudLoginFreeTrialEndedOrgUnsuspendedErr},
{RequireCloudLogin, cloudCfg(pauseTrialSuspendedOrgContextState), config.RequireCloudLoginPauseTrialOrgUnsuspendedErr},
{RequireCloudLogin, cloudCfg(normalSuspendedOrgContextState), config.RequireCloudLoginOrgUnsuspendedErr},
{RequireCloudLoginAllowFreeTrialEnded, onPremCfg, config.RequireCloudLoginErr},
{RequireCloudLoginAllowFreeTrialEnded, cloudCfg(normalSuspendedOrgContextState), config.RequireCloudLoginOrgUnsuspendedErr},
{RequireCloudLoginOrOnPremLogin, noContextCfg, config.RequireCloudLoginOrOnPremErr},
{RequireCloudLoginOrOnPremLogin, noContextCfg, config.RequireCloudLoginOrOnPremErr},
{RequireNonAPIKeyCloudLogin, apiKeyCloudCfg, config.RequireNonAPIKeyCloudLoginErr},
{RequireNonAPIKeyCloudLoginOrOnPremLogin, apiKeyCloudCfg, config.RequireNonAPIKeyCloudLoginOrOnPremLoginErr},
{RequireNonAPIKeyCloudLoginOrOnPremLogin, apiKeyCloudCfg, config.RequireNonAPIKeyCloudLoginOrOnPremLoginErr},
{RequireOnPremLogin, cloudCfg(regularOrgContextState), config.RunningOnPremCommandInCloudErr},
} {
cmd := &cobra.Command{Annotations: map[string]string{RunRequirement: test.req}}
err := ErrIfMissingRunRequirement(cmd, test.cfg)
require.Error(t, err)
require.Equal(t, test.err, err)
}
}
func TestErrIfMissingRunRequirement_Root(t *testing.T) {
err := ErrIfMissingRunRequirement(&cobra.Command{}, nil)
require.NoError(t, err)
}
func TestErrIfMissingRunRequirement_Subcommand(t *testing.T) {
a := &cobra.Command{Annotations: map[string]string{RunRequirement: RequireCloudLogin}}
b := &cobra.Command{}
a.AddCommand(b)
err := ErrIfMissingRunRequirement(b, onPremCfg)
require.Error(t, err)
require.Equal(t, err, config.RequireCloudLoginErr)
}