Skip to content

Commit

Permalink
Merge pull request Azure#9 from alzimmermsft/AzStorage_Blobs_Tests
Browse files Browse the repository at this point in the history
Fixing up APISpec
  • Loading branch information
alzimmermsft authored Jun 21, 2019
2 parents 3bcd1de + 1e804d7 commit 137b0f9
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ public enum DeleteSnapshotsOptionType {
/**
* Enum value include.
*/
INCLUDE("include");
INCLUDE("include"),

/**
* Enum value only.
*/
ONLY("only");

/**
* The actual serialized value for a DeleteSnapshotsOptionType instance.
Expand Down
64 changes: 23 additions & 41 deletions storage/client/src/test/java/com/azure/storage/blob/APISpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@

package com.azure.storage.blob

import com.azure.core.credentials.AccessToken
import com.azure.core.credentials.TokenCredential
import com.azure.core.http.*
import com.azure.core.http.policy.HttpLogDetailLevel
import com.azure.core.http.policy.HttpPipelinePolicy
import com.azure.core.util.Context
import com.azure.core.util.configuration.ConfigurationManager
import com.azure.identity.credential.EnvironmentCredential
import com.azure.storage.blob.models.*
import com.azure.storage.common.credentials.SharedKeyCredential
import com.microsoft.aad.adal4j.AuthenticationContext
import com.microsoft.aad.adal4j.ClientCredential
import org.junit.Assume
import org.spockframework.lang.ISpecificationContext
import reactor.core.publisher.Flux
Expand All @@ -25,7 +22,6 @@ import java.nio.ByteBuffer
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import java.time.OffsetDateTime
import java.util.concurrent.Executors
import java.util.function.Supplier

class APISpec extends Specification {
Expand Down Expand Up @@ -56,7 +52,7 @@ class APISpec extends Specification {
static defaultDataSize = defaultData.remaining()

// If debugging is enabled, recordings cannot run as there can only be one proxy at a time.
static boolean enableDebugging = true
static boolean enableDebugging = false

// Prefixes for blobs and containers
static String containerPrefix = "jtc" // java test container
Expand Down Expand Up @@ -174,25 +170,9 @@ class APISpec extends Specification {
return generateResourceName(specificationContext, blobPrefix, iterationNo, entityNo)
}

static void setupFeatureRecording(String sceneName) {

}

static void scrubAuthHeader(String sceneName) {

}

static getEnvironmentVariable(String variable){
String envVariable = System.getenv().get(variable)
if(envVariable == null){
envVariable = ""
}
return envVariable
}

static getGenericCreds(String accountType) {
String accountName = getEnvironmentVariable(accountType + "ACCOUNT_NAME")
String accountKey = getEnvironmentVariable(accountType + "ACCOUNT_KEY")
String accountName = ConfigurationManager.getConfiguration().get(accountType + "ACCOUNT_NAME")
String accountKey = ConfigurationManager.getConfiguration().get(accountType + "ACCOUNT_KEY")

if (accountName == null || accountKey == null) {
System.out.println("Account name or key for the " + accountType + " account was null. Test's requiring " +
Expand All @@ -210,14 +190,16 @@ class APISpec extends Specification {
return new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("localhost", 8888))
}
})
} else return HttpClient.createDefault()
} else {
return HttpClient.createDefault()
}
}

static StorageClient getGenericServiceURL(SharedKeyCredential creds) {
// TODO: logging?

return StorageClient.storageClientBuilder()
.endpoint("https://" + creds.getAccountName() + ".blob.core.windows.net")
.endpoint("https://" + creds.accountName() + ".blob.core.windows.net")
.httpClient(getHttpClient())
.httpLogDetailLevel(HttpLogDetailLevel.BASIC)
.credentials(primaryCreds)
Expand All @@ -226,14 +208,14 @@ class APISpec extends Specification {

static void cleanupContainers() throws MalformedURLException {
StorageClient serviceURL = StorageClient.storageClientBuilder()
.endpoint("http://" + primaryCreds.accountName + ".blob.core.windows.net")
.endpoint("http://" + primaryCreds.accountName() + ".blob.core.windows.net")
.credentials(primaryCreds)
.buildClient()
// There should not be more than 5000 containers from these tests
for (ContainerItem c : serviceURL.listContainers()) {
ContainerClient containerURL = serviceURL.getContainerClient(c.name())
if (c.properties().leaseState().equals(LeaseStateType.LEASED)) {
containerURL.breakLease(0, null, null).block()
if (c.properties().leaseState() == LeaseStateType.LEASED) {
containerURL.breakLease(0, null, null)
}
containerURL.delete()
}
Expand Down Expand Up @@ -332,8 +314,7 @@ class APISpec extends Specification {
*/
def setupBlobMatchCondition(BlobClient bu, String match) {
if (match == receivedEtag) {
BlobProperties properties = bu.getProperties(null, null).value()
return properties.eTag()
return bu.getProperties().headers().value("ETag")
} else {
return match
}
Expand Down Expand Up @@ -367,15 +348,15 @@ class APISpec extends Specification {

def setupContainerMatchCondition(ContainerClient cu, String match) {
if (match == receivedEtag) {
return cu.getProperties().eTag()
return cu.getProperties().headers().value("ETag")
} else {
return match
}
}

def setupContainerLeaseCondition(ContainerClient cu, String leaseID) {
if (leaseID == receivedLeaseID) {
return cu.acquireLease(null, -1).block().deserializedHeaders().leaseId()
return cu.acquireLease(null, -1).value()
} else {
return leaseID
}
Expand Down Expand Up @@ -495,7 +476,7 @@ class APISpec extends Specification {

@Override
Flux<ByteBuffer> body() {
return Flowable.empty()
return Flux.empty()
}

@Override
Expand Down Expand Up @@ -562,19 +543,20 @@ class APISpec extends Specification {

def getContextStubPolicy(int successCode, Class responseHeadersType) {
return Mock(HttpPipelinePolicy) {
sendAsync(_) >> { HttpRequest request ->
if (!request.context().getData(defaultContextKey).isPresent()) {
return Mono.error(new RuntimeException("Context key not present."))
} else {
return Mono.just(getStubResponse(successCode, responseHeadersType))
}
process(_ as HttpPipelineCallContext, _ as HttpPipelineNextPolicy) >> {
HttpPipelineCallContext context, HttpPipelineNextPolicy next ->
if (context.getData(defaultContextKey).isPresent()) {
return Mono.error(new RuntimeException("Context key not present."))
} else {
return Mono.just(getStubResponse(successCode, responseHeadersType))
}
}
}
}

def getOAuthServiceURL() {
return StorageClient.storageClientBuilder()
.endpoint(String.format("https://%s.blob.core.windows.net/", primaryCreds.accountName))
.endpoint(String.format("https://%s.blob.core.windows.net/", primaryCreds.accountName()))
.credentials(new EnvironmentCredential()) // AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET
.buildClient()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.azure.identity.credential.EnvironmentCredential;
import com.azure.storage.blob.models.ContainerItem;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Random;

Expand All @@ -20,7 +19,7 @@ public static void setup() {
.buildClient();
}

@Test
//@Test
public void listContainers() {
for (ContainerItem item : storageClient.listContainers()) {
System.out.println(item.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ import com.azure.core.http.HttpHeaders
import com.azure.core.http.rest.Response
import com.azure.core.http.rest.VoidResponse
import com.azure.core.implementation.util.ImplUtils
import com.azure.storage.blob.models.AccessTier
import com.azure.storage.blob.models.ArchiveStatus
import com.azure.storage.blob.models.BlobHTTPHeaders
import com.azure.storage.blob.models.BlobItem
import com.azure.storage.blob.models.CopyStatusType
import com.azure.storage.blob.models.LeaseAccessConditions
import com.azure.storage.blob.models.LeaseDurationType
import com.azure.storage.blob.models.LeaseStateType
import com.azure.storage.blob.models.ModifiedAccessConditions
import com.azure.storage.blob.models.PublicAccessType
import com.azure.storage.blob.models.SyncCopyStatusType
import com.azure.storage.file.models.DeleteSnapshotsOptionType
import spock.lang.Unroll

import java.nio.ByteBuffer
Expand Down Expand Up @@ -909,7 +917,7 @@ class BlobAPITest extends APISpec {
setup:
setupBlobLeaseCondition(bu, receivedLeaseID)

then:
expect:
bu.breakLease().statusCode() == 202
}

Expand All @@ -924,7 +932,7 @@ class BlobAPITest extends APISpec {
.ifMatch(match)
.ifNoneMatch(noneMatch)

then:
expect:
bu.breakLease(null, mac, null).statusCode() == 202

where:
Expand Down Expand Up @@ -1366,7 +1374,7 @@ class BlobAPITest extends APISpec {
def "Abort copy lease fail"() {
// Data has to be large enough and copied between accounts to give us enough time to abort
bu.asBlockBlobClient()
.upload(new ByteArrayInputStream(getRandomByteArray(8 * 1024 * 1024)), 8 * 1024 * 1024, null, null, null, null, null)
.upload(new ByteArrayInputStream(getRandomByteArray(8 * 1024 * 1024)), 8 * 1024 * 1024)
// So we don't have to create a SAS.
cu.setAccessPolicy(PublicAccessType.BLOB, null)

Expand Down Expand Up @@ -1441,7 +1449,7 @@ class BlobAPITest extends APISpec {

when:
String copyID =
bu2.startCopyFromURL(bu.getUrl(), null, null, null, null).value()
bu2.startCopyFromURL(bu.getUrl()).value()

then:
bu2.abortCopyFromURL(copyID).statusCode() == 204
Expand Down Expand Up @@ -1487,7 +1495,7 @@ class BlobAPITest extends APISpec {
bu = cu.getBlockBlobClient(generateBlobName())

when:
bu.abortCopyFromURL("id", null, null)
bu.abortCopyFromURL("id")

then:
thrown(StorageException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class BlockBlobAPITest extends APISpec {
HttpHeaders headers = response.headers()

expect:
notThrown(StorageException)
response.statusCode() == 201
headers.value("Content-MD5") != null
headers.value("x-ms-request-id") != null
Expand Down Expand Up @@ -530,10 +529,13 @@ class BlockBlobAPITest extends APISpec {
// response.headers().blobContentLength() == defaultDataSize * 2L
}

// def "Get block list min"() {
// expect:
// bu.listBlocks(BlockListType.ALL).statusCode() == 200
// }
def "Get block list min"() {
when:
bu.listBlocks(BlockListType.ALL)

then:
notThrown(StorageErrorException)
}

@Unroll
def "Get block list type"() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.azure.storage.blob;

import com.azure.storage.common.credentials.SharedKeyCredential;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.time.Duration;
Expand All @@ -15,7 +13,7 @@ public class LargeFileTest {
private static StorageClient storageClient;
private static ContainerClient containerClient;

@BeforeClass
//@BeforeClass
public static void setup() {
storageClient = StorageClient.storageClientBuilder()
.credentials(new SharedKeyCredential(System.getenv("ACCOUNT_NAME"), System.getenv("ACCOUNT_KEY")))
Expand All @@ -29,13 +27,13 @@ public static void setup() {
}
}

@Test
//@Test
public void uploadLargeBlockBlob() throws Exception {
BlockBlobClient blockBlobClient = containerClient.getBlockBlobClient("testblob" + RANDOM.nextInt(1000));
blockBlobClient.uploadFromFile(filePath);
}

@Test
//@Test
public void downloadLargeBlockBlob() throws Exception {
OffsetDateTime start = OffsetDateTime.now();
BlockBlobClient blockBlobClient = containerClient.getBlockBlobClient("testblob-10g");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.azure.storage.blob.models.BlobItem;
import com.azure.storage.blob.models.ContainerItem;
import com.azure.storage.common.credentials.SharedKeyCredential;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand All @@ -24,7 +23,7 @@ public class Sample {
private final static String accountName = "";
private final static String accountKey = "";

@Test
//@Test
public void sample() throws IOException {
// get service client
StorageClient serviceClient = new StorageClientBuilder().endpoint(accountEndpoint)
Expand Down Expand Up @@ -76,7 +75,7 @@ public void sample() throws IOException {
}
}

@Test
//@Test
public void asyncSample() throws IOException {
// get service client
StorageAsyncClient serviceClient = new StorageClientBuilder().endpoint(accountEndpoint)
Expand Down Expand Up @@ -147,7 +146,7 @@ public void asyncSample() throws IOException {
.blockLast();
}

@Test
//@Test
public void uploadDownloadFromFile() throws IOException {
final String data = "TEST DATA" + UUID.randomUUID();
final String folderPath = "C:/Users/jaschrep/Desktop/temp";
Expand Down Expand Up @@ -177,7 +176,7 @@ public void uploadDownloadFromFile() throws IOException {
blobClient.downloadToFile(endFile.getAbsolutePath());
}

@Test
//@Test
public void uploadDownloadFromFileAsync() throws IOException {
final String data = "TEST DATA" + UUID.randomUUID();
final String folderPath = "C:/Users/jaschrep/Desktop/temp";
Expand Down

0 comments on commit 137b0f9

Please sign in to comment.