Welcome to mirror list, hosted at ThFree Co, Russian Federation.

map_test.go « domain « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2306edefe80b7925b85646b64e4b0a449ff1e4f8 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package domain

import (
	"crypto/rand"
	"fmt"
	"io/ioutil"
	"os"
	"testing"
	"time"

	"github.com/karrick/godirwalk"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func getEntries(t *testing.T) godirwalk.Dirents {
	fis, err := godirwalk.ReadDirents(".", nil)

	require.NoError(t, err)

	return fis
}

func getEntriesForBenchmark(t *testing.B) godirwalk.Dirents {
	fis, err := godirwalk.ReadDirents(".", nil)

	require.NoError(t, err)

	return fis
}

func TestReadProjects(t *testing.T) {
	setUpTests()

	dm := make(Map)
	dm.ReadGroups("test.io", getEntries(t))

	var domains []string
	for d := range dm {
		domains = append(domains, d)
	}

	expectedDomains := []string{
		"group.test.io",
		"group.internal.test.io",
		"test.domain.com", // from config.json
		"other.domain.com",
		"domain.404.com",
		"group.404.test.io",
		"group.https-only.test.io",
		"test.my-domain.com",
		"test2.my-domain.com",
		"no.cert.com",
		"private.domain.com",
		"group.auth.test.io",
		"capitalgroup.test.io",
	}

	for _, expected := range domains {
		assert.Contains(t, domains, expected)
	}

	for _, actual := range domains {
		assert.Contains(t, expectedDomains, actual)
	}

	// Check that multiple domains in the same project are recorded faithfully
	exp1 := &domainConfig{Domain: "test.domain.com"}
	assert.Equal(t, exp1, dm["test.domain.com"].config)

	exp2 := &domainConfig{Domain: "other.domain.com", Certificate: "test", Key: "key"}
	assert.Equal(t, exp2, dm["other.domain.com"].config)

	// check subgroups
	domain, ok := dm["group.test.io"]
	require.True(t, ok, "missing group.test.io domain")
	subgroup, ok := domain.subgroups["subgroup"]
	require.True(t, ok, "missing group.test.io subgroup")
	_, ok = subgroup.projects["project"]
	require.True(t, ok, "missing project for subgrup in group.test.io domain")
}

// This write must be atomic, otherwise we cannot predict the state of the
// domain watcher goroutine. We cannot use ioutil.WriteFile because that
// has a race condition where the file is empty, which can get picked up
// by the domain watcher.
func writeRandomTimestamp(t *testing.T) {
	b := make([]byte, 10)
	n, _ := rand.Read(b)
	require.True(t, n > 0, "read some random bytes")

	temp, err := ioutil.TempFile(".", "TestWatch")
	require.NoError(t, err)
	_, err = temp.Write(b)
	require.NoError(t, err, "write to tempfile")
	require.NoError(t, temp.Close(), "close tempfile")

	require.NoError(t, os.Rename(temp.Name(), updateFile), "rename tempfile")
}

func TestWatch(t *testing.T) {
	setUpTests()

	require.NoError(t, os.RemoveAll(updateFile))

	update := make(chan Map)
	go Watch("gitlab.io", func(dm Map) {
		update <- dm
	}, time.Microsecond*50)

	defer os.Remove(updateFile)

	domains := recvTimeout(t, update)
	assert.NotNil(t, domains, "if the domains are fetched on start")

	writeRandomTimestamp(t)
	domains = recvTimeout(t, update)
	assert.NotNil(t, domains, "if the domains are updated after the creation")

	writeRandomTimestamp(t)
	domains = recvTimeout(t, update)
	assert.NotNil(t, domains, "if the domains are updated after the timestamp change")
}

func recvTimeout(t *testing.T, ch <-chan Map) Map {
	timeout := 5 * time.Second

	select {
	case dm := <-ch:
		return dm
	case <-time.After(timeout):
		t.Fatalf("timeout after %v waiting for domain update", timeout)
		return nil
	}
}

func BenchmarkReadGroups(b *testing.B) {
	testRoot, err := ioutil.TempDir("", "gitlab-pages-test")
	require.NoError(b, err)

	cwd, err := os.Getwd()
	require.NoError(b, err)

	defer func(oldWd, testWd string) {
		os.Chdir(oldWd)
		fmt.Printf("cleaning up test directory %s\n", testWd)
		os.RemoveAll(testWd)
	}(cwd, testRoot)

	require.NoError(b, os.Chdir(testRoot))

	nGroups := 10000
	b.Logf("creating fake domains directory with %d groups", nGroups)
	for i := 0; i < nGroups; i++ {
		for j := 0; j < 5; j++ {
			dir := fmt.Sprintf("%s/group-%d/project-%d", testRoot, i, j)
			require.NoError(b, os.MkdirAll(dir+"/public", 0755))

			fakeConfig := fmt.Sprintf(`{"Domains":[{"Domain":"foo.%d.%d.example.io","Certificate":"bar","Key":"baz"}]}`, i, j)
			require.NoError(b, ioutil.WriteFile(dir+"/config.json", []byte(fakeConfig), 0644))
		}
		if i%100 == 0 {
			fmt.Print(".")
		}
	}

	b.Run("ReadGroups", func(b *testing.B) {
		var dm Map
		for i := 0; i < 2; i++ {
			dm = make(Map)
			dm.ReadGroups("example.com", getEntriesForBenchmark(b))
		}
		b.Logf("found %d domains", len(dm))
	})
}