Skip to content

Commit

Permalink
new(pkg/driver): added tests for driver distros implementations.
Browse files Browse the repository at this point in the history
Moreover, fixed some discovered issues.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
  • Loading branch information
FedeDP authored and poiana committed Nov 24, 2023
1 parent cb049cb commit 42c069e
Show file tree
Hide file tree
Showing 18 changed files with 1,055 additions and 32 deletions.
9 changes: 3 additions & 6 deletions pkg/driver/distro/amzn.go
Expand Up @@ -16,8 +16,6 @@
package driverdistro

import (
"fmt"

"github.com/falcosecurity/driverkit/pkg/kernelrelease"
"gopkg.in/ini.v1"
)
Expand All @@ -32,10 +30,9 @@ type amzn struct {

//nolint:gocritic // the method shall not be able to modify kr
func (a *amzn) init(kr kernelrelease.KernelRelease, _ string, cfg *ini.File) error {
idKey := cfg.Section("").Key("VERSION_ID")
if idKey == nil {
// OS-release without `VERSION_ID` (can it happen?)
return fmt.Errorf("no VERSION_ID present for amzn")
idKey, err := cfg.Section("").GetKey("VERSION_ID")
if err != nil {
return err
}
// overwrite id
newID := ""
Expand Down
73 changes: 73 additions & 0 deletions pkg/driver/distro/amzn_test.go
@@ -0,0 +1,73 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2023 The Falco Authors
//
// Licensed 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.

package driverdistro

import (
"testing"

"github.com/falcosecurity/driverkit/pkg/kernelrelease"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/ini.v1"
)

func TestDistroAmznInit(t *testing.T) {
type config struct {
VersionID string `ini:"VERSION_ID"`
}
type testCase struct {
cfg interface{}
strExpected string
errExpected bool
}
testCases := []testCase{
{
cfg: &config{VersionID: ""},
strExpected: "amazonlinux",
},
{
cfg: &config{VersionID: "2"},
strExpected: "amazonlinux2",
},
{
cfg: &config{VersionID: "2022"},
strExpected: "amazonlinux2022",
},
{
cfg: &config{VersionID: "2023"},
strExpected: "amazonlinux2023",
},
{
cfg: &struct{}{},
errExpected: true,
},
}
for _, tCase := range testCases {
al := &amzn{generic: &generic{}}
cfg := ini.Empty()
err := cfg.ReflectFrom(tCase.cfg)
require.NoError(t, err)

kr := kernelrelease.KernelRelease{}
err = al.init(kr, "", cfg)
if tCase.errExpected {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tCase.strExpected, al.String())
}
}
}
13 changes: 6 additions & 7 deletions pkg/driver/distro/bottlerocket.go
Expand Up @@ -35,16 +35,15 @@ type bottlerocket struct {

//nolint:gocritic // the method shall not be able to modify kr
func (b *bottlerocket) init(kr kernelrelease.KernelRelease, id string, cfg *ini.File) error {
idKey := cfg.Section("").Key("VERSION_ID")
if idKey == nil {
// OS-release without `VERSION_ID` (can it happen?)
return fmt.Errorf("no VERSION_ID present for bottlerocket")
idKey, err := cfg.Section("").GetKey("VERSION_ID")
if err != nil {
return err
}
b.versionID = idKey.String()

idKey = cfg.Section("").Key("VARIANT_ID")
if idKey == nil {
return fmt.Errorf("no VARIANT_ID present for bottlerocket")
idKey, err = cfg.Section("").GetKey("VARIANT_ID")
if err != nil {
return err
}
b.variantID = strings.Split(idKey.String(), "-")[0]

Expand Down
79 changes: 79 additions & 0 deletions pkg/driver/distro/bottlerocket_test.go
@@ -0,0 +1,79 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2023 The Falco Authors
//
// Licensed 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.

package driverdistro

import (
"testing"

"github.com/falcosecurity/driverkit/pkg/kernelrelease"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/ini.v1"
)

func TestDistroBottlerocketFixup(t *testing.T) {
type config struct {
VersionID string `ini:"VERSION_ID"`
VariantID string `ini:"VARIANT_ID"`
}
type nonVersionConfig struct {
VariantID string `ini:"VARIANT_ID"`
}
type nonVariantConfig struct {
VersionID string `ini:"VERSION_ID"`
}

type testCase struct {
cfg interface{}
kvExpected string
errExpected bool
}
testCases := []testCase{
{
cfg: &config{VersionID: "1.11.0", VariantID: "aws-k8s-1.15"},
kvExpected: "1_1.11.0-aws",
},
{
cfg: &config{VersionID: "1.17.0", VariantID: "aws"},
kvExpected: "1_1.17.0-aws",
},
{
cfg: &nonVersionConfig{VariantID: "aws"},
errExpected: true,
},
{
cfg: &nonVariantConfig{VersionID: "1.11.0"},
errExpected: true,
},
}

for _, tCase := range testCases {
br := &bottlerocket{generic: &generic{}}
cfg := ini.Empty()
err := cfg.ReflectFrom(tCase.cfg)
require.NoError(t, err)

kr := kernelrelease.KernelRelease{}
err = br.init(kr, "", cfg)
if tCase.errExpected {
assert.Error(t, err)
} else {
assert.NoError(t, err)
fixedKr := br.FixupKernel(kr)
assert.Equal(t, tCase.kvExpected, fixedKr.KernelVersion)
}
}
}
82 changes: 82 additions & 0 deletions pkg/driver/distro/centos_test.go
@@ -0,0 +1,82 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2023 The Falco Authors
//
// Licensed 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.

package driverdistro

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDistroCentosCheck(t *testing.T) {
type testCase struct {
hostRoot string
preFn func() error
postFn func()
retExpected bool
}
testCases := []testCase{
{
// No centos-release file
hostRoot: "/foo",
preFn: func() error {
return nil
},
postFn: func() {},
retExpected: false,
},
{
// centos-release file present under hostroot
hostRoot: ".",
preFn: func() error {
if err := os.MkdirAll("./etc/", 0o755); err != nil {
return err
}
_, err := os.Create("./etc/centos-release")
return err
},
postFn: func() {
_ = os.RemoveAll("./etc/centos-release")
},
retExpected: true,
},
{
// centos-release file present but not under hostroot
hostRoot: "/foo",
preFn: func() error {
if err := os.MkdirAll("./etc/", 0o755); err != nil {
return err
}
_, err := os.Create("./etc/centos-release")
return err
},
postFn: func() {
_ = os.RemoveAll("./etc/centos-release")
},
retExpected: false,
},
}

for _, tCase := range testCases {
c := &centos{generic: &generic{}}
err := tCase.preFn()
require.NoError(t, err)
assert.Equal(t, tCase.retExpected, c.check(tCase.hostRoot))
tCase.postFn()
}
}
12 changes: 6 additions & 6 deletions pkg/driver/distro/cos.go
Expand Up @@ -44,13 +44,13 @@ type cos struct {
}

//nolint:gocritic // the method shall not be able to modify kr
func (c *cos) init(kr kernelrelease.KernelRelease, _ string, cfg *ini.File) error {
idKey := cfg.Section("").Key("BUILD_ID")
if idKey == nil {
// OS-release without `VERSION_ID` (can it happen?)
return fmt.Errorf("no BUILD_ID present for COS")
func (c *cos) init(kr kernelrelease.KernelRelease, id string, cfg *ini.File) error {
idKey, err := cfg.Section("").GetKey("BUILD_ID")
if err != nil {
return err
}
return c.generic.init(kr, idKey.String(), cfg)
c.buildID = idKey.String()
return c.generic.init(kr, id, cfg)
}

//nolint:gocritic // the method shall not be able to modify kr
Expand Down
63 changes: 63 additions & 0 deletions pkg/driver/distro/cos_test.go
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2023 The Falco Authors
//
// Licensed 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.

package driverdistro

import (
"testing"

"github.com/falcosecurity/driverkit/pkg/kernelrelease"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/ini.v1"
)

func TestDistroCosInit(t *testing.T) {
type config struct {
BuildID string `ini:"BUILD_ID"`
}

type testCase struct {
cfg interface{}
buildID string
errExpected bool
}
testCases := []testCase{
{
cfg: &config{BuildID: "17162.127.33"},
buildID: "17162.127.33",
},
{
cfg: &struct{}{},
errExpected: true,
},
}

for _, tCase := range testCases {
co := &cos{generic: &generic{}}
cfg := ini.Empty()
err := cfg.ReflectFrom(tCase.cfg)
require.NoError(t, err)

kr := kernelrelease.KernelRelease{}
err = co.init(kr, "", cfg)
if tCase.errExpected {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tCase.buildID, co.buildID)
}
}
}

0 comments on commit 42c069e

Please sign in to comment.