b2sim

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package b2sim provides an in-memory B2 wire server for blazer clients. Pass New() to b2.Transport to exercise the normal HTTP, retry, and JSON path. It is a testing aid, not a complete B2 emulator: it does not enforce application-key credentials or capabilities, retention, replication, encryption, lifecycle, CORS, or download-authorization signatures. Unsupported endpoints fail with a non-2xx response; they never silently succeed. With valid simulator authorization, b2_hide_file, b2_list_unfinished_large_files (so Writer.Resume), b2_list_parts, b2_copy_file, b2_copy_part (so Copy and Compose with copied parts), b2_download_file_by_id, b2_get_download_authorization, b2_get_bucket_notification_rules, b2_set_bucket_notification_rules, b2_update_file_retention, b2_update_file_legal_hold, b2_create_key (v4), b2_list_keys, and b2_delete_key return HTTP 501 unsupported_operation. Other unimplemented v4 endpoints also return 501; v3 endpoints return 404. Multipart part-number and minimum-size limits are enforced like B2.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Simulator

type Simulator struct {
	// contains filtered or unexported fields
}

Simulator is an independent, concurrency-safe in-memory B2 service. Construct it with New; the zero value is not usable. It stores bucket and file settings but does not enforce retention, replication, encryption, lifecycle, or download-authorization policies.

func New

func New() *Simulator

New creates an empty simulator. No sockets or external services are used.

Example
package main

import (
	"context"
	"fmt"

	"github.com/Backblaze/blazer/b2"
	"github.com/Backblaze/blazer/x/b2sim"
)

func main() {
	ctx := context.Background()
	sim := b2sim.New()
	client, err := b2.NewClient(ctx, "example-id", "example-key", b2.Transport(sim))
	if err != nil {
		panic(err)
	}
	bucket, err := client.NewBucket(ctx, "example-bucket", nil)
	if err != nil {
		panic(err)
	}
	fmt.Println(bucket.Name())
	fmt.Println(sim.Calls()["b2_create_bucket"])
}
Output:
example-bucket
1

func (*Simulator) Buckets

func (s *Simulator) Buckets() []string

Buckets returns a sorted snapshot of bucket names.

func (*Simulator) Calls

func (s *Simulator) Calls() map[string]int

Calls returns a snapshot of B2 endpoint counts for test assertions.

func (*Simulator) RoundTrip

func (s *Simulator) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip serves supported B2 requests entirely in memory. Request bodies are read before locking, so an arbitrary caller-owned Reader cannot hold the simulator's state lock while blocked.

func (*Simulator) Seed

func (s *Simulator) Seed(name string) string

Seed creates a private bucket for a test and returns its ID. Repeated names return the existing ID.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL