-
Notifications
You must be signed in to change notification settings - Fork 57
/
testStrainConfig.js
196 lines (171 loc) · 6.28 KB
/
testStrainConfig.js
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright 2018 Adobe. All rights reserved.
* This file is licensed to you 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 REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/* eslint-env mocha */
const assert = require('assert');
const fs = require('fs-extra');
const path = require('path');
const { HelixConfig } = require('@adobe/helix-shared');
const strainconfig = require('../src/strain-config-utils');
const proxyfile = path.resolve(__dirname, 'fixtures/proxystrains.yaml');
describe('Strain Config', () => {
const config = fs.readFileSync(path.resolve(__dirname, 'fixtures/config.yaml'), 'utf-8');
const invalid = fs.readFileSync(path.resolve(__dirname, 'fixtures/invalid.yaml'), 'utf-8');
it('config can be parsed', () => {
assert.equal(3, strainconfig.load(config).length);
});
it('config with proxy strains can be parsed', async () => {
const myconfig = await new HelixConfig().withConfigPath(proxyfile).init();
const mystrains = myconfig.strains;
assert.equal(Array.from(mystrains.entries()).length, 3);
});
it('invalid config does not throw errors', () => {
assert.equal(2, strainconfig.load(invalid).length);
});
it('New strains can be appended', () => {
const mystrains = strainconfig.load(config);
const newstrains = strainconfig.append(
strainconfig.append(mystrains, {
name: 'xdm-address',
content: { owner: 'adobe', repo: 'xdm', ref: 'address' },
}),
{ content: { owner: 'adobe', repo: 'xdm', ref: 'appendanother' } },
);
assert.equal(newstrains.length, 5);
});
it('New strains override existing strains with same name', () => {
const mystrains = strainconfig.load(config);
const newstrains = strainconfig.append(mystrains, {
name: 'xdm',
content: { owner: 'adobe', repo: 'xdm', ref: 'address' },
});
assert.equal(newstrains.length, 3);
});
});
describe('Generated names are stable', () => {
it('name() generates stable IDs', () => {
assert.deepEqual(
strainconfig.name({
content: { owner: 'foo', repo: 'bar' },
}),
strainconfig.name({
content: { repo: 'bar', owner: 'foo' },
}),
);
});
});
describe('Invalid values are rejected or fixed on the fly', () => {
const buggy = fs.readFileSync(path.resolve(__dirname, 'fixtures/buggy.yaml'), 'utf-8');
it('action names without a default path get a default path', () => {
const mystrains = strainconfig.load(buggy);
assert.equal('/trieloff/default/git-github-com-adobe-helix-cli-git--dirty', mystrains[0].code);
});
it('invalid code paths get ignored', () => {
const mystrains = strainconfig.load(buggy);
assert.equal(1, mystrains.length);
});
});
describe('Appending works without errors', () => {
it('Appending to an empty file works', () => {
const oldstrains = strainconfig.load('');
const strain = {
code: '/foobar/default/local--foobar--dirty',
content: {
repo: 'foo',
ref: 'master',
owner: 'null',
},
};
const newstrains = strainconfig.append(oldstrains, strain);
assert.equal(1, newstrains.length);
});
it('Appending to an existing file works', () => {
const config = fs.readFileSync(path.resolve(__dirname, 'fixtures/config.yaml'), 'utf-8');
const oldstrains = strainconfig.load(config);
const strain = {
code: '/foobar/default/local--foobar--dirty',
content: {
repo: 'foo',
ref: 'master',
owner: 'null',
},
};
const newstrains = strainconfig.append(oldstrains, strain);
assert.equal(4, newstrains.length);
});
});
describe('Understands Proxy Strains', () => {
it('proxies() returns all proxy strains #unit', async () => {
const config = await new HelixConfig().withConfigPath(proxyfile).init();
const mystrains = config.strains;
assert.equal(strainconfig.proxies(mystrains).length, 1);
});
it('addbackends() returns new backend definitions #unit', async () => {
const config = await new HelixConfig().withConfigPath(proxyfile).init();
const mystrains = config.strains;
const mybackends = strainconfig.addbackends(mystrains);
assert.deepEqual(Object.keys(mybackends), ['Proxy1921681001f402']);
assert.deepEqual(strainconfig.addbackends(mystrains), {
Proxy1921681001f402: {
address: '192.168.100.1',
between_bytes_timeout: 10000,
connect_timeout: 1000,
error_threshold: 0,
first_byte_timeout: 15000,
hostname: '192.168.100.1',
max_conn: 200,
name: 'Proxy1921681001f402',
port: 4503,
shield: 'iad-va-us',
ssl_cert_hostname: '192.168.100.1',
use_ssl: true,
weight: 100,
},
});
});
it('addbackends() handles empty lists well #unit', () => {
const mystrains = [];
assert.deepStrictEqual(strainconfig.addbackends(mystrains), {
});
});
it('addbackends() keeps existing backends in place #unit', () => {
const mystrains = [];
const backends = {
foo: "I'm a backend",
};
assert.deepStrictEqual(strainconfig.addbackends(mystrains, backends), backends);
});
it('addbackends() does not overwrite backends #unit', async () => {
const config = await new HelixConfig().withConfigPath(proxyfile).init();
const mystrains = config.strains;
const backends = {
foo: "I'm a backend",
};
assert.deepStrictEqual(strainconfig.addbackends(mystrains, backends), {
Proxy1921681001f402: {
address: '192.168.100.1',
between_bytes_timeout: 10000,
connect_timeout: 1000,
error_threshold: 0,
first_byte_timeout: 15000,
hostname: '192.168.100.1',
max_conn: 200,
name: 'Proxy1921681001f402',
port: 4503,
shield: 'iad-va-us',
ssl_cert_hostname: '192.168.100.1',
use_ssl: true,
weight: 100,
},
foo: "I'm a backend",
});
});
});