forked from maxlaverse/kubernetes-credentials-plugin
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8a6baa
commit 2d3b1d4
Showing
4 changed files
with
89 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/test/java/org/jenkinsci/plugins/kubernetes/credentials/KubectlTestBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.jenkinsci.plugins.kubernetes.credentials; | ||
|
||
import com.cloudbees.plugins.credentials.CredentialsScope; | ||
import com.cloudbees.plugins.credentials.impl.BaseStandardCredentials; | ||
import com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl; | ||
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl; | ||
import hudson.Launcher; | ||
import hudson.Proc; | ||
import hudson.model.Slave; | ||
import hudson.util.Secret; | ||
import org.apache.commons.compress.utils.IOUtils; | ||
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl; | ||
import org.jvnet.hudson.test.FakeLauncher; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.PretendSlave; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
|
||
/** | ||
* @author Max Laverse | ||
*/ | ||
public class KubectlTestBase { | ||
protected static final String CREDENTIAL_ID = "cred1234"; | ||
protected static final String PASSPHRASE = "test"; | ||
protected static final String USERNAME = "bob"; | ||
protected static final String PASSWORD = "s3cr3t"; | ||
protected static final String CA_CERTIFICATE = "-----BEGIN CERTIFICATE-----\na-certificate\n-----END CERTIFICATE-----"; | ||
protected static final String SERVER_URL = "https://localhost:6443"; | ||
protected static final String KUBECTL_BINARY = "kubectl"; | ||
|
||
protected String loadResource(String name) { | ||
try { | ||
return new String(IOUtils.toByteArray(getClass().getResourceAsStream(name))); | ||
} catch (Throwable t) { | ||
throw new RuntimeException("Could not read resource:[" + name + "]."); | ||
} | ||
} | ||
|
||
protected String getResourceFile(String name) { | ||
return getClass().getResource(name).getFile(); | ||
} | ||
|
||
protected Slave getFakeSlave(JenkinsRule r) throws Exception { | ||
PretendSlave slave = r.createPretendSlave(p -> { | ||
if (p.cmds().get(0).equals(KUBECTL_BINARY)) { | ||
PrintStream ps = new PrintStream(p.stdout()); | ||
ps.println("Call stubed for: " + String.join(" ", p.cmds())); | ||
ps.close(); | ||
return new FakeLauncher.FinishedProc(0); | ||
} | ||
return r.createLocalLauncher().launch(p); | ||
}); | ||
slave.setLabelString("mocked-kubectl"); | ||
return slave; | ||
} | ||
|
||
protected BaseStandardCredentials secretCredential() { | ||
return new StringCredentialsImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", Secret.fromString(PASSWORD)); | ||
} | ||
|
||
protected BaseStandardCredentials certificateCredential() { | ||
String storeFile = getResourceFile("/org/jenkinsci/plugins/kubernetes/credentials/kubernetes.pkcs12"); | ||
CertificateCredentialsImpl.KeyStoreSource keyStoreSource = new CertificateCredentialsImpl.FileOnMasterKeyStoreSource(storeFile); | ||
return new CertificateCredentialsImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", PASSPHRASE, keyStoreSource); | ||
} | ||
|
||
protected BaseStandardCredentials usernamePasswordCredential() { | ||
return new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", USERNAME, PASSWORD); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters