TestMain initializes an AWS KMS server using Docker, writes the HTTP address to testKMSServerURL, tries to generate a key for encryption-decryption using a backoff retry approach, and then sets testKMSARN to the ID of the generated key. It continues to run all the tests, which can make use of the va
(m *testing.M)
| 44 | // It continues to run all the tests, which can make use of the various `test*` |
| 45 | // variables. |
| 46 | func TestMain(m *testing.M) { |
| 47 | // Uses a sensible default on Windows (TCP/HTTP) and Linux/MacOS (socket) |
| 48 | pool, err := dockertest.NewPool("") |
| 49 | if err != nil { |
| 50 | logger.Fatalf("could not connect to docker: %s", err) |
| 51 | } |
| 52 | |
| 53 | // Pull the image, create a container based on it, and run it |
| 54 | // resource, err := pool.Run("nsmithuk/local-kms", testLocalKMSVersion, []string{}) |
| 55 | resource, err := pool.RunWithOptions(&dockertest.RunOptions{ |
| 56 | Repository: testLocalKMSImage, |
| 57 | Tag: testLocalKMSTag, |
| 58 | ExposedPorts: []string{"8080"}, |
| 59 | }) |
| 60 | if err != nil { |
| 61 | logger.Fatalf("could not start resource: %s", err) |
| 62 | } |
| 63 | |
| 64 | purgeResource := func() { |
| 65 | if err := pool.Purge(resource); err != nil { |
| 66 | logger.Printf("could not purge resource: %s", err) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | testKMSServerURL = fmt.Sprintf("http://127.0.0.1:%v", resource.GetPort("8080/tcp")) |
| 71 | masterKey := createTestMasterKey(dummyARN) |
| 72 | |
| 73 | kmsClient, err := createTestKMSClient(masterKey) |
| 74 | if err != nil { |
| 75 | purgeResource() |
| 76 | logger.Fatalf("could not create session: %s", err) |
| 77 | } |
| 78 | |
| 79 | var key *kms.CreateKeyOutput |
| 80 | if err := pool.Retry(func() error { |
| 81 | key, err = kmsClient.CreateKey(context.TODO(), &kms.CreateKeyInput{}) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | return nil |
| 86 | }); err != nil { |
| 87 | purgeResource() |
| 88 | logger.Fatalf("could not create key: %s", err) |
| 89 | } |
| 90 | |
| 91 | if key.KeyMetadata.Arn != nil { |
| 92 | testKMSARN = *key.KeyMetadata.Arn |
| 93 | } else { |
| 94 | purgeResource() |
| 95 | logger.Fatalf("could not set arn") |
| 96 | } |
| 97 | |
| 98 | // Run the tests, but only if we succeeded in setting up the AWS KMS server. |
| 99 | var code int |
| 100 | if err == nil { |
| 101 | code = m.Run() |
| 102 | } |
| 103 |
nothing calls this directly
no test coverage detected