forked from openshift-kni/eco-goinfra
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alexander Chuzhoy <[email protected]>
- Loading branch information
Showing
2 changed files
with
349 additions
and
2 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,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{}, | ||
}) | ||
} |