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}
33
34// TestMicrogenConfigs validates config entries.
35// We expect entries here to have reasonable production settings, whereas
36// the low level tooling is more permissive in the face of ambiguity.
37//
38// TODO: we should be able to do more substantial validation of config entries
39// given sufficient setup.  Consider fetching https://github.com/googleapis/googleapis
40// to ensure referenced entries are present.
41func TestMicrogenConfigs(t *testing.T) {
42	for k, entry := range microgenGapicConfigs {
43		if entry.importPath == "" {
44			t.Errorf("config %q (#%d) expected non-empty importPath", entry.inputDirectoryPath, k)
45		}
46		importParts := strings.Split(entry.importPath, "/")
47		v := importParts[len(importParts)-1]
48		if !strings.HasPrefix(v, "apiv") && !apivExceptions[entry.importPath] {
49			t.Errorf("config %q (#%d) expected the last part of import path %q to start with \"apiv\"", entry.inputDirectoryPath, k, entry.importPath)
50		}
51		if entry.inputDirectoryPath == "" {
52			t.Errorf("config %q (#%d) expected non-empty inputDirectoryPath field", entry.importPath, k)
53		}
54		if entry.pkg == "" {
55			t.Errorf("config %q (#%d) expected non-empty pkg field", entry.importPath, k)
56		}
57		// TODO: Consider if we want to allow this at a later point in time.  If this
58		// isn't supplied the config is technically valid, but the generated library
59		// won't include features such as retry policies.
60		if entry.gRPCServiceConfigPath == "" {
61			t.Errorf("config %q (#%d) expected non-empty gRPCServiceConfigPath", entry.importPath, k)
62		}
63		if entry.apiServiceConfigPath == "" {
64			t.Errorf("config %q (#%d) expected non-empty apiServiceConfigPath", entry.importPath, k)
65		}
66		// Internally, an empty release level means "ga" to the underlying tool, but we
67		// want to be explicit in this configuration.
68		if entry.releaseLevel == "" {
69			t.Errorf("config %q (#%d) expected non-empty releaseLevel field", entry.importPath, k)
70		} else if !allowedReleaseLevels[entry.releaseLevel] {
71			t.Errorf("config %q (#%d) invalid releaseLevel: %q", entry.importPath, k, entry.releaseLevel)
72		}
73	}
74}
75