Repository: helm/helm
File: pkg/registry/client_tls_test.go (Go)

Code before:
	os.RemoveAll(suite.WorkspaceDir)
}

func (suite *TLSRegistryClientTestSuite) Test_0_Login() {
	err := suite.RegistryClient.Login(suite.DockerRegistryHost,
		LoginOptBasicAuth("badverybad", "ohsobad"),
		LoginOptTLSClientConfig(tlsCert, tlsKey, tlsCA))
	suite.NotNil(err, "error logging into registry with bad credentials")

	err = suite.RegistryClient.Login(suite.DockerRegistryHost,
		LoginOptBasicAuth(testUsername, testPassword),
		LoginOptTLSClientConfig(tlsCert, tlsKey, tlsCA))
	suite.Nil(err, "no error logging into registry with good credentials")
}

func (suite *TLSRegistryClientTestSuite) Test_1_Push() {
	testPush(&suite.TestSuite)
}

func (suite *TLSRegistryClientTestSuite) Test_2_Pull() {
	testPull(&suite.TestSuite)
}

func (suite *TLSRegistryClientTestSuite) Test_3_Tags() {
	testTags(&suite.TestSuite)
}

func (suite *TLSRegistryClientTestSuite) Test_4_Logout() {
	err := suite.RegistryClient.Logout("this-host-aint-real:5000")
	if err != nil {
		// credential backend for mac generates an error
		suite.NotNil(err, "failed to delete the credential for this-host-aint-real:5000")
	}

	err = suite.RegistryClient.Logout(suite.DockerRegistryHost)
	suite.Nil(err, "no error logging out of registry")
}

func TestTLSRegistryClientTestSuite(t *testing.T) {
	suite.Run(t, new(TLSRegistryClientTestSuite))
}


Review comment (bug):
small suggestion here, 
what you think about, that instead of using, `suite.Nil(err, "error loading x509 key pair")`, which records the failure but continues execution of the test, we use `suite.Require().NoError(err)`, which would record the failure and then aborts the test immediately (like t.Fatal).

This way, if setup fails, the test exits immediately, because there’s maybe no point testing login when the TLS config can’t even be built

What do you think ? 

Code after:
func (suite *TLSRegistryClientTestSuite) TearDownSuite() {
	teardown(&suite.TestSuite)
	os.RemoveAll(suite.WorkspaceDir)
}

func (suite *TLSRegistryClientTestSuite) Test_0_Login() {
	err := suite.RegistryClient.Login(suite.DockerRegistryHost,
		LoginOptBasicAuth("badverybad", "ohsobad"),
		LoginOptTLSClientConfig(tlsCert, tlsKey, tlsCA))
	suite.NotNil(err, "error logging into registry with bad credentials")

	err = suite.RegistryClient.Login(suite.DockerRegistryHost,
		LoginOptBasicAuth(testUsername, testPassword),
		LoginOptTLSClientConfig(tlsCert, tlsKey, tlsCA))
	suite.Nil(err, "no error logging into registry with good credentials")
}

func (suite *TLSRegistryClientTestSuite) Test_1_Login() {
	err := suite.RegistryClient.Login(suite.DockerRegistryHost,
		LoginOptBasicAuth("badverybad", "ohsobad"),
		LoginOptTLSClientConfigFromConfig(&tls.Config{}))
	suite.NotNil(err, "error logging into registry with bad credentials")

	// Create a *tls.Config from tlsCert, tlsKey, and tlsCA.
	cert, err := tls.LoadX509KeyPair(tlsCert, tlsKey)
	suite.Nil(err, "error loading x509 key pair")
	rootCAs := x509.NewCertPool()
	caCert, err := os.ReadFile(tlsCA)
	suite.Nil(err, "error reading CA certificate")
	rootCAs.AppendCertsFromPEM(caCert)
	conf := &tls.Config{
		Certificates: []tls.Certificate{cert},
		RootCAs:      rootCAs,
	}

	err = suite.RegistryClient.Login(suite.DockerRegistryHost,
		LoginOptBasicAuth(testUsername, testPassword),
		LoginOptTLSClientConfigFromConfig(conf))
	suite.Nil(err, "no error logging into registry with good credentials")
}

func (suite *TLSRegistryClientTestSuite) Test_1_Push() {
	testPush(&suite.TestSuite)
}

func (suite *TLSRegistryClientTestSuite) Test_2_Pull() {
	testPull(&suite.TestSuite)
}

func (suite *TLSRegistryClientTestSuite) Test_3_Tags() {
	testTags(&suite.TestSuite)