github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/integration/integration_nexus_test.go (about) 1 //go:build integration 2 // +build integration 3 4 // can be executed with 5 // go test -v -tags integration -run TestNexusIntegration ./integration/... 6 7 package main 8 9 import ( 10 "context" 11 "fmt" 12 "net/http" 13 "path" 14 "strings" 15 "testing" 16 "time" 17 18 "github.com/SAP/jenkins-library/pkg/command" 19 "github.com/stretchr/testify/assert" 20 "github.com/testcontainers/testcontainers-go" 21 "github.com/testcontainers/testcontainers-go/wait" 22 ) 23 24 func assertFileCanBeDownloaded(t *testing.T, container IntegrationTestDockerExecRunner, url string) { 25 err := container.runScriptInsideContainer("curl -O " + url) 26 if err != nil { 27 t.Fatalf("Attempting to download file %s failed: %s", url, err) 28 } 29 container.assertHasFiles(t, "/project/"+path.Base(url)) 30 } 31 32 func TestNexusIntegrationV3UploadMta(t *testing.T) { 33 t.Parallel() 34 container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{ 35 Image: "sonatype/nexus3:3.25.1", 36 User: "nexus", 37 TestDir: []string{"testdata", "TestNexusIntegration", "mta"}, 38 Environment: map[string]string{"NEXUS_SECURITY_RANDOMPASSWORD": "false"}, 39 Setup: []string{ 40 "/opt/sonatype/start-nexus-repository-manager.sh &", 41 "curl https://ftp.fau.de/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz | tar xz -C /tmp", 42 "echo PATH=/tmp/apache-maven-3.6.3/bin:$PATH >> ~/.profile", 43 "until curl --fail --silent http://localhost:8081/service/rest/v1/status; do sleep 5; done", 44 }, 45 }) 46 defer container.terminate(t) 47 48 err := container.whenRunningPiperCommand("nexusUpload", "--groupId=mygroup", "--artifactId=mymta", 49 "--username=admin", "--password=admin123", "--mavenRepository=maven-releases", "--url=http://localhost:8081") 50 if err != nil { 51 t.Fatalf("Piper command failed %s", err) 52 } 53 54 container.assertHasOutput(t, "BUILD SUCCESS") 55 assertFileCanBeDownloaded(t, container, "http://localhost:8081/repository/maven-releases/mygroup/mymta/0.3.0/mymta-0.3.0.mtar") 56 assertFileCanBeDownloaded(t, container, "http://localhost:8081/repository/maven-releases/mygroup/mymta/0.3.0/mymta-0.3.0.yaml") 57 } 58 59 func TestNexusIntegrationV3UploadMaven(t *testing.T) { 60 t.Parallel() 61 container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{ 62 Image: "sonatype/nexus3:3.25.1", 63 User: "nexus", 64 TestDir: []string{"testdata", "TestNexusIntegration", "maven"}, 65 Environment: map[string]string{"NEXUS_SECURITY_RANDOMPASSWORD": "false"}, 66 Setup: []string{ 67 "/opt/sonatype/start-nexus-repository-manager.sh &", 68 "curl https://ftp.fau.de/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz | tar xz -C /tmp", 69 "echo PATH=/tmp/apache-maven-3.6.3/bin:$PATH >> ~/.profile", 70 "until curl --fail --silent http://localhost:8081/service/rest/v1/status; do sleep 5; done", 71 }, 72 }) 73 defer container.terminate(t) 74 75 err := container.whenRunningPiperCommand("nexusUpload", "--username=admin", "--password=admin123", 76 "--mavenRepository=maven-releases", "--url=http://localhost:8081") 77 if err != nil { 78 t.Fatalf("Piper command failed %s", err) 79 } 80 81 container.assertHasOutput(t, "BUILD SUCCESS") 82 assertFileCanBeDownloaded(t, container, "http://localhost:8081/repository/maven-releases/com/mycompany/app/my-app/1.0/my-app-1.0.pom") 83 assertFileCanBeDownloaded(t, container, "http://localhost:8081/repository/maven-releases/com/mycompany/app/my-app/1.0/my-app-1.0.jar") 84 } 85 86 func TestNexusIntegrationV3UploadNpm(t *testing.T) { 87 t.Parallel() 88 container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{ 89 Image: "sonatype/nexus3:3.25.1", 90 User: "nexus", 91 TestDir: []string{"testdata", "TestNexusIntegration", "npm"}, 92 Environment: map[string]string{"NEXUS_SECURITY_RANDOMPASSWORD": "false"}, 93 Setup: []string{ 94 "/opt/sonatype/start-nexus-repository-manager.sh &", 95 "curl https://nodejs.org/dist/v12.18.3/node-v12.18.3-linux-x64.tar.gz | tar xz -C /tmp", 96 "echo PATH=/tmp/node-v12.18.3-linux-x64/bin:$PATH >> ~/.profile", 97 "until curl --fail --silent http://localhost:8081/service/rest/v1/status; do sleep 5; done", 98 // Create npm repo because nexus does not bring one by default 99 "curl -u admin:admin123 -d '{\"name\": \"npm-repo\", \"online\": true, \"storage\": {\"blobStoreName\": \"default\", \"strictContentTypeValidation\": true, \"writePolicy\": \"ALLOW_ONCE\"}}' --header \"Content-Type: application/json\" -X POST http://localhost:8081/service/rest/beta/repositories/npm/hosted", 100 }, 101 }) 102 defer container.terminate(t) 103 104 err := container.whenRunningPiperCommand("nexusUpload", "--username=admin", "--password=admin123", 105 "--npmRepository=npm-repo", "--url=http://localhost:8081") 106 if err != nil { 107 t.Fatalf("Piper command failed %s", err) 108 } 109 110 container.assertHasOutput(t, "npm notice total files: 1") 111 assertFileCanBeDownloaded(t, container, "http://localhost:8081/repository/npm-repo/npm-nexus-upload-test/-/npm-nexus-upload-test-1.0.0.tgz") 112 } 113 114 func TestNexusIntegrationV2Upload(t *testing.T) { 115 ctx := context.Background() 116 req := testcontainers.ContainerRequest{ 117 Image: "sonatype/nexus:2.14.18-01", 118 ExposedPorts: []string{"8081/tcp"}, 119 WaitingFor: wait.ForLog("org.sonatype.nexus.bootstrap.jetty.JettyServer - Running").WithStartupTimeout(5 * time.Minute), // Nexus takes more than one minute to boot 120 } 121 nexusContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ 122 ContainerRequest: req, 123 Started: true, 124 }) 125 assert.NoError(t, err) 126 defer nexusContainer.Terminate(ctx) 127 ip, err := nexusContainer.Host(ctx) 128 assert.NoError(t, err) 129 port, err := nexusContainer.MappedPort(ctx, "8081") 130 assert.NoError(t, err, "Could not map port for nexus container") 131 nexusIpAndPort := fmt.Sprintf("%s:%s", ip, port.Port()) 132 url := "http://" + nexusIpAndPort + "/nexus/" 133 134 cmd := command.Command{} 135 cmd.SetDir("testdata/TestNexusIntegration/mta") 136 137 piperOptions := []string{ 138 "nexusUpload", 139 "--groupId=mygroup", 140 "--artifactId=mymta", 141 "--username=admin", 142 "--password=admin123", 143 "--mavenRepository=releases", 144 "--version=nexus2", 145 "--url=" + nexusIpAndPort + "/nexus/", 146 } 147 148 err = cmd.RunExecutable(getPiperExecutable(), piperOptions...) 149 assert.NoError(t, err, "Calling piper with arguments %v failed.", piperOptions) 150 151 cmd = command.Command{} 152 cmd.SetDir("testdata/TestNexusIntegration/maven") 153 154 piperOptions = []string{ 155 "nexusUpload", 156 "--username=admin", 157 "--password=admin123", 158 "--mavenRepository=releases", 159 "--version=nexus2", 160 "--url=" + nexusIpAndPort + "/nexus/", 161 } 162 163 err = cmd.RunExecutable(getPiperExecutable(), piperOptions...) 164 assert.NoError(t, err, "Calling piper with arguments %v failed.", piperOptions) 165 166 cmd = command.Command{} 167 cmd.SetDir("testdata/TestNexusIntegration/npm") 168 169 piperOptions = []string{ 170 "nexusUpload", 171 "--username=admin", 172 "--password=admin123", 173 "--npmRepository=npm-repo", 174 "--version=nexus2", 175 "--url=" + nexusIpAndPort + "/nexus/", 176 } 177 178 // Create npm repo for this test because nexus does not create one by default 179 payload := strings.NewReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<repository><data><name>npm-repo</name><repoPolicy>RELEASE</repoPolicy><repoType>hosted</repoType><id>npm-repo</id><exposed>true</exposed><provider>npm-hosted</provider><providerRole>org.sonatype.nexus.proxy.repository.Repository</providerRole><format>npm</format></data></repository>") 180 request, _ := http.NewRequest("POST", url+"service/local/repositories", payload) 181 request.Header.Add("Content-Type", "application/xml") 182 request.Header.Add("Authorization", "Basic YWRtaW46YWRtaW4xMjM=") 183 response, err := http.DefaultClient.Do(request) 184 assert.NoError(t, err) 185 fmt.Println(response) 186 assert.Equal(t, 201, response.StatusCode) 187 188 err = cmd.RunExecutable(getPiperExecutable(), piperOptions...) 189 assert.NoError(t, err, "Calling piper with arguments %v failed.", piperOptions) 190 191 resp, err := http.Get(url + "content/repositories/releases/com/mycompany/app/my-app-parent/1.0/my-app-parent-1.0.pom") 192 assert.NoError(t, err, "Downloading artifact failed") 193 assert.Equal(t, http.StatusOK, resp.StatusCode, "Get my-app-parent-1.0.pom: %s", resp.Status) 194 195 resp, err = http.Get(url + "content/repositories/releases/com/mycompany/app/my-app/1.0/my-app-1.0.pom") 196 assert.NoError(t, err, "Downloading artifact failed") 197 assert.Equal(t, http.StatusOK, resp.StatusCode, "Get my-app-1.0.pom: %s", resp.Status) 198 199 resp, err = http.Get(url + "content/repositories/releases/com/mycompany/app/my-app/1.0/my-app-1.0.jar") 200 assert.NoError(t, err, "Downloading artifact failed") 201 assert.Equal(t, http.StatusOK, resp.StatusCode, "Get my-app-1.0.jar: %s", resp.Status) 202 203 resp, err = http.Get(url + "content/repositories/releases/mygroup/mymta/0.3.0/mymta-0.3.0.yaml") 204 assert.NoError(t, err, "Downloading artifact failed") 205 assert.Equal(t, http.StatusOK, resp.StatusCode, "Get mymta-0.3.0.yaml: %s", resp.Status) 206 207 resp, err = http.Get(url + "content/repositories/releases/mygroup/mymta/0.3.0/mymta-0.3.0.mtar") 208 assert.NoError(t, err, "Downloading artifact failed") 209 assert.Equal(t, http.StatusOK, resp.StatusCode, "Get mymta-0.3.0.mtar: %s", resp.Status) 210 211 resp, err = http.Get(url + "content/repositories/npm-repo/npm-nexus-upload-test/-/npm-nexus-upload-test-1.0.0.tgz") 212 assert.NoError(t, err, "Downloading artifact failed") 213 assert.Equal(t, http.StatusOK, resp.StatusCode, "Get npm-nexus-upload-test-1.0.0.tgz: %s", resp.Status) 214 }