github.com/angryronald/go-kit@v0.0.0-20240505173814-ff2bd9c79dbf/test/docker/mongo/mongo.sample.go (about)

     1  package mongo
     2  
     3  // uncomment to try to run the code
     4  // package main
     5  
     6  // import (
     7  // 	"context"
     8  // 	"fmt"
     9  // 	"log"
    10  // 	"time"
    11  // 
    12  
    13  // 	"go.mongodb.org/mongo-driver/mongo"
    14  // 	mongoOptions "go.mongodb.org/mongo-driver/mongo/options"
    15  
    16  // 	"github.com/ory/dockertest/v3"
    17  // )
    18  
    19  // func main() {
    20  // 	// Create a new pool of Docker containers
    21  // 	pool, err := dockertest.NewPool("")
    22  // 	if err != nil {
    23  // 		log.Fatalf("Could not connect to Docker: %s", err)
    24  // 	}
    25  
    26  // 	// Set up options for MongoDB container
    27  // 	options := &dockertest.RunOptions{
    28  // 		Repository: "mongo",
    29  // 		Tag:        "4.4",
    30  // 		Env: []string{
    31  // 			"MONGO_INITDB_ROOT_USERNAME=root",
    32  // 			"MONGO_INITDB_ROOT_PASSWORD=example",
    33  // 		},
    34  // 	}
    35  
    36  // 	// Run MongoDB container
    37  // 	resource, err := pool.RunWithOptions(options)
    38  // 	if err != nil {
    39  // 		log.Fatalf("Could not start MongoDB container: %s", err)
    40  // 	}
    41  // 	defer func() {
    42  // 		// Clean up and remove the MongoDB container when done
    43  // 		if err := pool.Purge(resource); err != nil {
    44  // 			log.Fatalf("Could not purge MongoDB container: %s", err)
    45  // 		}
    46  // 	}()
    47  
    48  // 	// Wait for MongoDB to be ready
    49  // 	if err := pool.Retry(func() error {
    50  // 		clientOptions := mongoOptions.Client().ApplyURI(fmt.Sprintf(
    51  // 			"mongodb://root:example@%s:%s",
    52  // 			resource.GetBoundIP("27017/tcp"),
    53  // 			resource.GetPort("27017/tcp"),
    54  // 		))
    55  // 		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    56  // 		defer cancel()
    57  // 		client, err := mongo.Connect(ctx, clientOptions)
    58  // 		if err != nil {
    59  // 			return err
    60  // 		}
    61  // 		err = client.Ping(ctx, nil)
    62  // 		return err
    63  // 	}); err != nil {
    64  // 		log.Fatalf("Could not connect to MongoDB: %s", err)
    65  // 	}
    66  
    67  // 	// At this point, MongoDB should be running and ready to use.
    68  
    69  // 	// You can use the MongoDB Go driver to interact with MongoDB.
    70  // 	// For example, you can create a new MongoDB client and perform operations:
    71  
    72  // 	// Connect to MongoDB
    73  // 	clientOptions := mongoOptions.Client().ApplyURI(fmt.Sprintf(
    74  // 		"mongodb://root:example@%s:%s",
    75  // 		resource.GetBoundIP("27017/tcp"),
    76  // 		resource.GetPort("27017/tcp"),
    77  // 	))
    78  // 	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    79  // 	defer cancel()
    80  // 	client, err := mongo.Connect(ctx, clientOptions)
    81  // 	if err != nil {
    82  // 		log.Fatalf("Could not connect to MongoDB: %s", err)
    83  // 	}
    84  // 	defer func() {
    85  // 		// Disconnect from MongoDB when done
    86  // 		if err := client.Disconnect(ctx); err != nil {
    87  // 			log.Fatalf("Error disconnecting from MongoDB: %s", err)
    88  // 		}
    89  // 	}()
    90  
    91  // 	// Perform MongoDB operations here, e.g., create collections, insert documents, etc.
    92  
    93  // 	// Remember to handle errors appropriately and implement any additional testing logic.
    94  
    95  // 	fmt.Println("Connected to MongoDB")
    96  // }