Skip to content

Commit

Permalink
[JENKINS-54313] add extra unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jtnord committed Jan 29, 2019
1 parent e5255e1 commit 5f445a4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.client.utils.Serialization;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;

import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.CredentialsConvertionException;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.convertors.UsernamePasswordCredentialsConvertor;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.emptyString;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -84,6 +88,24 @@ public void canConvertAValidMappedSecret() throws Exception {
}
}

@Issue("JENKINS-54313")
@Test
public void canConvertAValidSecretWithNoDescription() throws Exception {
UsernamePasswordCredentialsConvertor convertor = new UsernamePasswordCredentialsConvertor();

try (InputStream is = get("valid-no-desc.yaml")) {
Secret secret = Serialization.unmarshal(is, Secret.class);
assertThat("The Secret was loaded correctly from disk", notNullValue());
UsernamePasswordCredentialsImpl credential = convertor.convert(secret);
assertThat(credential, notNullValue());
assertThat("credential id is mapped correctly", credential.getId(), is("a-test-usernamepass"));
assertThat("credential description is mapped correctly", credential.getDescription(), is(emptyString()));
assertThat("credential scope is mapped correctly", credential.getScope(), is(CredentialsScope.GLOBAL));
assertThat("credential username is mapped correctly", credential.getUsername(), is("myUsername"));
assertThat("credential password is mapped correctly", credential.getPassword().getPlainText(), is("Pa$$word"));
}
}


@Test
public void failsToConvertWhenUsernameMissing() throws Exception {
Expand Down Expand Up @@ -153,6 +175,7 @@ public void failsToConvertWhenDataEmpty() throws Exception {
}
}


private static final InputStream get(String resource) {
InputStream is = UsernamePasswordCredentialsConvertorTest.class.getResourceAsStream("UsernamePasswordCredentialsConvertorTest/" + resource);
if (is == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Secret
metadata:
# this is the jenkins id.
name: "a-test-usernamepass"
labels:
# so we know what type it is.
"jenkins.io/credentials-type": "usernamePassword"
type: Opaque
data:
# UTF-8 base64 encoded
username: bXlVc2VybmFtZQ== #myUsername
password: UGEkJHdvcmQ= #Pa$$word

0 comments on commit 5f445a4

Please sign in to comment.