Skip to content

Commit

Permalink
apiservers: added unit tests for openshiftapiserver.go (#381)
Browse files Browse the repository at this point in the history
Co-authored-by: nkononov <[email protected]>
  • Loading branch information
kononovn and kononovn authored May 1, 2024
1 parent 9e72b77 commit ecf4a63
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 28 deletions.
4 changes: 2 additions & 2 deletions pkg/apiservers/kubeapiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ type KubeAPIServerBuilder struct {

var kubeAPIServerObjName = "cluster"

// PullKubeAPIServerBuilder pulls existing kubeApiServer from the cluster.
func PullKubeAPIServerBuilder(apiClient *clients.Settings) (*KubeAPIServerBuilder, error) {
// PullKubeAPIServer pulls existing kubeApiServer from the cluster.
func PullKubeAPIServer(apiClient *clients.Settings) (*KubeAPIServerBuilder, error) {
glog.V(100).Infof("Pulling existing kubeApiServer from cluster")

if apiClient == nil {
Expand Down
9 changes: 2 additions & 7 deletions pkg/apiservers/kubeapiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
)

func TestPullKubeAPIServerBuilder(t *testing.T) {
func TestPullKubeAPIServer(t *testing.T) {
generateKubeAPIServer := func() *operatorv1.KubeAPIServer {
return &operatorv1.KubeAPIServer{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestPullKubeAPIServerBuilder(t *testing.T) {
testSettings = clients.GetTestClients(clients.TestClientParams{K8sMockObjects: runtimeObjects})
}

builderResult, err := PullKubeAPIServerBuilder(testSettings)
builderResult, err := PullKubeAPIServer(testSettings)

assert.Equal(t, testCase.expectedError, err)

Expand Down Expand Up @@ -202,26 +202,21 @@ func TestKubeAPIServerWaitUntilConditionTrue(t *testing.T) {
func TestKubeAPIServerWaitAllNodesAtTheLatestRevision(t *testing.T) {
testCases := []struct {
testKubeAPIServerBuilder *KubeAPIServerBuilder
condition string
expectedError error
}{
{
condition: "NodeInstallerProgressing",
testKubeAPIServerBuilder: buildValidKubeAPIServerBuilder(buildKubeAPIServerWithDummyObject()),
expectedError: nil,
},
{
condition: "unavailable",
testKubeAPIServerBuilder: buildValidKubeAPIServerBuilder(buildKubeAPIServerWithDummyObject()),
expectedError: fmt.Errorf("the unavailable condition not found exists: context deadline exceeded"),
},
{
condition: "",
testKubeAPIServerBuilder: buildValidKubeAPIServerBuilder(buildKubeAPIServerWithDummyObject()),
expectedError: fmt.Errorf("kubeAPIServer 'conditionType' cannot be empty"),
},
{
condition: "NodeInstallerProgressing",
testKubeAPIServerBuilder: buildValidKubeAPIServerBuilder(clients.GetTestClients(clients.TestClientParams{})),
expectedError: fmt.Errorf("cluster kubeAPIServer not found"),
},
Expand Down
46 changes: 27 additions & 19 deletions pkg/apiservers/openshiftapiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,8 @@ type OpenshiftAPIServerBuilder struct {
errorMsg string
}

// Exists checks whether the given openshiftAPIServer exists.
func (builder *OpenshiftAPIServerBuilder) Exists() bool {
if valid, _ := builder.validate(); !valid {
return false
}

var err error
builder.Object, err = builder.Get()

if err != nil {
glog.V(100).Infof("Failed to collect openshiftAPIServer object due to %s", err.Error())
}

return err == nil || !k8serrors.IsNotFound(err)
}

// PullOpenshiftAPIServerBuilder pulls existing openshiftApiServer from the cluster.
func PullOpenshiftAPIServerBuilder(apiClient *clients.Settings) (*OpenshiftAPIServerBuilder, error) {
// PullOpenshiftAPIServer pulls existing openshiftApiServer from the cluster.
func PullOpenshiftAPIServer(apiClient *clients.Settings) (*OpenshiftAPIServerBuilder, error) {
glog.V(100).Infof("Pulling existing openshiftApiServer from cluster")

if apiClient == nil {
Expand All @@ -75,6 +59,22 @@ func PullOpenshiftAPIServerBuilder(apiClient *clients.Settings) (*OpenshiftAPISe
return &builder, nil
}

// Exists checks whether the given openshiftAPIServer exists.
func (builder *OpenshiftAPIServerBuilder) Exists() bool {
if valid, _ := builder.validate(); !valid {
return false
}

var err error
builder.Object, err = builder.Get()

if err != nil {
glog.V(100).Infof("Failed to collect openshiftAPIServer object due to %s", err.Error())
}

return err == nil || !k8serrors.IsNotFound(err)
}

// Get returns openshiftAPIServer object if found.
func (builder *OpenshiftAPIServerBuilder) Get() (*operatorV1.OpenShiftAPIServer, error) {
if valid, err := builder.validate(); !valid {
Expand All @@ -89,7 +89,7 @@ func (builder *OpenshiftAPIServerBuilder) Get() (*operatorV1.OpenShiftAPIServer,
if err != nil {
glog.V(100).Infof("openshiftAPIServer object doesn't exist")

return nil, fmt.Errorf("openshiftAPIServer object doesn't exist")
return nil, err
}

return openshiftAPIServer, err
Expand All @@ -104,6 +104,10 @@ func (builder *OpenshiftAPIServerBuilder) GetCondition(conditionType string) (

glog.V(100).Infof("Get %s openshiftAPIServer %s condition", builder.Definition.Name, conditionType)

if conditionType == "" {
return nil, "", fmt.Errorf("openshiftAPIServer 'conditionType' cannot be empty")
}

if !builder.Exists() {
return nil, "", fmt.Errorf("%s openshiftAPIServer not found", builder.Definition.Name)
}
Expand Down Expand Up @@ -131,6 +135,10 @@ func (builder *OpenshiftAPIServerBuilder) WaitUntilConditionTrue(
return err
}

if conditionType == "" {
return fmt.Errorf("openshiftAPIServer 'conditionType' cannot be empty")
}

if !builder.Exists() {
return fmt.Errorf("%s openshiftAPIServer not found", builder.Definition.Name)
}
Expand Down
Loading

0 comments on commit ecf4a63

Please sign in to comment.