func verifySinglePageBlobUpload()

in testSuite/cmd/testblob.go [281:438]


func verifySinglePageBlobUpload(testBlobCmd TestBlobCommand) {

	fileInfo, err := os.Stat(testBlobCmd.Object)
	if err != nil {
		fmt.Println("error opening the destination blob on local disk ")
		os.Exit(1)
	}
	file, err := os.Open(testBlobCmd.Object)
	if err != nil {
		fmt.Println("error opening the file ", testBlobCmd.Object)
	}

	testCtx := context.WithValue(context.Background(), ste.ServiceAPIVersionOverride, defaultServiceApiVersion)

	isPage, err := verifyBlobType(testBlobCmd.Subject, testCtx, "PageBlob")

	if !isPage || err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	pageBlobClient, err := pageblob.NewClientWithNoCredential(testBlobCmd.Subject, &pageblob.ClientOptions{
		ClientOptions: azcore.ClientOptions{
			Telemetry: policy.TelemetryOptions{ApplicationID: common.UserAgent},
			Retry: policy.RetryOptions{
				MaxRetries:    ste.UploadMaxTries,
				TryTimeout:    10 * time.Minute,
				RetryDelay:    ste.UploadRetryDelay,
				MaxRetryDelay: ste.UploadMaxRetryDelay,
			},
			Transport: ste.NewAzcopyHTTPClient(0),
		}})
	if err != nil {
		fmt.Printf("error creating page blob client. failed with error %s\n", err.Error())
		os.Exit(1)
	}

	// get the blob properties and check the blob tier.
	if testBlobCmd.BlobTier != "" {
		blobProperties, err := pageBlobClient.GetProperties(testCtx, nil)
		if err != nil {
			fmt.Printf("error getting the properties of the blob. failed with error %s\n", err.Error())
			os.Exit(1)
		}
		// If the blob tier does not match the expected blob tier.
		if !strings.EqualFold(common.IffNotNil(blobProperties.AccessTier, ""), testBlobCmd.BlobTier) {
			fmt.Printf("Access blob tier type %s does not match the expected %s tier type\n", common.IffNotNil(blobProperties.AccessTier, ""), testBlobCmd.BlobTier)
			os.Exit(1)
		}
	}

	get, err := pageBlobClient.DownloadStream(testCtx, nil)
	if err != nil {
		fmt.Println("unable to get blob properties ", err.Error())
		os.Exit(1)
	}
	// reading all the bytes downloaded.
	blobBytesDownloaded, err := io.ReadAll(get.Body)
	if get.Body != nil {
		get.Body.Close()
	}
	if err != nil {
		fmt.Println("error reading the byes from response and failed with error ", err.Error())
		os.Exit(1)
	}

	expectedContentType := ""

	if testBlobCmd.NoGuessMimeType {
		expectedContentType = testBlobCmd.ContentType
	}

	if len(blobBytesDownloaded) != 0 {
		// memory mapping the resource on local path.
		mmap, err := NewMMF(file, false, 0, fileInfo.Size())
		if err != nil {
			fmt.Println("error mapping the destination blob file ", err.Error())
			os.Exit(1)
		}

		// calculating and verify the md5 of the resource
		// both locally and on the container.
		actualMd5 := md5.Sum(mmap)
		expectedMd5 := md5.Sum(blobBytesDownloaded)
		if actualMd5 != expectedMd5 {
			fmt.Println("the uploaded blob's md5 doesn't matches the actual blob's md5 for blob ", testBlobCmd.Object)
			os.Exit(1)
		}

		if !testBlobCmd.NoGuessMimeType {
			expectedContentType = strings.Split(http.DetectContentType(mmap), ";")[0]
		}

		mmap.Unmap()
	}

	// verify the content-type
	if testBlobCmd.CheckContentType && !validateString(expectedContentType, common.IffNotNil(get.ContentType, "")) {
		fmt.Printf(
			"mismatch content type between actual and user given blob content type, expected %q, actually %q\n",
			expectedContentType,
			common.IffNotNil(get.ContentType, ""))
		os.Exit(1)
	}

	// verify the user given metadata supplied while uploading the blob against the metadata actually present in the blob
	if !validateMetadata(testBlobCmd.Metadata, get.Metadata) {
		fmt.Println("meta data does not match between the actual and uploaded blob.")
		os.Exit(1)
	}

	//verify the content-encoding
	if !validateString(testBlobCmd.ContentEncoding, common.IffNotNil(get.ContentEncoding, "")) {
		fmt.Println("mismatch ContentEncoding between actual and user given blob")
		os.Exit(1)
	}

	if !validateString(testBlobCmd.CacheControl, common.IffNotNil(get.CacheControl, "")) {
		fmt.Println("mismatch CacheControl between actual and user given blob")
		os.Exit(1)
	}

	if !validateString(testBlobCmd.ContentDisposition, common.IffNotNil(get.ContentDisposition, "")) {
		fmt.Println("mismatch ContentDisposition between actual and user given blob")
		os.Exit(1)
	}

	if !validateString(testBlobCmd.ContentLanguage, common.IffNotNil(get.ContentLanguage, "")) {
		fmt.Println("mismatch ContentLanguage between actual and user given blob")
		os.Exit(1)
	}

	if testBlobCmd.CheckContentMD5 && len(get.ContentMD5) == 0 {
		fmt.Println("ContentMD5 should not be empty")
		os.Exit(1)
	}
	file.Close()

	// verify the number of pageranges.
	// this verifies the page-size and azcopy pageblob implementation.
	if testBlobCmd.VerifyBlockOrPageSize {
		numberOfPages := int(testBlobCmd.NumberOfBlocksOrPages)
		pager := pageBlobClient.NewGetPageRangesPager(nil)
		pageRanges := 0
		for pager.More() {
			resp, err := pager.NextPage(testCtx)
			if err != nil {
				fmt.Println("error getting the page blob list ", err.Error())
				os.Exit(1)
			}
			pageRanges += len(resp.PageRange)
		}
		if numberOfPages != (pageRanges) {
			fmt.Printf("number of blocks to be uploaded (%d) is different from the number of expected to be uploaded (%d)\n", pageRanges, numberOfPages)
			os.Exit(1)
		}
	}
}