From ee375a60415a666e0179463ff5c6c84e3be70d3a Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Fri, 1 Mar 2024 15:10:22 -0800 Subject: [PATCH] [receiver/chrony] generated lifecycle tests (#31524) **Description:** Add generated tests for chrony receiver; move initialization of the chrony client to the start function **Link to tracking Issue:** Relates to #27849 --- .chloggen/chronyreceiver_lifecycle.yaml | 27 ++++++++ receiver/chronyreceiver/factory.go | 12 ++-- .../generated_component_test.go | 68 +++++++++++++++++++ receiver/chronyreceiver/metadata.yaml | 4 ++ receiver/chronyreceiver/scraper.go | 3 +- receiver/chronyreceiver/scraper_test.go | 3 +- 6 files changed, 109 insertions(+), 8 deletions(-) create mode 100755 .chloggen/chronyreceiver_lifecycle.yaml create mode 100644 receiver/chronyreceiver/generated_component_test.go diff --git a/.chloggen/chronyreceiver_lifecycle.yaml b/.chloggen/chronyreceiver_lifecycle.yaml new file mode 100755 index 000000000000..e0eef769689b --- /dev/null +++ b/.chloggen/chronyreceiver_lifecycle.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: chronyreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: move initialization of the chrony client to the start function + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [27849] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/receiver/chronyreceiver/factory.go b/receiver/chronyreceiver/factory.go index ac0195111481..3ca0008dab82 100644 --- a/receiver/chronyreceiver/factory.go +++ b/receiver/chronyreceiver/factory.go @@ -34,13 +34,15 @@ func newMetricsReceiver( return nil, fmt.Errorf("wrong config provided: %w", errInvalidValue) } - chronyc, err := chrony.New(cfg.Endpoint, cfg.Timeout) - if err != nil { - return nil, err - } + s := newScraper(ctx, cfg, set) scraper, err := scraperhelper.NewScraper( metadata.Type.String(), - newScraper(ctx, chronyc, cfg, set).scrape, + s.scrape, + scraperhelper.WithStart(func(ctx context.Context, host component.Host) error { + chronyc, err := chrony.New(cfg.Endpoint, cfg.Timeout) + s.client = chronyc + return err + }), ) if err != nil { return nil, err diff --git a/receiver/chronyreceiver/generated_component_test.go b/receiver/chronyreceiver/generated_component_test.go new file mode 100644 index 000000000000..1cd0464e74db --- /dev/null +++ b/receiver/chronyreceiver/generated_component_test.go @@ -0,0 +1,68 @@ +// Code generated by mdatagen. DO NOT EDIT. + +package chronyreceiver + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/component/componenttest" + + "go.opentelemetry.io/collector/consumer/consumertest" + "go.opentelemetry.io/collector/receiver" + "go.opentelemetry.io/collector/receiver/receivertest" + + "go.opentelemetry.io/collector/confmap/confmaptest" +) + +func TestComponentLifecycle(t *testing.T) { + factory := NewFactory() + + tests := []struct { + name string + createFn func(ctx context.Context, set receiver.CreateSettings, cfg component.Config) (component.Component, error) + }{ + + { + name: "metrics", + createFn: func(ctx context.Context, set receiver.CreateSettings, cfg component.Config) (component.Component, error) { + return factory.CreateMetricsReceiver(ctx, set, cfg, consumertest.NewNop()) + }, + }, + } + + cm, err := confmaptest.LoadConf("metadata.yaml") + require.NoError(t, err) + cfg := factory.CreateDefaultConfig() + sub, err := cm.Sub("tests::config") + require.NoError(t, err) + require.NoError(t, component.UnmarshalConfig(sub, cfg)) + + for _, test := range tests { + t.Run(test.name+"-shutdown", func(t *testing.T) { + c, err := test.createFn(context.Background(), receivertest.NewNopCreateSettings(), cfg) + require.NoError(t, err) + err = c.Shutdown(context.Background()) + require.NoError(t, err) + }) + + t.Run(test.name+"-lifecycle", func(t *testing.T) { + + // TODO support lifecycle + t.SkipNow() + + firstRcvr, err := test.createFn(context.Background(), receivertest.NewNopCreateSettings(), cfg) + require.NoError(t, err) + host := componenttest.NewNopHost() + require.NoError(t, err) + require.NoError(t, firstRcvr.Start(context.Background(), host)) + require.NoError(t, firstRcvr.Shutdown(context.Background())) + secondRcvr, err := test.createFn(context.Background(), receivertest.NewNopCreateSettings(), cfg) + require.NoError(t, err) + require.NoError(t, secondRcvr.Start(context.Background(), host)) + require.NoError(t, secondRcvr.Shutdown(context.Background())) + }) + } +} diff --git a/receiver/chronyreceiver/metadata.yaml b/receiver/chronyreceiver/metadata.yaml index 50fc1adb4d50..6821151645e1 100644 --- a/receiver/chronyreceiver/metadata.yaml +++ b/receiver/chronyreceiver/metadata.yaml @@ -74,3 +74,7 @@ metrics: value_type: double attributes: - leap.status + +tests: + config: + skip_lifecycle: true diff --git a/receiver/chronyreceiver/scraper.go b/receiver/chronyreceiver/scraper.go index f0c6f53b51f9..3ed4426da6a3 100644 --- a/receiver/chronyreceiver/scraper.go +++ b/receiver/chronyreceiver/scraper.go @@ -20,9 +20,8 @@ type chronyScraper struct { mb *metadata.MetricsBuilder } -func newScraper(ctx context.Context, client chrony.Client, cfg *Config, set receiver.CreateSettings) *chronyScraper { +func newScraper(ctx context.Context, cfg *Config, set receiver.CreateSettings) *chronyScraper { return &chronyScraper{ - client: client, mb: metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, set, metadata.WithStartTime(pcommon.NewTimestampFromTime(clock.FromContext(ctx).Now())), ), diff --git a/receiver/chronyreceiver/scraper_test.go b/receiver/chronyreceiver/scraper_test.go index 53bd23f70d4a..b53e78661d3d 100644 --- a/receiver/chronyreceiver/scraper_test.go +++ b/receiver/chronyreceiver/scraper_test.go @@ -115,7 +115,8 @@ func TestChronyScraper(t *testing.T) { chronym.On("GetTrackingData").Return(tc.mockTracking, tc.mockErr) ctx := clock.Context(context.Background(), clck) - scraper := newScraper(ctx, chronym, tc.conf, receivertest.NewNopCreateSettings()) + scraper := newScraper(ctx, tc.conf, receivertest.NewNopCreateSettings()) + scraper.client = chronym metrics, err := scraper.scrape(ctx)