github.com/hashicorp/packer@v1.14.3/internal/hcp/api/client_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package api 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/go-openapi/strfmt" 11 "github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/stable/2019-12-10/models" 12 ) 13 14 func TestGetOldestProject(t *testing.T) { 15 testcases := []struct { 16 Name string 17 ProjectList []*models.HashicorpCloudResourcemanagerProject 18 ExpectProjectID string 19 ExpectErr bool 20 }{ 21 { 22 "Only one project, project exists, success", 23 []*models.HashicorpCloudResourcemanagerProject{ 24 { 25 ID: "test-project-exists", 26 }, 27 }, 28 "test-project-exists", 29 false, 30 }, 31 { 32 "Multiple projects, pick the oldest", 33 []*models.HashicorpCloudResourcemanagerProject{ 34 { 35 ID: "test-project-exists", 36 CreatedAt: strfmt.DateTime(time.Date(2023, 1, 1, 1, 0, 0, 0, time.UTC)), 37 }, 38 { 39 ID: "test-oldest-project", 40 CreatedAt: strfmt.DateTime(time.Date(2022, 1, 1, 1, 0, 0, 0, time.UTC)), 41 }, 42 }, 43 "test-oldest-project", 44 false, 45 }, 46 { 47 "Multiple projects, different order, pick the oldest", 48 []*models.HashicorpCloudResourcemanagerProject{ 49 { 50 ID: "test-oldest-project", 51 CreatedAt: strfmt.DateTime(time.Date(2022, 1, 1, 1, 0, 0, 0, time.UTC)), 52 }, 53 { 54 ID: "test-project-exists", 55 CreatedAt: strfmt.DateTime(time.Date(2023, 1, 1, 1, 0, 0, 0, time.UTC)), 56 }, 57 }, 58 "test-oldest-project", 59 false, 60 }, 61 { 62 "No projects, should error", 63 []*models.HashicorpCloudResourcemanagerProject{}, 64 "", 65 true, 66 }, 67 } 68 69 for _, tt := range testcases { 70 t.Run(tt.Name, func(t *testing.T) { 71 proj, err := getOldestProject(tt.ProjectList) 72 if (err != nil) != tt.ExpectErr { 73 t.Errorf("test findProjectByID, expected %t, got %t", 74 tt.ExpectErr, 75 err != nil) 76 } 77 78 if proj != nil && proj.ID != tt.ExpectProjectID { 79 t.Errorf("expected to select project %q, got %q", tt.ExpectProjectID, proj.ID) 80 } 81 }) 82 } 83 }