Skip to content

Commit

Permalink
ROX-14528: Add OCM mock client
Browse files Browse the repository at this point in the history
  • Loading branch information
stehessel committed Jan 24, 2023
1 parent 9e68ce4 commit e93ef82
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/dinosaur/pkg/environments/development.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func NewDevelopmentEnvLoader() environments.EnvLoader {
"ocm-debug": "false",
"ams-base-url": "https://api.stage.openshift.com",
"ocm-base-url": "https://api.stage.openshift.com",
"enable-ocm-mock": "false",
"enable-ocm-mock": "true",
"enable-https": "false",
"enable-metrics-https": "false",
"enable-terms-acceptance": "false",
Expand Down
14 changes: 14 additions & 0 deletions pkg/client/ocm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"

"github.com/pkg/errors"
pkgerrors "github.com/pkg/errors"

"github.com/golang/glog"
Expand Down Expand Up @@ -115,6 +116,19 @@ func NewClient(connection *sdkClient.Connection) Client {
return &client{connection: connection}
}

// NewMockClient returns a new OCM client with stubbed responses.
func NewMockClient() Client {
return &ClientMock{
GetOrganisationFromExternalIDFunc: func(externalID string) (*amsv1.Organization, error) {
org, err := amsv1.NewOrganization().
ID("12345678").
Name("stubbed-name").
Build()
return org, errors.Wrap(err, "failed to build organisation")
},
}
}

// Connection ...
func (c *client) Connection() *sdkClient.Connection {
return c.connection
Expand Down
4 changes: 4 additions & 0 deletions pkg/client/ocm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func (c *OCMConfig) AddFlags(fs *pflag.FlagSet) {

// ReadFiles ...
func (c *OCMConfig) ReadFiles() error {
if c.EnableMock {
return nil
}

err := shared.ReadFileValueString(c.ClientIDFile, &c.ClientID)
if err != nil {
return fmt.Errorf("reading client ID file: %w", err)
Expand Down
8 changes: 8 additions & 0 deletions pkg/providers/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func ServiceProviders() di.Option {
di.Provide(observatorium.NewObservatoriumClient),

di.Provide(func(config *ocm.OCMConfig) ocm.ClusterManagementClient {
if config.EnableMock {
return ocm.NewMockClient()
}

conn, _, err := ocm.NewOCMConnection(config, config.BaseURL)
if err != nil {
logger.Logger.Error(err)
Expand All @@ -77,6 +81,10 @@ func ServiceProviders() di.Option {
}),

di.Provide(func(config *ocm.OCMConfig) ocm.AMSClient {
if config.EnableMock {
return ocm.NewMockClient()
}

conn, _, err := ocm.NewOCMConnection(config, config.AmsURL)
if err != nil {
logger.Logger.Error(err)
Expand Down
3 changes: 3 additions & 0 deletions test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func NewHelperWithHooks(t *testing.T, httpServer *httptest.Server, configuration
h.JWTCA = authHelper.JWTCA
h.AuthHelper = authHelper

// Integration tests require a valid OCM client. This requires OCM service account credentials to be set.
ocmConfig.EnableMock = false

// Set server if provided
if httpServer != nil && ocmConfig.MockMode == ocm.MockModeEmulateServer {
workers.RepeatInterval = 1 * time.Second
Expand Down

0 comments on commit e93ef82

Please sign in to comment.