From b72166ff231b54500a6d81264f4da662a3bdc6cd Mon Sep 17 00:00:00 2001 From: Lukas Zapletal Date: Mon, 13 May 2024 18:36:00 +0200 Subject: [PATCH] refactor: gate init calls with build tags Signed-off-by: Lukas Zapletal --- cmd/spec/example_reservation.go | 2 +- internal/cache/redis.go | 2 +- internal/clients/http/azure/0init.go | 10 +++++++ internal/clients/http/azure/azure_client.go | 4 --- .../http/azure/azure_service_client.go | 4 --- internal/clients/http/ec2/0init.go | 10 +++++++ internal/clients/http/ec2/ec2_client.go | 5 ---- internal/clients/http/gcp/0init.go | 10 +++++++ internal/clients/http/gcp/gcp_client.go | 4 --- .../clients/http/gcp/gcp_service_client.go | 6 +--- internal/clients/http/image_builder/0init.go | 9 ++++++ .../http/image_builder/image_client.go | 4 --- internal/clients/http/rbac/0init.go | 9 ++++++ internal/clients/http/rbac/rbac_client.go | 4 --- internal/clients/http/sources/0init.go | 9 ++++++ .../clients/http/sources/sources_client.go | 4 --- internal/clients/interface.go | 28 ++++++++++++++----- internal/clients/regional_availability.go | 2 +- internal/clients/stubs/0init.go | 16 +++++++++++ internal/clients/stubs/context_getters.go | 4 --- internal/clients/stubs/ec2_stub.go | 5 ---- internal/clients/stubs/gcp_stub.go | 5 ---- internal/clients/stubs/image_builder_stub.go | 4 --- internal/clients/stubs/rbac_stub.go | 4 --- internal/clients/stubs/sources_stub.go | 6 ---- internal/dao/dao_interfaces.go | 16 ++++++++--- internal/dao/pgx/0init.go | 13 +++++++++ internal/dao/pgx/account_pgx.go | 4 --- internal/dao/pgx/pubkey_pgx.go | 4 --- internal/dao/pgx/reservation_pgx.go | 4 --- internal/dao/pgx/service_pgx.go | 4 --- internal/dao/pgx/stat_pgx.go | 4 --- internal/dao/stubs/0init.go | 11 ++++++++ internal/dao/stubs/account_dao.go | 4 --- internal/dao/stubs/pubkey_dao.go | 4 --- internal/dao/stubs/reservation_dao.go | 4 --- internal/dao/stubs/stub_errors.go | 1 - internal/middleware/etag_middleware.go | 4 +-- internal/notifications/interface.go | 2 +- internal/queue/enqueuer.go | 4 ++- internal/queue/jq/0init.go | 9 ++++++ internal/queue/jq/worker.go | 5 ---- internal/queue/stub/0init.go | 9 ++++++ internal/queue/stub/stub.go | 5 ---- internal/services/aws_reservation_service.go | 4 +-- internal/services/gcp_reservation_service.go | 4 +-- internal/testing/buildtag_test.go | 9 ++++++ 47 files changed, 171 insertions(+), 127 deletions(-) create mode 100644 internal/clients/http/azure/0init.go create mode 100644 internal/clients/http/ec2/0init.go create mode 100644 internal/clients/http/gcp/0init.go create mode 100644 internal/clients/http/image_builder/0init.go create mode 100644 internal/clients/http/rbac/0init.go create mode 100644 internal/clients/http/sources/0init.go create mode 100644 internal/clients/stubs/0init.go create mode 100644 internal/dao/pgx/0init.go create mode 100644 internal/dao/stubs/0init.go delete mode 100644 internal/dao/stubs/stub_errors.go create mode 100644 internal/queue/jq/0init.go create mode 100644 internal/queue/stub/0init.go create mode 100644 internal/testing/buildtag_test.go diff --git a/cmd/spec/example_reservation.go b/cmd/spec/example_reservation.go index ebb77ea2..0b79d394 100644 --- a/cmd/spec/example_reservation.go +++ b/cmd/spec/example_reservation.go @@ -9,7 +9,7 @@ import ( "github.com/RHEnVision/provisioning-backend/internal/ptr" ) -var ReservationTime time.Time = MustParseTime("2013-05-13T19:20:25Z") +var ReservationTime = MustParseTime("2013-05-13T19:20:25Z") func MustParseTime(t string) time.Time { result, err := time.Parse(time.RFC3339, t) diff --git a/internal/cache/redis.go b/internal/cache/redis.go index 636bd59c..dd7951a7 100644 --- a/internal/cache/redis.go +++ b/internal/cache/redis.go @@ -34,7 +34,7 @@ var ( // Forever is used for items that should be cached "forever". Expiration of 30 days // is used to allow cleanup of unused items. -const Forever time.Duration = 24 * time.Hour * 30 +const Forever = 24 * time.Hour * 30 type Cacheable interface { CacheKeyName() string diff --git a/internal/clients/http/azure/0init.go b/internal/clients/http/azure/0init.go new file mode 100644 index 00000000..3a6c1dc5 --- /dev/null +++ b/internal/clients/http/azure/0init.go @@ -0,0 +1,10 @@ +//go:build !integration && !test + +package azure + +import "github.com/RHEnVision/provisioning-backend/internal/clients" + +func init() { + clients.GetAzureClient = newAzureClient + clients.GetServiceAzureClient = newServiceClient +} diff --git a/internal/clients/http/azure/azure_client.go b/internal/clients/http/azure/azure_client.go index db884fed..6de4a445 100644 --- a/internal/clients/http/azure/azure_client.go +++ b/internal/clients/http/azure/azure_client.go @@ -19,10 +19,6 @@ type client struct { credential *azidentity.ClientSecretCredential } -func init() { - clients.GetAzureClient = newAzureClient -} - func newAzureClient(ctx context.Context, auth *clients.Authentication) (clients.Azure, error) { opts := azidentity.ClientSecretCredentialOptions{} identityClient, err := azidentity.NewClientSecretCredential(config.Azure.TenantID, config.Azure.ClientID, config.Azure.ClientSecret, &opts) diff --git a/internal/clients/http/azure/azure_service_client.go b/internal/clients/http/azure/azure_service_client.go index b035b40f..6c08f734 100644 --- a/internal/clients/http/azure/azure_service_client.go +++ b/internal/clients/http/azure/azure_service_client.go @@ -16,10 +16,6 @@ type serviceClient struct { credential *azidentity.ClientSecretCredential } -func init() { - clients.GetServiceAzureClient = newServiceClient -} - func newServiceClient(ctx context.Context) (clients.ServiceAzure, error) { opts := azidentity.ClientSecretCredentialOptions{} identityClient, err := azidentity.NewClientSecretCredential(config.Azure.TenantID, config.Azure.ClientID, config.Azure.ClientSecret, &opts) diff --git a/internal/clients/http/ec2/0init.go b/internal/clients/http/ec2/0init.go new file mode 100644 index 00000000..19ffdf18 --- /dev/null +++ b/internal/clients/http/ec2/0init.go @@ -0,0 +1,10 @@ +//go:build !integration && !test + +package ec2 + +import "github.com/RHEnVision/provisioning-backend/internal/clients" + +func init() { + clients.GetEC2Client = newAssumedEC2ClientWithRegion + clients.GetServiceEC2Client = newEC2ClientWithRegion +} diff --git a/internal/clients/http/ec2/ec2_client.go b/internal/clients/http/ec2/ec2_client.go index 9bb4c2c6..025688ef 100644 --- a/internal/clients/http/ec2/ec2_client.go +++ b/internal/clients/http/ec2/ec2_client.go @@ -34,11 +34,6 @@ type ec2Client struct { assumed bool } -func init() { - clients.GetEC2Client = newAssumedEC2ClientWithRegion - clients.GetServiceEC2Client = newEC2ClientWithRegion -} - func logger(ctx context.Context) *zerolog.Logger { logger := zerolog.Ctx(ctx).With().Str("client", "ec2").Logger() return &logger diff --git a/internal/clients/http/gcp/0init.go b/internal/clients/http/gcp/0init.go new file mode 100644 index 00000000..5db81908 --- /dev/null +++ b/internal/clients/http/gcp/0init.go @@ -0,0 +1,10 @@ +//go:build !integration && !test + +package gcp + +import "github.com/RHEnVision/provisioning-backend/internal/clients" + +func init() { + clients.GetGCPClient = newGCPClient + clients.GetServiceGCPClient = newServiceGCPClient +} diff --git a/internal/clients/http/gcp/gcp_client.go b/internal/clients/http/gcp/gcp_client.go index 432dc45d..2ecb1563 100644 --- a/internal/clients/http/gcp/gcp_client.go +++ b/internal/clients/http/gcp/gcp_client.go @@ -27,10 +27,6 @@ type gcpClient struct { options []option.ClientOption } -func init() { - clients.GetGCPClient = newGCPClient -} - // GCP SDK does not provide a single client, so only configuration can be shared and // clients need to be created and closed in each function. // The difference between the customer and service authentication is which Project ID was given: the service or the customer diff --git a/internal/clients/http/gcp/gcp_service_client.go b/internal/clients/http/gcp/gcp_service_client.go index 8844758e..c8cb1d7c 100644 --- a/internal/clients/http/gcp/gcp_service_client.go +++ b/internal/clients/http/gcp/gcp_service_client.go @@ -19,10 +19,6 @@ type gcpServiceClient struct { options []option.ClientOption } -func init() { - clients.GetServiceGCPClient = newServiceGCPClient -} - func newServiceGCPClient(ctx context.Context) (clients.ServiceGCP, error) { options := []option.ClientOption{ option.WithCredentialsJSON([]byte(config.GCP.JSON)), @@ -161,6 +157,6 @@ func getTotalStorage(disks []*computepb.ScratchDisks) int64 { } func mbToMib(mb float32) int64 { - var mib float32 = mb * 0.9536 + var mib = mb * 0.9536 return int64(mib) } diff --git a/internal/clients/http/image_builder/0init.go b/internal/clients/http/image_builder/0init.go new file mode 100644 index 00000000..b14a428c --- /dev/null +++ b/internal/clients/http/image_builder/0init.go @@ -0,0 +1,9 @@ +//go:build !integration && !test + +package image_builder + +import "github.com/RHEnVision/provisioning-backend/internal/clients" + +func init() { + clients.GetImageBuilderClient = newImageBuilderClient +} diff --git a/internal/clients/http/image_builder/image_client.go b/internal/clients/http/image_builder/image_client.go index 6afec2f3..6a1ee965 100644 --- a/internal/clients/http/image_builder/image_client.go +++ b/internal/clients/http/image_builder/image_client.go @@ -18,10 +18,6 @@ type ibClient struct { client *ClientWithResponses } -func init() { - clients.GetImageBuilderClient = newImageBuilderClient -} - func logger(ctx context.Context) zerolog.Logger { return zerolog.Ctx(ctx).With().Str("client", "ib").Logger() } diff --git a/internal/clients/http/rbac/0init.go b/internal/clients/http/rbac/0init.go new file mode 100644 index 00000000..b99f8f4b --- /dev/null +++ b/internal/clients/http/rbac/0init.go @@ -0,0 +1,9 @@ +//go:build !integration && !test + +package rbac + +import "github.com/RHEnVision/provisioning-backend/internal/clients" + +func init() { + clients.GetRbacClient = newClient +} diff --git a/internal/clients/http/rbac/rbac_client.go b/internal/clients/http/rbac/rbac_client.go index 5e889677..eae38a43 100644 --- a/internal/clients/http/rbac/rbac_client.go +++ b/internal/clients/http/rbac/rbac_client.go @@ -27,10 +27,6 @@ type rbac struct { client *ClientWithResponses } -func init() { - clients.GetRbacClient = newClient -} - func logger(ctx context.Context) zerolog.Logger { return zerolog.Ctx(ctx).With().Str("client", "ib").Logger() } diff --git a/internal/clients/http/sources/0init.go b/internal/clients/http/sources/0init.go new file mode 100644 index 00000000..7dcdac7b --- /dev/null +++ b/internal/clients/http/sources/0init.go @@ -0,0 +1,9 @@ +//go:build !integration && !test + +package sources + +import "github.com/RHEnVision/provisioning-backend/internal/clients" + +func init() { + clients.GetSourcesClient = newSourcesClient +} diff --git a/internal/clients/http/sources/sources_client.go b/internal/clients/http/sources/sources_client.go index bc561d67..030fdb6f 100644 --- a/internal/clients/http/sources/sources_client.go +++ b/internal/clients/http/sources/sources_client.go @@ -24,10 +24,6 @@ type sourcesClient struct { client *ClientWithResponses } -func init() { - clients.GetSourcesClient = newSourcesClient -} - func logger(ctx context.Context) zerolog.Logger { return zerolog.Ctx(ctx).With().Str("client", "sources").Logger() } diff --git a/internal/clients/interface.go b/internal/clients/interface.go index 1ddb5cb4..a5d88414 100644 --- a/internal/clients/interface.go +++ b/internal/clients/interface.go @@ -15,7 +15,9 @@ type AzureResourceGroup struct { // GetSourcesClient returns Sources interface implementation. There are currently // two implementations available: HTTP and stub -var GetSourcesClient func(ctx context.Context) (Sources, error) +var GetSourcesClient = func(ctx context.Context) (Sources, error) { + panic("client func not initialized") +} // Sources interface provides access to the Sources backend service API type Sources interface { @@ -34,7 +36,9 @@ type Sources interface { // GetImageBuilderClient returns ImageBuilder interface implementation. There are currently // two implementations available: HTTP and stub -var GetImageBuilderClient func(ctx context.Context) (ImageBuilder, error) +var GetImageBuilderClient = func(ctx context.Context) (ImageBuilder, error) { + panic("client func not initialized") +} // ImageBuilder interface provides access to the Image Builder backend service API type ImageBuilder interface { @@ -62,10 +66,14 @@ type ClientStatuser interface { } // GetEC2Client returns an EC2 facade interface with assumed role. -var GetEC2Client func(ctx context.Context, auth *Authentication, region string) (EC2, error) +var GetEC2Client = func(ctx context.Context, auth *Authentication, region string) (EC2, error) { + panic("client func not initialized") +} // GetServiceEC2Client returns an EC2 client for the service account. -var GetServiceEC2Client func(ctx context.Context, region string) (EC2, error) +var GetServiceEC2Client = func(ctx context.Context, region string) (EC2, error) { + panic("client func not initialized") +} type EC2 interface { ClientStatuser @@ -106,10 +114,14 @@ type EC2 interface { } // GetAzureClient returns an Azure client with customer's subscription ID. -var GetAzureClient func(ctx context.Context, auth *Authentication) (Azure, error) +var GetAzureClient = func(ctx context.Context, auth *Authentication) (Azure, error) { + panic("client func not initialized") +} // GetServiceAzureClient returns an Azure client for the service account itself. -var GetServiceAzureClient func(ctx context.Context) (ServiceAzure, error) +var GetServiceAzureClient = func(ctx context.Context) (ServiceAzure, error) { + panic("client func not initialized") +} type Azure interface { ClientStatuser @@ -132,7 +144,9 @@ type ServiceAzure interface { } // GetGCPClient returns a GCP facade interface. -var GetGCPClient func(ctx context.Context, auth *Authentication) (GCP, error) +var GetGCPClient = func(ctx context.Context, auth *Authentication) (GCP, error) { + panic("client func not initialized") +} // GetServiceGCPClient returns a GCP client for the service account. var GetServiceGCPClient func(ctx context.Context) (ServiceGCP, error) diff --git a/internal/clients/regional_availability.go b/internal/clients/regional_availability.go index d397171f..85f10aa7 100644 --- a/internal/clients/regional_availability.go +++ b/internal/clients/regional_availability.go @@ -40,7 +40,7 @@ func NewRegionalInstanceTypes() *RegionalTypeAvailability { } } -var ErrUnknownRegionZoneCombination error = errors.New("unknown region and zone combination") +var ErrUnknownRegionZoneCombination = errors.New("unknown region and zone combination") func key(region, zone string) string { if zone == "" { diff --git a/internal/clients/stubs/0init.go b/internal/clients/stubs/0init.go new file mode 100644 index 00000000..1b140b10 --- /dev/null +++ b/internal/clients/stubs/0init.go @@ -0,0 +1,16 @@ +//go:build integration || test + +package stubs + +import "github.com/RHEnVision/provisioning-backend/internal/clients" + +func init() { + clients.GetAzureClient = getAzureClient + clients.GetEC2Client = newEC2CustomerClientStubWithRegion + clients.GetServiceEC2Client = newEC2ServiceClientStubWithRegion + clients.GetGCPClient = newGCPCustomerClientStub + clients.GetServiceGCPClient = getServiceGCPClientStub + clients.GetImageBuilderClient = getImageBuilderClientStub + clients.GetRbacClient = getRbacClient + clients.GetSourcesClient = getSourcesClient +} diff --git a/internal/clients/stubs/context_getters.go b/internal/clients/stubs/context_getters.go index d81c7b38..891b90a6 100644 --- a/internal/clients/stubs/context_getters.go +++ b/internal/clients/stubs/context_getters.go @@ -10,10 +10,6 @@ type ctxKeyType int const azureCtxKey ctxKeyType = iota -func init() { - clients.GetAzureClient = getAzureClient -} - func WithAzureClient(parent context.Context) context.Context { ctx := context.WithValue(parent, azureCtxKey, &AzureClientStub{}) return ctx diff --git a/internal/clients/stubs/ec2_stub.go b/internal/clients/stubs/ec2_stub.go index ab60701b..d7d71c6e 100644 --- a/internal/clients/stubs/ec2_stub.go +++ b/internal/clients/stubs/ec2_stub.go @@ -22,11 +22,6 @@ type EC2ClientStub struct { Imported []*types.KeyPairInfo } -func init() { - clients.GetEC2Client = newEC2CustomerClientStubWithRegion - clients.GetServiceEC2Client = newEC2ServiceClientStubWithRegion -} - func WithEC2Client(parent context.Context) context.Context { ctx := context.WithValue(parent, ec2CtxKey, &EC2ClientStub{}) return ctx diff --git a/internal/clients/stubs/gcp_stub.go b/internal/clients/stubs/gcp_stub.go index 7ca9bc2a..5089dc8f 100644 --- a/internal/clients/stubs/gcp_stub.go +++ b/internal/clients/stubs/gcp_stub.go @@ -25,11 +25,6 @@ type ( GCPServiceClientStub struct{} ) -func init() { - clients.GetGCPClient = newGCPCustomerClientStub - clients.GetServiceGCPClient = getServiceGCPClientStub -} - func newGCPCustomerClientStub(ctx context.Context, auth *clients.Authentication) (clients.GCP, error) { return getCustomerGCPClientStub(ctx, auth) } diff --git a/internal/clients/stubs/image_builder_stub.go b/internal/clients/stubs/image_builder_stub.go index c8c2ff1b..63404d34 100644 --- a/internal/clients/stubs/image_builder_stub.go +++ b/internal/clients/stubs/image_builder_stub.go @@ -13,10 +13,6 @@ var imageBuilderCtxKey imageBuilderCtxKeyType = "image-builder-interface" type ImageBuilderClientStub struct{} -func init() { - clients.GetImageBuilderClient = getImageBuilderClientStub -} - func WithImageBuilderClient(parent context.Context) context.Context { ctx := context.WithValue(parent, imageBuilderCtxKey, &ImageBuilderClientStub{}) return ctx diff --git a/internal/clients/stubs/rbac_stub.go b/internal/clients/stubs/rbac_stub.go index 8aafbdce..c6ce4d31 100644 --- a/internal/clients/stubs/rbac_stub.go +++ b/internal/clients/stubs/rbac_stub.go @@ -8,10 +8,6 @@ import ( type rbacClient struct{} -func init() { - clients.GetRbacClient = getRbacClient -} - func getRbacClient(ctx context.Context) clients.Rbac { return rbacClient{} } diff --git a/internal/clients/stubs/sources_stub.go b/internal/clients/stubs/sources_stub.go index 9e5886ba..f693596b 100644 --- a/internal/clients/stubs/sources_stub.go +++ b/internal/clients/stubs/sources_stub.go @@ -23,12 +23,6 @@ type SourcesClientStub struct { auths map[string]*clients.Authentication } -func init() { - // We are currently using SourcesClientStub - clients.GetSourcesClient = getSourcesClient -} - -// SourcesClient func WithSourcesClient(parent context.Context) context.Context { ctx := context.WithValue(parent, sourcesCtxKey, &SourcesClientStub{auths: make(map[string]*clients.Authentication)}) return ctx diff --git a/internal/dao/dao_interfaces.go b/internal/dao/dao_interfaces.go index fa85ce82..bf4f9ba7 100644 --- a/internal/dao/dao_interfaces.go +++ b/internal/dao/dao_interfaces.go @@ -17,7 +17,9 @@ import ( "github.com/RHEnVision/provisioning-backend/internal/models" ) -var GetAccountDao func(ctx context.Context) AccountDao +var GetAccountDao = func(ctx context.Context) AccountDao { + panic("dao not initialized") +} // AccountDao represents an account (tenant) type AccountDao interface { @@ -29,7 +31,9 @@ type AccountDao interface { List(ctx context.Context, limit, offset int64) ([]*models.Account, error) } -var GetPubkeyDao func(ctx context.Context) PubkeyDao +var GetPubkeyDao = func(ctx context.Context) PubkeyDao { + panic("dao not initialized") +} // PubkeyDao represents Pubkeys (public part of ssh key pair) and corresponding Resources (uploaded pubkeys // to specific cloud providers in specific regions). @@ -47,7 +51,9 @@ type PubkeyDao interface { UnscopedDeleteResource(ctx context.Context, id int64) error } -var GetReservationDao func(ctx context.Context) ReservationDao +var GetReservationDao = func(ctx context.Context) ReservationDao { + panic("dao not initialized") +} // ReservationDao represents a reservation, an abstraction of one or more background jobs with // associated detail information different for different cloud providers (like number of vCPUs, @@ -118,7 +124,9 @@ type ReservationDao interface { Cleanup(ctx context.Context) error } -var GetStatDao func(ctx context.Context) StatDao +var GetStatDao = func(ctx context.Context) StatDao { + panic("dao not initialized") +} // StatDao represents stats about the application run type StatDao interface { diff --git a/internal/dao/pgx/0init.go b/internal/dao/pgx/0init.go new file mode 100644 index 00000000..bdeded88 --- /dev/null +++ b/internal/dao/pgx/0init.go @@ -0,0 +1,13 @@ +//go:build !integration && !test + +package pgx + +import "github.com/RHEnVision/provisioning-backend/internal/dao" + +func init() { + dao.GetAccountDao = getAccountDao + dao.GetPubkeyDao = getPubkeyDao + dao.GetReservationDao = getReservationDao + dao.GetServiceDao = getServiceDao + dao.GetStatDao = getStatDao +} diff --git a/internal/dao/pgx/account_pgx.go b/internal/dao/pgx/account_pgx.go index 61200080..ebfb3d35 100644 --- a/internal/dao/pgx/account_pgx.go +++ b/internal/dao/pgx/account_pgx.go @@ -14,10 +14,6 @@ import ( "github.com/rs/zerolog" ) -func init() { - dao.GetAccountDao = getAccountDao -} - type accountDao struct{} func getAccountDao(ctx context.Context) dao.AccountDao { diff --git a/internal/dao/pgx/pubkey_pgx.go b/internal/dao/pgx/pubkey_pgx.go index f105ce2b..8c2dea15 100644 --- a/internal/dao/pgx/pubkey_pgx.go +++ b/internal/dao/pgx/pubkey_pgx.go @@ -11,10 +11,6 @@ import ( "github.com/georgysavva/scany/v2/pgxscan" ) -func init() { - dao.GetPubkeyDao = getPubkeyDao -} - type pubkeyDao struct{} func getPubkeyDao(ctx context.Context) dao.PubkeyDao { diff --git a/internal/dao/pgx/reservation_pgx.go b/internal/dao/pgx/reservation_pgx.go index 0c7c9815..d7f4f5c7 100644 --- a/internal/dao/pgx/reservation_pgx.go +++ b/internal/dao/pgx/reservation_pgx.go @@ -16,10 +16,6 @@ import ( "github.com/rs/zerolog" ) -func init() { - dao.GetReservationDao = getReservationDao -} - type reservationDao struct{} func getReservationDao(ctx context.Context) dao.ReservationDao { diff --git a/internal/dao/pgx/service_pgx.go b/internal/dao/pgx/service_pgx.go index 4f19477c..f9000e3e 100644 --- a/internal/dao/pgx/service_pgx.go +++ b/internal/dao/pgx/service_pgx.go @@ -11,10 +11,6 @@ import ( "github.com/rs/zerolog" ) -func init() { - dao.GetServiceDao = getServiceDao -} - type serviceDao struct{} func getServiceDao(_ context.Context) dao.ServiceDao { diff --git a/internal/dao/pgx/stat_pgx.go b/internal/dao/pgx/stat_pgx.go index c5f84497..f208ac35 100644 --- a/internal/dao/pgx/stat_pgx.go +++ b/internal/dao/pgx/stat_pgx.go @@ -10,10 +10,6 @@ import ( "github.com/georgysavva/scany/v2/pgxscan" ) -func init() { - dao.GetStatDao = getStatDao -} - type statDao struct{} func getStatDao(ctx context.Context) dao.StatDao { diff --git a/internal/dao/stubs/0init.go b/internal/dao/stubs/0init.go new file mode 100644 index 00000000..ca716bea --- /dev/null +++ b/internal/dao/stubs/0init.go @@ -0,0 +1,11 @@ +//go:build integration || test + +package stubs + +import "github.com/RHEnVision/provisioning-backend/internal/dao" + +func init() { + dao.GetAccountDao = getAccountDao + dao.GetPubkeyDao = getPubkeyDao + dao.GetReservationDao = getReservationDao +} diff --git a/internal/dao/stubs/account_dao.go b/internal/dao/stubs/account_dao.go index ddade451..735030dd 100644 --- a/internal/dao/stubs/account_dao.go +++ b/internal/dao/stubs/account_dao.go @@ -36,10 +36,6 @@ func buildAccountDaoWithNullValue() *accountDaoStub { } } -func init() { - dao.GetAccountDao = getAccountDao -} - func getAccountDao(ctx context.Context) dao.AccountDao { return getAccountDaoStub(ctx) } diff --git a/internal/dao/stubs/pubkey_dao.go b/internal/dao/stubs/pubkey_dao.go index ffe7abcb..928a878e 100644 --- a/internal/dao/stubs/pubkey_dao.go +++ b/internal/dao/stubs/pubkey_dao.go @@ -14,10 +14,6 @@ type pubkeyDaoStub struct { resourceStore []*models.PubkeyResource } -func init() { - dao.GetPubkeyDao = getPubkeyDao -} - func PubkeyStubCount(ctx context.Context) int { pkdao := getPubkeyDaoStub(ctx) return len(pkdao.store) diff --git a/internal/dao/stubs/reservation_dao.go b/internal/dao/stubs/reservation_dao.go index cc0eadf2..3f7a35cc 100644 --- a/internal/dao/stubs/reservation_dao.go +++ b/internal/dao/stubs/reservation_dao.go @@ -16,10 +16,6 @@ type reservationDaoStub struct { instances map[int64][]*models.ReservationInstance } -func init() { - dao.GetReservationDao = getReservationDao -} - func AWSReservationStubCount(ctx context.Context) int { resDao := getReservationDaoStub(ctx) return len(resDao.storeAWS) diff --git a/internal/dao/stubs/stub_errors.go b/internal/dao/stubs/stub_errors.go deleted file mode 100644 index 851a1797..00000000 --- a/internal/dao/stubs/stub_errors.go +++ /dev/null @@ -1 +0,0 @@ -package stubs diff --git a/internal/middleware/etag_middleware.go b/internal/middleware/etag_middleware.go index e911efe6..1f8b562c 100644 --- a/internal/middleware/etag_middleware.go +++ b/internal/middleware/etag_middleware.go @@ -12,10 +12,10 @@ import ( ) // An InstanceTypeExpiration represents the default expiration time for instance types -const InstanceTypeExpiration time.Duration = 4 * time.Hour +const InstanceTypeExpiration = 4 * time.Hour // An OpenAPIExpiration represents the default expiration time for the OpenAPI JSON -const OpenAPIExpiration time.Duration = 30 * time.Minute +const OpenAPIExpiration = 30 * time.Minute // An InvalidETagErr is returned to prevent empty tag generation var InvalidETagErr = errors.New("empty etag provided") diff --git a/internal/notifications/interface.go b/internal/notifications/interface.go index 01eb1a3f..6a5dca3d 100644 --- a/internal/notifications/interface.go +++ b/internal/notifications/interface.go @@ -4,7 +4,7 @@ import ( "context" ) -var GetNotificationClient func(ctx context.Context) NotificationClient = getNoopNotificationClient +var GetNotificationClient = getNoopNotificationClient type NotificationClient interface { SuccessfulLaunch(ctx context.Context, reservationId int64) diff --git a/internal/queue/enqueuer.go b/internal/queue/enqueuer.go index d783bdf2..404060e5 100644 --- a/internal/queue/enqueuer.go +++ b/internal/queue/enqueuer.go @@ -6,4 +6,6 @@ import ( "github.com/RHEnVision/provisioning-backend/pkg/worker" ) -var GetEnqueuer func(ctx context.Context) worker.JobEnqueuer +var GetEnqueuer = func(ctx context.Context) worker.JobEnqueuer { + panic("enqueuer not initialized") +} diff --git a/internal/queue/jq/0init.go b/internal/queue/jq/0init.go new file mode 100644 index 00000000..b737f539 --- /dev/null +++ b/internal/queue/jq/0init.go @@ -0,0 +1,9 @@ +//go:build !integration && !test + +package jq + +import "github.com/RHEnVision/provisioning-backend/internal/queue" + +func init() { + queue.GetEnqueuer = getEnqueuer +} diff --git a/internal/queue/jq/worker.go b/internal/queue/jq/worker.go index 40b0abda..fd0dfb0e 100644 --- a/internal/queue/jq/worker.go +++ b/internal/queue/jq/worker.go @@ -6,7 +6,6 @@ import ( "github.com/RHEnVision/provisioning-backend/internal/config" "github.com/RHEnVision/provisioning-backend/internal/jobs" - "github.com/RHEnVision/provisioning-backend/internal/queue" "github.com/RHEnVision/provisioning-backend/pkg/worker" "github.com/rs/zerolog" ) @@ -20,10 +19,6 @@ func getEnqueuer(_ context.Context) worker.JobEnqueuer { return enqueuer } -func init() { - queue.GetEnqueuer = getEnqueuer -} - func RegisterJobs(logger *zerolog.Logger) { logger.Debug().Msg("Registering job queue handlers and interfaces") workers.RegisterHandler(jobs.TypeNoop, jobs.HandleNoop, jobs.NoopJobArgs{}) diff --git a/internal/queue/stub/0init.go b/internal/queue/stub/0init.go new file mode 100644 index 00000000..4f39a3ec --- /dev/null +++ b/internal/queue/stub/0init.go @@ -0,0 +1,9 @@ +//go:build integration || test + +package stub + +import "github.com/RHEnVision/provisioning-backend/internal/queue" + +func init() { + queue.GetEnqueuer = getEnqueuer +} diff --git a/internal/queue/stub/stub.go b/internal/queue/stub/stub.go index 5f0b7958..304414e2 100644 --- a/internal/queue/stub/stub.go +++ b/internal/queue/stub/stub.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" - "github.com/RHEnVision/provisioning-backend/internal/queue" "github.com/RHEnVision/provisioning-backend/pkg/worker" ) @@ -27,10 +26,6 @@ type stubEnqueuer struct { enqueued []*worker.Job } -func init() { - queue.GetEnqueuer = getEnqueuer -} - // WithEnqueuer returns new context with Job enqueue struct that keeps the jobs func WithEnqueuer(parent context.Context) context.Context { ctx := context.WithValue(parent, enqueueCtxKey, &stubEnqueuer{}) diff --git a/internal/services/aws_reservation_service.go b/internal/services/aws_reservation_service.go index e62aa7f8..7b48ab8e 100644 --- a/internal/services/aws_reservation_service.go +++ b/internal/services/aws_reservation_service.go @@ -27,8 +27,8 @@ import ( func CreateAWSReservation(w http.ResponseWriter, r *http.Request) { logger := zerolog.Ctx(r.Context()) - var accountId int64 = identity.AccountId(r.Context()) - var id identity.Principal = identity.Identity(r.Context()) + var accountId = identity.AccountId(r.Context()) + var id = identity.Identity(r.Context()) payload := &payloads.AWSReservationRequest{} if err := render.Bind(r, payload); err != nil { diff --git a/internal/services/gcp_reservation_service.go b/internal/services/gcp_reservation_service.go index a6135274..19a62f04 100644 --- a/internal/services/gcp_reservation_service.go +++ b/internal/services/gcp_reservation_service.go @@ -26,8 +26,8 @@ import ( func CreateGCPReservation(w http.ResponseWriter, r *http.Request) { logger := zerolog.Ctx(r.Context()) - var accountId int64 = identity.AccountId(r.Context()) - var id identity.Principal = identity.Identity(r.Context()) + var accountId = identity.AccountId(r.Context()) + var id = identity.Identity(r.Context()) payload := &payloads.GCPReservationRequest{} if err := render.Bind(r, payload); err != nil { diff --git a/internal/testing/buildtag_test.go b/internal/testing/buildtag_test.go new file mode 100644 index 00000000..6b6335da --- /dev/null +++ b/internal/testing/buildtag_test.go @@ -0,0 +1,9 @@ +//go:build !integration && !test + +package testing + +import "testing" + +func TestBuildTag(t *testing.T) { + t.Fatal("Execute tests with '-tags test' and/or '-tags integration'") +}