Skip to content

Commit ea426b0

Browse files
authored
ci change to avoid interactive prompt (#1899)
* Test Refactor OADP CLI installation in CI Dockerfile and e2e tests - Removed direct installation of oadp-cli from the Dockerfile. - Introduced a new CLISetup struct to handle the cloning, building, and installation of oadp-cli in a more structured manner. - Updated e2e tests to utilize the new CLISetup for verifying OADP CLI availability and configuration. * Debug * Manual build and push to path * Cleanup * Refactor CLI binary installation process in e2e tests - Changed the installation method from moving to copying the kubectl-oadp binary to /usr/local/bin. - Updated log messages to reflect the change in operation. - Improved error messages for better clarity on failure points. * Update
1 parent d9ff173 commit ea426b0

File tree

3 files changed

+175
-16
lines changed

3 files changed

+175
-16
lines changed

build/ci-Dockerfile

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ WORKDIR /go/src/github.com/openshift/oadp-operator
55

66
COPY ./ .
77

8-
# Clone and install oadp-cli
9-
RUN git clone https://github.com/migtools/oadp-cli.git /tmp/oadp-cli && \
10-
cd /tmp/oadp-cli && \
11-
make build && \
12-
cp kubectl-oadp /usr/local/bin/ && \
13-
chmod +x /usr/local/bin/kubectl-oadp; \
14-
rm -rf /tmp/oadp-cli
15-
168
# Install kubectl
179
RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" && \
1810
chmod +x kubectl && \

tests/e2e/backup_restore_cli_suite_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package e2e_test
33
import (
44
"fmt"
55
"log"
6-
"os/exec"
76
"strings"
87
"time"
98

@@ -196,14 +195,11 @@ var _ = ginkgo.Describe("Backup and restore tests via OADP CLI", ginkgo.Label("c
196195
}
197196

198197
ginkgo.BeforeAll(func() {
199-
// Verify OADP CLI is available (should be installed in Docker image)
200-
log.Print("Verifying OADP CLI is available...")
201-
cmd := exec.Command("kubectl", "oadp", "version")
202-
output, err := cmd.CombinedOutput()
203-
if err != nil {
204-
ginkgo.Fail(fmt.Sprintf("OADP CLI not available: %v, output: %s", err, string(output)))
198+
199+
cliSetup := lib.NewOADPCLISetup()
200+
if err := cliSetup.Install(); err != nil {
201+
ginkgo.Fail(fmt.Sprintf("OADP CLI setup failed: %v", err))
205202
}
206-
log.Printf("OADP CLI available. Version: %s", string(output))
207203
})
208204

209205
var _ = ginkgo.AfterEach(func(ctx ginkgo.SpecContext) {

tests/e2e/lib/cli_common.go

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package lib
22

33
import (
4+
"fmt"
45
"log"
6+
"os"
57
"os/exec"
8+
"path/filepath"
69
"strings"
710
)
811

@@ -56,3 +59,171 @@ func ParsePhaseFromYAML(yamlOutput string) string {
5659
}
5760
return ""
5861
}
62+
63+
type CLISetup struct {
64+
repoURL string
65+
installArgs []string
66+
namespace string
67+
}
68+
69+
func NewOADPCLISetup() *CLISetup {
70+
return &CLISetup{
71+
repoURL: "https://github.com/migtools/oadp-cli.git",
72+
installArgs: []string{"build"},
73+
namespace: "openshift-adp",
74+
}
75+
}
76+
77+
func (c *CLISetup) Install() error {
78+
tmpDir, err := c.createTempDir()
79+
if err != nil {
80+
return err
81+
}
82+
defer os.RemoveAll(tmpDir)
83+
84+
cloneDir := filepath.Join(tmpDir, "oadp-cli")
85+
86+
steps := []struct {
87+
name string
88+
fn func() error
89+
}{
90+
{"Cloning repository", func() error { return c.cloneRepo(cloneDir) }},
91+
{"Building and installing", func() error { return c.buildAndInstall(cloneDir) }},
92+
{"Verifying installation", func() error { return c.verifyInstallation() }},
93+
{"Configuring namespace", func() error { return c.configureNamespace() }},
94+
}
95+
96+
for _, step := range steps {
97+
log.Printf("OADP CLI Setup: %s...", step.name)
98+
if err := step.fn(); err != nil {
99+
return fmt.Errorf("%s failed: %w", step.name, err)
100+
}
101+
}
102+
103+
log.Print("OADP CLI setup completed successfully")
104+
return nil
105+
}
106+
107+
func (c *CLISetup) createTempDir() (string, error) {
108+
tmpDir, err := os.MkdirTemp("", "oadp-cli-*")
109+
if err != nil {
110+
return "", fmt.Errorf("failed to create temp directory: %w", err)
111+
}
112+
return tmpDir, nil
113+
}
114+
115+
func (c *CLISetup) cloneRepo(cloneDir string) error {
116+
return runCommand("git", []string{"clone", c.repoURL, cloneDir}, "")
117+
}
118+
119+
func (c *CLISetup) buildAndInstall(cloneDir string) error {
120+
// Build the binary
121+
log.Print("Building OADP CLI...")
122+
if err := runCommand("make", c.installArgs, cloneDir); err != nil {
123+
return fmt.Errorf("build failed: %w", err)
124+
}
125+
126+
// Verify the binary was created
127+
binaryPath := filepath.Join(cloneDir, "kubectl-oadp")
128+
if _, err := os.Stat(binaryPath); err != nil {
129+
return fmt.Errorf("kubectl-oadp binary not found at %s: %w", binaryPath, err)
130+
}
131+
132+
// Try multiple target locations, starting with user-writable paths
133+
targetPaths := []string{
134+
fmt.Sprintf("%s/bin/kubectl-oadp", os.Getenv("HOME")),
135+
"/usr/local/bin/kubectl-oadp",
136+
}
137+
138+
var targetPath string
139+
var moveErr error
140+
141+
for _, tp := range targetPaths {
142+
targetPath = tp
143+
log.Printf("Attempting to move binary from %s to %s", binaryPath, targetPath)
144+
145+
// Create directory if it doesn't exist (for ~/bin)
146+
if targetPath == fmt.Sprintf("%s/bin/kubectl-oadp", os.Getenv("HOME")) {
147+
binDir := filepath.Dir(targetPath)
148+
if err := os.MkdirAll(binDir, 0755); err != nil {
149+
log.Printf("Failed to create directory %s: %v", binDir, err)
150+
continue
151+
}
152+
}
153+
154+
if err := runCommand("mv", []string{binaryPath, targetPath}, ""); err != nil {
155+
log.Printf("Failed to move to %s: %v", targetPath, err)
156+
moveErr = err
157+
continue
158+
}
159+
160+
// Success!
161+
moveErr = nil
162+
break
163+
}
164+
165+
if moveErr != nil {
166+
return fmt.Errorf("failed to move binary to any location: %w", moveErr)
167+
}
168+
169+
// Make it executable
170+
if err := runCommand("chmod", []string{"+x", targetPath}, ""); err != nil {
171+
return fmt.Errorf("failed to make binary executable: %w", err)
172+
}
173+
174+
// If we installed to ~/bin, ensure it's in PATH
175+
if targetPath == fmt.Sprintf("%s/bin/kubectl-oadp", os.Getenv("HOME")) {
176+
homeBin := fmt.Sprintf("%s/bin", os.Getenv("HOME"))
177+
currentPath := os.Getenv("PATH")
178+
if !strings.Contains(currentPath, homeBin) {
179+
newPath := fmt.Sprintf("%s:%s", homeBin, currentPath)
180+
os.Setenv("PATH", newPath)
181+
log.Printf("Added %s to PATH", homeBin)
182+
}
183+
}
184+
185+
log.Printf("Successfully installed kubectl-oadp to %s", targetPath)
186+
return nil
187+
}
188+
189+
func (c *CLISetup) verifyInstallation() error {
190+
// Check current PATH
191+
cmd := exec.Command("bash", "-c", "echo $PATH")
192+
if output, err := cmd.CombinedOutput(); err == nil {
193+
log.Printf("Current PATH: %s", string(output))
194+
}
195+
196+
// Try to find kubectl-oadp binary
197+
cmd = exec.Command("which", "kubectl-oadp")
198+
if output, err := cmd.CombinedOutput(); err == nil {
199+
log.Printf("kubectl-oadp found at: %s", string(output))
200+
} else {
201+
log.Printf("kubectl-oadp not found in PATH: %v", err)
202+
}
203+
204+
// List kubectl plugins
205+
cmd = exec.Command("kubectl", "plugin", "list")
206+
if output, err := cmd.CombinedOutput(); err == nil {
207+
log.Printf("Available kubectl plugins: %s", string(output))
208+
}
209+
210+
return runCommand("kubectl", []string{"oadp", "version"}, "")
211+
}
212+
213+
func (c *CLISetup) configureNamespace() error {
214+
return runCommand("kubectl", []string{"oadp", "client", "config", "set", fmt.Sprintf("namespace=%s", c.namespace)}, "")
215+
}
216+
217+
func runCommand(name string, args []string, dir string) error {
218+
cmd := exec.Command(name, args...)
219+
if dir != "" {
220+
cmd.Dir = dir
221+
}
222+
223+
output, err := cmd.CombinedOutput()
224+
if err != nil {
225+
return fmt.Errorf("command '%s %s' failed: %v\nOutput: %s",
226+
name, strings.Join(args, " "), err, string(output))
227+
}
228+
return nil
229+
}

0 commit comments

Comments
 (0)