-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new(pkg/driver): added tests for driver distros implementations.
Moreover, fixed some discovered issues. Signed-off-by: Federico Di Pierro <[email protected]>
- Loading branch information
Showing
18 changed files
with
1,059 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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 emtyConfig struct { | ||
} | ||
|
||
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: &emtyConfig{}, | ||
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()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 := ¢os{generic: &generic{}} | ||
err := tCase.preFn() | ||
require.NoError(t, err) | ||
assert.Equal(t, tCase.retExpected, c.check(tCase.hostRoot)) | ||
tCase.postFn() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} |
Oops, something went wrong.