Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a simple (manual) test #182

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Amazon Credentials into a Docker CLI Authentication Token.
Amazon ECR plugin implements a Docker Token producer to convert Amazon
credentials to Jenkins’ API used by (mostly) all Docker-related plugins.

Thank's to this producer, you can select your existing registered Amazon
credentials for various Docker operations in Jenkins, for sample using the
Thanks to this producer, you can select your existing registered Amazon
credentials for various Docker operations in Jenkins, for example using the
Docker Build and Publish plugin:

![](.github/build-and-publish.png)
Expand Down Expand Up @@ -74,6 +74,23 @@ node {

## Development

### Testing

Unfortunately, testing against AWS isn't very straightforward, since you always
need an AWS account with correct setup, which might incur some costs. Current
tests try to make this as easy as possible. You need a user with read
permission to ECR (AWS IAM policy `AmazonEC2ContainerRegistryReadOnly` should
suffice) and an (empty) container registry. The test expect these details in
the following environment variables:

```shell
export AWS_ACCESS_KEY_ID=<your-key-id-here>
export AWS_SECRET_ACCESS_KEY=<your-secret-access-key-here>
export AWS_REGISTRY_HOST=<some-number>.dkr.ecr.us-east-1.amazonaws.com
```

When those are set correctly, `mvn test` should run those tests successfully.

### Code Style

This plugin uses [Google Java Code Style], which is enforced by the [spotless]
Expand Down
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@
<groupId>org.jenkins-ci.plugins.aws-java-sdk</groupId>
<artifactId>aws-java-sdk-ecr</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>docker-workflow</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.cloudbees.jenkins.plugins.amazonecr;

import com.cloudbees.jenkins.plugins.awscredentials.AWSCredentialsImpl;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

@WithJenkins
class AmazonECSRegistryCredentialPipelineAccessTest {
@Test
@EnabledIfEnvironmentVariable(named = "AWS_ACCESS_KEY_ID", matches = ".{10,}")
void pipelineCanLoginWithCredential(JenkinsRule r) throws Exception {
SystemCredentialsProvider.getInstance()
.getCredentials()
.add(new AWSCredentialsImpl(
CredentialsScope.GLOBAL,
"test",
System.getenv("AWS_ACCESS_KEY_ID"),
System.getenv("AWS_SECRET_ACCESS_KEY"),
"test"));

String script =
"docker.withRegistry('https://" + System.getenv("AWS_REGISTRY_HOST") + "', 'ecr:us-east-1:test') {}";

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testJob");
p.setDefinition(new CpsFlowDefinition(script, true));

r.assertBuildStatusSuccess(p.scheduleBuild2(0));
}
}
Loading