mirror of
https://github.com/hashicorp/packer.git
synced 2026-09-21 07:21:40 -04:00
* Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at https://hashi.co/license-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package api
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/go-openapi/strfmt"
|
|
"github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/preview/2019-12-10/models"
|
|
)
|
|
|
|
func TestGetOldestProject(t *testing.T) {
|
|
testcases := []struct {
|
|
Name string
|
|
ProjectList []*models.HashicorpCloudResourcemanagerProject
|
|
ExpectProjectID string
|
|
ExpectErr bool
|
|
}{
|
|
{
|
|
"Only one project, project exists, success",
|
|
[]*models.HashicorpCloudResourcemanagerProject{
|
|
{
|
|
ID: "test-project-exists",
|
|
},
|
|
},
|
|
"test-project-exists",
|
|
false,
|
|
},
|
|
{
|
|
"Multiple projects, pick the oldest",
|
|
[]*models.HashicorpCloudResourcemanagerProject{
|
|
{
|
|
ID: "test-project-exists",
|
|
CreatedAt: strfmt.DateTime(time.Date(2023, 1, 1, 1, 0, 0, 0, time.UTC)),
|
|
},
|
|
{
|
|
ID: "test-oldest-project",
|
|
CreatedAt: strfmt.DateTime(time.Date(2022, 1, 1, 1, 0, 0, 0, time.UTC)),
|
|
},
|
|
},
|
|
"test-oldest-project",
|
|
false,
|
|
},
|
|
{
|
|
"Multiple projects, different order, pick the oldest",
|
|
[]*models.HashicorpCloudResourcemanagerProject{
|
|
{
|
|
ID: "test-oldest-project",
|
|
CreatedAt: strfmt.DateTime(time.Date(2022, 1, 1, 1, 0, 0, 0, time.UTC)),
|
|
},
|
|
{
|
|
ID: "test-project-exists",
|
|
CreatedAt: strfmt.DateTime(time.Date(2023, 1, 1, 1, 0, 0, 0, time.UTC)),
|
|
},
|
|
},
|
|
"test-oldest-project",
|
|
false,
|
|
},
|
|
{
|
|
"No projects, should error",
|
|
[]*models.HashicorpCloudResourcemanagerProject{},
|
|
"",
|
|
true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range testcases {
|
|
t.Run(tt.Name, func(t *testing.T) {
|
|
proj, err := getOldestProject(tt.ProjectList)
|
|
if (err != nil) != tt.ExpectErr {
|
|
t.Errorf("test findProjectByID, expected %t, got %t",
|
|
tt.ExpectErr,
|
|
err != nil)
|
|
}
|
|
|
|
if proj != nil && proj.ID != tt.ExpectProjectID {
|
|
t.Errorf("expected to select project %q, got %q", tt.ExpectProjectID, proj.ID)
|
|
}
|
|
})
|
|
}
|
|
}
|