Skip to content

Commit

Permalink
lca: add unittest for seedgenerator
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Chuzhoy <[email protected]>
  • Loading branch information
achuzhoy committed May 23, 2024
1 parent ca98df4 commit c47e5ce
Show file tree
Hide file tree
Showing 2 changed files with 349 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/lca/seedgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func PullSeedGenerator(apiClient *clients.Settings, name string) (*SeedGenerator
if name == "" {
glog.V(100).Infof("The name of the seedgenerator is empty")

builder.errorMsg = "seedgenerator 'name' cannot be empty"
return nil, fmt.Errorf("seedgenerator 'name' cannot be empty")
}

if !builder.Exists() {
Expand All @@ -153,7 +153,9 @@ func (builder *SeedGeneratorBuilder) Delete() (*SeedGeneratorBuilder, error) {
builder.Definition.Name)

if !builder.Exists() {
return builder, fmt.Errorf("seedgenerator cannot be deleted because it does not exist")
builder.Object = nil

return builder, nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down Expand Up @@ -230,6 +232,12 @@ func (builder *SeedGeneratorBuilder) WithRecertImage(
return builder
}

if recertImage == "" {
glog.V(100).Infof("The name of the recertImage is empty")

builder.errorMsg = "recertImage 'name' cannot be empty"
}

glog.V(100).Infof("Setting recert image %s in seedgenerator", recertImage)

builder.Definition.Spec.RecertImage = recertImage
Expand Down
339 changes: 339 additions & 0 deletions pkg/lca/seedgenerator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,339 @@
package lca

import (
"fmt"
"testing"

"github.com/openshift-kni/eco-goinfra/pkg/clients"
lcasgv1 "github.com/openshift-kni/lifecycle-agent/api/seedgenerator/v1"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

func TestNewSeedGeneratorBuilder(t *testing.T) {
testCases := []struct {
name string
expectedErr string
}{
{
name: "seedimage",
expectedErr: "",
},
{
name: "",
expectedErr: "SeedGenerator name must be seedimage",
},
{
name: "seedgen2",
expectedErr: "SeedGenerator name must be seedimage",
},
}

for _, testCase := range testCases {
testSettings := clients.GetTestClients(clients.TestClientParams{})

testBuilder := NewSeedGeneratorBuilder(testSettings, testCase.name)

if testCase.expectedErr == "" {
assert.NotNil(t, testBuilder)
assert.Equal(t, testCase.name, testBuilder.Definition.Name)
assert.Equal(t, testBuilder.errorMsg, "")
} else {
assert.Equal(t, testCase.expectedErr, testBuilder.errorMsg)
}
}
}

func TestSeedGeneratorWithOptions(t *testing.T) {
testBuilder := buildTestBuilderWithFakeObjects()

testBuilder.WithOptions(func(builder *SeedGeneratorBuilder) (*SeedGeneratorBuilder, error) {
return builder, nil
})

assert.Equal(t, "", testBuilder.errorMsg)

testBuilder.WithOptions(func(builder *SeedGeneratorBuilder) (*SeedGeneratorBuilder, error) {
return builder, fmt.Errorf("error")
})

assert.Equal(t, "error", testBuilder.errorMsg)
}

func TestSeedGeneratorCreate(t *testing.T) {
testCases := []struct {
seedGenerator *SeedGeneratorBuilder
expectedError error
}{
{
seedGenerator: buildValidSeedGeneratorBuilder(clients.GetTestClients(clients.TestClientParams{})),
expectedError: nil,
},
{
seedGenerator: buildInValidSeedGeneratorBuilder(clients.GetTestClients(clients.TestClientParams{})),
expectedError: fmt.Errorf("SeedGenerator name must be seedimage"),
},
}

for _, testCase := range testCases {
testSeedGeneratorBuilder, err := testCase.seedGenerator.Create()
assert.Equal(t, err, testCase.expectedError)

if testCase.expectedError == nil {
assert.Equal(t, testSeedGeneratorBuilder.Definition.Name, testSeedGeneratorBuilder.Object.Name)
}
}
}

func TestSeedGeneratorPull(t *testing.T) {
testCases := []struct {
name string
expectedError error
addToRuntimeObjects bool
client bool
}{
{
name: "",
expectedError: fmt.Errorf("seedgenerator 'name' cannot be empty"),
addToRuntimeObjects: true,
client: true,
},
{
name: "notseedimage",
expectedError: fmt.Errorf("seedgenerator object notseedimage does not exist"),
addToRuntimeObjects: true,
client: true,
},
{
name: "seedimage",
expectedError: nil,
addToRuntimeObjects: true,
client: true,
},
{
name: "seedimage",
expectedError: fmt.Errorf("the apiClient is nil"),
addToRuntimeObjects: true,
client: false,
},
{
name: "seedimage",
expectedError: fmt.Errorf("seedgenerator object seedimage does not exist"),
addToRuntimeObjects: false,
client: true,
},
}

for _, testCase := range testCases {
var (
runtimeObjects []runtime.Object
testSettings *clients.Settings
)

testSG := generateSeedGenerator("seedimage")

if testCase.addToRuntimeObjects {
runtimeObjects = append(runtimeObjects, testSG)
}

if testCase.client {
testSettings = clients.GetTestClients(clients.TestClientParams{
K8sMockObjects: runtimeObjects,
})
}

// Test the PullSeedGenerator function
builderResult, err := PullSeedGenerator(testSettings, testCase.name)

// Check the error
assert.Equal(t, err, testCase.expectedError)

if testCase.expectedError == nil {
assert.NotNil(t, builderResult)
}
}
}

func TestSeedGeneratorDelete(t *testing.T) {
testCases := []struct {
seedGenerator *SeedGeneratorBuilder
expectedError error
}{
{
seedGenerator: buildValidSeedGeneratorBuilder(clients.GetTestClients(clients.TestClientParams{})),
expectedError: nil,
},
{
seedGenerator: buildInValidSeedGeneratorBuilder(clients.GetTestClients(clients.TestClientParams{})),
expectedError: fmt.Errorf("SeedGenerator name must be seedimage"),
},
{
seedGenerator: buildValidSeedGeneratorBuilder(buildSeedGeneratorTestClientWithDummyObject()),
expectedError: nil,
},
}

for _, testCase := range testCases {
testSeedGeneratorBuilder, err := testCase.seedGenerator.Delete()
assert.Equal(t, err, testCase.expectedError)

if testCase.expectedError == nil {
assert.Nil(t, testSeedGeneratorBuilder.Object)
} else {
assert.Equal(t, testCase.expectedError, err)
}

assert.Nil(t, testCase.seedGenerator.Object)
}
}

func TestSeedGeneratorGet(t *testing.T) {
testCases := []struct {
seedGenerator *SeedGeneratorBuilder
expectedError error
}{
{
seedGenerator: buildValidSeedGeneratorBuilder(clients.GetTestClients(clients.TestClientParams{})),
expectedError: fmt.Errorf("seedgenerators.lca.openshift.io \"seedimage\" not found"),
},
{
seedGenerator: buildInValidSeedGeneratorBuilder(clients.GetTestClients(clients.TestClientParams{})),
expectedError: fmt.Errorf("SeedGenerator name must be seedimage"),
},
{
seedGenerator: buildValidSeedGeneratorBuilder(buildSeedGeneratorTestClientWithDummyObject()),
expectedError: nil,
},
}

for _, testCase := range testCases {
testSeedGenerator, err := testCase.seedGenerator.Get()
if err != nil {
assert.Equal(t, err.Error(), testCase.expectedError.Error())
}

if testCase.expectedError == nil {
assert.Equal(t, testSeedGenerator.Name, testCase.seedGenerator.Definition.Name)
}
}
}

func TestSeedGeneratorExists(t *testing.T) {
testCases := []struct {
seedGenerator *SeedGeneratorBuilder
expectedStatus bool
}{
{
seedGenerator: buildValidSeedGeneratorBuilder(clients.GetTestClients(clients.TestClientParams{})),
expectedStatus: false,
},
{
seedGenerator: buildInValidSeedGeneratorBuilder(clients.GetTestClients(clients.TestClientParams{})),
expectedStatus: false,
},
{
seedGenerator: buildValidSeedGeneratorBuilder(buildSeedGeneratorTestClientWithDummyObject()),
expectedStatus: true,
},
}

for _, testCase := range testCases {
exist := testCase.seedGenerator.Exists()
assert.Equal(t, exist, testCase.expectedStatus)
}
}

func TestSeedGeneratorWithSeedImage(t *testing.T) {
testCases := []struct {
seedGenerator *SeedGeneratorBuilder
expectedError string
seedImage string
}{
{
seedGenerator: buildValidSeedGeneratorBuilder(clients.GetTestClients(clients.TestClientParams{})),
expectedError: "",
seedImage: "testSeedImage",
},
{
seedGenerator: buildValidSeedGeneratorBuilder(clients.GetTestClients(clients.TestClientParams{})),
expectedError: "seedImage 'name' cannot be empty",
seedImage: "",
},
}

for _, testCase := range testCases {
testSeedGeneratorBuilder := testCase.seedGenerator.WithSeedImage(testCase.seedImage)

assert.Nil(t, testSeedGeneratorBuilder.Object)

assert.Equal(t, testCase.expectedError, testSeedGeneratorBuilder.errorMsg)

assert.Nil(t, testCase.seedGenerator.Object)
}
}

func TestSeedGeneratorWithRecertImage(t *testing.T) {
testCases := []struct {
seedGenerator *SeedGeneratorBuilder
expectedError string
recertImage string
}{
{
seedGenerator: buildValidSeedGeneratorBuilder(clients.GetTestClients(clients.TestClientParams{})),
expectedError: "",
recertImage: "testSeedImage",
},
{
seedGenerator: buildValidSeedGeneratorBuilder(clients.GetTestClients(clients.TestClientParams{})),
expectedError: "recertImage 'name' cannot be empty",
recertImage: "",
},
}

for _, testCase := range testCases {
testSeedGeneratorBuilder := testCase.seedGenerator.WithRecertImage(testCase.recertImage)

assert.Nil(t, testSeedGeneratorBuilder.Object)

assert.Equal(t, testCase.expectedError, testSeedGeneratorBuilder.errorMsg)

assert.Nil(t, testCase.seedGenerator.Object)
}
}

func buildTestBuilderWithFakeObjects() *SeedGeneratorBuilder {
return NewSeedGeneratorBuilder(buildSeedGeneratorTestClientWithDummyObject(), "seedimage")
}

func generateSeedGenerator(name string) *lcasgv1.SeedGenerator {
return &lcasgv1.SeedGenerator{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: lcasgv1.SeedGeneratorSpec{},
}
}

func buildValidSeedGeneratorBuilder(apiClient *clients.Settings) *SeedGeneratorBuilder {
return NewSeedGeneratorBuilder(apiClient, "seedimage")
}

func buildInValidSeedGeneratorBuilder(apiClient *clients.Settings) *SeedGeneratorBuilder {
return NewSeedGeneratorBuilder(apiClient, "")
}

func buildSeedGeneratorTestClientWithDummyObject() *clients.Settings {
return clients.GetTestClients(clients.TestClientParams{
K8sMockObjects: buildDummySeedGeneratorRuntime("seedimage"),
})
}

func buildDummySeedGeneratorRuntime(name string) []runtime.Object {
return append([]runtime.Object{}, &lcasgv1.SeedGenerator{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: lcasgv1.SeedGeneratorSpec{},
})
}

0 comments on commit c47e5ce

Please sign in to comment.