Strengthen unit tests for cluster ID (#3972)

This commit is contained in:
Cecile Robert-Michon 2018-10-08 10:20:15 -07:00 коммит произвёл Jack Francis
Родитель 004b7f3c8d
Коммит 833fe44015
2 изменённых файлов: 55 добавлений и 32 удалений

Просмотреть файл

@ -8,6 +8,7 @@ import (
neturl "net/url"
"strconv"
"strings"
"sync"
"github.com/Azure/acs-engine/pkg/api/agentPoolOnlyApi/v20170831"
"github.com/Azure/acs-engine/pkg/api/agentPoolOnlyApi/v20180331"
@ -886,6 +887,7 @@ func (p *Properties) AreAgentProfilesCustomVNET() bool {
// GetClusterID creates a unique 8 string cluster ID.
func (p *Properties) GetClusterID() string {
var mutex = &sync.Mutex{}
if p.ClusterID == "" {
uniqueNameSuffixSize := 8
// the name suffix uniquely identifies the cluster and is generated off a hash
@ -899,7 +901,9 @@ func (p *Properties) GetClusterID() string {
h.Write([]byte(p.AgentPoolProfiles[0].Name))
}
rand.Seed(int64(h.Sum64()))
mutex.Lock()
p.ClusterID = fmt.Sprintf("%08d", rand.Uint32())[:uniqueNameSuffixSize]
mutex.Unlock()
}
return p.ClusterID
}

Просмотреть файл

@ -2,7 +2,6 @@ package api
import (
"log"
"regexp"
"testing"
"github.com/Azure/acs-engine/pkg/helpers"
@ -1650,42 +1649,62 @@ func TestAreAgentProfilesCustomVNET(t *testing.T) {
}
func TestGenerateClusterID(t *testing.T) {
p := &Properties{
AgentPoolProfiles: []*AgentPoolProfile{
{
Name: "foo_agent",
tests := []struct {
name string
properties *Properties
expectedClusterID string
}{
{
name: "From Master Profile",
properties: &Properties{
MasterProfile: &MasterProfile{
DNSPrefix: "foo_master",
},
AgentPoolProfiles: []*AgentPoolProfile{
{
Name: "foo_agent0",
},
},
},
expectedClusterID: "24569115",
},
{
name: "From Hosted Master Profile",
properties: &Properties{
HostedMasterProfile: &HostedMasterProfile{
DNSPrefix: "foo_hosted_master",
},
AgentPoolProfiles: []*AgentPoolProfile{
{
Name: "foo_agent1",
},
},
},
expectedClusterID: "42761241",
},
{
name: "No Master Profile",
properties: &Properties{
AgentPoolProfiles: []*AgentPoolProfile{
{
Name: "foo_agent2",
},
},
},
expectedClusterID: "11729301",
},
}
clusterID := p.GetClusterID()
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
actual := test.properties.GetClusterID()
r, err := regexp.Compile("[0-9]{8}")
if err != nil {
t.Errorf("unexpected error while parsing regex : %s", err.Error())
}
if !r.MatchString(clusterID) {
t.Fatal("ClusterID should be an 8 digit integer string")
}
p = &Properties{
HostedMasterProfile: &HostedMasterProfile{
DNSPrefix: "foodnsprefx",
},
}
clusterID = p.GetClusterID()
r, err = regexp.Compile("[0-9]{8}")
if err != nil {
t.Errorf("unexpected error while parsing regex : %s", err.Error())
}
if !r.MatchString(clusterID) {
t.Fatal("ClusterID should be an 8 digit integer string")
if actual != test.expectedClusterID {
t.Errorf("expected cluster ID %s, but got %s", test.expectedClusterID, actual)
}
})
}
}