-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathdomains_test.go
109 lines (88 loc) · 2.8 KB
/
domains_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package dao
import (
"context"
"sync"
"testing"
"github.com/content-services/content-sources-backend/pkg/candlepin_client"
"github.com/content-services/content-sources-backend/pkg/db"
"github.com/content-services/content-sources-backend/pkg/models"
"github.com/content-services/content-sources-backend/pkg/pulp_client"
uuid2 "github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type DomainSuite struct {
*DaoSuite
}
func TestDomainSuite(t *testing.T) {
m := DaoSuite{}
r := DomainSuite{DaoSuite: &m}
suite.Run(t, &r)
}
func (ds *DomainSuite) TestCreate() {
orgId := "DomainSuiteTest"
dd := domainDaoImpl{db: ds.tx}
name, err := dd.Create(context.Background(), orgId)
assert.NoError(ds.T(), err)
assert.NotEmpty(ds.T(), name)
// try again
name, err = dd.Create(context.Background(), orgId)
assert.NoError(ds.T(), err)
assert.NotEmpty(ds.T(), name)
name, err = dd.Fetch(context.Background(), orgId)
assert.NoError(ds.T(), err)
assert.NotEmpty(ds.T(), name)
}
func (ds *DomainSuite) TestList() {
dd := domainDaoImpl{db: ds.tx}
numOrgs := 5
var existingOrgs []models.Domain
res := ds.tx.Model(&models.Domain{}).Find(&existingOrgs)
assert.NoError(ds.T(), res.Error)
newOrgs := make([]models.Domain, numOrgs)
for i := 0; i < numOrgs; i++ {
orgID := randomHexadecimal(10)
name, err := dd.Create(context.Background(), orgID)
assert.NoError(ds.T(), err)
assert.NotEmpty(ds.T(), name)
newOrgs[i].OrgId = orgID
newOrgs[i].DomainName = name
}
expectedOrgs := append(newOrgs, existingOrgs...)
expectedCount := len(newOrgs) + len(existingOrgs)
orgs, err := dd.List(context.Background())
assert.NoError(ds.T(), err)
assert.Equal(ds.T(), expectedCount, len(orgs))
assert.ElementsMatch(ds.T(), expectedOrgs, orgs)
}
func TestConcurrentGetDomainName(t *testing.T) {
// Note, this test does not use a transaction, as it fails when multiple go routines are trying to do that
orgId := uuid2.NewString()
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
dDao := GetDomainDao(db.DB, pulp_client.NewMockPulpGlobalClient(t), candlepin_client.NewCandlepinClient())
dName, err := dDao.FetchOrCreateDomain(context.Background(), orgId)
assert.NoError(t, err)
assert.NotEmpty(t, dName)
wg.Done()
}()
}
wg.Wait()
}
func (ds *DomainSuite) TestDelete() {
orgId := "DomainSuiteTest"
dd := domainDaoImpl{db: ds.tx}
name, err := dd.Create(context.Background(), orgId)
assert.NoError(ds.T(), err)
assert.NotEmpty(ds.T(), name)
name, err = dd.Fetch(context.Background(), orgId)
assert.NoError(ds.T(), err)
assert.NotEmpty(ds.T(), name)
err = dd.Delete(context.Background(), orgId, name)
assert.NoError(ds.T(), err)
name, err = dd.Fetch(context.Background(), orgId)
assert.NoError(ds.T(), err)
assert.Empty(ds.T(), name)
}