2017-07-19 22:54:35 +00:00
|
|
|
package otc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2018-06-11 15:32:50 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-10-12 17:29:18 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-06-11 15:32:50 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2017-07-19 22:54:35 +00:00
|
|
|
)
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
type TestSuite struct {
|
2017-07-19 22:54:35 +00:00
|
|
|
suite.Suite
|
|
|
|
Mock *DNSMock
|
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func (s *TestSuite) TearDownSuite() {
|
2017-07-19 22:54:35 +00:00
|
|
|
s.Mock.ShutdownServer()
|
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func (s *TestSuite) SetupTest() {
|
2017-07-19 22:54:35 +00:00
|
|
|
s.Mock = NewDNSMock(s.T())
|
|
|
|
s.Mock.Setup()
|
|
|
|
s.Mock.HandleAuthSuccessfully()
|
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(TestSuite))
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func (s *TestSuite) createDNSProvider() (*DNSProvider, error) {
|
2017-07-19 22:54:35 +00:00
|
|
|
url := fmt.Sprintf("%s/v3/auth/token", s.Mock.Server.URL)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.UserName = fakeOTCUserName
|
|
|
|
config.Password = fakeOTCPassword
|
|
|
|
config.DomainName = fakeOTCDomainName
|
|
|
|
config.ProjectName = fakeOTCProjectName
|
|
|
|
config.IdentityEndpoint = url
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func (s *TestSuite) TestLogin() {
|
|
|
|
provider, err := s.createDNSProvider()
|
|
|
|
require.NoError(s.T(), err)
|
|
|
|
|
|
|
|
err = provider.loginRequest()
|
|
|
|
require.NoError(s.T(), err)
|
|
|
|
assert.Equal(s.T(), provider.baseURL, fmt.Sprintf("%s/v2", s.Mock.Server.URL))
|
|
|
|
assert.Equal(s.T(), fakeOTCToken, provider.token)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TestSuite) TestLoginEnv() {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer os.Clearenv()
|
|
|
|
|
2017-07-19 22:54:35 +00:00
|
|
|
os.Setenv("OTC_DOMAIN_NAME", "unittest1")
|
|
|
|
os.Setenv("OTC_USER_NAME", "unittest2")
|
|
|
|
os.Setenv("OTC_PASSWORD", "unittest3")
|
|
|
|
os.Setenv("OTC_PROJECT_NAME", "unittest4")
|
|
|
|
os.Setenv("OTC_IDENTITY_ENDPOINT", "unittest5")
|
|
|
|
|
|
|
|
provider, err := NewDNSProvider()
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(s.T(), err)
|
2018-09-15 17:07:24 +00:00
|
|
|
assert.Equal(s.T(), provider.config.DomainName, "unittest1")
|
|
|
|
assert.Equal(s.T(), provider.config.UserName, "unittest2")
|
|
|
|
assert.Equal(s.T(), provider.config.Password, "unittest3")
|
|
|
|
assert.Equal(s.T(), provider.config.ProjectName, "unittest4")
|
|
|
|
assert.Equal(s.T(), provider.config.IdentityEndpoint, "unittest5")
|
2017-07-19 22:54:35 +00:00
|
|
|
|
|
|
|
os.Setenv("OTC_IDENTITY_ENDPOINT", "")
|
|
|
|
|
|
|
|
provider, err = NewDNSProvider()
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(s.T(), err)
|
2018-09-15 17:07:24 +00:00
|
|
|
assert.Equal(s.T(), provider.config.IdentityEndpoint, "https://iam.eu-de.otc.t-systems.com:443/v3/auth/tokens")
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func (s *TestSuite) TestLoginEnvEmpty() {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer os.Clearenv()
|
2017-07-19 22:54:35 +00:00
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
_, err := NewDNSProvider()
|
2018-09-15 17:07:24 +00:00
|
|
|
assert.EqualError(s.T(), err, "otc: some credentials information are missing: OTC_DOMAIN_NAME,OTC_USER_NAME,OTC_PASSWORD,OTC_PROJECT_NAME")
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func (s *TestSuite) TestDNSProvider_Present() {
|
|
|
|
s.Mock.HandleListZonesSuccessfully()
|
|
|
|
s.Mock.HandleListRecordsetsSuccessfully()
|
|
|
|
|
|
|
|
provider, err := s.createDNSProvider()
|
|
|
|
require.NoError(s.T(), err)
|
2017-07-19 22:54:35 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
err = provider.Present("example.com", "", "foobar")
|
|
|
|
require.NoError(s.T(), err)
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func (s *TestSuite) TestDNSProvider_Present_EmptyZone() {
|
2017-07-19 22:54:35 +00:00
|
|
|
s.Mock.HandleListZonesEmpty()
|
|
|
|
s.Mock.HandleListRecordsetsSuccessfully()
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
provider, err := s.createDNSProvider()
|
|
|
|
require.NoError(s.T(), err)
|
2017-07-19 22:54:35 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
err = provider.Present("example.com", "", "foobar")
|
2017-07-19 22:54:35 +00:00
|
|
|
assert.NotNil(s.T(), err)
|
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func (s *TestSuite) TestDNSProvider_CleanUp() {
|
2017-07-19 22:54:35 +00:00
|
|
|
s.Mock.HandleListZonesSuccessfully()
|
|
|
|
s.Mock.HandleListRecordsetsSuccessfully()
|
2018-10-12 17:29:18 +00:00
|
|
|
s.Mock.HandleDeleteRecordsetsSuccessfully()
|
|
|
|
|
|
|
|
provider, err := s.createDNSProvider()
|
|
|
|
require.NoError(s.T(), err)
|
2017-07-19 22:54:35 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
err = provider.CleanUp("example.com", "", "foobar")
|
|
|
|
require.NoError(s.T(), err)
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func (s *TestSuite) TestDNSProvider_CleanUp_EmptyRecordset() {
|
2017-07-19 22:54:35 +00:00
|
|
|
s.Mock.HandleListZonesSuccessfully()
|
2018-10-12 17:29:18 +00:00
|
|
|
s.Mock.HandleListRecordsetsEmpty()
|
|
|
|
|
|
|
|
provider, err := s.createDNSProvider()
|
|
|
|
require.NoError(s.T(), err)
|
2017-07-19 22:54:35 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
err = provider.CleanUp("example.com", "", "foobar")
|
|
|
|
require.Error(s.T(), err)
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|