...
1
2
3
4 package create
5
6 import (
7 "regexp"
8 "strings"
9 "testing"
10
11 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
12 testcontroller "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/controller"
13 testgcp "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/gcp"
14
15 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
16 )
17
18 var matchEverythingRegex = regexp.MustCompile(".*")
19
20 func TestNames(t *testing.T) {
21 project := testgcp.GetDefaultProject(t)
22 samples := loadSamplesOntoUnstructs(t, matchEverythingRegex, project)
23 for _, s := range samples {
24 for _, r := range s.Resources {
25 validateResourceName(t, s.Name, r)
26 }
27 }
28 }
29
30 func TestLicenses(t *testing.T) {
31 beginsWithCopyrightRegex := regexp.MustCompile("^# Copyright 20[0-9]{2} Google LLC.*")
32 samples := mapSampleNamesToFilePaths(t, matchEverythingRegex)
33 for sampleName, files := range samples {
34 for _, f := range files {
35 b := testcontroller.ReadFileToBytes(t, f)
36 if !beginsWithCopyrightRegex.Match(b) {
37 t.Errorf("file '%v' in sample '%v' does not contain a license header", f, sampleName)
38 }
39 }
40 }
41 }
42
43 func validateResourceName(t *testing.T, sampleName string, u *unstructured.Unstructured) {
44
45
46
47
48 if u.GetKind() == "Service" {
49 if strings.HasSuffix(u.GetName(), ".com") {
50 t.Fatalf("invalid metadata.name value '%v' for Service resource in sample '%v': "+
51 "use %v instead of metadata.name to specify the service to enable",
52 u.GetName(), sampleName, k8s.ResourceIDFieldPath)
53 }
54 }
55
56 allowedNameFragments := []string{"sample", "dep"}
57 for _, nf := range allowedNameFragments {
58 if strings.Contains(u.GetName(), nf) {
59 return
60 }
61 }
62
63
64
65 t.Errorf("invalid metadata.name value '%v' in sample '%v': must contain one of {%v} to be valid",
66 u.GetName(), sampleName, strings.Join(allowedNameFragments, ","))
67 }
68
View as plain text