Skip to content

Commit fd6ee36

Browse files
committed
Add operator-sdk runner
Signed-off-by: Jakub Stejskal <xstejs24@gmail.com>
1 parent 554c4bf commit fd6ee36

File tree

5 files changed

+364
-0
lines changed

5 files changed

+364
-0
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@
121121
<artifactId>test-frame-kubernetes</artifactId>
122122
<version>${project.version}</version>
123123
</dependency>
124+
<dependency>
125+
<groupId>io.skodjob</groupId>
126+
<artifactId>test-frame-openshift</artifactId>
127+
<version>${project.version}</version>
128+
</dependency>
124129
<dependency>
125130
<groupId>io.skodjob</groupId>
126131
<artifactId>test-frame-log-collector</artifactId>

test-frame-openshift/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@
8686
<groupId>io.fabric8</groupId>
8787
<artifactId>kubernetes-client-api</artifactId>
8888
</dependency>
89+
<dependency>
90+
<groupId>org.slf4j</groupId>
91+
<artifactId>slf4j-api</artifactId>
92+
</dependency>
93+
<dependency>
94+
<groupId>org.apache.logging.log4j</groupId>
95+
<artifactId>log4j-slf4j2-impl</artifactId>
96+
<scope>test</scope>
97+
</dependency>
98+
<dependency>
99+
<groupId>org.junit.jupiter</groupId>
100+
<artifactId>junit-jupiter-api</artifactId>
101+
<scope>test</scope>
102+
</dependency>
89103
</dependencies>
90104
<build>
91105
<pluginManagement>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright Skodjob authors.
3+
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
4+
*/
5+
package io.skodjob.testframe.olm;
6+
7+
import io.skodjob.testframe.executor.Exec;
8+
import io.skodjob.testframe.executor.ExecResult;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
import java.security.InvalidParameterException;
13+
import java.util.ArrayList;
14+
import java.util.Arrays;
15+
import java.util.List;
16+
17+
/**
18+
* Runner for operator-sdk run command
19+
*/
20+
public class OperatorSdkRun {
21+
private static final Logger LOGGER = LoggerFactory.getLogger(OperatorSdkRun.class);
22+
23+
protected String namespace;
24+
protected String timeout;
25+
protected String installMode;
26+
protected String indexImage;
27+
protected String kubeconfig;
28+
protected String bundleImage;
29+
30+
private static final String OPTION_INDEX_IMAGE = "--index-image";
31+
private static final String OPTION_NAMESPACE = "--namespace";
32+
private static final String OPTION_KUBECONFIG = "--kubeconfig";
33+
private static final String OPTION_TIMEOUT = "--timeout";
34+
private static final String OPTION_INSTALL_MODE = "--install-mode";
35+
private static final String CMD = "operator-sdk";
36+
private static final String RUN = "run";
37+
private static final String BUNDLE = "bundle";
38+
39+
/**
40+
* Constructor of the runner
41+
*
42+
* @param operatorSdkRunBuilder operator-sdk run command configuration
43+
*/
44+
public OperatorSdkRun(OperatorSdkRunBuilder operatorSdkRunBuilder) {
45+
if (operatorSdkRunBuilder.getNamespace() == null) {
46+
throw new InvalidParameterException("Namespace is a mandatory parameter for OperatorSdkRun!");
47+
}
48+
49+
if (operatorSdkRunBuilder.getBundleImage() == null) {
50+
throw new InvalidParameterException("BundleImage is a mandatory parameter for OperatorSdkRun!");
51+
}
52+
53+
if (operatorSdkRunBuilder.getInstallMode() == null) {
54+
throw new InvalidParameterException("InstallMode is a mandatory parameter for OperatorSdkRun!");
55+
}
56+
57+
this.namespace = operatorSdkRunBuilder.getNamespace();
58+
this.bundleImage = operatorSdkRunBuilder.getBundleImage();
59+
this.installMode = operatorSdkRunBuilder.getInstallMode();
60+
this.timeout = operatorSdkRunBuilder.getTimeout();
61+
this.indexImage = operatorSdkRunBuilder.getIndexImage();
62+
this.kubeconfig = operatorSdkRunBuilder.getKubeconfig();
63+
}
64+
65+
/**
66+
* Run the built command via Executor and return it result
67+
*
68+
* @return ExecResult with data from the executor
69+
*/
70+
public ExecResult run() {
71+
List<String> command = new ArrayList<>(Arrays.asList(CMD, RUN, BUNDLE, bundleImage));
72+
73+
command.add(OPTION_NAMESPACE);
74+
command.add(namespace);
75+
command.add(OPTION_INSTALL_MODE);
76+
command.add(installMode);
77+
78+
if (indexImage != null) {
79+
command.add(OPTION_INDEX_IMAGE);
80+
command.add(indexImage);
81+
}
82+
83+
if (timeout != null) {
84+
command.add(OPTION_TIMEOUT);
85+
command.add(String.valueOf(timeout));
86+
}
87+
88+
if (kubeconfig != null) {
89+
command.add(OPTION_KUBECONFIG);
90+
command.add(kubeconfig);
91+
}
92+
93+
return Exec.exec(command);
94+
}
95+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* Copyright Skodjob authors.
3+
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
4+
*/
5+
package io.skodjob.testframe.olm;
6+
7+
/**
8+
* Builder class for the {@link OperatorSdkRun}
9+
*/
10+
public class OperatorSdkRunBuilder {
11+
12+
private String namespace;
13+
private String timeout;
14+
private String installMode;
15+
private String indexImage;
16+
private String kubeconfig;
17+
private String bundleImage;
18+
19+
20+
/**
21+
* Constructor for creating {@link OperatorSdkRunBuilder} with parameters from
22+
* current instance of {@link OperatorSdkRun}.
23+
*
24+
* @param operatorSdkRun current instance of {@link OperatorSdkRun}
25+
*/
26+
public OperatorSdkRunBuilder(OperatorSdkRun operatorSdkRun) {
27+
this.namespace = operatorSdkRun.namespace;
28+
this.timeout = operatorSdkRun.timeout;
29+
this.installMode = operatorSdkRun.installMode;
30+
this.indexImage = operatorSdkRun.indexImage;
31+
this.kubeconfig = operatorSdkRun.kubeconfig;
32+
this.bundleImage = operatorSdkRun.bundleImage;
33+
}
34+
35+
/**
36+
* Empty constructor, we can use the "with" methods to build the LogCollector's configuration
37+
*/
38+
public OperatorSdkRunBuilder() {
39+
// empty constructor
40+
}
41+
42+
/**
43+
* Method for setting the namespace, where the bundle container will be deployed
44+
*
45+
* @param namespace where the bundle container will be deployed
46+
* @return {@link OperatorSdkRunBuilder} object
47+
*/
48+
public OperatorSdkRunBuilder withNamespace(String namespace) {
49+
this.namespace = namespace;
50+
return this;
51+
}
52+
53+
/**
54+
* Method for setting the timeout - string dictating the maximum time that run can run.
55+
* The command will return an error if the timeout is exceeded.
56+
*
57+
* @param timeout string dictating the maximum time that run
58+
* @return {@link OperatorSdkRunBuilder} object
59+
*/
60+
public OperatorSdkRunBuilder withTimeout(String timeout) {
61+
this.timeout = timeout;
62+
return this;
63+
}
64+
65+
/**
66+
* Method for setting the index-image for run command
67+
*
68+
* @param indexImage specifies an index image in which to inject the given bundle
69+
* @return {@link OperatorSdkRunBuilder} object
70+
*/
71+
public OperatorSdkRunBuilder withIndexImage(String indexImage) {
72+
this.indexImage = indexImage;
73+
return this;
74+
}
75+
76+
/**
77+
* Method for setting the install-mode for run command
78+
*
79+
* @param installMode (AllNamespace|OwnNamespace|SingleNamespace=)
80+
* @return {@link OperatorSdkRunBuilder} object
81+
*/
82+
public OperatorSdkRunBuilder withInstallMode(String installMode) {
83+
this.installMode = installMode;
84+
return this;
85+
}
86+
87+
/**
88+
* Method for setting the kubeconfig for run command
89+
*
90+
* @param kubeconfig the local path to a kubeconfig
91+
* @return {@link OperatorSdkRunBuilder} object
92+
*/
93+
public OperatorSdkRunBuilder withKubeConfig(String kubeconfig) {
94+
this.kubeconfig = kubeconfig;
95+
return this;
96+
}
97+
98+
/**
99+
* Method for setting the bundle-image for run command
100+
*
101+
* @param bundleImage specifies the Operator bundle image
102+
* @return {@link OperatorSdkRunBuilder} object
103+
*/
104+
public OperatorSdkRunBuilder withBundleImage(String bundleImage) {
105+
this.bundleImage = bundleImage;
106+
return this;
107+
}
108+
109+
/**
110+
* Get namespace
111+
*
112+
* @return namespace
113+
*/
114+
public String getNamespace() {
115+
return namespace;
116+
}
117+
118+
/**
119+
* Get timeout
120+
*
121+
* @return timeout
122+
*/
123+
public String getTimeout() {
124+
return timeout;
125+
}
126+
127+
/**
128+
* Get installMode
129+
*
130+
* @return installMode
131+
*/
132+
public String getInstallMode() {
133+
return installMode;
134+
}
135+
136+
/**
137+
* Get indexImage
138+
*
139+
* @return indexImage
140+
*/
141+
public String getIndexImage() {
142+
return indexImage;
143+
}
144+
145+
/**
146+
* Get kubeconfig
147+
*
148+
* @return kubeconfig
149+
*/
150+
public String getKubeconfig() {
151+
return kubeconfig;
152+
}
153+
154+
/**
155+
* Get bundleImage
156+
*
157+
* @return bundleImage
158+
*/
159+
public String getBundleImage() {
160+
return bundleImage;
161+
}
162+
163+
/**
164+
* Method for building the {@link OperatorSdkRun} object
165+
*
166+
* @return {@link OperatorSdkRun} configured by the specified parameters
167+
*/
168+
public OperatorSdkRun build() {
169+
return new OperatorSdkRun(this);
170+
}
171+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright Skodjob authors.
3+
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
4+
*/
5+
package io.skodjob.testframe.olm;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.security.InvalidParameterException;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertNotNull;
13+
import static org.junit.jupiter.api.Assertions.assertThrows;
14+
15+
public class OperatorSdkRunBuilderTest {
16+
17+
@Test
18+
void testBuilder() {
19+
OperatorSdkRun operatorSdkRun = new OperatorSdkRunBuilder()
20+
.withTimeout("2m")
21+
.withNamespace("namespace-1")
22+
.withBundleImage("my-bundle-image")
23+
.withIndexImage("my-index-image")
24+
.withKubeConfig("path-to-kubeconfig")
25+
.withInstallMode("automatic")
26+
.build();
27+
28+
assertNotNull(operatorSdkRun);
29+
30+
assertEquals(operatorSdkRun.bundleImage, "my-bundle-image");
31+
assertEquals(operatorSdkRun.timeout, "2m");
32+
assertEquals(operatorSdkRun.indexImage, "my-index-image");
33+
assertEquals(operatorSdkRun.namespace, "namespace-1");
34+
assertEquals(operatorSdkRun.installMode, "automatic");
35+
assertEquals(operatorSdkRun.kubeconfig, "path-to-kubeconfig");
36+
}
37+
38+
@Test
39+
void testMissingNamespace() {
40+
OperatorSdkRunBuilder operatorSdkRunBuilder = new OperatorSdkRunBuilder()
41+
.withTimeout("2m")
42+
.withBundleImage("my-bundle-image")
43+
.withIndexImage("my-index-image")
44+
.withKubeConfig("path-to-kubeconfig")
45+
.withInstallMode("automatic");
46+
47+
InvalidParameterException thrown = assertThrows(InvalidParameterException.class, operatorSdkRunBuilder::build);
48+
49+
assertEquals("Namespace is a mandatory parameter for OperatorSdkRun!", thrown.getMessage());
50+
}
51+
52+
@Test
53+
void testMissingBundleImage() {
54+
OperatorSdkRunBuilder operatorSdkRunBuilder = new OperatorSdkRunBuilder()
55+
.withTimeout("2m")
56+
.withNamespace("namespace-1")
57+
.withIndexImage("my-index-image")
58+
.withKubeConfig("path-to-kubeconfig")
59+
.withInstallMode("automatic");
60+
61+
InvalidParameterException thrown = assertThrows(InvalidParameterException.class, operatorSdkRunBuilder::build);
62+
63+
assertEquals("BundleImage is a mandatory parameter for OperatorSdkRun!", thrown.getMessage());
64+
}
65+
66+
@Test
67+
void testWrongInstallMode() {
68+
OperatorSdkRunBuilder operatorSdkRunBuilder = new OperatorSdkRunBuilder()
69+
.withTimeout("2m")
70+
.withNamespace("namespace-1")
71+
.withIndexImage("my-index-image")
72+
.withBundleImage("my-bundle-image")
73+
.withKubeConfig("path-to-kubeconfig");
74+
75+
InvalidParameterException thrown = assertThrows(InvalidParameterException.class, operatorSdkRunBuilder::build);
76+
77+
assertEquals("InstallMode is a mandatory parameter for OperatorSdkRun!", thrown.getMessage());
78+
}
79+
}

0 commit comments

Comments
 (0)