From dac6cdbad83a8e583453726f07717bbbe491e54f Mon Sep 17 00:00:00 2001 From: Jeff Li Date: Tue, 7 Dec 2021 07:00:27 +0800 Subject: [PATCH] fix: test failure cases on windows (#1047) * fix: utils.NewConfig returns a file not found error in windows os * Fix: set `service.name` so that `Output` does not fail due to system environment * fix: golang.org/x/sys version mismatch * chore: use `WithResource` in the `ExampleExporter` to change `service.name` * fix: `TestTransportErrorStatus` connection refused assert on Windows machines * test: TestHostNetwork Wait for results until go test times out * fix: windows net listen failed Co-authored-by: Anthony Mirabella --- .github/workflows/ci.yml | 4 +--- .../metric/cortex/utils/config_utils_test.go | 11 +++++++++++ exporters/metric/datadog/example_test.go | 12 +++++++++--- .../otellambda/xrayconfig/xrayconfig_test.go | 9 ++++++++- instrumentation/host/host_test.go | 19 +++++++++---------- .../net/http/otelhttp/test/transport_test.go | 11 ++++++++++- 6 files changed, 48 insertions(+), 18 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f96cb63c088..cfc1a35969c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -108,9 +108,7 @@ jobs: strategy: matrix: go-version: [1.17, 1.16] - # TODO: support all of these. - #os: [ubuntu-latest, macos-latest, windows-latest] - os: [ubuntu-latest, macos-latest] + os: [ubuntu-latest, macos-latest, windows-latest] # GitHub Actions does not support arm* architectures on default # runners. It is possible to acomplish this with a self-hosted runner # if we want to add this in the future: diff --git a/exporters/metric/cortex/utils/config_utils_test.go b/exporters/metric/cortex/utils/config_utils_test.go index da89a2e7665..7b288708519 100644 --- a/exporters/metric/cortex/utils/config_utils_test.go +++ b/exporters/metric/cortex/utils/config_utils_test.go @@ -32,6 +32,17 @@ func initYAML(yamlBytes []byte, path string) (afero.Fs, error) { // Create an in-memory file system. fs := afero.NewMemMapFs() + // https://github.com/spf13/viper/blob/v1.8.1/viper.go#L480 + // absPathify uses filepath.Clean, so here you also need to use filepath.Clean + if filepath.IsAbs(path) { + path = filepath.Clean(path) + } else { + p, err := filepath.Abs(path) + if err == nil { + path = filepath.Clean(p) + } + } + // Retrieve the directory path. dirPath := filepath.Dir(path) diff --git a/exporters/metric/datadog/example_test.go b/exporters/metric/datadog/example_test.go index 7f84032b02a..2103444b1f8 100644 --- a/exporters/metric/datadog/example_test.go +++ b/exporters/metric/datadog/example_test.go @@ -24,13 +24,16 @@ import ( "github.com/DataDog/datadog-go/statsd" - "go.opentelemetry.io/contrib/exporters/metric/datadog" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric/global" controller "go.opentelemetry.io/otel/sdk/metric/controller/basic" "go.opentelemetry.io/otel/sdk/metric/processor/basic" "go.opentelemetry.io/otel/sdk/metric/selector/simple" + "go.opentelemetry.io/otel/sdk/resource" + semconv "go.opentelemetry.io/otel/semconv/v1.4.0" + + "go.opentelemetry.io/contrib/exporters/metric/datadog" ) type TestUDPServer struct { @@ -57,12 +60,15 @@ func ExampleExporter() { go func() { defer exp.Close() processor := basic.NewFactory(selector, exp) - cont := controller.New(processor, controller.WithExporter(exp), controller.WithCollectPeriod(time.Second*10)) + cont := controller.New(processor, controller.WithExporter(exp), controller.WithCollectPeriod(time.Second*10), + controller.WithResource(resource.Default()), + controller.WithResource(resource.NewSchemaless(semconv.ServiceNameKey.String("ExampleExporter")))) ctx := context.Background() err := cont.Start(ctx) if err != nil { panic(err) } + defer func() { handleErr(cont.Stop(ctx)) }() global.SetMeterProvider(cont) meter := global.Meter("marwandist") @@ -98,7 +104,7 @@ func ExampleExporter() { } // Output: - // myrecorder.max:100|g|#env:dev,l:1,service.name:unknown_service:datadog.test,telemetry.sdk.language:go,telemetry.sdk.name:opentelemetry,telemetry.sdk.version:1.2.0 + // myrecorder.max:100|g|#env:dev,l:1,service.name:ExampleExporter,telemetry.sdk.language:go,telemetry.sdk.name:opentelemetry,telemetry.sdk.version:1.2.0 // } diff --git a/instrumentation/github.com/aws/aws-lambda-go/otellambda/xrayconfig/xrayconfig_test.go b/instrumentation/github.com/aws/aws-lambda-go/otellambda/xrayconfig/xrayconfig_test.go index a0a3ea74c77..b0dae0aa977 100644 --- a/instrumentation/github.com/aws/aws-lambda-go/otellambda/xrayconfig/xrayconfig_test.go +++ b/instrumentation/github.com/aws/aws-lambda-go/otellambda/xrayconfig/xrayconfig_test.go @@ -18,6 +18,7 @@ import ( "context" "os" "reflect" + "runtime" "testing" "time" @@ -67,6 +68,12 @@ func setEnvVars() { _ = os.Setenv("AWS_REGION", "us-texas-1") _ = os.Setenv("AWS_LAMBDA_FUNCTION_VERSION", "$LATEST") _ = os.Setenv("_X_AMZN_TRACE_ID", "Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1") + + // fix issue: "The requested service provider could not be loaded or initialized." + // Guess: The env for Windows in GitHub action is incomplete + if runtime.GOOS == "windows" && os.Getenv("SYSTEMROOT") == "" { + _ = os.Setenv("SYSTEMROOT", `C:\Windows`) + } } // Vars for end to end testing @@ -161,7 +168,7 @@ func TestWrapEndToEnd(t *testing.T) { customerHandler := func() (string, error) { return "hello world", nil } - mockCollector := runMockCollectorAtEndpoint(t, "localhost:4317") + mockCollector := runMockCollectorAtEndpoint(t, ":4317") defer func() { _ = mockCollector.Stop() }() diff --git a/instrumentation/host/host_test.go b/instrumentation/host/host_test.go index 5fa501a54d5..4d85e9adb2e 100644 --- a/instrumentation/host/host_test.go +++ b/instrumentation/host/host_test.go @@ -28,9 +28,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.opentelemetry.io/contrib/instrumentation/host" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric/metrictest" + + "go.opentelemetry.io/contrib/instrumentation/host" ) func getMetric(provider *metrictest.MeterProvider, name string, lbl attribute.KeyValue) float64 { @@ -218,20 +219,18 @@ func TestHostNetwork(t *testing.T) { require.NoError(t, err) // As we are going to read the /proc file system for this info, sleep a while: - time.Sleep(time.Second) - - provider.RunAsyncInstruments() + require.Eventually(t, func() bool { + hostAfter, err := net.IOCountersWithContext(ctx, false) + require.NoError(t, err) - hostAfter, err := net.IOCountersWithContext(ctx, false) - require.NoError(t, err) + return uint64(howMuch) <= hostAfter[0].BytesSent-hostBefore[0].BytesSent && + uint64(howMuch) <= hostAfter[0].BytesRecv-hostBefore[0].BytesRecv + }, 30*time.Second, time.Second/2) + provider.RunAsyncInstruments() hostTransmit := getMetric(provider, "system.network.io", host.AttributeNetworkTransmit[0]) hostReceive := getMetric(provider, "system.network.io", host.AttributeNetworkReceive[0]) - // Check that the network transmit/receive used is greater than before: - require.LessOrEqual(t, uint64(howMuch), hostAfter[0].BytesSent-hostBefore[0].BytesSent) - require.LessOrEqual(t, uint64(howMuch), hostAfter[0].BytesRecv-hostBefore[0].BytesRecv) - // Check that the recorded measurements reflect the same change: require.LessOrEqual(t, uint64(howMuch), uint64(hostTransmit)-hostBefore[0].BytesSent) require.LessOrEqual(t, uint64(howMuch), uint64(hostReceive)-hostBefore[0].BytesRecv) diff --git a/instrumentation/net/http/otelhttp/test/transport_test.go b/instrumentation/net/http/otelhttp/test/transport_test.go index febd87c131e..d0809f4e048 100644 --- a/instrumentation/net/http/otelhttp/test/transport_test.go +++ b/instrumentation/net/http/otelhttp/test/transport_test.go @@ -19,6 +19,7 @@ import ( "io/ioutil" "net/http" "net/http/httptest" + "runtime" "strings" "testing" @@ -117,7 +118,15 @@ func TestTransportErrorStatus(t *testing.T) { t.Errorf("expected error status code on span; got: %q", got) } - if got := span.Status().Description; !strings.Contains(got, "connect: connection refused") { + errSubstr := "connect: connection refused" + if runtime.GOOS == "windows" { + // tls.Dial returns an error that does not contain the substring "connection refused" + // on Windows machines + // + // ref: "dial tcp 127.0.0.1:50115: connectex: No connection could be made because the target machine actively refused it." + errSubstr = "No connection could be made because the target machine actively refused it" + } + if got := span.Status().Description; !strings.Contains(got, errSubstr) { t.Errorf("expected error status message on span; got: %q", got) } }