backstage

Форк
0
/
TemplateEntityV1beta3.test.ts 
184 строки · 5.3 Кб
1
/*
2
 * Copyright 2020 The Backstage Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
import { entityKindSchemaValidator } from '@backstage/catalog-model';
18
import type {
19
  TemplateEntityV1beta3,
20
  TemplateParametersV1beta3,
21
} from './TemplateEntityV1beta3';
22
import schema from './Template.v1beta3.schema.json';
23

24
const validator = entityKindSchemaValidator(schema);
25

26
describe('templateEntityV1beta3Validator', () => {
27
  let entity: TemplateEntityV1beta3;
28

29
  beforeEach(() => {
30
    entity = {
31
      apiVersion: 'scaffolder.backstage.io/v1beta3',
32
      kind: 'Template',
33
      metadata: {
34
        name: 'test',
35
      },
36
      spec: {
37
        type: 'website',
38
        owner: 'team-b',
39
        parameters: {
40
          required: ['owner'],
41
          'backstage:permissions': { tags: ['one', 'two'] },
42
          properties: {
43
            owner: {
44
              type: 'string',
45
              title: 'Owner',
46
              description: 'Who is going to own this component',
47
            },
48
          },
49
        },
50
        steps: [
51
          {
52
            id: 'fetch',
53
            name: 'Fetch',
54
            action: 'fetch:plan',
55
            input: {
56
              url: './template',
57
            },
58
            if: '${{ parameters.owner }}',
59
            'backstage:permissions': {
60
              tags: ['one', 'two'],
61
            },
62
          },
63
        ],
64
        output: {
65
          fetchUrl: '${{ steps.fetch.output.targetUrl }}',
66
        },
67
      },
68
    };
69
  });
70

71
  it('happy path: accepts valid data', async () => {
72
    expect(validator(entity)).toBe(entity);
73
  });
74

75
  it('ignores unknown apiVersion', async () => {
76
    (entity as any).apiVersion = 'backstage.io/v1beta0';
77
    expect(validator(entity)).toBe(false);
78
  });
79

80
  it('ignores unknown kind', async () => {
81
    (entity as any).kind = 'Wizard';
82
    expect(validator(entity)).toBe(false);
83
  });
84

85
  it('rejects missing type', async () => {
86
    delete (entity as any).spec.type;
87
    expect(() => validator(entity)).toThrow(/type/);
88
  });
89

90
  it('accepts any other type', async () => {
91
    (entity as any).spec.type = 'hallo';
92
    expect(validator(entity)).toBe(entity);
93
  });
94

95
  it('accepts missing parameters', async () => {
96
    delete (entity as any).spec.parameters;
97
    expect(validator(entity)).toBe(entity);
98
  });
99

100
  it('accepts missing outputs', async () => {
101
    delete (entity as any).spec.outputs;
102
    expect(validator(entity)).toBe(entity);
103
  });
104

105
  it('rejects empty type', async () => {
106
    (entity as any).spec.type = '';
107
    expect(() => validator(entity)).toThrow(/type/);
108
  });
109

110
  it('rejects missing steps', async () => {
111
    delete (entity as any).spec.steps;
112
    expect(() => validator(entity)).toThrow(/steps/);
113
  });
114

115
  it('accepts step with missing id', async () => {
116
    delete (entity as any).spec.steps[0].id;
117
    expect(validator(entity)).toBe(entity);
118
  });
119

120
  it('accepts step with missing name', async () => {
121
    delete (entity as any).spec.steps[0].name;
122
    expect(validator(entity)).toBe(entity);
123
  });
124

125
  it('rejects step with missing action', async () => {
126
    delete (entity as any).spec.steps[0].action;
127
    expect(() => validator(entity)).toThrow(/action/);
128
  });
129

130
  it('accepts missing owner', async () => {
131
    delete (entity as any).spec.owner;
132
    expect(validator(entity)).toBe(entity);
133
  });
134

135
  it('rejects empty owner', async () => {
136
    (entity as any).spec.owner = '';
137
    expect(() => validator(entity)).toThrow(/owner/);
138
  });
139

140
  it('rejects wrong type owner', async () => {
141
    (entity as any).spec.owner = 5;
142
    expect(() => validator(entity)).toThrow(/owner/);
143
  });
144

145
  it('accepts missing if', async () => {
146
    delete (entity as any).spec.steps[0].if;
147
    expect(validator(entity)).toBe(entity);
148
  });
149

150
  it('accepts boolean in if', async () => {
151
    (entity as any).spec.steps[0].if = true;
152
    expect(validator(entity)).toBe(entity);
153
  });
154

155
  it('accepts empty if', async () => {
156
    (entity as any).spec.steps[0].if = '';
157
    expect(validator(entity)).toBe(entity);
158
  });
159

160
  it('rejects wrong type if', async () => {
161
    (entity as any).spec.steps[0].if = 5;
162
    expect(() => validator(entity)).toThrow(/if/);
163
  });
164

165
  it('rejects parameters with wrong backstage:permissions', async () => {
166
    (entity.spec.parameters as TemplateParametersV1beta3)[
167
      'backstage:permissions'
168
    ]!.tags = true as unknown as [];
169
    expect(() => validator(entity)).toThrow(/must be array/);
170

171
    (entity.spec.parameters as TemplateParametersV1beta3)[
172
      'backstage:permissions'
173
    ] = true as {};
174
    expect(() => validator(entity)).toThrow(/must be object/);
175
  });
176

177
  it('rejects steps with wrong backstage:permissions', async () => {
178
    entity.spec.steps[0]['backstage:permissions']!.tags = true as unknown as [];
179
    expect(() => validator(entity)).toThrow(/must be array/);
180

181
    entity.spec.steps[0]['backstage:permissions'] = true as {};
182
    expect(() => validator(entity)).toThrow(/must be object/);
183
  });
184
});
185

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.