|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go/aws/endpoints" |
| 10 | + "github.com/spf13/viper" |
| 11 | + "github.com/stretchr/testify/suite" |
| 12 | +) |
| 13 | + |
| 14 | +type cliTestSuite struct { |
| 15 | + suite.Suite |
| 16 | + viper *viper.Viper |
| 17 | + logger *log.Logger |
| 18 | +} |
| 19 | + |
| 20 | +func (suite *cliTestSuite) Setup() { |
| 21 | + // Disable any logging that isn't attached to the logger unless using the verbose flag |
| 22 | + log.SetOutput(ioutil.Discard) |
| 23 | + log.SetFlags(0) |
| 24 | + |
| 25 | + // Setup logger |
| 26 | + var logger = log.New(os.Stdout, "", log.LstdFlags) |
| 27 | + |
| 28 | + // Remove the flags for the logger |
| 29 | + logger.SetFlags(0) |
| 30 | + suite.SetLogger(logger) |
| 31 | + |
| 32 | + // Setup viper |
| 33 | + v := viper.New() |
| 34 | + suite.SetViper(v) |
| 35 | +} |
| 36 | + |
| 37 | +func (suite *cliTestSuite) SetViper(v *viper.Viper) { |
| 38 | + suite.viper = v |
| 39 | +} |
| 40 | + |
| 41 | +func (suite *cliTestSuite) SetLogger(logger *log.Logger) { |
| 42 | + suite.logger = logger |
| 43 | +} |
| 44 | + |
| 45 | +func TestCLISuite(t *testing.T) { |
| 46 | + suite.Run(t, &cliTestSuite{}) |
| 47 | +} |
| 48 | + |
| 49 | +func (suite *cliTestSuite) TestStringSliceContains() { |
| 50 | + suite.True(stringSliceContains([]string{"hello", "world"}, "hello")) |
| 51 | + suite.False(stringSliceContains([]string{"hello", "world"}, "planet")) |
| 52 | +} |
| 53 | + |
| 54 | +func (suite *cliTestSuite) TestCheckVault() { |
| 55 | + suite.Setup() |
| 56 | + |
| 57 | + testValues := []string{ |
| 58 | + "", |
| 59 | + VaultAWSKeychainNameDefault, |
| 60 | + } |
| 61 | + for _, testValue := range testValues { |
| 62 | + suite.viper.Set(VaultAWSKeychainNameFlag, testValue) |
| 63 | + suite.viper.Set(VaultAWSProfileFlag, "profile") |
| 64 | + suite.NoError(checkVault(suite.viper)) |
| 65 | + } |
| 66 | + testValuesWithErrors := []string{ |
| 67 | + "AnyOtherKeychainName", |
| 68 | + } |
| 69 | + for _, testValue := range testValuesWithErrors { |
| 70 | + suite.viper.Set(VaultAWSKeychainNameFlag, testValue) |
| 71 | + suite.viper.Set(VaultAWSProfileFlag, "profile") |
| 72 | + suite.Error(checkVault(suite.viper)) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func (suite *cliTestSuite) TestCheckRegion() { |
| 77 | + suite.Setup() |
| 78 | + |
| 79 | + testValues := []string{ |
| 80 | + endpoints.UsEast1RegionID, |
| 81 | + endpoints.UsEast2RegionID, |
| 82 | + endpoints.UsWest1RegionID, |
| 83 | + endpoints.UsWest2RegionID, |
| 84 | + } |
| 85 | + for _, testValue := range testValues { |
| 86 | + suite.viper.Set(AWSRegionFlag, testValue) |
| 87 | + suite.NoError(checkRegion(suite.viper)) |
| 88 | + } |
| 89 | + testValuesWithErrors := []string{ |
| 90 | + "AnyOtherRegionName", |
| 91 | + } |
| 92 | + for _, testValue := range testValuesWithErrors { |
| 93 | + suite.viper.Set(AWSRegionFlag, testValue) |
| 94 | + suite.Error(checkRegion(suite.viper)) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func (suite *cliTestSuite) TestCheckAccountID() { |
| 99 | + suite.Setup() |
| 100 | + testValues := []string{ |
| 101 | + "012345678901", |
| 102 | + "123456789012", |
| 103 | + } |
| 104 | + for _, testValue := range testValues { |
| 105 | + suite.viper.Set(AWSAccountIDFlag, testValue) |
| 106 | + suite.NoError(checkAccountID(suite.viper)) |
| 107 | + } |
| 108 | + testValuesWithErrors := []string{ |
| 109 | + "", |
| 110 | + "12345678901", |
| 111 | + "1234567890123", |
| 112 | + } |
| 113 | + for _, testValue := range testValuesWithErrors { |
| 114 | + suite.viper.Set(AWSAccountIDFlag, testValue) |
| 115 | + suite.Error(checkAccountID(suite.viper)) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func (suite *cliTestSuite) TestCheckIAMUser() { |
| 120 | + suite.Setup() |
| 121 | + |
| 122 | + testValues := []string{ |
| 123 | + "test", |
| 124 | + } |
| 125 | + for _, testValue := range testValues { |
| 126 | + suite.viper.Set(IAMUserFlag, testValue) |
| 127 | + suite.NoError(checkIAMUser(suite.viper)) |
| 128 | + } |
| 129 | + testValuesWithErrors := []string{ |
| 130 | + "", |
| 131 | + } |
| 132 | + for _, testValue := range testValuesWithErrors { |
| 133 | + suite.viper.Set(IAMUserFlag, testValue) |
| 134 | + suite.Error(checkIAMUser(suite.viper)) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func (suite *cliTestSuite) TestCheckIAMRole() { |
| 139 | + suite.Setup() |
| 140 | + |
| 141 | + testValues := []string{ |
| 142 | + "test", |
| 143 | + } |
| 144 | + for _, testValue := range testValues { |
| 145 | + suite.viper.Set(IAMRoleFlag, testValue) |
| 146 | + suite.NoError(checkIAMRole(suite.viper)) |
| 147 | + } |
| 148 | + testValuesWithErrors := []string{ |
| 149 | + "", |
| 150 | + } |
| 151 | + for _, testValue := range testValuesWithErrors { |
| 152 | + suite.viper.Set(IAMRoleFlag, testValue) |
| 153 | + suite.Error(checkIAMRole(suite.viper)) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func (suite *cliTestSuite) TestCheckOutput() { |
| 158 | + suite.Setup() |
| 159 | + |
| 160 | + testValues := []string{ |
| 161 | + "text", |
| 162 | + "json", |
| 163 | + "yaml", |
| 164 | + "table", |
| 165 | + } |
| 166 | + for _, testValue := range testValues { |
| 167 | + suite.viper.Set(OutputFlag, testValue) |
| 168 | + suite.NoError(checkOutput(suite.viper)) |
| 169 | + } |
| 170 | + testValuesWithErrors := []string{ |
| 171 | + "bad", |
| 172 | + } |
| 173 | + for _, testValue := range testValuesWithErrors { |
| 174 | + suite.viper.Set(OutputFlag, testValue) |
| 175 | + suite.Error(checkOutput(suite.viper)) |
| 176 | + } |
| 177 | +} |
0 commit comments