Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add private registry provider version #405

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions errors.go
Expand Up @@ -155,6 +155,16 @@ var (
ErrInvalidCommentID = errors.New("invalid value for comment ID")

ErrInvalidCommentBody = errors.New("invalid value for comment body")

ErrInvalidNamespace = errors.New("invalid value for namespace")

ErrInvalidPrivateProviderNamespaceDoesntMatchOrganization = errors.New("invalid namespace must match organization name for private providers")

ErrInvalidRegistryName = errors.New("invalid value for registry-name")

ErrInvalidRegistryNameType = errors.New("invalid type for registry-name. Please use 'RegistryName'")
Copy link
Collaborator

@brandonc brandonc May 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this error is used (And if I understand the point of the error, the value should be "registry-providers")


ErrInvalidKeyID = errors.New("invalid value for key-id")
)

// Missing values for required field/option
Expand Down Expand Up @@ -268,4 +278,6 @@ var (
ErrEmptyTeamName = errors.New("team name can not be empty")

ErrInvalidEmail = errors.New("email is invalid")

ErrRequiredPrivateRegistry = errors.New("only private registry is allowed")
)
2 changes: 2 additions & 0 deletions generate_mocks.sh
Expand Up @@ -35,6 +35,8 @@ mockgen -source=policy_set.go -destination=mocks/policy_set_mocks.go -package=mo
mockgen -source=policy_set_parameter.go -destination=mocks/policy_set_parameter_mocks.go -package=mocks
mockgen -source=policy_set_version.go -destination=mocks/policy_set_version_mocks.go -package=mocks
mockgen -source=registry_module.go -destination=mocks/registry_module_mocks.go -package=mocks
mockgen -source=registry_provider.go -destination=mocks/registry_provider_mocks.go -package=mocks
mockgen -source=registry_provider_version.go -destination=mocks/registry_provider_version_mocks.go -package=mocks
mockgen -source=run.go -destination=mocks/run_mocks.go -package=mocks
mockgen -source=run_task.go -destination=mocks/run_tasks.go -package=mocks
mockgen -source=run_trigger.go -destination=mocks/run_trigger_mocks.go -package=mocks
Expand Down
149 changes: 144 additions & 5 deletions helper_test.go
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"sync"
"testing"
Expand Down Expand Up @@ -717,23 +718,24 @@ func createRegistryModuleWithVersion(t *testing.T, client *Client, org *Organiza
}

func createRunTask(t *testing.T, client *Client, org *Organization) (*RunTask, func()) {
runTaskURL := os.Getenv("TFC_RUN_TASK_URL")
if runTaskURL == "" {
t.Error("Cannot create a run task with an empty URL. You must set TFC_RUN_TASK_URL for run task related tests.")
}

var orgCleanup func()

if org == nil {
org, orgCleanup = createOrganization(t, client)
}

runTaskURL := os.Getenv("TFC_RUN_TASK_URL")
if runTaskURL == "" {
t.Error("Cannot create a run task with an empty URL. You must set TFC_RUN_TASK_URL for run task related tests.")
}

ctx := context.Background()
r, err := client.RunTasks.Create(ctx, org.Name, RunTaskCreateOptions{
Name: "tst-" + randomString(t),
URL: runTaskURL,
Category: "task",
})

if err != nil {
t.Fatal(err)
}
Expand All @@ -751,6 +753,139 @@ func createRunTask(t *testing.T, client *Client, org *Organization) (*RunTask, f
}
}

func createPrivateRegistryProvider(t *testing.T, client *Client, org *Organization) (*RegistryProvider, func()) {
var orgCleanup func()

if org == nil {
org, orgCleanup = createOrganization(t, client)
}

ctx := context.Background()

options := RegistryProviderCreateOptions{
Name: "tst-name-" + randomString(t),
Namespace: org.Name,
RegistryName: PrivateRegistry,
}

prv, err := client.RegistryProviders.Create(ctx, org.Name, options)

if err != nil {
t.Fatal(err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should return something here, since prv may be nil and the next line will panic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the line does we want it to - Fatal is equivalent to Log plus FailNow. FailNow stops execution, calls runtime.Goexit, and continues execution with the next test or benchmark.

}

prv.Organization = org

return prv, func() {
id := RegistryProviderID {
OrganizationName: org.Name,
RegistryName: prv.RegistryName,
Namespace: prv.Namespace,
Name: prv.Name,
}

if err := client.RegistryProviders.Delete(ctx, id); err != nil {
t.Errorf("Error destroying registry provider! WARNING: Dangling resources\n"+
"may exist! The full error is shown below.\n\n"+
"Registry Provider: %s/%s\nError: %s", prv.Namespace, prv.Name, err)
}

if orgCleanup != nil {
orgCleanup()
}
}
}

func createPublicRegistryProvider(t *testing.T, client *Client, org *Organization) (*RegistryProvider, func()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💅 this and the previous method have so much in common, I'm wondering if there should be a third method that parameterizes PublicRegistry/PrivateRegistry ?

var orgCleanup func()

if org == nil {
org, orgCleanup = createOrganization(t, client)
}

ctx := context.Background()

options := RegistryProviderCreateOptions{
Name: "tst-name-" + randomString(t),
Namespace: "tst-namespace-" + randomString(t),
RegistryName: PublicRegistry,
}

prv, err := client.RegistryProviders.Create(ctx, org.Name, options)

if err != nil {
t.Fatal(err)
}

prv.Organization = org

return prv, func() {
id := RegistryProviderID {
OrganizationName: org.Name,
RegistryName: prv.RegistryName,
Namespace: prv.Namespace,
Name: prv.Name,
}

if err := client.RegistryProviders.Delete(ctx, id); err != nil {
t.Errorf("Error destroying registry provider! WARNING: Dangling resources\n"+
"may exist! The full error is shown below.\n\n"+
"Registry Provider: %s/%s\nError: %s", prv.Namespace, prv.Name, err)
}

if orgCleanup != nil {
orgCleanup()
}
}
}

func createRegistryProviderVersion(t *testing.T, client *Client, provider *RegistryProvider) (*RegistryProviderVersion, func()) {
var providerCleanup func()

if provider == nil {
provider, providerCleanup = createPrivateRegistryProvider(t, client, nil)
}

providerID := RegistryProviderID {
OrganizationName: provider.Organization.Name,
RegistryName: provider.RegistryName,
Namespace: provider.Namespace,
Name: provider.Name,
}

ctx := context.Background()

options := RegistryProviderVersionCreateOptions{
Version: randomSemver(t),
KeyID: randomString(t),
}

prvv, err := client.RegistryProviderVersions.Create(ctx, providerID, options)

if err != nil {
t.Fatal(err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same 'return' concern as above

}

prvv.RegistryProvider = provider

return prvv, func() {
id := RegistryProviderVersionID {
Version: options.Version,
RegistryProviderID: providerID,
}

if err := client.RegistryProviderVersions.Delete(ctx, id); err != nil {
t.Errorf("Error destroying registry provider version! WARNING: Dangling resources\n"+
"may exist! The full error is shown below.\n\n"+
"Registry Provider Version: %s/%s/%s\nError: %s", prvv.RegistryProvider.Namespace, prvv.RegistryProvider.Name, prvv.Version, err)
}

if providerCleanup != nil {
providerCleanup()
}
}
}

func createSSHKey(t *testing.T, client *Client, org *Organization) (*SSHKey, func()) {
var orgCleanup func()

Expand Down Expand Up @@ -1271,6 +1406,10 @@ func randomString(t *testing.T) string {
return v
}

func randomSemver(t *testing.T) string {
return fmt.Sprintf("%d.%d.%d", rand.Intn(99)+3, rand.Intn(99)+1, rand.Intn(99)+1)
}

// skips a test if the environment is for Terraform Cloud.
func skipIfCloud(t *testing.T) {
if !enterpriseEnabled() {
Expand Down