Skip to content

Commit c06df8b

Browse files
committed
[TF-7737] Add changes to enable testing for Private Module Registry
1 parent c4b9321 commit c06df8b

4 files changed

+110
-3
lines changed

errors.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,9 @@ var (
222222

223223
ErrRequiredAgentPoolID = errors.New("'agent' execution mode requires an agent pool ID to be specified")
224224

225-
ErrRequiredAgentMode = errors.New("specifying an agent pool ID requires 'agent' execution mode")
226-
227-
ErrRequiredCategory = errors.New("category is required")
225+
ErrRequiredAgentMode = errors.New("specifying an agent pool ID requires 'agent' execution mode")
226+
ErrRequiredBranchWhenTestsEnabled = errors.New("VCS branch is required when enabling tests")
227+
ErrRequiredCategory = errors.New("category is required")
228228

229229
ErrRequiredDestinationType = errors.New("destination type is required")
230230

registry_module.go

+22
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ type RegistryModule struct {
132132
NoCode bool `jsonapi:"attr,no-code"`
133133
Permissions *RegistryModulePermissions `jsonapi:"attr,permissions"`
134134
Status RegistryModuleStatus `jsonapi:"attr,status"`
135+
TestConfig *TestConfig `jsonapi:"attr,test-config"`
135136
VCSRepo *VCSRepo `jsonapi:"attr,vcs-repo"`
136137
VersionStatuses []RegistryModuleVersionStatuses `jsonapi:"attr,version-statuses"`
137138
CreatedAt string `jsonapi:"attr,created-at"`
@@ -239,6 +240,10 @@ type RegistryModuleCreateWithVCSConnectionOptions struct {
239240
//
240241
// **Note: This field is still in BETA and subject to change.**
241242
InitialVersion *string `jsonapi:"attr,initial-version,omitempty"`
243+
244+
// Optional: Flag to enable tests for the module
245+
// **Note: This field is still in BETA and subject to change.**
246+
TestConfig *RegistryModuleTestConfigOptions `jsonapi:"attr,test-config,omitempty"`
242247
}
243248

244249
// RegistryModuleCreateVersionOptions is used when updating a registry module
@@ -252,6 +257,14 @@ type RegistryModuleUpdateOptions struct {
252257
// Optional: Flag to enable no-code provisioning for the whole module.
253258
// **Note: This field is still in BETA and subject to change.**
254259
NoCode *bool `jsonapi:"attr,no-code,omitempty"`
260+
261+
// Optional: Flag to enable tests for the module
262+
// **Note: This field is still in BETA and subject to change.**
263+
TestConfig *RegistryModuleTestConfigOptions `jsonapi:"attr,test-config,omitempty"`
264+
}
265+
266+
type RegistryModuleTestConfigOptions struct {
267+
TestsEnabled *bool `jsonapi:"attr,tests-enabled,omitempty"`
255268
}
256269

257270
type RegistryModuleVCSRepoOptions struct {
@@ -696,6 +709,15 @@ func (o RegistryModuleCreateWithVCSConnectionOptions) valid() error {
696709
if o.VCSRepo == nil {
697710
return ErrRequiredVCSRepo
698711
}
712+
713+
if o.TestConfig != nil {
714+
if *o.TestConfig.TestsEnabled {
715+
if !validString(o.VCSRepo.Branch) {
716+
return ErrRequiredBranchWhenTestsEnabled
717+
}
718+
}
719+
}
720+
699721
return o.VCSRepo.valid()
700722
}
701723

registry_module_integration_test.go

+80
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,86 @@ func TestRegistryModulesCreateBranchBasedWithVCSConnection(t *testing.T) {
762762
})
763763
}
764764

765+
func TestRegistryModulesCreateBranchBasedWithVCSConnectionWithTesting(t *testing.T) {
766+
skipUnlessBeta(t)
767+
768+
githubIdentifier := os.Getenv("GITHUB_REGISTRY_MODULE_IDENTIFIER")
769+
if githubIdentifier == "" {
770+
t.Skip("Export a valid GITHUB_REGISTRY_MODULE_IDENTIFIER before running this test")
771+
}
772+
repositoryName := strings.Split(githubIdentifier, "/")[1]
773+
registryModuleProvider := strings.SplitN(repositoryName, "-", 3)[1]
774+
registryModuleName := strings.SplitN(repositoryName, "-", 3)[2]
775+
776+
githubBranch := os.Getenv("GITHUB_REGISTRY_MODULE_BRANCH")
777+
if githubBranch == "" {
778+
githubBranch = "main"
779+
}
780+
781+
client := testClient(t)
782+
ctx := context.Background()
783+
784+
orgTest, orgTestCleanup := createOrganization(t, client)
785+
defer orgTestCleanup()
786+
787+
oauthTokenTest, oauthTokenTestCleanup := createOAuthToken(t, client, orgTest)
788+
defer oauthTokenTestCleanup()
789+
790+
t.Run("with valid options", func(t *testing.T) {
791+
options := RegistryModuleCreateWithVCSConnectionOptions{
792+
VCSRepo: &RegistryModuleVCSRepoOptions{
793+
OrganizationName: String(orgTest.Name),
794+
Identifier: String(githubIdentifier),
795+
OAuthTokenID: String(oauthTokenTest.ID),
796+
DisplayIdentifier: String(githubIdentifier),
797+
Branch: String(githubBranch),
798+
},
799+
TestConfig: &RegistryModuleTestConfigOptions{
800+
TestsEnabled: Bool(true),
801+
},
802+
}
803+
rm, err := client.RegistryModules.CreateWithVCSConnection(ctx, options)
804+
require.NoError(t, err)
805+
assert.NotEmpty(t, rm.ID)
806+
assert.Equal(t, registryModuleName, rm.Name)
807+
assert.Equal(t, registryModuleProvider, rm.Provider)
808+
assert.Equal(t, githubBranch, rm.VCSRepo.Branch)
809+
810+
t.Run("tests are enabled", func(t *testing.T) {
811+
assert.NotEmpty(t, rm.TestConfig)
812+
assert.True(t, rm.TestConfig.TestsEnabled)
813+
})
814+
})
815+
816+
t.Run("with invalid options", func(t *testing.T) {
817+
options := RegistryModuleCreateWithVCSConnectionOptions{
818+
VCSRepo: &RegistryModuleVCSRepoOptions{
819+
Identifier: String(githubIdentifier),
820+
OAuthTokenID: String(oauthTokenTest.ID),
821+
DisplayIdentifier: String(githubIdentifier),
822+
Branch: String(githubBranch),
823+
},
824+
}
825+
_, err := client.RegistryModules.CreateWithVCSConnection(ctx, options)
826+
require.Equal(t, err, ErrInvalidOrg)
827+
828+
t.Run("when the the module is not branch based and test are enabled", func(t *testing.T) {
829+
options := RegistryModuleCreateWithVCSConnectionOptions{
830+
VCSRepo: &RegistryModuleVCSRepoOptions{
831+
Identifier: String(githubIdentifier),
832+
OAuthTokenID: String(oauthTokenTest.ID),
833+
DisplayIdentifier: String(githubIdentifier),
834+
},
835+
TestConfig: &RegistryModuleTestConfigOptions{
836+
TestsEnabled: Bool(true),
837+
},
838+
}
839+
_, err := client.RegistryModules.CreateWithVCSConnection(ctx, options)
840+
require.Equal(t, err, ErrRequiredBranchWhenTestsEnabled)
841+
})
842+
})
843+
}
844+
765845
func TestRegistryModulesCreateWithGithubApp(t *testing.T) {
766846
githubIdentifier := os.Getenv("GITHUB_REGISTRY_MODULE_IDENTIFIER")
767847
if githubIdentifier == "" {

test_config.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package tfe
2+
3+
type TestConfig struct {
4+
TestsEnabled bool `jsonapi:"attr,tests-enabled"`
5+
}

0 commit comments

Comments
 (0)