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 new IOHandler to CLI isolation group test #6417

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 3 additions & 26 deletions tools/cli/isolation_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@
package cli

import (
"bytes"
"errors"
"flag"
"fmt"
"os"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -415,50 +413,29 @@ func TestAdminGetDomainIsolationGroups(t *testing.T) {

// Set up mocks for the current test case
tt.setupMocks(adminClient)
ioHandler := &testIOHandler{}

// Create mock app with clientFactoryMock, including any deps errors
app := NewCliApp(&clientFactoryMock{
serverAdminClient: adminClient,
})
}, WithIOHandler(ioHandler))

// Set up CLI context with flags
set := flag.NewFlagSet("test", 0)
set.String(FlagDomain, tt.flagDomain, "Domain flag")
set.String(FlagFormat, tt.flagFormat, "Format flag")
c := cli.NewContext(app, set, nil)

r, w, _ := os.Pipe()

// Save the original os.Stdout
origStdout := os.Stdout
// Defer restoring os.Stdout back to its original state
defer func() {
os.Stdout = origStdout
w.Close()
}()

// Redirect os.Stdout to the pipe
os.Stdout = w

// Call the function under test
err := AdminGetDomainIsolationGroups(c)

w.Close()
var buf bytes.Buffer
_, err2 := buf.ReadFrom(r)
if err2 != nil {
t.Fatalf("Failed to read from pipe: %v", err)
}
output := buf.String()

// Check the expected outcome
if tt.expectedError != "" {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.expectedError)
} else {
assert.Contains(t, output, "zone-1")
assert.Contains(t, output, "zone-2")
assert.NoError(t, err)
assert.Contains(t, ioHandler.outputBytes.String(), "zone-1")
}
})
}
Expand Down
Loading