Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add initial testcase #4

Merged
merged 2 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions pkg/detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ func NewDetector(attrs ResourceAttributesFetcher) *Detector {

// CloudPlatform returns the platform on which this program is running.
func (d *Detector) CloudPlatform() Platform {
d.attrs = fetcher

switch {
case d.isGCE(): // TODO(zchee): not implemented yet.
return UnknownPlatform // GCE
Expand Down
82 changes: 82 additions & 0 deletions pkg/detector/detector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2022 The zap-cloudlogging Authors
// SPDX-License-Identifier: BSD-3-Clause

package detector

import (
"testing"
)

func TestCloudPlatformAppEngineStandard(t *testing.T) {
d := NewDetector(&fakeResourceGetter{
envVars: map[string]string{
EnvAppEngineFlexService: "foo",
EnvAppEngineEnv: "standard",
},
})
platform := d.CloudPlatform()

if platform != AppEngineStandard {
t.Fatalf("got %d but want %d", platform, AppEngineStandard)
}
}

func TestCloudPlatformAppEngineFlex(t *testing.T) {
d := NewDetector(&fakeResourceGetter{
envVars: map[string]string{
EnvAppEngineFlexService: "foo",
EnvAppEngineFlexVersion: "001",
EnvAppEngineFlexInstance: "foo",
},
})
platform := d.CloudPlatform()

if platform != AppEngineFlex {
t.Fatalf("got %d but want %d", platform, AppEngineFlex)
}
}

func TestCloudPlatformCloudRun(t *testing.T) {
d := NewDetector(&fakeResourceGetter{
envVars: map[string]string{
EnvCloudRunService: "foo",
EnvCloudRunRevision: "foo-001",
EnvCloudRunConfig: "foo",
},
})
platform := d.CloudPlatform()

if platform != CloudRun {
t.Fatalf("got %d but want %d", platform, CloudRun)
}
}

func TestCloudPlatformCloudRunJobs(t *testing.T) {
d := NewDetector(&fakeResourceGetter{
envVars: map[string]string{
EnvCloudRunJobsService: "foo",
EnvCloudRunJobsRevision: "foo-001",
},
})
platform := d.CloudPlatform()

if platform != CloudRunJobs {
t.Fatalf("got %d but want %d", platform, CloudRunJobs)
}
}

func TestCloudPlatformCloudFunctions(t *testing.T) {
d := NewDetector(&fakeResourceGetter{
envVars: map[string]string{
EnvCloudFunctionsTarget: "foo",
EnvCloudFunctionsSignatureType: "foo",
EnvCloudFunctionsKService: "foo",
EnvCloudFunctionsKRevision: "foo",
},
})
platform := d.CloudPlatform()

if platform != CloudFunctions {
t.Fatalf("got %d but want %d", platform, CloudFunctions)
}
}
38 changes: 38 additions & 0 deletions pkg/detector/fake_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2022 The zap-cloudlogging Authors
// SPDX-License-Identifier: BSD-3-Clause

package detector

// fakeResourceGetter mocks internal.ResourceAtttributesGetter interface to retrieve env vars and metadata
type fakeResourceGetter struct {
envVars map[string]string
metaVars map[string]string
fsPaths map[string]string
}

func (g *fakeResourceGetter) EnvVar(name string) string {
if g.envVars != nil {
if v, ok := g.envVars[name]; ok {
return v
}
}
return ""
}

func (g *fakeResourceGetter) Metadata(path string) string {
if g.metaVars != nil {
if v, ok := g.metaVars[path]; ok {
return v
}
}
return ""
}

func (g *fakeResourceGetter) ReadAll(path string) string {
if g.fsPaths != nil {
if v, ok := g.fsPaths[path]; ok {
return v
}
}
return ""
}
194 changes: 194 additions & 0 deletions pkg/monitoredresource/monitoredresource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// Copyright 2022 The zap-cloudlogging Authors
// SPDX-License-Identifier: BSD-3-Clause

package monitoredresource

import (
"sync"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/zchee/zapcl/pkg/detector"
mrpb "google.golang.org/genproto/googleapis/api/monitoredres"
)

const (
there = "anyvalue"
projectID = "test-project"
zoneID = "test-region-zone"
regionID = "test-region"
serviceName = "test-service"
version = "1.0"
instanceName = "test-12345"
qualifiedZoneName = "projects/" + projectID + "/zones/" + zoneID
qualifiedRegionName = "projects/" + projectID + "/regions/" + regionID
funcSignature = "test-cf-signature"
funcTarget = "test-cf-target"
crConfig = "test-cr-config"
clusterName = "test-k8s-cluster"
podName = "test-k8s-pod-name"
containerName = "test-k8s-container-name"
namespaceName = "test-k8s-namespace-name"
instanceID = "test-instance-12345"
)

// fakeResourceGetter mocks internal.ResourceAtttributesGetter interface to retrieve env vars and metadata
type fakeResourceGetter struct {
envVars map[string]string
metaVars map[string]string
fsPaths map[string]string
}

// func (g *fakeResourceGetter) ProjectID() (string, error) { return projectID, nil }
// func (g *fakeResourceGetter) InstanceID() (string, error) { return instanceID, nil }
// func (g *fakeResourceGetter) InstanceName() (string, error) { return instanceName, nil }
// func (g *fakeResourceGetter) Zone() (string, error) { return zoneID, nil }

// func (g *fakeResourceGetter) InstanceAttributeValue(s string) (string, error) { return g.Get(s) }

func (g *fakeResourceGetter) EnvVar(name string) string {
if g.envVars != nil {
if v, ok := g.envVars[name]; ok {
return v
}
}
return ""
}

func (g *fakeResourceGetter) Metadata(path string) string {
if g.metaVars != nil {
if v, ok := g.metaVars[path]; ok {
return v
}
}
return ""
}

func (g *fakeResourceGetter) ReadAll(path string) string {
if g.fsPaths != nil {
if v, ok := g.fsPaths[path]; ok {
return v
}
}
return ""
}

// setupDetectResource resets sync.Once on detectResource and enforces mocked resource attribute getter
func setupDetectedResource(envVars, metaVars, fsPaths map[string]string) {
resourceDetector.once = new(sync.Once)
fake := &fakeResourceGetter{
envVars: envVars,
metaVars: metaVars,
fsPaths: fsPaths,
}
resourceDetector.attrs = fake
resourceDetector.pb = nil
}

func TestResourceDetection(t *testing.T) {
tests := []struct {
name string
envVars map[string]string
metaVars map[string]string
fsPaths map[string]string
want *MonitoredResource
}{
{
name: "CloudFunction",
envVars: map[string]string{
detector.EnvCloudFunctionsTarget: funcTarget,
detector.EnvCloudFunctionsSignatureType: funcSignature,
detector.EnvCloudFunctionsKService: serviceName,
detector.EnvCloudRunRevision: regionID,
},
metaVars: map[string]string{
"": there,
"project/project-id": projectID,
"instance/region": qualifiedRegionName,
},
want: &MonitoredResource{
LogID: "cloudfunctions.googleapis.com%2Fcloud-functions",
MonitoredResource: &mrpb.MonitoredResource{
Type: "cloud_function",
Labels: map[string]string{
"project_id": projectID,
"region": regionID,
"function_name": serviceName,
},
},
},
},
{
name: "CloudRun",
envVars: map[string]string{
detector.EnvCloudRunConfig: crConfig,
detector.EnvCloudRunService: serviceName,
detector.EnvCloudRunRevision: version,
},
metaVars: map[string]string{
"": there,
"project/project-id": projectID,
"instance/region": qualifiedRegionName,
},
want: &MonitoredResource{
LogID: "run.googleapis.com%2Fstdout",
MonitoredResource: &mrpb.MonitoredResource{
Type: "cloud_run_revision",
Labels: map[string]string{
"project_id": projectID,
"location": regionID,
"service_name": serviceName,
"revision_name": version,
"configuration_name": crConfig,
},
},
},
},
{
name: "CloudRunJobs",
envVars: map[string]string{
detector.EnvCloudRunJobsService: serviceName,
detector.EnvCloudRunJobsRevision: version,
},
metaVars: map[string]string{
"": there,
"project/project-id": projectID,
"instance/region": qualifiedRegionName,
},
want: &MonitoredResource{
LogID: "run.googleapis.com%2Fstdout",
MonitoredResource: &mrpb.MonitoredResource{
Type: "cloud_run_job",
Labels: map[string]string{
"project_id": projectID,
"location": regionID,
"job_name": serviceName,
},
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
setupDetectedResource(tt.envVars, tt.metaVars, tt.fsPaths)
got := Detect()
if diff := cmp.Diff(got, tt.want, cmpopts.IgnoreUnexported(mrpb.MonitoredResource{})); diff != "" {
t.Errorf("got(-),want(+):\n%s", diff)
}
})
}
}

// var benchmarkResultHolder *mrpb.MonitoredResource
//
// func BenchmarkDetectResource(b *testing.B) {
// var result *mrpb.MonitoredResource
//
// for n := 0; n < b.N; n++ {
// result = detectResource()
// }
//
// benchmarkResultHolder = result
// }