gcs_test.go

Overview

The gcs_test.go file provides unit testing infrastructure and mock implementations for the Google Cloud Storage (GCS) backend artifact service within the Artifact Management topic. It defines an in-memory fake client that simulates GCS operations such as storing, retrieving, listing, and deleting objects (artifacts) entirely in memory. This allows testing the artifact service logic without requiring actual access to Google Cloud infrastructure.

The primary goal of this file is to support reliable and isolated testing of the GCS artifact service implementation by mocking GCS client interfaces (gcsClient, gcsBucket, gcsObject, etc.) using thread-safe, in-memory data structures. It also includes a test function that integrates with the broader artifact service test suite to validate conformance.


Classes, Functions, and Methods

newGCSArtifactServiceForTesting(bucketName string) (artifact.Service, error)


TestGCSArtifactService(t *testing.T)


Mock Implementations

The following types implement mock versions of GCS client interfaces. They simulate GCS behavior using in-memory data structures and provide thread-safe access to enable parallel test execution.


fakeClient


fakeBucket


fakeObject


fakeWriter


fakeObjectIterator


Important Implementation Details


Interaction with System Components


Usage Example

func TestExample(t *testing.T) {
    service, err := newGCSArtifactServiceForTesting("my-test-bucket")
    if err != nil {
        t.Fatal(err)
    }

    // Use the service to save, load, or delete artifacts in tests.
    // Example: save an artifact and verify version increment.
    
    // Note: For comprehensive tests, see TestGCSArtifactService.
}

Mermaid Diagram: Structure of gcs_test.go

classDiagram
class fakeClient {
-inMemoryBucket: gcsBucket
+bucket()
}
class fakeBucket {
-mu: Mutex
-objectsMap: map[string]*fakeObject
+object()
+objects()
}
class fakeObject {
-mu: Mutex
-name: string
-data: []byte
-deleted: bool
-contentType: string
+newWriter()
+attrs()
+delete()
+newReader()
}
class fakeWriter {
-obj: *fakeObject
-buffer: bytes.Buffer
-contentType: string
+Write()
+Close()
+SetContentType()
}
class fakeObjectIterator {
-objects: []*fakeObject
-index: int
+next()
}
fakeClient "1" --> "1" fakeBucket : contains
fakeBucket "1" --> "*" fakeObject : stores
fakeBucket --> fakeObjectIterator : returns iterator
fakeObject --> fakeWriter : creates writer
fakeObjectIterator --> fakeObject : iterates over

This documentation references the broader Artifact Management and Google Cloud Storage Service topics for detailed service interfaces, naming conventions, and artifact versioning schemes. The testing mocks here implement the client interfaces described therein, enabling the development and validation of artifact persistence logic in isolation.