github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/block/azure/main_test.go (about) 1 package azure_test 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 "net" 8 "os" 9 "testing" 10 "time" 11 12 "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" 13 "github.com/benburkert/dns" 14 "github.com/ory/dockertest/v3" 15 "github.com/treeverse/lakefs/pkg/block/azure" 16 ) 17 18 const ( 19 azuriteContainerTimeoutSeconds = 10 * 60 // 10 min 20 containerName = "container1" 21 accountName = "account1" 22 accountKey = "key1" 23 domain = azure.BlobEndpointTestDomain // TLD for test 24 ) 25 26 var ( 27 pool *dockertest.Pool 28 blockURL string 29 ) 30 31 func createDNSResolver() { 32 zone := dns.Zone{ 33 Origin: domain + ".", 34 TTL: 5 * time.Minute, 35 RRs: dns.RRSet{ 36 accountName: map[dns.Type][]dns.Record{ 37 dns.TypeA: { 38 &dns.A{A: net.IPv4(127, 0, 0, 1).To4()}, 39 }, 40 }, 41 }, 42 } 43 44 mux := new(dns.ResolveMux) 45 mux.Handle(dns.TypeANY, zone.Origin, &zone) 46 client := &dns.Client{ 47 Resolver: mux, 48 } 49 net.DefaultResolver = &net.Resolver{ 50 PreferGo: true, 51 Dial: client.Dial, 52 } 53 } 54 55 func runAzurite(dockerPool *dockertest.Pool) (string, func()) { 56 ctx := context.Background() 57 resource, err := dockerPool.Run("mcr.microsoft.com/azure-storage/azurite", "3.26.0", []string{ 58 fmt.Sprintf("AZURITE_ACCOUNTS=%s:%s", accountName, accountKey), 59 }) 60 if err != nil { 61 panic(err) 62 } 63 64 accountHost := accountName + "." + domain 65 createDNSResolver() 66 67 // set cleanup 68 closer := func() { 69 err := dockerPool.Purge(resource) 70 if err != nil { 71 panic("could not purge Azurite container: " + err.Error()) 72 } 73 } 74 75 // expire, just to make sure 76 err = resource.Expire(azuriteContainerTimeoutSeconds) 77 if err != nil { 78 panic("could not expire Azurite container: " + err.Error()) 79 } 80 81 // create connection and test container 82 port := resource.GetPort("10000/tcp") 83 url := fmt.Sprintf("http://%s:%s", accountHost, port) 84 cred, err := azblob.NewSharedKeyCredential(accountName, accountKey) 85 if err != nil { 86 panic(err) 87 } 88 89 blob, err := azblob.NewClientWithSharedKeyCredential(url, cred, nil) 90 if err != nil { 91 panic(err) 92 } 93 _, err = blob.CreateContainer(ctx, containerName, nil) 94 if err != nil { 95 panic(err) 96 } 97 98 // return container URL 99 return url, closer 100 } 101 102 func TestMain(m *testing.M) { 103 var err error 104 pool, err = dockertest.NewPool("") 105 if err != nil { 106 log.Fatalf("Could not connect to Docker: %s", err) 107 } 108 var cleanup func() 109 blockURL, cleanup = runAzurite(pool) 110 code := m.Run() 111 cleanup() 112 os.Exit(code) 113 }