diff --git a/internal/inventory/aws/fetcher_ec2_test.go b/internal/inventory/aws/fetcher_ec2_test.go index 23cd6a2688..06171c2630 100644 --- a/internal/inventory/aws/fetcher_ec2_test.go +++ b/internal/inventory/aws/fetcher_ec2_test.go @@ -25,21 +25,13 @@ import ( "github.com/aws/aws-sdk-go-v2/service/ec2/types" "github.com/elastic/elastic-agent-libs/logp" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "github.com/elastic/cloudbeat/internal/inventory" ec2beat "github.com/elastic/cloudbeat/internal/resources/providers/awslib/ec2" "github.com/elastic/cloudbeat/internal/resources/utils/pointers" ) -type mockInstancesProvider struct { - instances []*ec2beat.Ec2Instance - err error -} - -func (m *mockInstancesProvider) DescribeInstances(_ context.Context) ([]*ec2beat.Ec2Instance, error) { - return m.instances, m.err -} - func TestFetch(t *testing.T) { instance1 := &ec2beat.Ec2Instance{ Instance: types.Instance{ @@ -130,11 +122,12 @@ func TestFetch(t *testing.T) { } logger := logp.NewLogger("test_fetcher_ec2") + provider := newMockInstancesProvider(t) + provider.EXPECT().DescribeInstances(mock.Anything).Return(in, nil) + fetcher := Ec2Fetcher{ - logger: logger, - provider: &mockInstancesProvider{ - instances: in, - }, + logger: logger, + provider: provider, } ch := make(chan inventory.AssetEvent) diff --git a/internal/inventory/aws/mock_instances_provider.go b/internal/inventory/aws/mock_instances_provider.go new file mode 100644 index 0000000000..8344e5c6b0 --- /dev/null +++ b/internal/inventory/aws/mock_instances_provider.go @@ -0,0 +1,108 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Code generated by mockery v2.37.1. DO NOT EDIT. + +package aws + +import ( + context "context" + + ec2 "github.com/elastic/cloudbeat/internal/resources/providers/awslib/ec2" + mock "github.com/stretchr/testify/mock" +) + +// mockInstancesProvider is an autogenerated mock type for the instancesProvider type +type mockInstancesProvider struct { + mock.Mock +} + +type mockInstancesProvider_Expecter struct { + mock *mock.Mock +} + +func (_m *mockInstancesProvider) EXPECT() *mockInstancesProvider_Expecter { + return &mockInstancesProvider_Expecter{mock: &_m.Mock} +} + +// DescribeInstances provides a mock function with given fields: ctx +func (_m *mockInstancesProvider) DescribeInstances(ctx context.Context) ([]*ec2.Ec2Instance, error) { + ret := _m.Called(ctx) + + var r0 []*ec2.Ec2Instance + var r1 error + if rf, ok := ret.Get(0).(func(context.Context) ([]*ec2.Ec2Instance, error)); ok { + return rf(ctx) + } + if rf, ok := ret.Get(0).(func(context.Context) []*ec2.Ec2Instance); ok { + r0 = rf(ctx) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]*ec2.Ec2Instance) + } + } + + if rf, ok := ret.Get(1).(func(context.Context) error); ok { + r1 = rf(ctx) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// mockInstancesProvider_DescribeInstances_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'DescribeInstances' +type mockInstancesProvider_DescribeInstances_Call struct { + *mock.Call +} + +// DescribeInstances is a helper method to define mock.On call +// - ctx context.Context +func (_e *mockInstancesProvider_Expecter) DescribeInstances(ctx interface{}) *mockInstancesProvider_DescribeInstances_Call { + return &mockInstancesProvider_DescribeInstances_Call{Call: _e.mock.On("DescribeInstances", ctx)} +} + +func (_c *mockInstancesProvider_DescribeInstances_Call) Run(run func(ctx context.Context)) *mockInstancesProvider_DescribeInstances_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context)) + }) + return _c +} + +func (_c *mockInstancesProvider_DescribeInstances_Call) Return(_a0 []*ec2.Ec2Instance, _a1 error) *mockInstancesProvider_DescribeInstances_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *mockInstancesProvider_DescribeInstances_Call) RunAndReturn(run func(context.Context) ([]*ec2.Ec2Instance, error)) *mockInstancesProvider_DescribeInstances_Call { + _c.Call.Return(run) + return _c +} + +// newMockInstancesProvider creates a new instance of mockInstancesProvider. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func newMockInstancesProvider(t interface { + mock.TestingT + Cleanup(func()) +}) *mockInstancesProvider { + mock := &mockInstancesProvider{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/internal/inventory/inventory_test.go b/internal/inventory/inventory_test.go index 7e2c342b41..1d5ea6ddf4 100644 --- a/internal/inventory/inventory_test.go +++ b/internal/inventory/inventory_test.go @@ -28,28 +28,11 @@ import ( "github.com/elastic/elastic-agent-libs/logp" "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "github.com/elastic/cloudbeat/internal/resources/utils/pointers" ) -type mockAssetPublisher struct { - mockF func(events []beat.Event) -} - -func (m mockAssetPublisher) PublishAll(events []beat.Event) { - m.mockF(events) -} - -type mockAssetFetcher struct { - eventsToPublish []AssetEvent -} - -func (m mockAssetFetcher) Fetch(_ context.Context, assetChannel chan<- AssetEvent) { - for _, e := range m.eventsToPublish { - assetChannel <- e - } -} - func TestAssetInventory_Run(t *testing.T) { now := func() time.Time { return time.Date(2024, 1, 1, 1, 1, 1, 0, time.Local) } expected := []beat.Event{ @@ -98,58 +81,54 @@ func TestAssetInventory_Run(t *testing.T) { } publishedCh := make(chan []beat.Event) - publisher := mockAssetPublisher{ - mockF: func(e []beat.Event) { - publishedCh <- e - }, - } + publisher := NewMockAssetPublisher(t) + publisher.EXPECT().PublishAll(mock.Anything).Run(func(e []beat.Event) { + publishedCh <- e + }) - fetchers := []AssetFetcher{ - mockAssetFetcher{ - eventsToPublish: []AssetEvent{ - NewAssetEvent( - AssetClassification{ - Category: CategoryInfrastructure, - SubCategory: SubCategoryCompute, - Type: TypeVirtualMachine, - SubStype: SubTypeEC2, - }, - "arn:aws:ec2:us-east::ec2/234567890", - "test-server", - WithTags(map[string]string{"Name": "test-server", "key": "value"}), - WithCloud(AssetCloud{ - Provider: AwsCloudProvider, - Region: "us-east", - }), - WithHost(AssetHost{ - Architecture: string(types.ArchitectureValuesX8664), - ImageId: pointers.Ref("image-id"), - InstanceType: "instance-type", - Platform: "linux", - PlatformDetails: pointers.Ref("ubuntu"), - }), - WithIAM(AssetIAM{ - Id: pointers.Ref("a123123"), - Arn: pointers.Ref("123123:123123:123123"), - }), - WithNetwork(AssetNetwork{ - NetworkId: pointers.Ref("vpc-id"), - SubnetId: pointers.Ref("subnetId"), - Ipv6Address: pointers.Ref("ipv6"), - PublicIpAddress: pointers.Ref("public-ip-addr"), - PrivateIpAddress: pointers.Ref("private-ip-addre"), - PublicDnsName: pointers.Ref("public-dns"), - PrivateDnsName: pointers.Ref("private-dns"), - }), - ), + fetcher := NewMockAssetFetcher(t) + fetcher.EXPECT().Fetch(mock.Anything, mock.Anything).Run(func(_ context.Context, assetChannel chan<- AssetEvent) { + assetChannel <- NewAssetEvent( + AssetClassification{ + Category: CategoryInfrastructure, + SubCategory: SubCategoryCompute, + Type: TypeVirtualMachine, + SubStype: SubTypeEC2, }, - }, - } + "arn:aws:ec2:us-east::ec2/234567890", + "test-server", + WithTags(map[string]string{"Name": "test-server", "key": "value"}), + WithCloud(AssetCloud{ + Provider: AwsCloudProvider, + Region: "us-east", + }), + WithHost(AssetHost{ + Architecture: string(types.ArchitectureValuesX8664), + ImageId: pointers.Ref("image-id"), + InstanceType: "instance-type", + Platform: "linux", + PlatformDetails: pointers.Ref("ubuntu"), + }), + WithIAM(AssetIAM{ + Id: pointers.Ref("a123123"), + Arn: pointers.Ref("123123:123123:123123"), + }), + WithNetwork(AssetNetwork{ + NetworkId: pointers.Ref("vpc-id"), + SubnetId: pointers.Ref("subnetId"), + Ipv6Address: pointers.Ref("ipv6"), + PublicIpAddress: pointers.Ref("public-ip-addr"), + PrivateIpAddress: pointers.Ref("private-ip-addre"), + PublicDnsName: pointers.Ref("public-dns"), + PrivateDnsName: pointers.Ref("private-dns"), + }), + ) + }) logger := logp.NewLogger("test_run") inventory := AssetInventory{ logger: logger, - fetchers: fetchers, + fetchers: []AssetFetcher{fetcher}, publisher: publisher, bufferFlushInterval: 10 * time.Millisecond, bufferMaxSize: 1, diff --git a/internal/inventory/mock_asset_enricher.go b/internal/inventory/mock_asset_enricher.go new file mode 100644 index 0000000000..88d4cddd32 --- /dev/null +++ b/internal/inventory/mock_asset_enricher.go @@ -0,0 +1,82 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Code generated by mockery v2.37.1. DO NOT EDIT. + +package inventory + +import mock "github.com/stretchr/testify/mock" + +// MockAssetEnricher is an autogenerated mock type for the AssetEnricher type +type MockAssetEnricher struct { + mock.Mock +} + +type MockAssetEnricher_Expecter struct { + mock *mock.Mock +} + +func (_m *MockAssetEnricher) EXPECT() *MockAssetEnricher_Expecter { + return &MockAssetEnricher_Expecter{mock: &_m.Mock} +} + +// Execute provides a mock function with given fields: asset +func (_m *MockAssetEnricher) Execute(asset *AssetEvent) { + _m.Called(asset) +} + +// MockAssetEnricher_Execute_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Execute' +type MockAssetEnricher_Execute_Call struct { + *mock.Call +} + +// Execute is a helper method to define mock.On call +// - asset *AssetEvent +func (_e *MockAssetEnricher_Expecter) Execute(asset interface{}) *MockAssetEnricher_Execute_Call { + return &MockAssetEnricher_Execute_Call{Call: _e.mock.On("Execute", asset)} +} + +func (_c *MockAssetEnricher_Execute_Call) Run(run func(asset *AssetEvent)) *MockAssetEnricher_Execute_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(*AssetEvent)) + }) + return _c +} + +func (_c *MockAssetEnricher_Execute_Call) Return() *MockAssetEnricher_Execute_Call { + _c.Call.Return() + return _c +} + +func (_c *MockAssetEnricher_Execute_Call) RunAndReturn(run func(*AssetEvent)) *MockAssetEnricher_Execute_Call { + _c.Call.Return(run) + return _c +} + +// NewMockAssetEnricher creates a new instance of MockAssetEnricher. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockAssetEnricher(t interface { + mock.TestingT + Cleanup(func()) +}) *MockAssetEnricher { + mock := &MockAssetEnricher{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/internal/inventory/mock_asset_fetcher.go b/internal/inventory/mock_asset_fetcher.go new file mode 100644 index 0000000000..428db3e05e --- /dev/null +++ b/internal/inventory/mock_asset_fetcher.go @@ -0,0 +1,87 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Code generated by mockery v2.37.1. DO NOT EDIT. + +package inventory + +import ( + context "context" + + mock "github.com/stretchr/testify/mock" +) + +// MockAssetFetcher is an autogenerated mock type for the AssetFetcher type +type MockAssetFetcher struct { + mock.Mock +} + +type MockAssetFetcher_Expecter struct { + mock *mock.Mock +} + +func (_m *MockAssetFetcher) EXPECT() *MockAssetFetcher_Expecter { + return &MockAssetFetcher_Expecter{mock: &_m.Mock} +} + +// Fetch provides a mock function with given fields: ctx, assetChannel +func (_m *MockAssetFetcher) Fetch(ctx context.Context, assetChannel chan<- AssetEvent) { + _m.Called(ctx, assetChannel) +} + +// MockAssetFetcher_Fetch_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Fetch' +type MockAssetFetcher_Fetch_Call struct { + *mock.Call +} + +// Fetch is a helper method to define mock.On call +// - ctx context.Context +// - assetChannel chan<- AssetEvent +func (_e *MockAssetFetcher_Expecter) Fetch(ctx interface{}, assetChannel interface{}) *MockAssetFetcher_Fetch_Call { + return &MockAssetFetcher_Fetch_Call{Call: _e.mock.On("Fetch", ctx, assetChannel)} +} + +func (_c *MockAssetFetcher_Fetch_Call) Run(run func(ctx context.Context, assetChannel chan<- AssetEvent)) *MockAssetFetcher_Fetch_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(chan<- AssetEvent)) + }) + return _c +} + +func (_c *MockAssetFetcher_Fetch_Call) Return() *MockAssetFetcher_Fetch_Call { + _c.Call.Return() + return _c +} + +func (_c *MockAssetFetcher_Fetch_Call) RunAndReturn(run func(context.Context, chan<- AssetEvent)) *MockAssetFetcher_Fetch_Call { + _c.Call.Return(run) + return _c +} + +// NewMockAssetFetcher creates a new instance of MockAssetFetcher. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockAssetFetcher(t interface { + mock.TestingT + Cleanup(func()) +}) *MockAssetFetcher { + mock := &MockAssetFetcher{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/internal/inventory/mock_asset_publisher.go b/internal/inventory/mock_asset_publisher.go new file mode 100644 index 0000000000..b93736c6c8 --- /dev/null +++ b/internal/inventory/mock_asset_publisher.go @@ -0,0 +1,85 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Code generated by mockery v2.37.1. DO NOT EDIT. + +package inventory + +import ( + beat "github.com/elastic/beats/v7/libbeat/beat" + mock "github.com/stretchr/testify/mock" +) + +// MockAssetPublisher is an autogenerated mock type for the AssetPublisher type +type MockAssetPublisher struct { + mock.Mock +} + +type MockAssetPublisher_Expecter struct { + mock *mock.Mock +} + +func (_m *MockAssetPublisher) EXPECT() *MockAssetPublisher_Expecter { + return &MockAssetPublisher_Expecter{mock: &_m.Mock} +} + +// PublishAll provides a mock function with given fields: _a0 +func (_m *MockAssetPublisher) PublishAll(_a0 []beat.Event) { + _m.Called(_a0) +} + +// MockAssetPublisher_PublishAll_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'PublishAll' +type MockAssetPublisher_PublishAll_Call struct { + *mock.Call +} + +// PublishAll is a helper method to define mock.On call +// - _a0 []beat.Event +func (_e *MockAssetPublisher_Expecter) PublishAll(_a0 interface{}) *MockAssetPublisher_PublishAll_Call { + return &MockAssetPublisher_PublishAll_Call{Call: _e.mock.On("PublishAll", _a0)} +} + +func (_c *MockAssetPublisher_PublishAll_Call) Run(run func(_a0 []beat.Event)) *MockAssetPublisher_PublishAll_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].([]beat.Event)) + }) + return _c +} + +func (_c *MockAssetPublisher_PublishAll_Call) Return() *MockAssetPublisher_PublishAll_Call { + _c.Call.Return() + return _c +} + +func (_c *MockAssetPublisher_PublishAll_Call) RunAndReturn(run func([]beat.Event)) *MockAssetPublisher_PublishAll_Call { + _c.Call.Return(run) + return _c +} + +// NewMockAssetPublisher creates a new instance of MockAssetPublisher. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockAssetPublisher(t interface { + mock.TestingT + Cleanup(func()) +}) *MockAssetPublisher { + mock := &MockAssetPublisher{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +}