Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

replace duplicate isContainer call #596

Merged
merged 4 commits into from
Mar 15, 2024
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
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.

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)
}
Loading