2016-12-07 07:16:53 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
2017-07-13 10:15:27 +00:00
|
|
|
"bytes"
|
2018-04-24 06:33:49 +00:00
|
|
|
"context"
|
2016-12-07 07:16:53 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-04-24 06:33:49 +00:00
|
|
|
"github.com/Azure/azure-sdk-for-go/services/containerregistry/mgmt/2017-10-01/containerregistry"
|
2016-12-07 07:16:53 +00:00
|
|
|
"github.com/Azure/go-autorest/autorest/to"
|
|
|
|
)
|
|
|
|
|
|
|
|
type fakeClient struct {
|
2018-04-24 06:33:49 +00:00
|
|
|
results []containerregistry.Registry
|
2016-12-07 07:16:53 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 06:33:49 +00:00
|
|
|
func (f *fakeClient) List(ctx context.Context) ([]containerregistry.Registry, error) {
|
2016-12-07 07:16:53 +00:00
|
|
|
return f.results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test(t *testing.T) {
|
|
|
|
configStr := `
|
|
|
|
{
|
|
|
|
"aadClientId": "foo",
|
|
|
|
"aadClientSecret": "bar"
|
|
|
|
}`
|
2018-04-24 06:33:49 +00:00
|
|
|
result := []containerregistry.Registry{
|
|
|
|
{
|
|
|
|
Name: to.StringPtr("foo"),
|
|
|
|
RegistryProperties: &containerregistry.RegistryProperties{
|
2018-07-20 08:39:31 +00:00
|
|
|
LoginServer: to.StringPtr("*.azurecr.io"),
|
2016-12-07 07:16:53 +00:00
|
|
|
},
|
2018-04-24 06:33:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: to.StringPtr("bar"),
|
|
|
|
RegistryProperties: &containerregistry.RegistryProperties{
|
2018-07-20 08:39:31 +00:00
|
|
|
LoginServer: to.StringPtr("*.azurecr.cn"),
|
2016-12-07 07:16:53 +00:00
|
|
|
},
|
2018-04-24 06:33:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: to.StringPtr("baz"),
|
|
|
|
RegistryProperties: &containerregistry.RegistryProperties{
|
2018-07-20 08:39:31 +00:00
|
|
|
LoginServer: to.StringPtr("*.azurecr.de"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: to.StringPtr("bus"),
|
|
|
|
RegistryProperties: &containerregistry.RegistryProperties{
|
|
|
|
LoginServer: to.StringPtr("*.azurecr.us"),
|
2016-12-07 07:16:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fakeClient := &fakeClient{
|
|
|
|
results: result,
|
|
|
|
}
|
|
|
|
|
|
|
|
provider := &acrProvider{
|
|
|
|
registryClient: fakeClient,
|
|
|
|
}
|
2017-07-13 10:15:27 +00:00
|
|
|
provider.loadConfig(bytes.NewBufferString(configStr))
|
2016-12-07 07:16:53 +00:00
|
|
|
|
|
|
|
creds := provider.Provide()
|
|
|
|
|
2018-04-24 06:33:49 +00:00
|
|
|
if len(creds) != len(result) {
|
|
|
|
t.Errorf("Unexpected list: %v, expected length %d", creds, len(result))
|
2016-12-07 07:16:53 +00:00
|
|
|
}
|
|
|
|
for _, cred := range creds {
|
|
|
|
if cred.Username != "foo" {
|
|
|
|
t.Errorf("expected 'foo' for username, saw: %v", cred.Username)
|
|
|
|
}
|
|
|
|
if cred.Password != "bar" {
|
|
|
|
t.Errorf("expected 'bar' for password, saw: %v", cred.Username)
|
|
|
|
}
|
|
|
|
}
|
2018-04-24 06:33:49 +00:00
|
|
|
for _, val := range result {
|
2017-01-19 12:22:12 +00:00
|
|
|
registryName := getLoginServer(val)
|
2016-12-07 07:16:53 +00:00
|
|
|
if _, found := creds[registryName]; !found {
|
|
|
|
t.Errorf("Missing expected registry: %s", registryName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|