|
| 1 | +/** |
| 2 | + * Copyright (C) 2011-2020 Red Hat, Inc. (https://github.com/Commonjava/indy) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.commonjava.indy.pkg.npm.content; |
| 17 | + |
| 18 | +import org.apache.commons.io.IOUtils; |
| 19 | +import org.commonjava.indy.client.core.IndyClientException; |
| 20 | +import org.commonjava.indy.ftest.core.AbstractContentManagementTest; |
| 21 | +import org.commonjava.indy.ftest.core.category.TimingDependent; |
| 22 | +import org.commonjava.indy.model.core.RemoteRepository; |
| 23 | +import org.commonjava.indy.model.core.io.IndyObjectMapper; |
| 24 | +import org.commonjava.indy.pkg.npm.model.DistTag; |
| 25 | +import org.commonjava.indy.pkg.npm.model.PackageMetadata; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.experimental.categories.Category; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.InputStream; |
| 31 | + |
| 32 | +import static org.commonjava.indy.pkg.npm.model.NPMPackageTypeDescriptor.NPM_PKG_KEY; |
| 33 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 34 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 35 | +import static org.junit.Assert.assertThat; |
| 36 | + |
| 37 | +/** |
| 38 | + * This case tests if remote package.json will time out by using ONLY storage level timeout. |
| 39 | + * We need to disable scheduler in order to test it. |
| 40 | + * Given: <br /> |
| 41 | + * <ul> |
| 42 | + * <li>remote repo A contains package.json for a given package</li> |
| 43 | + * <li>remote repo A metadata timeout is 1s</li> |
| 44 | + * </ul> |
| 45 | + * When: <br /> |
| 46 | + * <ul> |
| 47 | + * <li>the first time package.json is retrieved via remote repo A by client</li> |
| 48 | + * <li>expect the remote server with new version in package.json</li> |
| 49 | + * <li>wait for 3s for the cached file to timeout</li> |
| 50 | + * <li>the second time package.json is retrieved via remote repo A</li> |
| 51 | + * </ul> |
| 52 | + * Then: <br /> |
| 53 | + * <ul> |
| 54 | + * <li>the version 1 of package.json expires in remote repository A, the version 2 is retrieved successfully</li> |
| 55 | + * </ul> |
| 56 | + */ |
| 57 | +public class NPMMetadataTimeoutNoSchedulerTest |
| 58 | + extends AbstractContentManagementTest |
| 59 | +{ |
| 60 | + |
| 61 | + private static final String REPO = "A"; |
| 62 | + |
| 63 | + private static final String PATH_JQUERY = "jquery"; |
| 64 | + |
| 65 | + private static final String PATH_BABEL_PARSER = "@babel/parser"; |
| 66 | + |
| 67 | + // !!!IMPORTANT: disable scheduler in order to test storage level timeout |
| 68 | + @Override |
| 69 | + protected boolean isSchedulerEnabled() |
| 70 | + { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + @Category( TimingDependent.class ) |
| 76 | + public void test() |
| 77 | + throws Exception |
| 78 | + { |
| 79 | + IndyObjectMapper mapper = new IndyObjectMapper( true ); |
| 80 | + |
| 81 | + final PackageMetadata src = new PackageMetadata(); |
| 82 | + final DistTag dts = new DistTag(); |
| 83 | + dts.setBeta( "1" ); |
| 84 | + src.setDistTags( dts ); |
| 85 | + |
| 86 | + server.expect( "GET", server.formatUrl( REPO, PATH_JQUERY ), ( req, resp ) -> { |
| 87 | + resp.setStatus( 200 ); |
| 88 | + mapper.writeValue( resp.getWriter(), src ); |
| 89 | + resp.getWriter().flush(); |
| 90 | + } ); |
| 91 | + |
| 92 | + server.expect( "GET", server.formatUrl( REPO, PATH_BABEL_PARSER ), ( req, resp ) -> { |
| 93 | + resp.setStatus( 200 ); |
| 94 | + mapper.writeValue( resp.getWriter(), src ); |
| 95 | + resp.getWriter().flush(); |
| 96 | + } ); |
| 97 | + |
| 98 | + final RemoteRepository repo = new RemoteRepository( NPM_PKG_KEY, REPO, server.formatUrl( REPO ) ); |
| 99 | + repo.setMetadataTimeoutSeconds( 1 ); |
| 100 | + |
| 101 | + client.stores().create( repo, "adding npm remote repo", RemoteRepository.class ); |
| 102 | + |
| 103 | + // First retrieval |
| 104 | + verifyMetadataBetaTag( "1", PATH_JQUERY, repo ); |
| 105 | + assertThat( "Metadata not retrieved!", client.content().exists( repo.getKey(), PATH_JQUERY, true ), |
| 106 | + equalTo( true ) ); |
| 107 | + assertThat( "Metadata not retrieved!", |
| 108 | + client.content().exists( repo.getKey(), PATH_JQUERY + "/package.json", true ), equalTo( true ) ); |
| 109 | + |
| 110 | + verifyMetadataBetaTag( "1", PATH_BABEL_PARSER, repo ); |
| 111 | + assertThat( "Metadata not retrieved!", client.content().exists( repo.getKey(), PATH_BABEL_PARSER, true ), |
| 112 | + equalTo( true ) ); |
| 113 | + assertThat( "Metadata not retrieved!", |
| 114 | + client.content().exists( repo.getKey(), PATH_BABEL_PARSER + "/package.json", true ), |
| 115 | + equalTo( true ) ); |
| 116 | + |
| 117 | + // wait for repo metadata timeout |
| 118 | + Thread.sleep( 3000 ); |
| 119 | + |
| 120 | + assertThat( "Metadata not cleaned up!", client.content().exists( repo.getKey(), PATH_JQUERY, true ), |
| 121 | + equalTo( false ) ); |
| 122 | + assertThat( "Metadata not cleaned up!", |
| 123 | + client.content().exists( repo.getKey(), PATH_JQUERY + "/package.json", true ), equalTo( false ) ); |
| 124 | + |
| 125 | + assertThat( "Metadata not cleaned up!", client.content().exists( repo.getKey(), PATH_BABEL_PARSER, true ), |
| 126 | + equalTo( false ) ); |
| 127 | + assertThat( "Metadata not cleaned up!", |
| 128 | + client.content().exists( repo.getKey(), PATH_BABEL_PARSER + "/package.json", true ), |
| 129 | + equalTo( false ) ); |
| 130 | + |
| 131 | + |
| 132 | + logger.info( "\n\n\n\nRE-REQUEST STARTS HERE\n\n\n\n" ); |
| 133 | + |
| 134 | + // Second retrieval |
| 135 | + dts.setBeta( "2" ); |
| 136 | + verifyMetadataBetaTag( "2", PATH_JQUERY, repo ); |
| 137 | + verifyMetadataBetaTag( "2", PATH_BABEL_PARSER, repo ); |
| 138 | + } |
| 139 | + |
| 140 | + private void verifyMetadataBetaTag( final String betaTag, final String path, RemoteRepository repo ) |
| 141 | + throws IndyClientException, IOException |
| 142 | + { |
| 143 | + try (InputStream remote = client.content().get( repo.getKey(), path )) |
| 144 | + { |
| 145 | + assertThat( remote, notNullValue() ); |
| 146 | + String json = IOUtils.toString( remote ); |
| 147 | + PackageMetadata merged = |
| 148 | + new IndyObjectMapper( true ).readValue( json, PackageMetadata.class ); |
| 149 | + |
| 150 | + assertThat( merged.getDistTags().getBeta(), equalTo( betaTag ) ); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + protected boolean createStandardTestStructures() |
| 156 | + { |
| 157 | + return false; |
| 158 | + } |
| 159 | +} |
0 commit comments