1// Copyright 2020 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package generator
16
17import (
18	"strings"
19	"testing"
20)
21
22var allowedReleaseLevels = map[string]bool{
23	"alpha": true,
24	"beta":  true,
25	"ga":    true,
26}
27
28var apivExceptions = map[string]bool{
29	"cloud.google.com/go/longrunning/autogen":   true,
30	"cloud.google.com/go/firestore/apiv1/admin": true,
31	"cloud.google.com/go/cloudbuild/apiv1/v2":   true,
32	"cloud.google.com/go/monitoring/apiv3/v2":   true,
33}
34
35// TestMicrogenConfigs validates config entries.
36// We expect entries here to have reasonable production settings, whereas
37// the low level tooling is more permissive in the face of ambiguity.
38//
39// TODO: we should be able to do more substantial validation of config entries
40// given sufficient setup.  Consider fetching https://github.com/googleapis/googleapis
41// to ensure referenced entries are present.
42func TestMicrogenConfigs(t *testing.T) {
43	for k, entry := range microgenGapicConfigs {
44		if entry.importPath == "" {
45			t.Errorf("config %q (#%d) expected non-empty importPath", entry.inputDirectoryPath, k)
46		}
47		importParts := strings.Split(entry.importPath, "/")
48		v := importParts[len(importParts)-1]
49		if !strings.HasPrefix(v, "apiv") && !apivExceptions[entry.importPath] {
50			t.Errorf("config %q (#%d) expected the last part of import path %q to start with \"apiv\"", entry.inputDirectoryPath, k, entry.importPath)
51		}
52		if entry.inputDirectoryPath == "" {
53			t.Errorf("config %q (#%d) expected non-empty inputDirectoryPath field", entry.importPath, k)
54		}
55		if entry.pkg == "" {
56			t.Errorf("config %q (#%d) expected non-empty pkg field", entry.importPath, k)
57		}
58		if entry.apiServiceConfigPath == "" {
59			t.Errorf("config %q (#%d) expected non-empty apiServiceConfigPath", entry.importPath, k)
60		}
61		if p := entry.apiServiceConfigPath; p != "" && strings.HasSuffix(p, "gapic.yaml") {
62			t.Errorf("config %q (#%d) should not use GAPIC yaml for apiServiceConfigPath", entry.importPath, k)
63		}
64		// Internally, an empty release level means "ga" to the underlying tool, but we
65		// want to be explicit in this configuration.
66		if entry.releaseLevel == "" {
67			t.Errorf("config %q (#%d) expected non-empty releaseLevel field", entry.importPath, k)
68		} else if !allowedReleaseLevels[entry.releaseLevel] {
69			t.Errorf("config %q (#%d) invalid releaseLevel: %q", entry.importPath, k, entry.releaseLevel)
70		}
71	}
72}
73