Skip to content

Commit

Permalink
cloud: Make the test bool private
Browse files Browse the repository at this point in the history
Add a NewTestClient function to copy the setting from the mock client, add TestOn and TestOff
functions to control it.

Related: RHEL-60148
  • Loading branch information
bcl committed Feb 13, 2025
1 parent 408c390 commit b94a4f0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
12 changes: 10 additions & 2 deletions cloud/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func NewClient(ctx context.Context, socket HTTPClient, socketPath string) Client
}
}

// NewTestClient returns the initialized client with test value set
// from the mock client passed into it.
func NewTestClient(ctx context.Context, mock *MockClient, socketPath string) Client {
client := NewClient(ctx, mock, socketPath)
client.test = mock.test
return client
}

// InitClientUnixSocket configures the client to use a unix domain socket
// This configures the cloud.Client with the socket path
// It must be called before using any of the cloud.Client functions.
Expand All @@ -64,7 +72,7 @@ type Client struct {
host string // defaults to localhost
socketPath string
rawFunc func(string, string, int, []byte) // Pass the raw json data to a user function
Test bool // Used to fake the presense of the socket for testing
test bool // Used to fake the presense of the socket for testing
}

// SetRawCallback sets a function that will be called with the server response
Expand Down Expand Up @@ -233,7 +241,7 @@ func AppendQuery(url, query string) string {
}

func (c Client) Exists() bool {
if c.Test {
if c.test {
return true
}
return checkSocketError(c.socketPath, nil) == nil
Expand Down
12 changes: 11 additions & 1 deletion cloud/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TearDownTemporaryRepository(dir string) error {
type MockClient struct {
DoFunc func(req *http.Request) (*http.Response, error)
Req http.Request
Test bool
test bool
}

// Do saves the request in m.Req and runs the function set in m.DoFunc
Expand All @@ -67,3 +67,13 @@ func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
m.Req = *req
return m.DoFunc(req)
}

// TestOff turns off the test flag used to fake the presense of the socket file
func (m *MockClient) TestOff() {
m.test = false
}

// TestOn turns on the test flag used to fake the presense of the socket file
func (m *MockClient) TestOn() {
m.test = true
}
9 changes: 4 additions & 5 deletions cmd/composer-cli/root/test_utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ func cobraInit() {
cobra.OnInitialize(func() {
// This function is called at the start of each command execution
Client = weldr.NewClient(context.Background(), &mockWeldrClient, 1, "")
Cloud = cloud.NewClient(context.Background(), &mockCloudClient, "")
Cloud.Test = mockCloudClient.Test
Cloud = cloud.NewTestClient(context.Background(), &mockCloudClient, "")
setupJSONOutput()
})
cobraInitialized = true
Expand All @@ -149,20 +148,20 @@ func cobraInit() {
// SetupCmdTest initializes the weldr client with a Mock Client used to capture test details
// Pass in a function to be run when the client queries the server. See weldr test functions.
func SetupCmdTest(f func(request *http.Request) (*http.Response, error)) *weldr.MockClient {
mockCloudClient.Test = false
mockCloudClient.TestOff()
mockWeldrClient = weldr.MockClient{
DoFunc: f,
}
return &mockWeldrClient
}

// SetupCloudCmdTest initializes the cloud client with a Mock Client used to capture test details
// Pass in a function to be run when the client queries the server. See cloud test functions.
// Pass in a function to be run when the client queries the server. Set cloud test functions.
func SetupCloudCmdTest(f func(request *http.Request) (*http.Response, error)) *cloud.MockClient {
mockCloudClient = cloud.MockClient{
DoFunc: f,
Test: true,
}
mockCloudClient.TestOn()
return &mockCloudClient
}

Expand Down

0 comments on commit b94a4f0

Please sign in to comment.