Skip to content

Commit

Permalink
replace duplicate isContainer call (#596)
Browse files Browse the repository at this point in the history
* replace duplicate isContainer call and introduce IsContainer mocking for Virtualization test
  • Loading branch information
oliveromahony authored Mar 15, 2024
1 parent 2a918fb commit 79edf03
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 21 deletions.
11 changes: 6 additions & 5 deletions src/core/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Environment interface {
GetContainerID() (string, error)
GetNetOverflow() (float64, error)
IsContainer() bool
Virtualization() (string, string)
}

type ConfigApplyMarker interface {
Expand Down Expand Up @@ -152,7 +153,7 @@ func (env *EnvironmentType) NewHostInfoWithContext(ctx context.Context, agentVer
Uname: getUnixName(),
Partitons: disks,
Network: env.networks(),
Processor: processors(hostInformation.KernelArch),
Processor: env.processors(hostInformation.KernelArch),
Release: releaseInfo("/etc/os-release"),
Tags: *tags,
AgentAccessibleDirs: configDirs,
Expand Down Expand Up @@ -729,15 +730,15 @@ func callSyscall(mib []int32) ([]byte, uint64, error) {
return buf, length, nil
}

func processors(architecture string) (res []*proto.CpuInfo) {
func (env *EnvironmentType) processors(architecture string) (res []*proto.CpuInfo) {
log.Debug("Reading CPU information for dataplane host")
cpus, err := cpu.Info()
if err != nil {
log.Warnf("%v", err)
return []*proto.CpuInfo{}
}

hypervisor, virtual := virtualization()
hypervisor, virtual := env.Virtualization()

for _, item := range cpus {
processor := proto.CpuInfo{
Expand Down Expand Up @@ -858,7 +859,7 @@ func formatBytes(bytes int) string {
}
}

func virtualization() (string, string) {
func (env *EnvironmentType) Virtualization() (string, string) {
ctx := context.Background()
defer ctx.Done()
// doesn't check k8s
Expand All @@ -868,7 +869,7 @@ func virtualization() (string, string) {
return "", "host"
}

if virtualizationSystem == "docker" || isContainer() {
if virtualizationSystem == "docker" || env.IsContainer() {
log.Debugf("Virtualization detected as container with role %v", virtualizationRole)
return "container", virtualizationRole
}
Expand Down
33 changes: 27 additions & 6 deletions src/core/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,33 +440,54 @@ func TestNetworks(t *testing.T) {
assert.NotNil(t, networks.Default)
}

// MockEnvironment is a mock implementation of the Environment interface for testing purposes.
type MockEnvironment struct {
isContainer bool
}

// IsContainer is a mock function for IsContainer() method.
func (m *MockEnvironment) IsContainer() bool {
// Mock implementation for testing.
return m.isContainer
}

func (m *MockEnvironment) Virtualization() (string, string) {
realEnv := &EnvironmentType{}
return realEnv.Virtualization()
}

func TestVirtualization(t *testing.T) {
mockEnv := &MockEnvironment{isContainer: false}

// Test normal VM
virtualizationFunc = func(ctx context.Context) (string, string, error) {
virtualizationFunc = func(_ context.Context) (string, string, error) {
return "LXC", "host", nil
}

virtualizationSystem, virtualizationRole := virtualization()
virtualizationSystem, virtualizationRole := mockEnv.Virtualization()

assert.Equal(t, "LXC", virtualizationSystem)
assert.Equal(t, "host", virtualizationRole)

// Test container
virtualizationFunc = func(ctx context.Context) (string, string, error) {
virtualizationFunc = func(_ context.Context) (string, string, error) {
return "docker", "host", nil
}

virtualizationSystem, virtualizationRole = virtualization()
mockEnv.isContainer = true

virtualizationSystem, virtualizationRole = mockEnv.Virtualization()

assert.Equal(t, "container", virtualizationSystem)
assert.Equal(t, "host", virtualizationRole)
}

func TestProcessors(t *testing.T) {
processorInfo := processors("arm64")
env := EnvironmentType{}
processorInfo := env.processors("arm64")
// at least one network interface
assert.GreaterOrEqual(t, processorInfo[0].GetCpus(), int32(1))
// non empty architecture
// non-empty architecture
assert.NotEmpty(t, processorInfo[0].GetArchitecture())
assert.Equal(t, "arm64", processorInfo[0].GetArchitecture())
}
Expand Down
70 changes: 70 additions & 0 deletions src/core/fake_environment_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/extensions/advanced-metrics/reader/mocks/net_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/utils/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,8 @@ func (m *MockEnvironment) IsContainer() bool {
ret := m.Called()
return ret.Get(0).(bool)
}

func (m *MockEnvironment) Virtualization() (string, string) {
ret := m.Called()
return ret.Get(0).(string), ret.Get(0).(string)
}

0 comments on commit 79edf03

Please sign in to comment.