Skip to content

Commit f21c30d

Browse files
committed
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.
1 parent 4ba8f18 commit f21c30d

File tree

3 files changed

+89
-16
lines changed

3 files changed

+89
-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: 85 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,85 @@ 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{"install", "ASSUME_DEFAULT=true"},
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+
return runCommand("make", c.installArgs, cloneDir)
121+
}
122+
123+
func (c *CLISetup) verifyInstallation() error {
124+
return runCommand("kubectl", []string{"oadp", "version"}, "")
125+
}
126+
127+
func (c *CLISetup) configureNamespace() error {
128+
return runCommand("kubectl", []string{"oadp", "client", "config", "set", fmt.Sprintf("namespace=%s", c.namespace)}, "")
129+
}
130+
131+
func runCommand(name string, args []string, dir string) error {
132+
cmd := exec.Command(name, args...)
133+
if dir != "" {
134+
cmd.Dir = dir
135+
}
136+
137+
output, err := cmd.CombinedOutput()
138+
if err != nil {
139+
return fmt.Errorf("command '%s %s' failed: %v\nOutput: %s",
140+
name, strings.Join(args, " "), err, string(output))
141+
}
142+
return nil
143+
}

0 commit comments

Comments
 (0)