github.com/pluralsh/plural-cli@v0.9.5/cmd/plural/api_test.go (about) 1 package plural_test 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/mock" 9 10 "github.com/pluralsh/plural-cli/cmd/plural" 11 "github.com/pluralsh/plural-cli/pkg/api" 12 "github.com/pluralsh/plural-cli/pkg/test/mocks" 13 ) 14 15 func TestListArtifacts(t *testing.T) { 16 tests := []struct { 17 name string 18 args []string 19 artifacts []api.Artifact 20 expectedResponse string 21 expectedError string 22 }{ 23 { 24 name: `test "api list artifacts" with single response`, 25 args: []string{plural.ApplicationName, "api", "list", "artifacts", "test"}, 26 artifacts: []api.Artifact{{ 27 Id: "abc", 28 Name: "test", 29 Blob: "test", 30 Sha: "xyz", 31 Platform: "aws", 32 }}, 33 expectedResponse: `+-----+------+----------+------+-----+ 34 | ID | NAME | PLATFORM | BLOB | SHA | 35 +-----+------+----------+------+-----+ 36 | abc | test | aws | test | xyz | 37 +-----+------+----------+------+-----+ 38 `, 39 }, 40 { 41 name: `test "api list artifacts" without REPO_ID parameter`, 42 args: []string{plural.ApplicationName, "api", "list", "artifacts"}, 43 expectedError: "Not enough arguments provided: needs REPO_ID. Try running --help to see usage.", 44 artifacts: []api.Artifact{}, 45 }, 46 } 47 for _, test := range tests { 48 t.Run(test.name, func(t *testing.T) { 49 client := mocks.NewClient(t) 50 if test.expectedError == "" { 51 client.On("ListArtifacts", mock.AnythingOfType("string")).Return(test.artifacts, nil) 52 } 53 app := plural.CreateNewApp(&plural.Plural{Client: client}) 54 app.HelpName = plural.ApplicationName 55 os.Args = test.args 56 res, err := captureStdout(app, os.Args) 57 if test.expectedError != "" { 58 assert.Equal(t, err.Error(), test.expectedError) 59 } else { 60 assert.NoError(t, err) 61 assert.Equal(t, test.expectedResponse, res) 62 } 63 64 }) 65 } 66 } 67 68 func TestGetInstallations(t *testing.T) { 69 tests := []struct { 70 name string 71 args []string 72 installations []*api.Installation 73 expectedResponse string 74 }{ 75 { 76 name: `test "api list installations"`, 77 args: []string{plural.ApplicationName, "api", "list", "installations"}, 78 installations: []*api.Installation{ 79 {Id: "123", Repository: &api.Repository{Id: "abc", Name: "test-1", Publisher: &api.Publisher{Name: "Plural"}}}, 80 {Id: "456", Repository: &api.Repository{Id: "def", Name: "test-2", Publisher: &api.Publisher{Name: "Plural"}}}, 81 }, 82 expectedResponse: `+------------+---------------+-----------+ 83 | REPOSITORY | REPOSITORY ID | PUBLISHER | 84 +------------+---------------+-----------+ 85 | test-1 | abc | Plural | 86 | test-2 | def | Plural | 87 +------------+---------------+-----------+ 88 `, 89 }, 90 { 91 name: `test "api list installations" when Repository is nil`, 92 args: []string{plural.ApplicationName, "api", "list", "installations"}, 93 installations: []*api.Installation{{Id: "abc"}}, 94 expectedResponse: `+------------+---------------+-----------+ 95 | REPOSITORY | REPOSITORY ID | PUBLISHER | 96 +------------+---------------+-----------+ 97 +------------+---------------+-----------+ 98 `, 99 }, 100 { 101 name: `test "api list installations" when Publisher is nil`, 102 args: []string{plural.ApplicationName, "api", "list", "installations"}, 103 installations: []*api.Installation{{Id: "abc", Repository: &api.Repository{Id: "abc", Name: "test"}}}, 104 expectedResponse: `+------------+---------------+-----------+ 105 | REPOSITORY | REPOSITORY ID | PUBLISHER | 106 +------------+---------------+-----------+ 107 | test | abc | | 108 +------------+---------------+-----------+ 109 `, 110 }, 111 } 112 for _, test := range tests { 113 t.Run(test.name, func(t *testing.T) { 114 client := mocks.NewClient(t) 115 client.On("GetInstallations").Return(test.installations, nil) 116 app := plural.CreateNewApp(&plural.Plural{Client: client}) 117 app.HelpName = plural.ApplicationName 118 os.Args = test.args 119 res, err := captureStdout(app, os.Args) 120 assert.NoError(t, err) 121 122 assert.Equal(t, test.expectedResponse, res) 123 }) 124 } 125 } 126 127 func TestGetCharts(t *testing.T) { 128 tests := []struct { 129 name string 130 args []string 131 charts []*api.Chart 132 expectedResponse string 133 expectedError string 134 }{ 135 { 136 name: `test "api list charts" with single response`, 137 args: []string{plural.ApplicationName, "api", "list", "charts", "test"}, 138 charts: []*api.Chart{{ 139 Id: "123", 140 Name: "test", 141 Description: "test chart", 142 LatestVersion: "0.1.0", 143 }}, 144 expectedResponse: `+-----+------+-------------+----------------+ 145 | ID | NAME | DESCRIPTION | LATEST VERSION | 146 +-----+------+-------------+----------------+ 147 | 123 | test | test chart | 0.1.0 | 148 +-----+------+-------------+----------------+ 149 `, 150 }, 151 { 152 name: `test "api list charts" without REPO_ID parameter`, 153 args: []string{plural.ApplicationName, "api", "list", "charts"}, 154 charts: []*api.Chart{}, 155 expectedError: "Not enough arguments provided: needs REPO_ID. Try running --help to see usage.", 156 }, 157 } 158 for _, test := range tests { 159 t.Run(test.name, func(t *testing.T) { 160 client := mocks.NewClient(t) 161 if test.expectedError == "" { 162 client.On("GetCharts", mock.AnythingOfType("string")).Return(test.charts, nil) 163 } 164 app := plural.CreateNewApp(&plural.Plural{Client: client}) 165 app.HelpName = plural.ApplicationName 166 os.Args = test.args 167 res, err := captureStdout(app, os.Args) 168 if test.expectedError != "" { 169 assert.Equal(t, err.Error(), test.expectedError) 170 } else { 171 assert.NoError(t, err) 172 assert.Equal(t, test.expectedResponse, res) 173 } 174 }) 175 } 176 } 177 178 func TestGetTerraform(t *testing.T) { 179 tests := []struct { 180 name string 181 args []string 182 terraform []*api.Terraform 183 expectedResponse string 184 expectedError string 185 }{ 186 { 187 name: `test "api list terraform"`, 188 args: []string{plural.ApplicationName, "api", "list", "terraform", "test"}, 189 terraform: []*api.Terraform{ 190 { 191 Id: "123", 192 Name: "test-1", 193 Description: "test terraform", 194 }, 195 { 196 Id: "456", 197 Name: "test-2", 198 Description: "test terraform", 199 }, 200 }, 201 expectedResponse: `+-----+--------+----------------+ 202 | ID | NAME | DESCRIPTION | 203 +-----+--------+----------------+ 204 | 123 | test-1 | test terraform | 205 | 456 | test-2 | test terraform | 206 +-----+--------+----------------+ 207 `, 208 }, 209 { 210 name: `test "api list terraform" without REPO_ID parameter`, 211 args: []string{plural.ApplicationName, "api", "list", "terraform"}, 212 terraform: []*api.Terraform{}, 213 expectedError: "Not enough arguments provided: needs REPO_ID. Try running --help to see usage.", 214 }, 215 } 216 for _, test := range tests { 217 t.Run(test.name, func(t *testing.T) { 218 client := mocks.NewClient(t) 219 if test.expectedError == "" { 220 client.On("GetTerraform", mock.AnythingOfType("string")).Return(test.terraform, nil) 221 } 222 223 app := plural.CreateNewApp(&plural.Plural{Client: client}) 224 app.HelpName = plural.ApplicationName 225 os.Args = test.args 226 res, err := captureStdout(app, os.Args) 227 if test.expectedError != "" { 228 assert.Equal(t, err.Error(), test.expectedError) 229 } else { 230 assert.NoError(t, err) 231 assert.Equal(t, test.expectedResponse, res) 232 } 233 }) 234 } 235 } 236 237 func TestGetVersons(t *testing.T) { 238 tests := []struct { 239 name string 240 args []string 241 versions []*api.Version 242 expectedResponse string 243 expectedError string 244 }{ 245 { 246 name: `test "api list versions"`, 247 args: []string{plural.ApplicationName, "api", "list", "versions", "abc"}, 248 versions: []*api.Version{ 249 { 250 Id: "abc", 251 Version: "1", 252 }, 253 { 254 Id: "abc", 255 Version: "2", 256 }, 257 }, 258 expectedResponse: `+-----+---------+ 259 | ID | VERSION | 260 +-----+---------+ 261 | abc | 1 | 262 | abc | 2 | 263 +-----+---------+ 264 `, 265 }, 266 { 267 name: `test "api list versions" without CHART_ID parameter`, 268 args: []string{plural.ApplicationName, "api", "list", "versions"}, 269 versions: []*api.Version{}, 270 expectedError: "Not enough arguments provided: needs CHART_ID. Try running --help to see usage.", 271 }, 272 } 273 for _, test := range tests { 274 t.Run(test.name, func(t *testing.T) { 275 client := mocks.NewClient(t) 276 if test.expectedError == "" { 277 client.On("GetVersions", mock.AnythingOfType("string")).Return(test.versions, nil) 278 } 279 app := plural.CreateNewApp(&plural.Plural{Client: client}) 280 app.HelpName = plural.ApplicationName 281 os.Args = test.args 282 res, err := captureStdout(app, os.Args) 283 if test.expectedError != "" { 284 assert.Equal(t, err.Error(), test.expectedError) 285 } else { 286 assert.NoError(t, err) 287 assert.Equal(t, test.expectedResponse, res) 288 } 289 }) 290 } 291 } 292 293 func TestGetChartInstallations(t *testing.T) { 294 tests := []struct { 295 name string 296 args []string 297 chartInstallations []*api.ChartInstallation 298 expectedResponse string 299 expectedError string 300 }{ 301 { 302 name: `test "api list chartinstallations"`, 303 args: []string{plural.ApplicationName, "api", "list", "chartinstallations", "abc"}, 304 chartInstallations: []*api.ChartInstallation{ 305 { 306 Id: "abc", 307 Chart: &api.Chart{ 308 Id: "abc", 309 Name: "test-1", 310 }, 311 Version: &api.Version{ 312 Version: "1", 313 }, 314 }, 315 { 316 Id: "abc", 317 Chart: &api.Chart{ 318 Id: "abc", 319 Name: "test-2", 320 }, 321 Version: &api.Version{ 322 Version: "2", 323 }, 324 }, 325 }, 326 expectedResponse: `+-----+----------+------------+---------+ 327 | ID | CHART ID | CHART NAME | VERSION | 328 +-----+----------+------------+---------+ 329 | abc | abc | test-1 | 1 | 330 | abc | abc | test-2 | 2 | 331 +-----+----------+------------+---------+ 332 `, 333 }, 334 { 335 name: `test "api list chartinstallations" without REPO_ID parameter`, 336 args: []string{plural.ApplicationName, "api", "list", "chartinstallations"}, 337 chartInstallations: []*api.ChartInstallation{}, 338 expectedError: "Not enough arguments provided: needs REPO_ID. Try running --help to see usage.", 339 }, 340 } 341 for _, test := range tests { 342 t.Run(test.name, func(t *testing.T) { 343 client := mocks.NewClient(t) 344 if test.expectedError == "" { 345 client.On("GetChartInstallations", mock.AnythingOfType("string")).Return(test.chartInstallations, nil) 346 } 347 app := plural.CreateNewApp(&plural.Plural{Client: client}) 348 app.HelpName = plural.ApplicationName 349 os.Args = test.args 350 res, err := captureStdout(app, os.Args) 351 if test.expectedError != "" { 352 assert.Equal(t, err.Error(), test.expectedError) 353 } else { 354 assert.NoError(t, err) 355 assert.Equal(t, test.expectedResponse, res) 356 } 357 }) 358 } 359 } 360 361 func TestGetTerraformInstallations(t *testing.T) { 362 tests := []struct { 363 name string 364 args []string 365 terraformInstallations []*api.TerraformInstallation 366 expectedResponse string 367 expectedError string 368 }{ 369 { 370 name: `test "api list terraforminstallations"`, 371 args: []string{plural.ApplicationName, "api", "list", "terraforminstallations", "abc"}, 372 terraformInstallations: []*api.TerraformInstallation{ 373 { 374 Id: "abc", 375 Terraform: &api.Terraform{ 376 Id: "cde", 377 Name: "tf-1", 378 }, 379 }, 380 { 381 Id: "abc", 382 Terraform: &api.Terraform{ 383 Id: "fgh", 384 Name: "tf-2", 385 }, 386 }, 387 }, 388 expectedResponse: `+-----+--------------+------+ 389 | ID | TERRAFORM ID | NAME | 390 +-----+--------------+------+ 391 | abc | cde | tf-1 | 392 | abc | fgh | tf-2 | 393 +-----+--------------+------+ 394 `, 395 }, 396 { 397 name: `test "api list terraforminstallations" without REPO_ID parameter`, 398 args: []string{plural.ApplicationName, "api", "list", "terraforminstallations"}, 399 terraformInstallations: []*api.TerraformInstallation{}, 400 expectedError: "Not enough arguments provided: needs REPO_ID. Try running --help to see usage.", 401 }, 402 } 403 for _, test := range tests { 404 t.Run(test.name, func(t *testing.T) { 405 client := mocks.NewClient(t) 406 if test.expectedError == "" { 407 client.On("GetTerraformInstallations", mock.AnythingOfType("string")).Return(test.terraformInstallations, nil) 408 } 409 app := plural.CreateNewApp(&plural.Plural{Client: client}) 410 app.HelpName = plural.ApplicationName 411 os.Args = test.args 412 res, err := captureStdout(app, os.Args) 413 if test.expectedError != "" { 414 assert.Equal(t, err.Error(), test.expectedError) 415 } else { 416 assert.NoError(t, err) 417 assert.Equal(t, test.expectedResponse, res) 418 } 419 }) 420 } 421 }