(suite *TestRegistry, tlsEnabled, insecure bool)
| 70 | } |
| 71 | |
| 72 | func setup(suite *TestRegistry, tlsEnabled, insecure bool) { |
| 73 | suite.WorkspaceDir = testWorkspaceDir |
| 74 | err := os.RemoveAll(suite.WorkspaceDir) |
| 75 | require.NoError(suite.T(), err, "no error removing test workspace dir") |
| 76 | err = os.Mkdir(suite.WorkspaceDir, 0700) |
| 77 | require.NoError(suite.T(), err, "no error creating test workspace dir") |
| 78 | |
| 79 | var out bytes.Buffer |
| 80 | |
| 81 | suite.Out = &out |
| 82 | credentialsFile := filepath.Join(suite.WorkspaceDir, CredentialsFileBasename) |
| 83 | |
| 84 | // init test client |
| 85 | opts := []ClientOption{ |
| 86 | ClientOptDebug(true), |
| 87 | ClientOptEnableCache(true), |
| 88 | ClientOptWriter(suite.Out), |
| 89 | ClientOptCredentialsFile(credentialsFile), |
| 90 | ClientOptBasicAuth(testUsername, testPassword), |
| 91 | } |
| 92 | |
| 93 | if tlsEnabled { |
| 94 | var tlsConf *tls.Config |
| 95 | if insecure { |
| 96 | tlsConf, err = tlsutil.NewTLSConfig( |
| 97 | tlsutil.WithInsecureSkipVerify(true), |
| 98 | ) |
| 99 | } else { |
| 100 | tlsConf, err = tlsutil.NewTLSConfig( |
| 101 | tlsutil.WithCertKeyPairFiles(tlsCert, tlsKey), |
| 102 | tlsutil.WithCAFile(tlsCA), |
| 103 | ) |
| 104 | } |
| 105 | httpClient := &http.Client{ |
| 106 | Transport: &http.Transport{ |
| 107 | TLSClientConfig: tlsConf, |
| 108 | }, |
| 109 | } |
| 110 | suite.Nil(err, "no error loading tls config") |
| 111 | opts = append(opts, ClientOptHTTPClient(httpClient)) |
| 112 | } else { |
| 113 | opts = append(opts, ClientOptPlainHTTP()) |
| 114 | } |
| 115 | |
| 116 | suite.RegistryClient, err = NewClient(opts...) |
| 117 | suite.Nil(err, "no error creating registry client") |
| 118 | |
| 119 | // create htpasswd file (w BCrypt, which is required) |
| 120 | pwBytes, err := bcrypt.GenerateFromPassword([]byte(testPassword), bcrypt.DefaultCost) |
| 121 | suite.Nil(err, "no error generating bcrypt password for test htpasswd file") |
| 122 | htpasswdPath := filepath.Join(suite.WorkspaceDir, testHtpasswdFileBasename) |
| 123 | err = os.WriteFile(htpasswdPath, fmt.Appendf(nil, "%s:%s\n", testUsername, string(pwBytes)), 0644) |
| 124 | suite.Nil(err, "no error creating test htpasswd file") |
| 125 | |
| 126 | // Registry config |
| 127 | config := &configuration.Configuration{} |
| 128 | ln, err := net.Listen("tcp", "127.0.0.1:0") |
| 129 | suite.Nil(err, "no error finding free port for test registry") |
no test coverage detected
searching dependent graphs…